123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Listeners;
- use App\Events\FinishAccount;
- use App\Models\Amount;
- use App\Models\OrderW;
- use App\Models\Store;
- use App\Models\OrderFinishFailsW;
- use App\Handlers\SignHandler;
- use App\Models\User;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Http\Request;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class FinishAccountListeners
- {
- /**
- * Create the event listener.
- *
- * @return void
- */
- public function __construct()
- {
- }
- /**
- * Handle the event.
- *
- * @param FinishAccount $event
- * @return void
- */
- public function handle(FinishAccount $event)
- {
- $order_no=$event->data;
- Log::info('订单:'.$order_no.'开始分账完结');
- $order=OrderW::where('order_no',$order_no)->first();
- if($order->apply_cancel==2){
- Log::info($order->order_no.'订单已取消,不能完结订单。');
- exit;
- }
- $sub_mchid = Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $handler=new SignHandler();
- $mch_private_key=$handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handler->nonce_str();//随机字符串
- $data=[
- 'sub_mchid' => $sub_mchid,
- 'transaction_id'=> $order->wechat_pay_no,//微信支付单号
- 'out_order_no' =>'WJ'.substr($order->order_no,2),//订单号
- 'description' =>'分账完结',
- ];
- $sign=$handler->sign($url,'POST',$timestamp,$nonce,json_encode($data),$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,json_encode($data),$header);
- Log::info($result);
- $result=json_decode($result,true);
- if(isset($result['order_id']) && !empty($result['order_id'])){
- Log::info('分账完结成功');
- DB::transaction(function()use($order,$result) {
- OrderW::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>$result['order_id']]);
- $total_fee=round($order->pay_money*(1-0.006),2);
- // $total_fee=$order->pay_money;
- //店铺信息
- $store = Store::withTrashed()->where('is_failure',0)->where('id', $order->store_id)->lockForUpdate()->first();
- $start_amount = $store->available_amount;
- $store->available_amount += $total_fee;
- $start_money = $store->pending_amount;
- $store->pending_amount -= $order->pay_money;
- $fee = round($order->pay_money - round($order->pay_money*54/10000,2),2);
- $store->account += $fee;
- $store->save();
- $amount=Amount::where('order_no','WJ'.substr($order->order_no,2))->where('transaction_id',$order->wechat_pay_no)->where('type',2)->first();
- if(!$amount){
- Amount::create([
- 'user_id' => $order->user_id,
- 'store_id' => $order->store_id,
- 'order_no' => 'WJ'.substr($order->order_no,2),
- 'transaction_id' => $order->wechat_pay_no,
- 'money' => $order->pay_money,
- 'type' => 2, //结账
- 'status' => 1, //成功
- 'service_fee' => $order->pay_money-$total_fee,
- 'start_money' => $start_money,//结账前冻结金额
- 'end_money' => $store->pending_amount,//结账后冻结金额
- 'start_amount' => $start_amount,//结账前可用金额
- 'end_amount' => $store->available_amount,//结账后可用金额
- 'remark' => '到账',
- 'account' => $store->account,
- 'actual_money' => $fee
- ]);
- }
- OrderFinishFailsW::where('order_no',$order->order_no)->update([
- 'status'=>1
- ]);
- },5);
- Log::info('订单:'.$order_no.'分账完结成功');
- }else{
- Log::info('分账完结失败,记录失败订单');
- $finish_fails=OrderFinishFailsW::where('order_no',$order->order_no)->where('status',0)->first();
- if(empty($finish_fails)){
- OrderFinishFailsW::create([
- 'order_no'=>$order->order_no,
- 'store_id'=>$order->store_id,
- 'code'=>$result['code'],
- 'message'=>$result['message'],
- 'status'=>0,
- ]);
- }else{
- $day = floor((time()-strtotime($order->pay_at))/(24*60*60));
- Log::info('订单:'.$order_no.'分账完结失败,当前时间:'.date("Y-m-d H:i:s",time()).',订单支付时间:'.$order->pay_at.'支付已完成'.$day.'天');
- //分账资金的冻结期默认是180天。从订单支付成功之日起,180天内需要发起分账,若180天内未发起分账,待分账资金将会自动解冻给分账方。
- if($day>=185){
- if($result['message']=='分账金额不足'){
- DB::transaction(function()use($order) {
- OrderW::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>'no']);
- $total_fee=round($order->pay_money*(1-0.006),2);
- // $total_fee=$order->pay_money;
- //店铺信息
- $store = Store::withTrashed()->where('is_failure',0)->where('id', $order->store_id)->lockForUpdate()->first();
- $start_amount = $store->available_amount;
- $store->available_amount += $total_fee;
- $start_money = $store->pending_amount;
- $store->pending_amount -= $order->pay_money;
- $store->save();
- Amount::create([
- 'user_id' => $order->user_id,
- 'store_id' => $order->store_id,
- 'order_no' => 'AJ'.substr($order->order_no,2),
- 'transaction_id' => $order->wechat_pay_no,
- 'money' => $order->pay_money,
- 'type' => 2, //结账
- 'status' => 1, //成功
- 'service_fee' => $order->pay_money-$total_fee,
- 'start_money' => $start_money,//结账前冻结金额
- 'end_money' => $store->pending_amount,//结账后冻结金额
- 'start_amount' => $start_amount,//结账前可用金额
- 'end_amount' => $store->available_amount,//结账后可用金额
- 'remark' => '超过180天系统自动解冻到账',
- 'deleted_at' => date("Y-m-d H:i:s")
- ]);
- OrderFinishFailsW::where('order_no',$order->order_no)->update([
- 'status'=>1
- ]);
- },5);
- Log::info('订单:'.$order_no.'完结超时,结束完结');
- }
- }
- }
- }
- }
- }
|