Przeglądaj źródła

回调500和重复入账

doge 1 dzień temu
rodzic
commit
73785234a3
1 zmienionych plików z 79 dodań i 37 usunięć
  1. 79 37
      app/Services/PaymentOrderService.php

+ 79 - 37
app/Services/PaymentOrderService.php

@@ -4,6 +4,8 @@ namespace App\Services;
 
 
 use App\Constants\HttpStatus;
 use App\Constants\HttpStatus;
 use App\Models\PaymentOrder;
 use App\Models\PaymentOrder;
+use App\Models\User;
+use App\Models\Wallet as WalletModel;
 use Exception;
 use Exception;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
@@ -360,28 +362,15 @@ class PaymentOrderService extends BaseService
                 return true;
                 return true;
             }
             }
 
 
-            $info->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
-            $info->state = $params['status'];
-            if ((string)$params['status'] === JdPayService::PAY_STATUS_SUCCESS) {
-                $info->status = self::STATUS_SUCCESS;
-                $wallet = WalletService::findOne(['member_id' => $info->member_id]);
-                $balance = $wallet->available_balance;
-                $available_balance = bcadd($balance, JdPayService::amount($info->amount), 10);
-                $wallet->available_balance = $available_balance;
-                $wallet->save();
-
-                BalanceLogService::addLog($info->member_id, $info->amount, $balance, $available_balance, '三方充值', $info->id, '');
-                self::rechargesBibiReturn($info->member_id, $info->amount, $info->id);
+            $processed = self::applyPayCallback($info, JdPayService::amount($info->amount), (string)$params['status'], JdPayService::PAY_STATUS_SUCCESS, JdPayService::PAY_STATUS_FAIL, $params);
+            if ($processed && (string)$params['status'] === JdPayService::PAY_STATUS_SUCCESS) {
 
 
                 $text = "✅ 支付成功 \n";
                 $text = "✅ 支付成功 \n";
                 $text .= "充值金额:{$info->amount} RMB \n";
                 $text .= "充值金额:{$info->amount} RMB \n";
                 $text .= "订单号:{$info->order_no} \n";
                 $text .= "订单号:{$info->order_no} \n";
                 $text .= "您充值的金额已到账,请注意查收!";
                 $text .= "您充值的金额已到账,请注意查收!";
-                self::sendMessage($info->member_id, $text);
-            } elseif ((string)$params['status'] === JdPayService::PAY_STATUS_FAIL) {
-                $info->status = self::STATUS_FAIL;
+                self::notifyUser($info->member_id, $text);
             }
             }
-            $info->save();
             return true;
             return true;
         }
         }
 
 
@@ -402,7 +391,7 @@ class PaymentOrderService extends BaseService
                     $text .= "订单号:{$params['outTradeNo']} \n";
                     $text .= "订单号:{$params['outTradeNo']} \n";
                     $text .= "失败原因:支付金额与订单金额不一致 \n";
                     $text .= "失败原因:支付金额与订单金额不一致 \n";
                     $text .= "请联系客服处理!";
                     $text .= "请联系客服处理!";
-                    self::sendMessage($info->member_id, $text);
+                    self::notifyUser($info->member_id, $text);
                     return false;
                     return false;
                 }
                 }
 
 
@@ -416,32 +405,19 @@ class PaymentOrderService extends BaseService
 
 
                 // 付款
                 // 付款
                 if ($info->type == self::TYPE_PAY) {
                 if ($info->type == self::TYPE_PAY) {
-                    $info->state = $params['state'];
-                    if ($params['state'] == 1) {
-                        $info->status = self::STATUS_SUCCESS;
-                        $wallet = WalletService::findOne(['member_id' => $info->member_id]);
-                        $balance = $wallet->available_balance;
-                        $available_balance = bcadd($balance, $payAmount, 10);
-                        $wallet->available_balance = $available_balance;
-                        $wallet->save();
-
-                        // 记录余额变动日志
-                        BalanceLogService::addLog($info->member_id, $payAmount, $balance, $available_balance, '三方充值', $info->id, '');
-
-                        self::rechargesBibiReturn($info->member_id, $payAmount, $info->id);
+                    $processed = self::applyPayCallback($info, $payAmount, (string)$params['state'], '1', null, $params);
+                    if ($processed && $params['state'] == 1) {
 
 
                         $text = "✅ 支付成功 \n";
                         $text = "✅ 支付成功 \n";
                         $text .= "充值金额:{$payAmount} RMB \n";
                         $text .= "充值金额:{$payAmount} RMB \n";
                         $text .= "订单号:{$params['outTradeNo']} \n";
                         $text .= "订单号:{$params['outTradeNo']} \n";
                         $text .= "您充值的金额已到账,请注意查收!";
                         $text .= "您充值的金额已到账,请注意查收!";
-                        self::sendMessage($info->member_id, $text);
+                        self::notifyUser($info->member_id, $text);
                     } else {
                     } else {
-                        $info->status = self::STATUS_FAIL;
                         $text = "❌ 支付失败 \n";
                         $text = "❌ 支付失败 \n";
                         $text .= "充值金额:{$payAmount} RMB \n";
                         $text .= "充值金额:{$payAmount} RMB \n";
                         $text .= "订单号:{$params['outTradeNo']} \n";
                         $text .= "订单号:{$params['outTradeNo']} \n";
                     }
                     }
-                    $info->save();
                     return true;
                     return true;
                 }
                 }
             }
             }
