123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Amount;
- use App\Models\Store;
- use App\Handlers\SignHandler;
- use App\Models\WithdrawLog;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class Withdraw extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'withdraw';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '定时查询提现状态';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- // Log::info('监测查询提现状态定时任务。。。。。');
- $amount=Amount::where('status','0')->where('status_code','CREATE_SUCCESS')->where('type',3)->orderBy('id')->limit(50)->get();
- // Log::info($amount);
- $handler=new SignHandler();
- foreach($amount as $key=>$val){
- $withdraw_id=$val->transaction_id;
- $out_request_no=$val->order_no;
- $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->value('sub_mchid');
- // $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$out_request_no.'?sub_mchid='.$sub_mchid;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handler->nonce_str();//随机字符串
- $body="";
- $sign=$handler->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
- ];
- $result=$handler->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- if(!isset($result['status'])){
- Log::info($val->order_no.':提现查询失败,out-request-no:'.$out_request_no.'?sub_mchid:'.$sub_mchid);
- // Log::info($result);
- continue;
- }
- DB::transaction(function()use($result,$val,$withdraw_id,$out_request_no,$sub_mchid){
- if($result['status']=='SUCCESS'){
- $store=Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->lockForUpdate()->first();
- $start_amount=$store->available_amount;
- $store->available_amount -= $val->money;
- $store->withdrawal += $val->money;
- $store->account -= $result['amount']/100;
- $store->save();
- $amo = Amount::where('transaction_id',$result['withdraw_id'])->where('order_no',$result['out_request_no'])->where('type',3)->where('status',1)->first();
- if(!$amo){
- Amount::create([
- 'transaction_id'=>$result['withdraw_id'],
- 'order_no'=>$result['out_request_no'],
- 'store_id'=>$store->id,
- 'money'=>$result['amount']/100,
- 'type'=>3,//提现
- 'status'=>1,
- 'status_code'=>$result['status'],
- 'service_fee'=>0,
- 'remark'=>'提现成功',
- 'remark_amount'=>$result['account_type'].'-'.$result['account_number'].'-'.$result['account_bank'].'-'.$store->account_bank.'-'.$store->account_number,
- 'account'=>$store->account
- ]);
- }
- Amount::where('id',$val->id)->update(['status_code'=>'SUCCESS']);
- // Amount::where('transaction_id',$withdraw_id)
- // ->update([
- // 'start_money'=>$store->pending_amount,
- // 'end_money'=>$store->pending_amount,
- // 'start_amount'=>$start_amount,
- // 'end_amount'=> $store->available_amount,
- // 'status'=>1,
- // 'status_code'=>$result['status'],
- // 'account'=> $store->account,
- // 'actual_money'=> $result['amount']/100
- // ]);
- }else{
- // Log::info($val->order_no.':提现查询失败,out-request-no:'.$out_request_no.'?sub_mchid:'.$sub_mchid);
- // Log::info(json_encode($result));
- if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- $reason=$result['reason'];
- }else{
- $reason=null;
- }
- Amount::where('transaction_id',$withdraw_id)
- ->update([
- 'status'=>0,
- 'status_code'=>$result['status'],
- 'reason'=>$reason,
- ]);
- }
- WithdrawLog::updateOrCreate(
- ['sub_mchid' => $result['sub_mchid'],'sp_mchid' => $result['sp_mchid'],'withdraw_id' => $result['withdraw_id'],'out_request_no' => $result['out_request_no']], //查询条件
- [
- 'status' => $result['status'],'amount' => $result['amount'],'create_time' => $result['create_time'],'update_time' => $result['update_time'],'reason' => $result['reason'],
- 'remark' => $result['remark'],'bank_memo' => $result['bank_memo'],'account_type' => $result['account_type'],'account_number' => $result['account_number'],
- 'account_bank' => $result['account_bank'],'bank_name' => array_key_exists('bank_name',$result)?$result['bank_name']:null
- ]); //添加或者修改的数据
- });
- }
- }
- }
|