seven il y a 1 semaine
Parent
commit
b02c362ff8

+ 3 - 0
app/Http/Controllers/api/Pay.php

@@ -21,6 +21,9 @@ class Pay extends Controller
         $data = $request->all();
         // 记录到专用支付日志
         Log::channel('payment')->info('支付回调', $data);
+
+        
+
         echo 'success';
     }
 

+ 40 - 12
app/Services/Payment/SanJinService.php

@@ -23,6 +23,19 @@ class SanJinService extends BaseService
     {
         return 'https://botpc28.testx2.cc/api/pay/notify';
     }
+
+    // 获取商户ID
+    public static function getMerchantId()
+    {
+        return config('app.tree_payment_merchant_id');
+    }
+    // 获取商户ID
+    public static function getSecret()
+    {
+        return config('app.tree_payment_secret');
+    }
+
+
     
     /**
      * @description: 获取请求客户端
@@ -47,8 +60,8 @@ class SanJinService extends BaseService
      */    
     public static function findBalance()
     {
-        $merchant_id  = config('app.tree_payment_merchant_id');
-        $secret = config('app.tree_payment_secret');
+        $merchant_id  = self::getMerchantId();
+        $secret = self::getSecret();
         $sign = md5($merchant_id . $secret);
         $data = [
             'merchantNum' => $merchant_id,
@@ -70,8 +83,8 @@ class SanJinService extends BaseService
      */    
     public static function findPayoutOrderInfo($order_no)
     {
-        $merchant_id  = config('app.tree_payment_merchant_id');
-        $secret = config('app.tree_payment_secret');
+        $merchant_id  = self::getMerchantId();
+        $secret = self::getSecret();
 
         $data = [];
         $data['merchantNum'] = $merchant_id;
@@ -93,8 +106,8 @@ class SanJinService extends BaseService
      */    
     public static function findPayOrderInfo($order_no)
     {
-        $merchant_id  = config('app.tree_payment_merchant_id');
-        $secret = config('app.tree_payment_secret');
+        $merchant_id  = self::getMerchantId();
+        $secret = self::getSecret();
 
         $data = [];
         $data['merchantNum'] = $merchant_id;
@@ -113,8 +126,8 @@ class SanJinService extends BaseService
     public static function pay($amount, $order_no, $payType = self::ALIPAY_TO_ALIPAY)
     {
         $data = [];
-        $merchant_id  = config('app.tree_payment_merchant_id');
-        $secret = config('app.tree_payment_secret');
+        $merchant_id  = self::getMerchantId();
+        $secret = self::getSecret();
         $notify_url = self::getNotifyUrl();
         $data['merchantNum'] = $merchant_id;
         $data['amount'] = $amount;
@@ -144,8 +157,8 @@ class SanJinService extends BaseService
     public static function payout($amount, $order_no, $bank_name, $account, $card_no, $payType = self::ALIPAY_TO_ALIPAY)
     {
         $data = [];
-        $merchant_id  = config('app.tree_payment_merchant_id');
-        $secret = config('app.tree_payment_secret');
+        $merchant_id  = self::getMerchantId();
+        $secret = self::getSecret();
         $notify_url = self::getNotifyUrl();
 
         $data['merchantNum'] = $merchant_id;
@@ -156,10 +169,10 @@ class SanJinService extends BaseService
 
         switch($payType){
             case self::ALIPAY_TO_ALIPAY:
-                $bankName = '支付宝';
+                $bank_name = '支付宝';
                 break;
             case self::NUMBER_RMB:
-                $bankName = '数字人民币';
+                $bank_name = '数字人民币';
                 break;
         }
 
@@ -184,4 +197,19 @@ class SanJinService extends BaseService
         
         return $result;
     }
+
+    /**
+     * @description: 异步通知签名加密
+     * @param {*} $state
+     * @param {*} $order_no
+     * @param {*} $amount
+     * @return {*}
+     */    
+    public static function verifyNotifySign($state,$order_no,$amount)
+    {
+        $merchant_id = self::getMerchantId();
+        $secret = self::getSecret();
+        $sign = md5($state.$merchant_id.$order_no.$amount,$secret);
+        return $sign;
+    }
 }

+ 112 - 1
app/Services/PaymentOrderService.php

@@ -64,6 +64,9 @@ class PaymentOrderService extends BaseService
         if(isset($search['channel']) && !empty($search['channel'])){
             $where[] = ['channel', '=', $search['channel']];
         }
+        if(isset($search['order_no']) && !empty($search['order_no'])){
+            $where[] = ['order_no', '=', $search['order_no']];
+        }
         if(isset($search['id']) && !empty($search['id'])){
             $where[] = ['id', '=', $search['id']];
         }
@@ -144,12 +147,31 @@ class PaymentOrderService extends BaseService
         return $msg;
     }
 
+    /**
+     * @description: 创建代付订单
+     * @param {*} $memberId 会员
+     * @param {*} $amount   金额
+     * @param {*} $channel  提现通道
+     * @param {*} $bank_name  银行名称/支付宝
+     * @param {*} $account  姓名
+     * @param {*} $card_no  银行卡号/支付宝账号
+     * @return {*}
+     */    
     public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
     {
         $default_amount = $amount;
         $result = [];
         $result['chat_id'] = $memberId;
 
+        if($amount < 100){
+            $result['text'] = '提现金额最少100';
+            return $result;
+        }
+        if($amount > 49999){
+            $result['text'] = '提现金额最多49999';
+            return $result;
+        }
+
         // 在调用三方支付前开始事务
         DB::beginTransaction();
         
@@ -173,6 +195,7 @@ class PaymentOrderService extends BaseService
             $order_no = self::createOrderNo('sj'.$data['type'].'_', $memberId);
             $data['order_no'] = $order_no;
             $data['member_id'] = $memberId;
+            $data['free'] = $amount * 0.002 + 2;
             $amount = number_format($amount, 2, '.', '');
             $data['amount'] = $amount;
             $data['channel'] = $channel;
@@ -181,6 +204,7 @@ class PaymentOrderService extends BaseService
             $data['card_no'] = $card_no;
             $data['callback_url'] = SanJinService::getNotifyUrl();
             $data['status'] = self::STATUS_STAY;
+            $data['remark'] = '提现费率:0.2%+2';
 
             // 先预扣款(锁定资金)
             $wallet->available_balance = $available_balance;
@@ -202,7 +226,7 @@ class PaymentOrderService extends BaseService
                 $available_balance,
                 '三方提现',
                 $id,
-                '钱宝提现'
+                '钱宝提现费率:0.2%+2'
             );
 
             // 提交事务,确保预扣款成功
@@ -290,4 +314,91 @@ class PaymentOrderService extends BaseService
         return $result;
     }
 
+    /**
+     * @description: 接收三方订单
+     * @param {*} $params
+     * @return {*}
+     */    
+    public static function receiveOrder($params)
+    {
+        // 判断商户号是否一致
+        if($params['merchantNum'] == SanJinService::getMerchantId()){
+
+            $info = self::findOne(['order_no' => $params['orderNo']]);
+            if($info){
+                // 判断金额是不是正确认
+                if($info->amount != $params['amount']){
+                    return false;
+                }
+            
+                // 验证签名
+                $sign = SanJinService::verifyNotifySign($params['state'],$params['orderNo'],$params['amount']);
+                if($params['sign'] != $sign){
+                    return false;
+                }
+
+                // 代付
+                if($info->type == self::TYPE_PAYOUT){
+                    self::onSubmitPayout($params,$info);
+                }
+                // 代收
+                if($info->type == self::TYPE_PAY){
+
+                }
+            }
+
+        }
+        
+    }
+
+    /**
+     * @description: 处理代付订单
+     * @param {*} $params
+     * @return {*}
+     */    
+    public static function onSubmitPayout($params,$info)
+    {
+        $data = [];
+        $result = [];
+        $chat_id = $info->member_id;
+        DB::transaction();
+        
+        try{
+            if($params['state'] == 1){
+                $data['status'] = self::STATUS_SUCCESS;
+                $res = self::model()::where(['order_no' => $params['orderNo']])->update($data);
+                if($res){
+                    DB::commit();
+                    $text = "✅ 提现通知 \n";
+                    $text .= "提现平台:{$info->bank_name} \n";
+                    $text .= "收款人:{$info->account} \n";
+                    $text .= "收款卡号:{$info->card_no} \n";
+                    $text .= "提现金额:{$info->amount} \n";
+                    $text .= "提现成功,金额已到账,请注意查收!";
+                    self::sendMessage($chat_id,$text);
+                }
+            }else{
+                $data['status'] = self::STATUS_FAIL;
+                $res = self::model()::where(['order_no' => $params['orderNo']])->update($data);
+                if($res){
+                    DB::commit();
+                    $text = "❌ 提现通知 \n";
+                    $text .= "提现平台:{$info->bank_name} \n";
+                    $text .= "收款人:{$info->account} \n";
+                    $text .= "收款卡号:{$info->card_no} \n";
+                    $text .= "提现金额:{$info->amount} \n";
+                    $text .= "提现失败,金额已返回钱包,请注意查收!";
+                    self::sendMessage($chat_id,$text);
+                }
+            }
+        }catch(\Exception $e){
+            DB::rollBack();
+            // 回滚失败,需要记录告警,人工干预
+            Log::error('提现失败回滚异常: ' . $e->getMessage(), $params);
+        }
+
+
+        
+    }
+
 }

+ 33 - 0
database/migrations/2025_11_11_165501_add_fee_to_payment_orders_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payment_orders', function (Blueprint $table) {
+            //
+            $table->decimal('fee', 18, 2)->default(0)->comment('手续费');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payment_orders', function (Blueprint $table) {
+            //
+        });
+    }
+};