@@ -468,6 +444,56 @@ class PaymentOrderService extends BaseService
 
 
     }
     }
 
 
+    private static function applyPayCallback(PaymentOrder $info, $payAmount, string $status, string $successStatus, ?string $failStatus, array $params): bool
+    {
+        return DB::transaction(function () use ($info, $payAmount, $status, $successStatus, $failStatus, $params) {
+            $order = PaymentOrder::where('id', $info->id)->lockForUpdate()->first();
+            if (!$order || $order->status != self::STATUS_PROCESS) {
+                return false;
+            }
+
+            $order->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
+            $order->state = $status;
+
+            if ($status === $successStatus) {
+                $order->status = self::STATUS_SUCCESS;
+                $order->save();
+
+                $wallet = WalletModel::where('member_id', $order->member_id)->lockForUpdate()->first();
+                if (!$wallet) {
+                    throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
+                }
+
+                $balance = $wallet->available_balance;
+                $availableBalance = bcadd($balance, $payAmount, 10);
+                $wallet->available_balance = $availableBalance;
+                $wallet->save();
+
+                BalanceLogService::addLog($order->member_id, $payAmount, $balance, $availableBalance, '三方充值', $order->id, '');
+
+                $rate = ConfigService::getVal('recharges_bibi_return');
+                $bonusAmount = $payAmount * $rate;
+                if ($rate > 0 && $bonusAmount > 0) {
+                    $bonusBeforeBalance = $wallet->available_balance;
+                    $bonusAfterBalance = bcadd($bonusBeforeBalance, $bonusAmount, 10);
+                    $wallet->available_balance = $bonusAfterBalance;
+                    $wallet->save();
+
+                    BalanceLogService::addLog($order->member_id, $bonusAmount, $bonusBeforeBalance, $bonusAfterBalance, '优惠活动', $order->id, '充值笔笔返');
+                }
+
+                return true;
+            }
+
+            if ($failStatus === null || $status === $failStatus) {
+                $order->status = self::STATUS_FAIL;
+            }
+            $order->save();
+
+            return true;
+        }, 3);
+    }
+
 
 
     /**
     /**
      * 拒绝提现
      * 拒绝提现
@@ -798,7 +824,7 @@ class PaymentOrderService extends BaseService
                     $text .= "收款地址:{$info->card_no} \n";
                     $text .= "收款地址:{$info->card_no} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现成功,金额已到账,请注意查收!";
                     $text .= "提现成功,金额已到账,请注意查收!";
-                    self::sendMessage($chat_id, $text);
+                    self::notifyUser($chat_id, $text);
                 }
                 }
             } elseif ((string)$params['status'] === JdPayService::REMIT_STATUS_FAIL) {
             } elseif ((string)$params['status'] === JdPayService::REMIT_STATUS_FAIL) {
                 $data['status'] = self::STATUS_FAIL;
                 $data['status'] = self::STATUS_FAIL;
@@ -818,7 +844,7 @@ class PaymentOrderService extends BaseService
                     $text .= "收款地址:{$info->card_no} \n";
                     $text .= "收款地址:{$info->card_no} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现失败,金额已返回钱包,请注意查收!";
                     $text .= "提现失败,金额已返回钱包,请注意查收!";
-                    self::sendMessage($chat_id, $text);
+                    self::notifyUser($chat_id, $text);
                 }
                 }
             }
             }
 
 
@@ -849,7 +875,7 @@ class PaymentOrderService extends BaseService
                     $text .= "收款卡号:{$info->card_no} \n";
                     $text .= "收款卡号:{$info->card_no} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现成功,金额已到账,请注意查收!";
                     $text .= "提现成功,金额已到账,请注意查收!";
-                    self::sendMessage($chat_id, $text);
+                    self::notifyUser($chat_id, $text);
                 }
                 }
             } else {
             } else {
                 $data['status'] = self::STATUS_FAIL;
                 $data['status'] = self::STATUS_FAIL;
@@ -871,7 +897,7 @@ class PaymentOrderService extends BaseService
                     $text .= "收款卡号:{$info->card_no} \n";
                     $text .= "收款卡号:{$info->card_no} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现金额:{$info->amount} \n";
                     $text .= "提现失败,金额已返回钱包,请注意查收!";
                     $text .= "提现失败,金额已返回钱包,请注意查收!";
-                    self::sendMessage($chat_id, $text);
+                    self::notifyUser($chat_id, $text);
                 }
                 }
             }
             }
 
 
@@ -976,6 +1002,22 @@ class PaymentOrderService extends BaseService
         }
         }
     }
     }
 
 
+    private static function notifyUser($chatId, string $text): void
+    {
+        if ((int)User::where('member_id', $chatId)->value('from') !== -1) {
+            return;
+        }
+
+        try {
+            self::sendMessage($chatId, $text);
+        } catch (\Throwable $e) {
+            Log::channel('payment_error')->warning('支付订单用户通知失败', [
+                'chat_id' => $chatId,
+                'error' => $e->getMessage(),
+            ]);
+        }
+    }
+
     public static function syncPayOrder()
     public static function syncPayOrder()
     {
     {
         $list = static::$MODEL::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();
         $list = static::$MODEL::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();