Parcourir la source

修改提现回调

jun il y a 4 mois
Parent
commit
b98674ebb6

+ 5 - 2
app/Http/Controllers/Api/CustomAmount.php

@@ -45,8 +45,8 @@ class CustomAmount extends Api{
             $value['insert_time']	= date('Y-m-d H:i:s',$value['insert_time']);
             // 获取子列表
             $value['type_state']	= (string) $CustomAmountRecord->getPayType($value['buy_type'],$value['pay_type'],'name');
-            if ($value['buy_type'] == 2 &&  $value['status'] == 1){
-                $value['state'] 	= '提现中';
+            if ($value['buy_type'] == 2){
+                $value['state'] 	= $CustomAmountRecord->getState($value['status'],'state');
             }
             // 重组
             $list['data'][$key] 	= $value;
@@ -68,6 +68,9 @@ class CustomAmount extends Api{
         // 接收参数
         $recordId		= request('record_id',0);
         $info           = $CustomAmountRecord::query()->where(['custom_uid'=>$uid,'id'=>$recordId])->first(['id','buy_type','pay_type','prefix','description','insert_time','pay_time','status','transfer_bill_no','balance','amount']);
+        if($info){
+            $info['id_code']    =   $CustomAmountRecord->idToCode($info['id']);
+        }
         return			json_send(['code'=>'success','msg'=>'获取成功','data'=>$info]);
     }
 }

+ 37 - 28
app/Http/Controllers/Api/WechatTransfer.php

@@ -16,13 +16,13 @@ use WeChatPay\Crypto\Rsa;
 
 /**
  * 微信支付接口
- * 
+ *
  * @author JUN
- * 
+ *
  * */
 class WechatTransfer extends Api{
     /**
-     * 小程序微信支付提现				/api/wechat_pay/transfer
+     * 小程序微信提现	商户转账			/api/wechat_pay/transfer
      *
      * */
     public function transfer(Custom $Custom,CustomAmount $CustomAmount,CustomAmountRecord $CustomAmountRecord)
@@ -60,7 +60,6 @@ class WechatTransfer extends Api{
             $amountUpdate				= [
                 'amount'=>DB::raw('amount-'.$amount),
                 'use_amount'=>DB::raw('use_amount+'.$amount),
-                'transfer_amount'=>DB::raw('transfer_amount+'.$amount),
                 'update_time'=>time()
             ];
             $result             =   $CustomAmount::query()->where(['custom_uid'=>$uid])->update($amountUpdate);
@@ -81,7 +80,7 @@ class WechatTransfer extends Api{
                 'pay_type'	        =>	1,
                 'balance'	        =>	$balance,
                 'description'	    =>	'余额提现',
-                'status'	        =>	0,
+                'status'	        =>	1,
             ];
             //写入余额记录
             $recordResult     =   $CustomAmountRecord->add($data);
@@ -101,11 +100,11 @@ class WechatTransfer extends Api{
         ];
         return json_send(['code'=>'success','msg'=>'成功','data'=>$list]);
     }
-	
-	/**
-	 * 小程序微信支付回调				/api/wechat_pay/notify
-	 * 
-	 * */
+
+    /**
+     * 小程序微信商户转账回调				/api/wechat_pay/notify
+     *
+     * */
     public function notify()
     {
         Log::error('wechat/transfer_notify', '提现转账回调开始');
@@ -160,13 +159,18 @@ class WechatTransfer extends Api{
                 // 把解密后的文本转换为PHP Array数组
                 $inBodyResourceArray = (array)json_decode($inBodyResource, true);
                 Log::error('wechat/transfer_notify', '打印解密后的结果:' . json_encode($inBodyResourceArray));
-                Log::error('wechat/transfer_notify', '参数:' . $inBodyResourceArray['trade_state'] . '订单号' . $inBodyResourceArray['out_trade_no'] . '微信支付号' . $inBodyResourceArray['transaction_id']);
-                if ($inBodyResourceArray['trade_state'] == "SUCCESS") {
-                    $status      =  2;
-                    //更新余额订单状态
+                Log::error('wechat/transfer_notify', '参数:' . $inBodyResourceArray['state'] . '订单号' . $inBodyResourceArray['out_bill_no'] . '微信支付号' . $inBodyResourceArray['transfer_bill_no']);
+                $amountInfo = CustomAmountRecord::query()->where('out_bill_no','=',$inBodyResourceArray['out_bill_no'])->first();
+                if (!$amountInfo) {
+                    Log::error('wechat/transfer_notify', '获取余额记录信息失败' . json_encode($amountInfo));
+                    return json_send(['code'=>'FAIL']);
+                }
+                if ($inBodyResourceArray['state'] == "SUCCESS") {
+                    $transfer_amount    =   $inBodyResourceArray['transfer_amount']/100;
+                    //更新余额记录状态
                     $data = [
                         'pay_time' => time(),
-                        'status'=>$status,
+                        'status'=>2,
                     ];
                     $res = CustomAmountRecord::query()->where('out_bill_no','=',$inBodyResourceArray['out_bill_no'])->update($data);
                     Log::error('wechat/transfer_notify', '更新余额记录,snowflake_id:'.$inBodyResourceArray['out_bill_no'].';'. json_encode($res));
@@ -174,29 +178,34 @@ class WechatTransfer extends Api{
                         Log::error('wechat/transfer_notify', '更新余额记录失败' . json_encode($res));
                         return json_send(['code'=>'FAIL']);
                     }
+                    //更新用户提现成功数额
+                    $res = CustomAmount::query()->where('custom_uid','=',$amountInfo['custom_uid'])->update(['transfer_amount'=>DB::raw('transfer_amount+'.$transfer_amount)]);
+                    Log::error('wechat/transfer_notify', '更新用户提现成功数额,custom_uid:'.$amountInfo['custom_uid'].';'. json_encode($res));
+                    if (!$res) {
+                        Log::error('wechat/transfer_notify', '更新余额记录失败' . json_encode($res));
+                        return json_send(['code'=>'FAIL']);
+                    }
                     Log::error('wechat/transfer_notify', '支付回调完成 通知返回' . json_encode($data));
-                }elseif ($inBodyResourceArray['trade_state'] == "FAIL"){
-                    $status      =  3;
+                }elseif ($inBodyResourceArray['state'] == "FAIL"){
                     //更新余额订单状态
                     $data = [
-                        'status'=>$status
+                        'status'=>3
                     ];
                     $res    = CustomAmountRecord::query()->where('out_bill_no','=',$inBodyResourceArray['out_bill_no'])->update($data);
-                    Log::error('wechat/transfer_notify', '更新余额记录,snowflake_id:'.$inBodyResourceArray['out_bill_no'].';'. json_encode($res));
+                    Log::error('wechat/transfer_notify', '更新余额记录,out_bill_no:'.$inBodyResourceArray['out_bill_no'].';'. json_encode($res));
                     if (!$res) {
                         Log::error('wechat/transfer_notify', '更新余额记录失败' . json_encode($res));
                         return json_send(['code'=>'FAIL']);
                     }
-                    $amountInfo = CustomAmountRecord::query()->where('out_bill_no','=',$inBodyResourceArray['out_bill_no'])->first();
-                    if ($amountInfo){
-                        Log::error('wechat/transfer_notify', '获取余额记录信息失败' . json_encode($res));
-                        return json_send(['code'=>'FAIL']);
-                    }
                     //退回余额
-                    $res    =   CustomAmount::query()->where('uid','=',$amountInfo['custom_uid'])->increment('amount',$amountInfo['amount']);
-                    if (!$res){
-                        Log::error('wechat/transfer_notify', '退回余额失败' . json_encode($amountInfo));
-                        return json_send(['code'=>'FAIL']);
+                    $amountUpdate				= [
+                        'amount'=>DB::raw('amount+'.$amountInfo['amount']),
+                        'use_amount'=>DB::raw('use_amount-'.$amountInfo['amount']),
+                        'update_time'=>time()
+                    ];
+                    $result             =   CustomAmount::query()->where(['custom_uid'=>$amountInfo['custom_uid']])->update($amountUpdate);
+                    if (!$result) {
+                        return json_send(['code'=>'error','msg'=>'提现失败退回余额失败','data'=>['error'=>'退回用户余额失败']]);
                     }
                     Log::error('wechat/transfer_notify', '回调完成 通知返回' . json_encode($amountInfo));
                 }

+ 20 - 1
app/Models/CustomAmountRecord.php

@@ -3,6 +3,7 @@
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use App\Models\Traits\Amount\BuyType;
+use App\Models\Traits\Amount\Status;
 
 /**
  * 客户余额记录
@@ -10,7 +11,7 @@ use App\Models\Traits\Amount\BuyType;
  */
 class CustomAmountRecord extends Model
 {
-    use HasFactory,BuyType;
+    use HasFactory,BuyType,Status;
 
     // 与模型关联的表名
     protected $table = 'amount_record';
@@ -50,6 +51,24 @@ class CustomAmountRecord extends Model
         return                              $result;
     }
 
+    /**
+     * 编码转id
+     *
+     * @param  string $code 编码
+     *
+     */
+    public function codeToId($code){
+        return intval(str_ireplace('klye','',$code));
+    }
 
+    /**
+     * id转编码
+     *
+     * @param  int  $id 编码
+     *
+     */
+    public function idToCode($id){
+        return 'klye'. str_pad($id, 9, '0', STR_PAD_LEFT);
+    }
 
 }

+ 85 - 0
app/Models/Traits/Amount/Status.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Models\Traits\Amount;
+
+
+/**
+ * 订单状态
+ * 
+ */
+trait Status
+{
+
+    // 交易类型  与 支付方式
+    private     $statusList        =   ['1'=>[
+                                        'id'            =>1,
+                                        'name'          =>'申请提现',
+                                        'state'         =>'提现中',
+                                    ],'2'=>[
+                                        'id'            =>2,
+                                        'name'          =>'提现成功',
+                                        'state'         =>'提现成功',
+                                    ],'3'=>[
+                                        'id'            =>3,
+                                        'name'          =>'提现失败',// 已发货未收货
+                                        'state'         =>'提现失败',
+                                    ]];
+
+    /**
+     * 交易类型列表
+     * 
+     */
+    public function getStatusList(){
+        // 返回数据
+        return                  $this->statusList;
+    }
+
+    /**
+     * 获取交易类型
+     * 
+     * @param int       $status     交易状态
+     * @param string    $field      字段
+     * 
+     */
+    public function getState($status,$field=''){
+        // 获取交易类型
+        $status            = isset($this->statusList[$status]) ? $this->statusList[$status] : [];
+        // 如果存在需要的字段
+        if( $field )        return isset($status[$field]) ? $status[$field] : null;
+        // 返回结果
+        return              $status;
+    }
+
+
+    /**
+     * 获取交易类型
+     * 
+     * @param   string       $state     交易状态
+     * 
+     */
+    public function getStateToStatus($state){
+        // 状态列表
+        $list               = $this->getStatusList();
+        // 状态
+        foreach ($list as $value) {
+            if($value['state'] == $state )  return $value['id'];
+        }
+        // 返回结果
+        return              -1;
+    }
+
+
+    /**
+     * 获取交易类型
+     * 
+     * @param   string       getWeibanStatus     交易状态
+     * 
+     */
+    public function getWeibanStatus($weibanStatus){
+        // 状态列表
+        $list               = ['待付款'=>0,'待发货'=>2,'待收货'=>3,'已完成'=>8,'已退款'=>6,'取消交易'=>4];
+        // 返回结果
+        return              isset($list[$weibanStatus]) ? $list[$weibanStatus] : 0;
+    }
+
+}

+ 1 - 1
config/wechatpay.php

@@ -4,7 +4,7 @@ return array(
     'mchid'                 => "1612111355",//微信直连商户号mchid
     'certificate'           => '68E43B250D73977F1DC0E0D0827AF25A8BFB1777', //证书序列号
     'APIV3'                 => 'iD1xB1mH6zO7pP2wD1rU4aD2nH7oL2lN',
-    'notify_url'            => 'https://kailin.dfwy.tech/api/wechat_pay/notify',
+    'notify_url'            => 'https://kailin.dfwy.tech/api/wechat_transfer/notify',
     'private_key'           => '-----BEGIN PRIVATE KEY-----
 MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDBcmSf2j2gN8Ie
 +vx0GxqFDzxetgJX+sCju9wyxSiPiFGqNRp1dXyzmb0WMDZ4EHSuwhgMp9xdY6BE