FinishAccountListeners.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\FinishAccount;
  4. use App\Models\Amount;
  5. use App\Models\OrderW;
  6. use App\Models\Store;
  7. use App\Models\OrderFinishFailsW;
  8. use App\Handlers\SignHandler;
  9. use App\Models\User;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class FinishAccountListeners
  17. {
  18. /**
  19. * Create the event listener.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. }
  26. /**
  27. * Handle the event.
  28. *
  29. * @param FinishAccount $event
  30. * @return void
  31. */
  32. public function handle(FinishAccount $event)
  33. {
  34. $order_no=$event->data;
  35. Log::info('订单:'.$order_no.'开始分账完结');
  36. $order=OrderW::where('order_no',$order_no)->first();
  37. if($order->apply_cancel==2){
  38. Log::info($order->order_no.'订单已取消,不能完结订单。');
  39. exit;
  40. }
  41. $sub_mchid = Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->value('sub_mchid');
  42. $url='https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
  43. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  44. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  45. $handler=new SignHandler();
  46. $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  47. $timestamp=time();//时间戳
  48. $nonce=$handler->nonce_str();//随机字符串
  49. $data=[
  50. 'sub_mchid' => $sub_mchid,
  51. 'transaction_id'=> $order->wechat_pay_no,//微信支付单号
  52. 'out_order_no' =>'WJ'.substr($order->order_no,2),//订单号
  53. 'description' =>'分账完结',
  54. ];
  55. $sign=$handler->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  56. $header=[
  57. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  58. 'Accept:application/json',
  59. 'User-Agent:' . $merchant_id,
  60. 'Content-Type:application/json',
  61. 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
  62. ];
  63. $result=$handler->curl($url,json_encode($data),$header);
  64. Log::info($result);
  65. $result=json_decode($result,true);
  66. if(isset($result['order_id']) && !empty($result['order_id'])){
  67. Log::info('分账完结成功');
  68. DB::transaction(function()use($order,$result) {
  69. OrderW::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>$result['order_id']]);
  70. $total_fee=round($order->pay_money*(1-0.006),2);
  71. // $total_fee=$order->pay_money;
  72. //店铺信息
  73. $store = Store::withTrashed()->where('is_failure',0)->where('id', $order->store_id)->lockForUpdate()->first();
  74. $start_amount = $store->available_amount;
  75. $store->available_amount += $total_fee;
  76. $start_money = $store->pending_amount;
  77. $store->pending_amount -= $order->pay_money;
  78. $fee = round($order->pay_money - round($order->pay_money*54/10000,2),2);
  79. $store->account += $fee;
  80. $store->save();
  81. $amount=Amount::where('order_no','WJ'.substr($order->order_no,2))->where('transaction_id',$order->wechat_pay_no)->where('type',2)->first();
  82. if(!$amount){
  83. Amount::create([
  84. 'user_id' => $order->user_id,
  85. 'store_id' => $order->store_id,
  86. 'order_no' => 'WJ'.substr($order->order_no,2),
  87. 'transaction_id' => $order->wechat_pay_no,
  88. 'money' => $order->pay_money,
  89. 'type' => 2, //结账
  90. 'status' => 1, //成功
  91. 'service_fee' => $order->pay_money-$total_fee,
  92. 'start_money' => $start_money,//结账前冻结金额
  93. 'end_money' => $store->pending_amount,//结账后冻结金额
  94. 'start_amount' => $start_amount,//结账前可用金额
  95. 'end_amount' => $store->available_amount,//结账后可用金额
  96. 'remark' => '到账',
  97. 'account' => $store->account,
  98. 'actual_money' => $fee
  99. ]);
  100. }
  101. OrderFinishFailsW::where('order_no',$order->order_no)->update([
  102. 'status'=>1
  103. ]);
  104. },5);
  105. Log::info('订单:'.$order_no.'分账完结成功');
  106. }else{
  107. Log::info('分账完结失败,记录失败订单');
  108. $finish_fails=OrderFinishFailsW::where('order_no',$order->order_no)->where('status',0)->first();
  109. if(empty($finish_fails)){
  110. OrderFinishFailsW::create([
  111. 'order_no'=>$order->order_no,
  112. 'store_id'=>$order->store_id,
  113. 'code'=>$result['code'],
  114. 'message'=>$result['message'],
  115. 'status'=>0,
  116. ]);
  117. }else{
  118. $day = floor((time()-strtotime($order->pay_at))/(24*60*60));
  119. Log::info('订单:'.$order_no.'分账完结失败,当前时间:'.date("Y-m-d H:i:s",time()).',订单支付时间:'.$order->pay_at.'支付已完成'.$day.'天');
  120. //分账资金的冻结期默认是180天。从订单支付成功之日起,180天内需要发起分账,若180天内未发起分账,待分账资金将会自动解冻给分账方。
  121. if($day>=185){
  122. if($result['message']=='分账金额不足'){
  123. DB::transaction(function()use($order) {
  124. OrderW::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>'no']);
  125. $total_fee=round($order->pay_money*(1-0.006),2);
  126. // $total_fee=$order->pay_money;
  127. //店铺信息
  128. $store = Store::withTrashed()->where('is_failure',0)->where('id', $order->store_id)->lockForUpdate()->first();
  129. $start_amount = $store->available_amount;
  130. $store->available_amount += $total_fee;
  131. $start_money = $store->pending_amount;
  132. $store->pending_amount -= $order->pay_money;
  133. $store->save();
  134. Amount::create([
  135. 'user_id' => $order->user_id,
  136. 'store_id' => $order->store_id,
  137. 'order_no' => 'AJ'.substr($order->order_no,2),
  138. 'transaction_id' => $order->wechat_pay_no,
  139. 'money' => $order->pay_money,
  140. 'type' => 2, //结账
  141. 'status' => 1, //成功
  142. 'service_fee' => $order->pay_money-$total_fee,
  143. 'start_money' => $start_money,//结账前冻结金额
  144. 'end_money' => $store->pending_amount,//结账后冻结金额
  145. 'start_amount' => $start_amount,//结账前可用金额
  146. 'end_amount' => $store->available_amount,//结账后可用金额
  147. 'remark' => '超过180天系统自动解冻到账',
  148. 'deleted_at' => date("Y-m-d H:i:s")
  149. ]);
  150. OrderFinishFailsW::where('order_no',$order->order_no)->update([
  151. 'status'=>1
  152. ]);
  153. },5);
  154. Log::info('订单:'.$order_no.'完结超时,结束完结');
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }