Ken 6 дней назад
Родитель
Сommit
3b07bbc369

+ 7 - 0
app/Models/PaymentOrder.php

@@ -3,6 +3,13 @@
 
 namespace App\Models;
 
+/**
+ * @property $amount
+ * @property $bank_name
+ * @property $account
+ * @property $card_no
+ * @property $status
+ */
 class PaymentOrder extends BaseModel
 {
 

+ 18 - 66
app/Services/PaymentOrderService.php

@@ -323,77 +323,27 @@ class PaymentOrderService extends BaseService
     }
 
     /**
-     * @description: 创建代付订单
-     * @param {*} $memberId 会员ID
-     * @param {*} $amount   代付金额
-     * @param {*} $channel  提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝
-     * @param {*} $bank_name  银行名称/支付宝
-     * @param {*} $account  姓名
-     * @param {*} $card_no  银行卡号/支付宝账号
-     * @return {*}
+     * @description: 创建代付订单 (根据 orderId 将钱转到用户指定账户)
+     * @param int $orderId PaymentOrder 表的ID
      */
-    public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no): array
+    public static function createPayout(int $orderId)
     {
         DB::beginTransaction();
-        $result['chat_id'] = $memberId;
-        $default_amount = $amount;
         try {
-            $wallet = WalletService::findOne(['member_id' => $memberId]);
-            if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
-            $balance = $wallet->available_balance;
-            if (bccomp($balance, $amount, 2) < 0) {
-                throw new Exception("您的钱包余额不足!", HttpStatus::CUSTOM_ERROR);
-            }
-            $available_balance = bcsub($balance, $amount, 10);
-
-            // 先预扣款(锁定资金)
-            $wallet->available_balance = $available_balance;
-            if (!$wallet->save()) throw new Exception('钱包更新失败!', HttpStatus::CUSTOM_ERROR);
-
-
-            $data = [];
-            $data['type'] = self::TYPE_PAYOUT;
-            $data['order_no'] = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
-            $data['member_id'] = $memberId;
-            $data['fee'] = $amount * 0.002 + 2;
-            $data['amount'] = number_format($amount, 2, '.', '');
-            $data['channel'] = $channel;
-            $data['bank_name'] = $bank_name;
-            $data['account'] = $account;
-            $data['card_no'] = $card_no;
-            $data['callback_url'] = QianBaoService::getNotifyUrl();
-            $data['status'] = self::STATUS_STAY;
-            $data['remark'] = '提现费率:0.2%+2';
-            // 创建待处理状态的提现记录
-            $info = PaymentOrder::create($data);
-            // 记录余额变动日志
-            BalanceLogService::addLog(
-                $memberId,
-                $default_amount,
-                $balance,
-                $available_balance,
-                '三方提现',
-                $info->id,
-                '钱宝提现费率:0.2%+2'
-            );
-
-
-            $balance = bcadd($available_balance, 0, 2);
-            $text = "✅ 提现申请已提交!\n\n";
-            $text .= "钱包余额:{$balance} RMB\n";
-            $text .= "提现金额:{$default_amount} RMB\n";
-            $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
-            $result['text'] = $text;
+            $order = PaymentOrder::where('id', $orderId)->first();
+            $amount = $order->amount;
+            $amount = number_format($amount, 2, '.', '');
+            $ret = QianBaoService::payout($amount, $order->order_no, $order->bank_name, $order->account, $order->card_no);
+            Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
+            if ($ret['code'] != 200) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
+            $order->status = self::STATUS_PROCESS;
+            $order->save();
             DB::commit();
-        } //
-        catch (Exception $e) {
+        } catch (Exception $e) {
             DB::rollBack();
-            $result['text'] = "系统发生了错误,请联系管理员";
-            if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
-                $result['text'] = $e->getMessage();
-            }
+            return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
         }
