|
|
@@ -12,12 +12,9 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Models\AccountLogModel;
|
|
|
-use App\Models\BalanceLogModel;
|
|
|
-use App\Models\DepositModel;
|
|
|
use App\Models\MemberModel;
|
|
|
-use App\Models\MessageModel;
|
|
|
+use App\Models\OrderModel;
|
|
|
use App\Models\PaymentModel;
|
|
|
-use App\Services\Api\MessageService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Yansongda\Pay\Pay;
|
|
|
use Yansongda\Pay\Provider\Wechat;
|
|
|
@@ -60,17 +57,21 @@ class PaymentService extends BaseService
|
|
|
|
|
|
/**
|
|
|
* 创建支付
|
|
|
- * @param string $scene 场景,deposit-保证金,depositRefund-保证金退款,withdraw-提现
|
|
|
+ * @param string $scene 场景,vip-VIP会员
|
|
|
* @param int $payType
|
|
|
+ * @param int $isMin 是否是小程序
|
|
|
* @return false|\Yansongda\Pay\Provider\Alipay|Wechat
|
|
|
*/
|
|
|
- public function createPay($scene, $payType = 10)
|
|
|
+ public function createPay($scene, $payType = 10, $payPt= '')
|
|
|
{
|
|
|
$config = ConfigService::make()->getConfigOptionByGroup(6);
|
|
|
if ($payType == 10) {
|
|
|
$appid = isset($config['wxpay_appid']) ? $config['wxpay_appid'] : '';
|
|
|
+ $mpAppid = isset($config['wxpay_mp_appid']) ? $config['wxpay_mp_appid'] : '';
|
|
|
+ $minAppid = isset($config['wxpay_min_appid']) ? $config['wxpay_min_appid'] : '';
|
|
|
$mchid = isset($config['wxpay_mchd']) ? $config['wxpay_mchd'] : '';
|
|
|
$secretV3Key = isset($config['wxpay_key_v3']) ? $config['wxpay_key_v3'] : '';
|
|
|
+ $secretV2Key = isset($config['wxpay_key_v2']) ? $config['wxpay_key_v2'] : '';
|
|
|
$wxpaySecretCert = isset($config['wxpay_secret_cert']) ? $config['wxpay_secret_cert'] : '';
|
|
|
$wxpayPublicCert = isset($config['wxpay_public_cert']) ? $config['wxpay_public_cert'] : '';
|
|
|
if (empty($appid) || empty($mchid) || empty($secretV3Key)) {
|
|
|
@@ -81,15 +82,32 @@ class PaymentService extends BaseService
|
|
|
// 支付参数
|
|
|
$payConfig = config('payment.wechat');
|
|
|
$payConfig['wechat']['default']['mch_id'] = $mchid;
|
|
|
- $payConfig['wechat']['default']['app_id'] = $appid;
|
|
|
- $payConfig['wechat']['default']['mch_secret_key'] = $secretV3Key;
|
|
|
+ if($payPt == 'min'){
|
|
|
+ // 小程序支付
|
|
|
+ $payConfig['wechat']['default']['mini_app_id'] = $minAppid? $minAppid : $appid;
|
|
|
+ }else if($payPt == 'mp'){
|
|
|
+ // 公众号
|
|
|
+ $payConfig['wechat']['default']['mp_app_id'] = $mpAppid? $mpAppid : $appid;
|
|
|
+ }else{
|
|
|
+ // APP支付
|
|
|
+ $payConfig['wechat']['default']['app_id'] = $appid;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($secretV3Key){
|
|
|
+ $payConfig['wechat']['default']['mch_secret_key'] = $secretV3Key;
|
|
|
+ }else if($secretV2Key){
|
|
|
+ $payConfig['wechat']['default']['mch_secret_key_v2'] = $secretV2Key;
|
|
|
+ }
|
|
|
+
|
|
|
if ($wxpaySecretCert) {
|
|
|
$payConfig['wechat']['default']['mch_secret_cert'] = $wxpaySecretCert;
|
|
|
}
|
|
|
if ($wxpayPublicCert) {
|
|
|
$payConfig['wechat']['default']['mch_public_cert_path'] = $wxpayPublicCert;
|
|
|
}
|
|
|
- $payConfig['wechat']['default']['notify_url'] = url('/api/notify/' . $scene . '/10');
|
|
|
+ //$payConfig['wechat']['default']['notify_url'] = url('/api/notify/' . $scene . '/10');
|
|
|
+ $payConfig['wechat']['default']['notify_url'] = url(env('APP_URL').'api/notify/' . $scene . '/10');
|
|
|
+
|
|
|
$this->config = $payConfig;
|
|
|
|
|
|
return Pay::wechat($payConfig);
|
|
|
@@ -129,13 +147,90 @@ class PaymentService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 微信小程序支付
|
|
|
+ * @param $userInfo
|
|
|
+ * @param $order
|
|
|
+ * @param string $scene
|
|
|
+ * @return false|\Yansongda\Supports\Collection
|
|
|
+ */
|
|
|
+ public function minPay($userInfo, $order, $scene = 'pay')
|
|
|
+ {
|
|
|
+ $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
|
|
|
+ $openid = isset($order['openid']) ? $order['openid'] : '';
|
|
|
+ if ($amount < 0) {
|
|
|
+ $this->error = 2615;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($openid)){
|
|
|
+ $this->error = 2614;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PR');
|
|
|
+ // 是否调用过支付,是则用新的支付单号
|
|
|
+ if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
|
|
|
+ $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
|
|
|
+ }
|
|
|
+ $body = isset($order['body']) ? $order['body'] : '';
|
|
|
+ $payData = [
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
+ 'description' => $body ? $body : '订单支付',
|
|
|
+ 'amount' => [
|
|
|
+ 'total' => intval($amount * 100),
|
|
|
+ 'currency' => 'CNY'
|
|
|
+ ],
|
|
|
+ 'payer' => [
|
|
|
+ 'openid' => $openid,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 创建支付
|
|
|
+ try {
|
|
|
+ $pay = $this->createPay($scene, 10, 'min');
|
|
|
+ RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
|
|
|
+ if (empty($pay)) {
|
|
|
+ $this->error = 2616;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pay = $pay->mini($payData);
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}_error", ['order' => $order,'error'=>$exception->getTrace(), 'config' => $this->config], 7200);
|
|
|
+ $this->error = $exception->getMessage();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($pay->package) {
|
|
|
+ $data = [
|
|
|
+ 'user_id' => $userInfo['id'],
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
+ 'order_no' => $order['order_no'],
|
|
|
+ 'params' => json_encode($pay, 256),
|
|
|
+ 'total_fee' => $amount,
|
|
|
+ 'pay_type' => 10,
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'status' => 2,
|
|
|
+ 'mark' => 1,
|
|
|
+ ];
|
|
|
+ if ($this->model->insertGetId($data)) {
|
|
|
+ $this->error = 2617;
|
|
|
+ return $pay;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2618;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 微信支付
|
|
|
* @param $userInfo
|
|
|
* @param $order
|
|
|
* @param string $scene
|
|
|
* @return false|\Yansongda\Supports\Collection
|
|
|
*/
|
|
|
- public function wechatPay($userInfo, $order, $scene = 'deposit')
|
|
|
+ public function wechatPay($userInfo, $order, $scene = 'pay')
|
|
|
{
|
|
|
$amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
|
|
|
if ($amount < 0) {
|
|
|
@@ -269,305 +364,186 @@ class PaymentService extends BaseService
|
|
|
$payAt = '';
|
|
|
$notifyData = [];
|
|
|
|
|
|
- // 微信支付
|
|
|
- if ($payType == 10) {
|
|
|
- $resource = isset($data['resource']) ? $data['resource'] : [];
|
|
|
- $ciphertext = isset($resource['ciphertext']) ? $resource['ciphertext'] : [];
|
|
|
- $tradeStatus = isset($ciphertext['trade_state']) ? $ciphertext['trade_state'] : '';
|
|
|
- if ($tradeStatus != 'SUCCESS') {
|
|
|
- $this->error = 2622;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $outTradeNo = isset($ciphertext['out_trade_no']) ? $ciphertext['out_trade_no'] : '';
|
|
|
- $transactionId = isset($ciphertext['transaction_id']) ? $ciphertext['transaction_id'] : '';
|
|
|
- if (empty($outTradeNo)) {
|
|
|
- $this->error = 2623;
|
|
|
- return false;
|
|
|
- }
|
|
|
- $payAt = isset($ciphertext['success_time']) ? date('Y-m-d H:i:s', strtotime($ciphertext['success_time'])) : date('Y-m-d H:i:s');
|
|
|
- $amount = isset($ciphertext['amount']) ? $ciphertext['amount'] : [];
|
|
|
- $payTotal = isset($amount['total']) ? moneyFormat($amount['total'] / 100, 3) : 0;
|
|
|
- $notifyData = $ciphertext;
|
|
|
- if ($payTotal <= 0) {
|
|
|
- $this->error = 2624;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- } // 支付宝支付
|
|
|
- else if ($payType == 20) {
|
|
|
- // TRADE_SUCCESS
|
|
|
- $tradeStatus = isset($data['trade_status']) ? $data['trade_status'] : '';
|
|
|
- if ($tradeStatus != 'TRADE_SUCCESS' && $tradeStatus != 'TRADE_FINISHED') {
|
|
|
- $this->error = 2622;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $outTradeNo = isset($data['out_trade_no']) ? $data['out_trade_no'] : '';
|
|
|
- if (empty($outTradeNo)) {
|
|
|
- $this->error = 2623;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $payTotal = isset($data['total_amount']) ? floatval($data['total_amount']) : 0;
|
|
|
- $transactionId = isset($data['trade_no']) ? trim($data['trade_no']) : '';
|
|
|
- $payAt = isset($data['send_pay_date']) ? trim($data['send_pay_date']) : date('Y-m-d H:i:s');
|
|
|
- $notifyData = $data;
|
|
|
- if ($payTotal <= 0) {
|
|
|
- $this->error = 2624;
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 支付信息
|
|
|
- $paymentInfo = $this->model->with(['user'])->where(['out_trade_no' => $outTradeNo, 'mark' => 1])
|
|
|
- ->select(['user_id', 'order_no', 'pay_type', 'total_fee', 'status'])
|
|
|
- ->first();
|
|
|
- $status = isset($paymentInfo['status']) ? $paymentInfo['status'] : 0;
|
|
|
- $totalFee = isset($paymentInfo['total_fee']) ? $paymentInfo['total_fee'] : 0;
|
|
|
- $paymentPayType = isset($paymentInfo['pay_type']) ? $paymentInfo['pay_type'] : 0;
|
|
|
- $orderNo = isset($paymentInfo['order_no']) ? $paymentInfo['order_no'] : '';
|
|
|
- $payUserId = isset($paymentInfo['user_id'])? $paymentInfo['user_id'] : 0;
|
|
|
- $payUser = isset($paymentInfo['user'])? $paymentInfo['user'] : [];
|
|
|
- $username = isset($payUser['mobile'])? $payUser['mobile'] : $payUserId;
|
|
|
- if (empty($paymentInfo) || empty($orderNo) || $payUserId<=0) {
|
|
|
- $this->error = 2625;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ try {
|
|
|
|
|
|
- // 验证支付状态
|
|
|
- if ($status == 1) {
|
|
|
- $this->error = 2626;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 微信支付
|
|
|
+ if ($payType == 10) {
|
|
|
+ $resource = isset($data['resource']) ? $data['resource'] : [];
|
|
|
+ $ciphertext = isset($resource['ciphertext']) ? $resource['ciphertext'] : [];
|
|
|
+ $tradeStatus = isset($ciphertext['trade_state']) ? $ciphertext['trade_state'] : '';
|
|
|
+ if ($tradeStatus != 'SUCCESS') {
|
|
|
+ $this->error = 2622;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 验证支付方式
|
|
|
- if ($paymentPayType != $payType) {
|
|
|
- $this->error = 2627;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $outTradeNo = isset($ciphertext['out_trade_no']) ? $ciphertext['out_trade_no'] : '';
|
|
|
+ $transactionId = isset($ciphertext['transaction_id']) ? $ciphertext['transaction_id'] : '';
|
|
|
+ if (empty($outTradeNo)) {
|
|
|
+ $this->error = 2623;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $payAt = isset($ciphertext['success_time']) ? date('Y-m-d H:i:s', strtotime($ciphertext['success_time'])) : date('Y-m-d H:i:s');
|
|
|
+ $amount = isset($ciphertext['amount']) ? $ciphertext['amount'] : [];
|
|
|
+ $payTotal = isset($amount['total']) ? moneyFormat($amount['total'] / 100, 3) : 0;
|
|
|
+ $notifyData = $ciphertext;
|
|
|
+ if ($payTotal <= 0) {
|
|
|
+ $this->error = 2624;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if ($payTotal != $totalFee || $payTotal <= 0) {
|
|
|
- $this->error = 2628;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ } // 支付宝支付
|
|
|
+ else if ($payType == 20) {
|
|
|
+ // TRADE_SUCCESS
|
|
|
+ $tradeStatus = isset($data['trade_status']) ? $data['trade_status'] : '';
|
|
|
+ if ($tradeStatus != 'TRADE_SUCCESS' && $tradeStatus != 'TRADE_FINISHED') {
|
|
|
+ $this->error = 2622;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 删除久远旧记录
|
|
|
- $this->model->where(['mark' => 1])->where('create_time','<=', time() - 60 * 86400)->delete();
|
|
|
+ $outTradeNo = isset($data['out_trade_no']) ? $data['out_trade_no'] : '';
|
|
|
+ if (empty($outTradeNo)) {
|
|
|
+ $this->error = 2623;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 更新订单数据
|
|
|
- DB::beginTransaction();
|
|
|
- $updateData = ['transaction_id' => $transactionId, 'result' => json_encode($notifyData, 256), 'pay_at' => $payAt, 'status' => 1, 'update_time' => time()];
|
|
|
- if (!$this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->update($updateData)) {
|
|
|
- $this->error = 2632;
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $payTotal = isset($data['total_amount']) ? floatval($data['total_amount']) : 0;
|
|
|
+ $transactionId = isset($data['trade_no']) ? trim($data['trade_no']) : '';
|
|
|
+ $payAt = isset($data['send_pay_date']) ? trim($data['send_pay_date']) : date('Y-m-d H:i:s');
|
|
|
+ $notifyData = $data;
|
|
|
+ if ($payTotal <= 0) {
|
|
|
+ $this->error = 2624;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /* TODO 订单验证和状态处理 */
|
|
|
- // 充值订单
|
|
|
- if ($scene == 'deposit') {
|
|
|
- $orderInfo = DepositModel::where(['order_no' => $orderNo, 'type' => 1, 'mark' => 1])
|
|
|
- ->select(['id as order_id', 'user_id', 'order_no', 'money as pay_money','before_money', 'pay_at as pay_time', 'pay_status', 'status'])
|
|
|
+ // 支付信息
|
|
|
+ $paymentInfo = $this->model->with(['user'])->where(['out_trade_no' => $outTradeNo, 'mark' => 1])
|
|
|
+ ->select(['user_id', 'order_no', 'pay_type', 'total_fee', 'status'])
|
|
|
->first();
|
|
|
- $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
|
|
|
- // 验证订单
|
|
|
- if (empty($orderInfo)) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2629;
|
|
|
+ $status = isset($paymentInfo['status']) ? $paymentInfo['status'] : 0;
|
|
|
+ $totalFee = isset($paymentInfo['total_fee']) ? $paymentInfo['total_fee'] : 0;
|
|
|
+ $paymentPayType = isset($paymentInfo['pay_type']) ? $paymentInfo['pay_type'] : 0;
|
|
|
+ $orderType = isset($paymentInfo['order_type']) ? $paymentInfo['order_type'] : 0;
|
|
|
+ $orderNo = isset($paymentInfo['order_no']) ? $paymentInfo['order_no'] : '';
|
|
|
+ $payUserId = isset($paymentInfo['user_id']) ? $paymentInfo['user_id'] : 0;
|
|
|
+ if (empty($paymentInfo) || empty($orderNo) || $payUserId <= 0) {
|
|
|
+ $this->error = 2625;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 订单状态
|
|
|
- if ($orderStatus != 1) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2630;
|
|
|
+ // 验证支付状态
|
|
|
+ if ($status == 1) {
|
|
|
+ $this->error = 2626;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- $updateData = ['pay_status' => 2, 'pay_at' => $payAt, 'transaction_id' => $transactionId, 'status' => 3, 'update_time' => time()];
|
|
|
- if (!DepositModel::where(['order_no' => $orderNo, 'type' => 1, 'mark' => 1])->update($updateData)) {
|
|
|
- $this->error = 2633;
|
|
|
- DB::rollBack();
|
|
|
+ // 验证支付方式
|
|
|
+ if ($paymentPayType != $payType) {
|
|
|
+ $this->error = 2627;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- } // 保证金退款
|
|
|
- else if ($scene == 'depositRefund') {
|
|
|
- $orderInfo = DepositModel::where(['refund_no' => $orderNo, 'type' => 1, 'mark' => 1])
|
|
|
- ->select(['id as order_id', 'user_id', 'refund_no as order_no', 'refund_money as pay_money','before_money', 'pay_at as pay_time', 'refund_status as status'])
|
|
|
- ->first();
|
|
|
- $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
|
|
|
- // 验证订单
|
|
|
- if (empty($orderInfo)) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2629;
|
|
|
+ if ($payTotal != $totalFee || $payTotal <= 0) {
|
|
|
+ $this->error = 2628;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 订单状态
|
|
|
- if ($orderStatus != 2) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2639;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 删除久远旧记录
|
|
|
+ $this->model->where(['mark' => 1])->where('create_time', '<=', time() - 60 * 86400)->delete();
|
|
|
|
|
|
- // 订单打款状态
|
|
|
- if ($orderStatus == 4) {
|
|
|
+ // 更新订单数据
|
|
|
+ DB::beginTransaction();
|
|
|
+ $updateData = ['transaction_id' => $transactionId, 'result' => json_encode($notifyData, 256), 'pay_at' => $payAt, 'status' => 1, 'update_time' => time()];
|
|
|
+ if (!$this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->update($updateData)) {
|
|
|
+ $this->error = 2632;
|
|
|
DB::rollBack();
|
|
|
- $this->error = 2630;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- $updateData = ['refund_pay_at' => $payAt, 'refund_transaction_id' => $transactionId, 'refund_status' => 4, 'update_time' => time()];
|
|
|
- if (!DepositModel::where(['refund_no' => $orderNo, 'type' => 1, 'mark' => 1])->update($updateData)) {
|
|
|
- $this->error = 2633;
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
- }
|
|
|
- } else if($scene == 'withdraw'){
|
|
|
- $orderInfo = BalanceLogModel::where(['order_no' => $orderNo, 'type' => 2, 'mark' => 1])
|
|
|
- ->select(['id as order_id', 'user_id', 'order_no', 'money as pay_money','before_money', 'pay_at as pay_time', 'pay_status', 'status'])
|
|
|
- ->first();
|
|
|
- $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
|
|
|
- $payStatus = isset($orderInfo['pay_status']) ? $orderInfo['pay_status'] : 0;
|
|
|
- // 验证订单
|
|
|
- if (empty($orderInfo)) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2629;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ /* TODO 订单验证和状态处理 */
|
|
|
+ $orderInfo = [];
|
|
|
+ // 商城订单支付
|
|
|
+ if ($scene == 'shop') {
|
|
|
+ $orderInfo = OrderModel::with(['member'])->where(['order_no' => $orderNo, 'mark' => 1])
|
|
|
+ ->select(['id as order_id', 'user_id', 'expired_at', 'order_no', 'total as pay_money', 'pay_at as pay_time', 'remark', 'status'])
|
|
|
+ ->first();
|
|
|
+ $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
|
|
|
+ // 验证订单
|
|
|
+ if (empty($orderInfo)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2629;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 订单状态
|
|
|
- if ($orderStatus != 2) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2639;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // 订单状态
|
|
|
+ if ($orderStatus != 1) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = 2630;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 订单打款状态
|
|
|
- if ($payStatus != 1) {
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2630;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $updateData = ['pay_at' => $payAt, 'transaction_id' => $transactionId, 'status' => 2, 'update_time' => time()];
|
|
|
+ if (!OrderModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
|
|
|
+ $this->error = 2633;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- $updateData = ['pay_status' => 2, 'pay_at' => $payAt, 'transaction_id' => $transactionId, 'update_time' => time()];
|
|
|
- if (!BalanceLogModel::where(['order_no' => $orderNo, 'type' => 2, 'mark' => 1])->update($updateData)) {
|
|
|
- $this->error = 2633;
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 场景业务回调处理
|
|
|
- $pushData = []; // 推送消息数据
|
|
|
- $orderUserId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
|
|
|
- RedisService::set("caches:notify:{$orderNo}_{$scene}:data", ['order' => $orderInfo, 'notify' => $data], 7200);
|
|
|
- switch ($scene) {
|
|
|
- case 'deposit': // 保证金充值
|
|
|
- $updateData = ['deposit'=>DB::raw("deposit + {$payTotal}"),'update_time'=>time()];
|
|
|
- if(!MemberModel::where(['id'=> $orderUserId,'mark'=>1])->update($updateData)){
|
|
|
+ // 退款
|
|
|
+ else if ($scene == 'refund') {
|
|
|
+ $orderInfo = OrderModel::where(['order_no' => $orderNo, 'mark' => 1])
|
|
|
+ ->select(['id as order_id', 'user_id', 'order_no', 'total as pay_money', 'remark','refund_remark', 'pay_at as pay_time', 'refund_status as status'])
|
|
|
+ ->first();
|
|
|
+ $refundRemark = isset($orderInfo['refund_remark']) ? $orderInfo['refund_remark'] : 'refund_remark';
|
|
|
+ $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
|
|
|
+ // 验证订单
|
|
|
+ if (empty($orderInfo)) {
|
|
|
DB::rollBack();
|
|
|
- $this->error = 2180;
|
|
|
+ $this->error = 2629;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 缴纳保证金消息
|
|
|
- $message = "用户{$username}充值了{$payTotal}元保证金";
|
|
|
- $msgData = [
|
|
|
- 'from_uid' => $orderUserId,
|
|
|
- 'to_uid' => 0,
|
|
|
- 'type' => 3,// 保证金消息
|
|
|
- 'msg_type' => 4,
|
|
|
- 'title' => '充值保证金消息',
|
|
|
- 'description' => $message,
|
|
|
- 'order_no' => $orderNo,
|
|
|
- 'content' => json_encode([
|
|
|
- 'title'=> $message.'<span class="ele-text-primary">查看订单</span>',
|
|
|
- 'order_no'=> $orderNo,
|
|
|
- 'money'=> $payTotal,
|
|
|
- 'date'=> date('Y-m-d H:i:s'),
|
|
|
- 'user_id'=> $orderUserId,
|
|
|
- 'type'=> 'deposit',
|
|
|
- 'remark'=> '充值保证金',
|
|
|
- ],256),
|
|
|
- 'chat_key' => getChatKey($orderUserId,0),
|
|
|
- 'create_time' => time(),
|
|
|
- 'update_time' => time(),
|
|
|
- 'is_read' => 2,
|
|
|
- 'status' => 1
|
|
|
- ];
|
|
|
- MessageModel::insertGetId($msgData);
|
|
|
- case 'depositRefund': // 保证金退款
|
|
|
- $deposit = isset($orderInfo['before_money'])? $orderInfo['before_money'] : 0;
|
|
|
- // 明细处理
|
|
|
- $logId = AccountLogModel::where(['user_id'=>$orderUserId,'source_order_no'=> $orderNo,'mark'=>1])->value('id');
|
|
|
- $log = [
|
|
|
- 'user_id' => $orderUserId,
|
|
|
- 'source_order_no' => isset($orderInfo['order_no']) ? $orderInfo['order_no'] : '',
|
|
|
- 'type' => $scene=='deposit'? 2 : 3,
|
|
|
- 'money' => $payTotal,
|
|
|
- 'before_money' => $scene=='deposit'?$deposit:0,
|
|
|
- 'date'=> date('Y-m-d'),
|
|
|
- 'create_time' => time(),
|
|
|
- 'remark' => $scene == 'deposit'? '充值保证金' :'退保申请',
|
|
|
- 'status' => 1,
|
|
|
- 'mark' => 1,
|
|
|
- ];
|
|
|
- if($logId){
|
|
|
- AccountLogModel::where(['id'=> $logId])->update([
|
|
|
- 'status'=>1,
|
|
|
- 'update_time'=>time(),
|
|
|
- ]);
|
|
|
- }else if (!AccountLogModel::insertGetId($log)) {
|
|
|
+ // 订单状态
|
|
|
+ if ($orderStatus != 2) {
|
|
|
DB::rollBack();
|
|
|
- $this->error = 2180;
|
|
|
+ $this->error = 2639;
|
|
|
return false;
|
|
|
-
|
|
|
}
|
|
|
- break;
|
|
|
- case 'withdraw': // 收入提现// 明细处理
|
|
|
- $money = isset($orderInfo['before_money'])? $orderInfo['before_money'] : 0;
|
|
|
- $logId = AccountLogModel::where(['user_id'=>$orderUserId,'source_order_no'=> $orderNo,'mark'=>1])->value('id');
|
|
|
- $log = [
|
|
|
- 'user_id' => $orderUserId,
|
|
|
- 'source_order_no' => isset($orderInfo['order_no']) ? $orderInfo['order_no'] : '',
|
|
|
- 'type' => 4,
|
|
|
- 'money' => $payTotal,
|
|
|
- 'before_money' => $money,
|
|
|
- 'date'=> date('Y-m-d'),
|
|
|
- 'create_time' => time(),
|
|
|
- 'remark' => '收入提现',
|
|
|
- 'status' => 1,
|
|
|
- 'mark' => 1,
|
|
|
- ];
|
|
|
- if($logId){
|
|
|
- AccountLogModel::where(['id'=> $logId])->update([
|
|
|
- 'status'=>1,
|
|
|
- 'update_time'=>time(),
|
|
|
- ]);
|
|
|
- }else if (!AccountLogModel::insertGetId($log)) {
|
|
|
+
|
|
|
+ // 订单打款状态
|
|
|
+ if ($orderStatus == 1) {
|
|
|
DB::rollBack();
|
|
|
- $this->error = 2180;
|
|
|
+ $this->error = 2630;
|
|
|
return false;
|
|
|
+ }
|
|
|
|
|
|
+ $updateData = ['refund_status' => 1,'refund_remark'=>$refundRemark?$refundRemark.' 已退款成功':'已退款成功', 'update_time' => time()];
|
|
|
+ if (!OrderModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
|
|
|
+ $this->error = 2633;
|
|
|
+ DB::rollBack();
|
|
|
+ return false;
|
|
|
}
|
|
|
- break;
|
|
|
- default:
|
|
|
- DB::rollBack();
|
|
|
- $this->error = 2631;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ($pushData && !MessageService::make()->pushMessage($orderInfo['user_id'], $pushData)) {
|
|
|
- $this->error = MessageService::make()->getError();
|
|
|
+ // TODO 场景业务回调处理
|
|
|
+ $orderUserId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
|
|
|
+ RedisService::set("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}", ['order' => $orderInfo, 'notify' => $data], 7200);
|
|
|
+ switch ($scene) {
|
|
|
+ case 'shop': //
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2638;
|
|
|
+ DB::commit();
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $exception){
|
|
|
+ $this->error = $exception->getMessage();
|
|
|
+ RedisService::set("caches:payments:notify_{$scene}:catch_" . $orderNo.'_error', ['notify' => $data,'error' => $exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -579,7 +555,7 @@ class PaymentService extends BaseService
|
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
|
|
|
*/
|
|
|
- public function refund($order, $scene='depositRefund')
|
|
|
+ public function refund($order, $scene='vip')
|
|
|
{
|
|
|
$money = isset($order['money'])? $order['money'] : 0;
|
|
|
$payType = isset($order['pay_type'])? $order['pay_type'] : 0;
|
|
|
@@ -647,113 +623,4 @@ class PaymentService extends BaseService
|
|
|
$this->error = 2176;
|
|
|
return $refundStatus;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 企业打款到余额
|
|
|
- * @param $order 订单参数:order_no-单号,pay_money-打款金额,account-打款账号(微信openid或支付宝账号),body-打款备注
|
|
|
- * @param string $scene
|
|
|
- * @param int $payType
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public function transfer($order, $scene='withdraw', $payType=10)
|
|
|
- {
|
|
|
- $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('WD');
|
|
|
- $body = isset($order['body']) ? $order['body'] : '';
|
|
|
- $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
|
|
|
- $account = isset($order['account']) ? $order['account'] : ''; // 微信openid或支付宝账号
|
|
|
- $realName = isset($order['real_name']) ? $order['real_name'] : ''; // 实名
|
|
|
- if ($amount < 0) {
|
|
|
- $this->error = 2037;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if(empty($account)){
|
|
|
- $this->error = $payType == 10? 2044 : 2045;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 创建支付
|
|
|
- $result = [];
|
|
|
- try {
|
|
|
- $config = ConfigService::make()->getConfigOptionByGroup(6);
|
|
|
- if ($payType == 10) {
|
|
|
- $appid = isset($config['wxpay_appid']) ? $config['wxpay_appid'] : '';
|
|
|
- $payData = [
|
|
|
- 'appid' => $appid, // 微信小程序的app_id
|
|
|
- 'out_batch_no' => $outTradeNo, // 商家批次单号
|
|
|
- 'batch_name' => $body? $body : '收入提现', // 该笔批量转账的名称
|
|
|
- 'batch_remark' => $body? $body : '收入提现', // 转账说明
|
|
|
- 'total_amount' => intval($amount * 100), // 转账金额,单位:分
|
|
|
- 'total_num' => 1, // 转账总笔数
|
|
|
- 'transfer_detail_list' => [
|
|
|
- [
|
|
|
- 'out_detail_no' => $outTradeNo, // 商家明细单号
|
|
|
- 'transfer_amount' => intval($amount * 100), // 转账金额
|
|
|
- 'transfer_remark' => $body? $body : '收入提现', // 单条转账备注(微信用户会收到该备注)
|
|
|
- 'openid' => $account, // 转账用户的 openid
|
|
|
- ],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
- $pay = $this->createPay($scene, $payType);
|
|
|
- RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}_pay", ['order' => $order, 'config' => $this->config], 7200);
|
|
|
- if (empty($pay)) {
|
|
|
- $this->error = 2616;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- $result = $pay->transfer($payData);
|
|
|
- RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}_result", ['order' => $order,'result'=> $result, 'config' => $this->config], 7200);
|
|
|
- $msg = isset($result['message'])? trim($result['message']) : '';
|
|
|
- $orderId = isset($result['batch_id'])? trim($result['batch_id']) : '';
|
|
|
- if(empty($orderId)){
|
|
|
- $this->error = $msg? $msg : 2046;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $this->error = 2047;
|
|
|
- return ['order_no'=> $outTradeNo,'order_id'=> $orderId];
|
|
|
- }else if ($payType == 20){
|
|
|
- $payData = [
|
|
|
- 'out_biz_no' => $outTradeNo,//商户订单号,内部订单号
|
|
|
- 'trans_amount' => $amount, //订单总金额,单位为元,精确到小数点后两位,
|
|
|
- 'biz_scene' => 'DIRECT_TRANSFER', //描述特定的业务场景,可传的参数如下:DIRECT_TRANSFER:单笔无密转账到支付宝,B2C现金红包PERSONAL_COLLECTION:C2C现金红包-领红包
|
|
|
- 'product_code' => 'TRANS_ACCOUNT_NO_PWD', //业务产品码,单笔无密转账到支付宝账户固定为:TRANS_ACCOUNT_NO_PWD;收发现金红包固定为:STD_RED_PACKET;
|
|
|
- 'remark' => $body? $body : '收入提现',//业务备注
|
|
|
- 'order_title' => $body? $body : '收入提现',//转账业务的标题,用于在支付宝用户的账单里显示
|
|
|
- 'payee_info' => [
|
|
|
- 'identity' => $account,//参与方的唯一标识'208823395231'
|
|
|
- 'name' => $realName,//参与方的唯一标识'208823395231'
|
|
|
- 'identity_type' => 'ALIPAY_LOGON_ID',//参与方的标识类型,目前支持如下类型:1、ALIPAY_USER_ID 支付宝的会员ID2、ALIPAY_LOGON_ID:支付宝登录号,支持邮箱和手机号格式3、ALIPAY_OPEN_ID:支付宝openid
|
|
|
- ],//收款方信息
|
|
|
- ];
|
|
|
- $pay = $this->createPay($scene, $payType);
|
|
|
- RedisService::set("caches:payments:alipay:{$scene}_{$outTradeNo}_pay", ['order' => $order, 'config' => $this->config], 7200);
|
|
|
- if (empty($pay)) {
|
|
|
- $this->error = 2616;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- $result = $pay->transfer($payData);
|
|
|
- RedisService::set("caches:payments:alipay:{$scene}_{$outTradeNo}_result", ['order' => $order,'result'=> $result, 'config' => $this->config], 7200);
|
|
|
- $code = isset($result['code'])? intval($result['code']) : '';
|
|
|
- $msg = isset($result['sub_msg'])? trim($result['sub_msg']) : '';
|
|
|
- if($code != 10000){
|
|
|
- $this->error = $msg? $msg : 2046;
|
|
|
- return false;
|
|
|
- }
|
|
|
- $this->error = 2047;
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- $this->error = 2046;
|
|
|
- return false;
|
|
|
- } catch (\Exception $exception) {
|
|
|
- RedisService::set("caches:payments:trabsfer:{$scene}_{$outTradeNo}_error", ['order' => $order,'error'=>$exception->getTrace(), 'config' => $this->config], 7200);
|
|
|
- $this->error = $exception->getMessage();
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
}
|