FinishAccountListeners.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\FinishAccount;
  4. use App\Models\Amount;
  5. use App\Models\Order;
  6. use App\Models\Store;
  7. use App\Handlers\SignHandler;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class FinishAccountListeners
  11. {
  12. /**
  13. * Create the event listener.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. //
  20. }
  21. /**
  22. * Handle the event.
  23. *
  24. * @param FinishAccount $event
  25. * @return void
  26. */
  27. public function handle(FinishAccount $event)
  28. {
  29. $order_no=$event->data;
  30. Log::info('订单:'.$order_no.'开始分账完结');
  31. $order=Order::where('order_no',$order_no)->first();
  32. $sub_mchid = Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->value('sub_mchid');
  33. $url='https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
  34. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  35. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  36. $handler=new SignHandler();
  37. $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  38. $timestamp=time();//时间戳
  39. $nonce=$handler->nonce_str();//随机字符串
  40. $data=[
  41. 'sub_mchid' => $sub_mchid,
  42. 'transaction_id'=> $order->wechat_pay_no,//微信支付单号
  43. 'out_order_no' =>'WJ'.substr($order->order_no,2),//订单号
  44. 'description' =>'分账完结',
  45. ];
  46. $sign=$handler->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  47. $header=[
  48. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  49. 'Accept:application/json',
  50. 'User-Agent:' . $merchant_id,
  51. 'Content-Type:application/json',
  52. 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
  53. ];
  54. $result=$handler->curl($url,json_encode($data),$header);
  55. Log::info($result);
  56. $result=json_decode($result,true);
  57. if(isset($result['order_id']) && !empty($result['order_id'])){
  58. Log::info('分账完结成功');
  59. DB::transaction(function()use($order,$result) {
  60. Order::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>$result['order_id']]);
  61. $total_fee=round($order->pay_money*(1-0.006),2);
  62. //店铺信息
  63. $store = Store::withTrashed()->where('is_failure',0)->where('id', $order->store_id)->lockForUpdate()->first();
  64. $start_amount = $store->available_amount;
  65. $store->available_amount += $total_fee;
  66. $start_money = $store->pending_amount;
  67. $store->pending_amount -= $order->pay_money;
  68. $fee = round($order->pay_money - round($order->pay_money*54/10000,2),2);
  69. $store->account += $fee;
  70. $store->save();
  71. Amount::create([
  72. 'user_id' => $order->user_id,
  73. 'store_id' => $order->store_id,
  74. 'order_no' => 'WJ'.substr($order->order_no,2),
  75. 'transaction_id' => $order->wechat_pay_no,
  76. 'money' => $order->pay_money,
  77. 'type' => 2, //结账
  78. 'status' => 1, //成功
  79. 'service_fee' => $order->pay_money-$total_fee,
  80. 'start_money' => $start_money,//结账前冻结金额
  81. 'end_money' => $store->pending_amount,//结账后冻结金额
  82. 'start_amount' => $start_amount,//结账前可用金额
  83. 'end_amount' => $store->available_amount,//结账后可用金额
  84. 'remark' => '到账',
  85. 'account' => $store->account,
  86. 'actual_money' => $fee
  87. ]);
  88. },5);
  89. Log::info('订单:'.$order_no.'分账完结成功');
  90. }else{
  91. Log::info('分账完结失败');
  92. }
  93. }
  94. }