WithdrawCorrectionTwo.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Amount;
  4. use App\Models\Store;
  5. use App\Handlers\SignHandler;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. class WithdrawCorrectionTwo extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'withdraw_correction_two';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '提现修正';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. Log::info('提现修正2任务正在运行 ing');
  39. $date=date("Y-m-d",strtotime('-7 days'));
  40. $handle=new SignHandler();
  41. $refunds=Amount::where('type',3)->where('status',1)->whereDate('created_at',$date)->orderBy('id')->get();
  42. foreach($refunds as $key=>$val){
  43. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->value('sub_mchid');
  44. $withdraw_id=$val->transaction_id;
  45. $out_request_no=$val->order_no;
  46. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  47. // $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$out_request_no.'?sub_mchid='.$sub_mchid;
  48. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  49. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  50. $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  51. $timestamp=time();//时间戳
  52. $nonce=$handle->nonce_str();//随机字符串
  53. $body="";
  54. $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  55. $header=[
  56. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  57. 'Accept:application/json',
  58. 'User-Agent:' . $merchant_id,
  59. 'Content-Type:application/json',
  60. 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
  61. ];
  62. $result=$handle->curl($url,'',$header,'GET');
  63. $result=json_decode($result,true);
  64. if($val->status_code<>$result['status']){
  65. Log::info('提现2状态有误:'.$val->store_id.'---'.$val->order_no.'---'.$result['status']);
  66. Store::withTrashed()->where('is_failure',0)->where('id',$val->store_id)->decrement('withdrawal',$val->money);
  67. if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  68. $reason=$result['reason'];
  69. }else{
  70. $reason=null;
  71. }
  72. Amount::where('id',$val->id)
  73. ->update([
  74. 'status'=>0,
  75. 'status_code'=>$result['status'],
  76. 'reason'=>$reason
  77. ]);
  78. }
  79. }
  80. }
  81. }