-        return $result;
+        return ['code' => 0, 'msg' => 'ok'];
     }
 
     /**
@@ -481,7 +431,8 @@ class PaymentOrderService extends BaseService
             // 提交事务,确保预扣款成功
             DB::commit();
 
-        } catch (Exception $e) {
+        } //
+        catch (Exception $e) {
             // 预扣款失败,回滚事务
             DB::rollBack();
             $result['text'] = '系统繁忙,请稍后重试!';
@@ -521,7 +472,8 @@ class PaymentOrderService extends BaseService
                 $result['text'] = '提现申请已提交,系统处理中...';
             }
 
-        } else {
+        } //
+        else {
             // 三方支付失败,需要回滚之前的预扣款
             DB::beginTransaction();
             try {

+ 72 - 3
app/Services/QianBaoWithdrawService.php

@@ -2,12 +2,16 @@
 
 namespace App\Services;
 
+use App\Constants\HttpStatus;
 use App\Constants\StepStatus;
 use App\Models\Bank;
 use App\Models\Config;
 use App\Models\PaymentOrder;
 use App\Models\Wallet;
+use App\Services\Payment\QianBaoService;
+use Exception;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
 use Telegram\Bot\Api;
 use Telegram\Bot\Exceptions\TelegramSDKException;
 
@@ -182,7 +186,7 @@ class QianBaoWithdrawService
                 $channel = "DF005";
                 break;
         }
-        $list = Bank::where('member_id',$chatId)->where('channel', $channel)->get();
+        $list = Bank::where('member_id', $chatId)->where('channel', $channel)->get();
         $keyboard = [];
         foreach ($list as $item) {
             $keyboard[] = [['text' => $item->getAlias(), 'callback_data' => "withdrawAddress@@choose_qb_{$item->getId()}"]];
@@ -379,11 +383,76 @@ class QianBaoWithdrawService
         $id = Cache::get("{$chatId}_QB_BANK_ID");
         $bank = Bank::where('id', $id)->first();
         $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
-        $res = PaymentOrderService::createPayout($chatId, $amount, $bank->getChannel(), $bank->getBankName(), $bank->getAccount(), $bank->getCardNo());
+        $res = static::createOrder($chatId, $amount, $bank->getChannel(), $bank->getBankName(), $bank->getAccount(), $bank->getCardNo());
         $res['message_id'] = $messageId;
         return $res;
     }
 
+    //创建提现订单
+    private static function createOrder($memberId, $amount, $channel, $bank_name, $account, $card_no)
+    {
+        DB::beginTransaction();
+        $result['chat_id'] = $memberId;
+        $default_amount = $amount;
+        try {
+            $wallet = WalletService::findOne(['member_id' => $memberId]);
+            if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
+            $balance = $wallet->available_balance;
+            if (bccomp($balance, $amount, 2) < 0) {
+                throw new Exception("您的钱包余额不足!", HttpStatus::CUSTOM_ERROR);
+            }
+            $available_balance = bcsub($balance, $amount, 10);
+
+            // 先预扣款(锁定资金)
+            $wallet->available_balance = $available_balance;
+            if (!$wallet->save()) throw new Exception('钱包更新失败!', HttpStatus::CUSTOM_ERROR);
+
+
+            $data = [];
+            $data['type'] = PaymentOrderService::TYPE_PAYOUT;
+            $data['order_no'] = PaymentOrderService::createOrderNo('sj' . $data['type'] . '_', $memberId);
+            $data['member_id'] = $memberId;
+            $data['fee'] = $amount * 0.002 + 2;
+            $data['amount'] = number_format($amount, 2, '.', '');
+            $data['channel'] = $channel;
+            $data['bank_name'] = $bank_name;
+            $data['account'] = $account;
+            $data['card_no'] = $card_no;
+            $data['callback_url'] = QianBaoService::getNotifyUrl();
+            $data['status'] = PaymentOrderService::STATUS_STAY;
+            $data['remark'] = '提现费率:0.2%+2';
+            // 创建待处理状态的提现记录
+            $info = PaymentOrder::create($data);
+            // 记录余额变动日志
+            BalanceLogService::addLog(
+                $memberId,
+                $default_amount,
+                $balance,
+                $available_balance,
+                '三方提现',
+                $info->id,
+                '钱宝提现费率:0.2%+2'
+            );
+
+
+            $balance = bcadd($available_balance, 0, 2);
+            $text = "✅ 提现申请已提交!\n\n";
+            $text .= "钱包余额:{$balance} RMB\n";
+            $text .= "提现金额:{$default_amount} RMB\n";
+            $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
+            $result['text'] = $text;
+            DB::commit();
+        } //
+        catch (Exception $e) {
+            DB::rollBack();
+            $result['text'] = "系统发生了错误,请联系管理员";
+            if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
+                $result['text'] = $e->getMessage();
+            }
+        }
+        return $result;
+    }
+
     //银行卡管理
     private static function banks($chatId, $messageId, $channel = ""): array
     {
@@ -546,7 +615,7 @@ class QianBaoWithdrawService
 
         return [
             'chat_id' => $chatId,
-            'text' =>lang("请输入姓名"),
+            'text' => lang("请输入姓名"),
             'message_id' => $messageId,
         ];
     }