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 ]); //添加或者修改的数据 }); } } }