Ken il y a 6 jours
Parent
commit
53fba13d25
1 fichiers modifiés avec 91 ajouts et 37 suppressions
  1. 91 37
      app/Services/PaymentOrderService.php

+ 91 - 37
app/Services/PaymentOrderService.php

@@ -1,23 +1,14 @@
 <?php
 
-
 namespace App\Services;
 
-use App\Services\BaseService;
+use App\Constants\HttpStatus;
 use App\Models\PaymentOrder;
-use App\Models\Config;
 use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Log;
-
 use App\Services\Payment\QianBaoService;
 use App\Services\Payment\SanJinService;
 
-use App\Services\WalletService;
-use App\Services\BalanceLogService;
-use Telegram\Bot\FileUpload\InputFile;
-
 /**
  * 投注
  */
@@ -172,14 +163,14 @@ class PaymentOrderService extends BaseService
         $geText = '';
         foreach ($product as $k => $v) {
             if ($v['type'] == $paymentType) {
-                if($v['type'] == 'zfbge'){
-                    if(in_array($amount,$v['fixed'])){
+                if ($v['type'] == 'zfbge') {
+                    if (in_array($amount, $v['fixed'])) {
                         $channel = $k;
-                        
-                    }else{
-                        $geText .= "❌ 此充值通道固定充值金额为".implode(',',$v['fixed'])."请务必输入区间金额!";
+
+                    } else {
+                        $geText .= "❌ 此充值通道固定充值金额为" . implode(',', $v['fixed']) . "请务必输入区间金额!";
                     }
-                }else{
+                } else {
                     if ($amount >= $v['min'] && $amount <= $v['max']) {
                         $channel = $k;
                         $rate = $v['rate'];
@@ -198,7 +189,7 @@ class PaymentOrderService extends BaseService
                         $max = $v['max'];
                     }
                 }
-                
+
             }
         }
 
@@ -210,7 +201,7 @@ class PaymentOrderService extends BaseService
             // $text .= "请重新填写充值的金额!";
             $text = "❌ 此充值通道充值金额{$min}-{$max}请务必输入区间金额!";
             $result['text'] = $text;
-            if($geText){
+            if ($geText) {
                 $result['text'] = $geText;
             }
             return $result;
@@ -222,14 +213,14 @@ class PaymentOrderService extends BaseService
         $data['amount'] = $amount;
         $data['channel'] = $channel;
         $data['fee'] = $amount * $rate;
-        $data['bank_name'] = SanJinService::getChannel($paymentType)??'';
+        $data['bank_name'] = SanJinService::getChannel($paymentType) ?? '';
         $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
         $data['order_no'] = $order_no;
         $data['callback_url'] = SanJinService::getNotifyUrl();
         $data['remark'] = '充值费率:' . $rate;
         $data['status'] = self::STATUS_STAY;
         $ret = SanJinService::pay(($amount * 100), $order_no, $channel);
-        Log::error('三斤支付发起:',$ret);
+        Log::error('三斤支付发起:', $ret);
         if ($ret['code'] == 0) {
 
             $qrCode = asset(self::createPaymentQrCode($ret['data']['payUrl']));
@@ -330,17 +321,80 @@ class PaymentOrderService extends BaseService
         }
     }
 
+
+    public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
+    {
+
+        DB::beginTransaction();
+        $result['chat_id'] = $memberId;
+        try {
+            $default_amount = $amount;
+
+            $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'
+            );
+            $text = "✅ 提现申请已提交!\n\n";
+            $text .= "钱包余额:{$available_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;
+    }
+
     /**
-     * @description: 创建代付订单
-     * @param {*} $memberId 会员
-     * @param {*} $amount   金额
+     * @description: 创建代付订单 (自动直接到账,包括用户钱包的扣款和提现记录的生成以及余额日志的创建)
+     * @param {*} $memberId 会员ID
+     * @param {*} $amount   代付金额
      * @param {*} $channel  提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝
      * @param {*} $bank_name  银行名称/支付宝
      * @param {*} $account  姓名
      * @param {*} $card_no  银行卡号/支付宝账号
      * @return {*}
      */
-    public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
+    public static function autoCreatePayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
     {
         $default_amount = $amount;
         $result = [];
@@ -606,7 +660,7 @@ class PaymentOrderService extends BaseService
      * @description: 查询支付订单
      * @param {*} $id
      * @return {*}
-     */    
+     */
     public static function singlePayOrder($id)
     {
         $msg = [];
@@ -614,11 +668,11 @@ class PaymentOrderService extends BaseService
         $info = self::findOne(['id' => $id]);
         if ($info && $info->status == self::STATUS_PROCESS) {
             $ret = SanJinService::queryOrder($info->order_no);
-            Log::error('三斤支付查询订单:',$ret);
-            if($ret['code'] == 0){
-                $item= [];
+            Log::error('三斤支付查询订单:', $ret);
+            if ($ret['code'] == 0) {
+                $item = [];
                 $item['state'] = $ret['data']['state'];
-                if($ret['data']['state'] == 1){
+                if ($ret['data']['state'] == 1) {
                     $item['status'] = self::STATUS_SUCCESS;
                     $info->update($item);
 
@@ -641,24 +695,24 @@ class PaymentOrderService extends BaseService
 
                     $msg['code'] = self::YES;
                     $msg['msg'] = '支付成功';
-                }else{
+                } else {
                     $msg['msg'] = '支付中';
                 }
 
-            }else{
-                $msg['msg'] = '查询失败:'.$ret['message'];
+            } else {
+                $msg['msg'] = '查询失败:' . $ret['message'];
             }
-        }else{
+        } else {
             $msg['msg'] = '该状态无法查询';
         }
 
         return $msg;
-        
+
     }
 
     public static function syncPayOrder()
     {
-        $list = self::model()::where('state',0 )->where('type',self::TYPE_PAY)->take(100)->get();
+        $list = self::model()::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();
         // foreach($list->toArray() as $k => $v){
         //      $item= [];
         //     if($v['status'] == self::STATUS_SUCCESS){
@@ -668,12 +722,12 @@ class PaymentOrderService extends BaseService
         //         $ret = SanJinService::queryOrder($v['order_no']);
         //         var_dump($ret);
         //         if($ret['code'] == 0){
-                
+
         //             $item['state'] = $ret['data']['state'];
         //             self::model()::where(['id'=>$v['id']])->update($item);
         //         }
         //     }
-            
+
         // }
     }