wesmiler 6 mēneši atpakaļ
vecāks
revīzija
27c7fa64fe
1 mainītis faili ar 182 papildinājumiem un 175 dzēšanām
  1. 182 175
      app/Services/PaymentService.php

+ 182 - 175
app/Services/PaymentService.php

@@ -367,218 +367,225 @@ 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'] : [];
-        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 == 'vip') {
-            $orderInfo = OrderModel::with(['vip'])->where(['order_no' => $orderNo, 'mark' => 1])
-                ->select(['id as order_id', 'user_id','goods_id','expired_at', 'order_no', 'total as pay_money', 'pay_at as pay_time','remark', '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;
+            $orderNo = isset($paymentInfo['order_no']) ? $paymentInfo['order_no'] : '';
+            $payUserId = isset($paymentInfo['user_id']) ? $paymentInfo['user_id'] : 0;
+            $payUser = isset($paymentInfo['user']) ? $paymentInfo['user'] : [];
+            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_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();
+            // 验证支付方式
+            if ($paymentPayType != $payType) {
+                $this->error = 2627;
                 return false;
             }
 
-        } // 保证金退款
-        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', '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_status' => 4, 'update_time' => time()];
-            if (!OrderModel::where(['order_no' => $orderNo,'mark' => 1])->update($updateData)) {
-                $this->error = 2633;
-                DB::rollBack();
-                return false;
-            }
-        }
+            /* TODO 订单验证和状态处理 */
+            // 充值订单
+            if ($scene == 'vip') {
+                $orderInfo = OrderModel::with(['vip'])->where(['order_no' => $orderNo, 'mark' => 1])
+                    ->select(['id as order_id', 'user_id', 'goods_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;
+                }
 
-        // 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 'vip': // 购买VIP
-                $vipInfo = isset($orderInfo['vip'])? $orderInfo['vip'] : [];
-                $vipType = isset($vipInfo['type'])? $vipInfo['type'] : 0;
-                $expiredAt = isset($orderInfo['expired_at'])? $orderInfo['expired_at'] : '';
-                $remark = isset($orderInfo['remark']) && $orderInfo['remark']? $orderInfo['remark'] : '购买VIP会员';
-                if(empty($vipInfo) || $expiredAt <= date('Y-m-d H:i:s') || $vipType<=0){
+                // 订单状态
+                if ($orderStatus != 1) {
                     DB::rollBack();
-                    $this->error = 'VIP参数错误';
+                    $this->error = 2630;
                     return false;
                 }
-                $field = "zg_vip";
-                if($vipType == 2){
-                    $field = "zsb_vip";
-                }else if($vipType==3){
-                    $field = "video_vip";
+
+                $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 = ["is_{$field}"=> 1,"{$field}_expired"=> $expiredAt,'update_time'=>time()];
-                RedisService::set("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}_update", ['order' => $orderInfo,'update'=>$updateData, 'notify' => $data], 600);
-                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', '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;
+                    return false;
+                }
+
+                // 订单状态
+                if ($orderStatus != 2) {
                     DB::rollBack();
                     $this->error = 2639;
                     return false;
                 }
-                // 账单记录
-                $log = [
-                    'user_id'=>$orderUserId,
-                    'source_order_no'=> $orderNo,
-                    'type'=> 1,
-                    'money'=> $price,
-                    'date'=>date('Y-m-d'),
-                    'create_time'=>time(),
-                    'remark'=> $remark,
-                    'status'=> 1,
-                    'mark'=>1
-                ];
-                RedisService::set("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}_log", ['order' => $orderInfo,'log'=>$log, 'notify' => $data], 600);
-                if(!AccountLogModel::insertGetId($log)){
-                    $this->error = 2635;
+
+                // 订单打款状态
+                if ($orderStatus == 4) {
+                    DB::rollBack();
+                    $this->error = 2630;
                     return false;
                 }
 
-                break;
-            default:
-                DB::rollBack();
-                $this->error = 2631;
-                return false;
-        }
+                $updateData = ['refund_status' => 4, 'update_time' => time()];
+                if (!OrderModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
+                    $this->error = 2633;
+                    DB::rollBack();
+                    return false;
+                }
+            }
+
+            // 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 'vip': // 购买VIP
+                    $vipInfo = isset($orderInfo['vip']) ? $orderInfo['vip'] : [];
+                    $vipType = isset($vipInfo['type']) ? $vipInfo['type'] : 0;
+                    $expiredAt = isset($orderInfo['expired_at']) ? $orderInfo['expired_at'] : '';
+                    $remark = isset($orderInfo['remark']) && $orderInfo['remark'] ? $orderInfo['remark'] : '购买VIP会员';
+                    if (empty($vipInfo) || $expiredAt <= date('Y-m-d H:i:s') || $vipType <= 0) {
+                        DB::rollBack();
+                        $this->error = 'VIP参数错误';
+                        return false;
+                    }
+                    $field = "zg_vip";
+                    if ($vipType == 2) {
+                        $field = "zsb_vip";
+                    } else if ($vipType == 3) {
+                        $field = "video_vip";
+                    }
+                    $updateData = ["is_{$field}" => 1, "{$field}_expired" => $expiredAt, 'update_time' => time()];
+                    RedisService::set("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}_update", ['order' => $orderInfo, 'update' => $updateData, 'notify' => $data], 600);
+                    if (!MemberModel::where(['id' => $orderUserId, 'mark' => 1])->update($updateData)) {
+                        DB::rollBack();
+                        $this->error = 2639;
+                        return false;
+                    }
+                    // 账单记录
+                    $log = [
+                        'user_id' => $orderUserId,
+                        'source_order_no' => $orderNo,
+                        'type' => 1,
+                        'money' => $price,
+                        'date' => date('Y-m-d'),
+                        'create_time' => time(),
+                        'remark' => $remark,
+                        'status' => 1,
+                        'mark' => 1
+                    ];
+                    RedisService::set("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}_log", ['order' => $orderInfo, 'log' => $log, 'notify' => $data], 600);
+                    if (!AccountLogModel::insertGetId($log)) {
+                        $this->error = 2635;
+                        return false;
+                    }
+
+                    break;
+                default:
+                    DB::rollBack();
+                    $this->error = 2631;
+                    return false;
+            }
 
-        $this->error = 2638;
-        DB::commit();
-        return true;
+            $this->error = 2638;
+            DB::commit();
+            return true;
+        } catch (\Exception $exception){
+            $this->error = $exception->getMessage();
+            RedisService::set("caches:payments:notify_{$scene}:error_" . $orderNo, ['notify' => $data,'error' => $exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
+            return false;
+        }
     }
 
     /**