input('order_no'); try{ DB::transaction(function()use($order_no, &$order ,&$seconds){ $order=OrderW::where('order_no',$order_no)->lockForUpdate()->first(); $seconds=Carbon::now()->subSeconds(60)->toDateTimeString();//限制调起支付60秒 if(empty($order)){ throw new \Exception("订单有误"); } if($order->prepare_pay_at >= $seconds){ throw new \Exception("订单正在支付中"); } $order->prepare_pay_at=Carbon::now(); $order->save(); }); }catch(\Exception $e){ return $this->error('450001',$e->getMessage()); } $store=Store::where('id',$order->store_id)->first(); if(empty($store)){ return $this->error('450001','店铺不存在或者已被删除'); } if($store->status==1){ return $this->error('450001','店铺已被禁用'); } if(empty($order)){ return $this->error('450001','订单有误'); } if($order->apply_cancel == 2){ return $this->error('450001','订单已取消'); } if($order->is_pay != 0 || $order->status != 0){ return $this->error('450001','订单状态有误'); } if($order->account <= 0){ return $this->error('450001','支付金额有误'); } $openid=User::where('id',Auth::user()->id)->value('openid'); $options = $this->options($order->store_id); $payment = Factory::payment($options); $jssdk = $payment->jssdk; Log::info(round($order->account/1000,2)); Log::info(round($order->account/10)); $two_minutes=Carbon::now()->addSeconds(55)->toDateTimeString();//限制支付时间55秒 $cancel_time=Carbon::parse($order->created_at)->addMinutes(59)->toDateTimeString();//系统自动取消时间前一分钟 if(Carbon::now() >= $cancel_time){ return $this->error('450001','订单将被取消'); } if($two_minutes <= $cancel_time){ $expire=date("YmdHis",strtotime($two_minutes)); }else{ $expire=date("YmdHis",strtotime($cancel_time)); } Log::info('ASASS'.$expire); $attributes = [ 'time_expire' => $expire, 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI 'body' => '订单支付', // 订单说明 'out_trade_no' => mt_rand('1000','9999').'_'.$order_no, // 自定义订单号 'total_fee' => $order->account*100, // 单位:分 'sub_openid' => $openid, // 当前用户的openId 'profit_sharing' => 'Y' ]; $result = $payment->order->unify($attributes); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { $prepayId = $result['prepay_id']; $config = $jssdk->sdkConfig($prepayId); return response()->json([ 'error_code'=>200, 'msg'=>'成功', 'data'=>$config ]); } if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) { Log::info('订单支付失败:'.$order->user_id.'/'.$order_no.'/'.$order->account.'/'.$result['return_msg']); return response()->json([ 'error_code'=>401, 'msg'=> $result['return_msg'], ]); } Log::info('订单支付失败:'.$order->user_id.'/'.$order_no.'/'.$order->money.'/'.$result['err_code_des']); return response()->json([ 'error_code'=>401, 'msg'=> $result['err_code_des'], ]); } protected function options($id){ return [ 'app_id' => 'wxf536016d4923e584',//config('wechat.payment.default.app_id'), 'mch_id' => config('wechat.payment.default.mch_id'), 'sub_mch_id' => Store::where('id',$id)->value('sub_mchid'), 'key' => config('wechat.payment.default.key'), 'notify_url' => url('api/payment/notify') ]; } public function notify(Request $request) { Log::info('支付完成开始微信回调'); $options = [ 'app_id' => 'wxf536016d4923e584',//config('wechat.payment.default.app_id'), 'mch_id' => config('wechat.payment.default.mch_id'), 'key' => config('wechat.payment.default.key'), 'notify_url' => url('api/payment/notify') ]; $payment = Factory::payment($options); $response = $payment->handlePaidNotify(function ($message, $fail) { // 根据返回的订单号查询订单数据 Log::error($message); LogW::create([ 'content'=>json_encode($message), 'type'=>1 ]); $rand_no=explode('_',$message['out_trade_no'])[0]; $message['out_trade_no']=explode('_',$message['out_trade_no'])[1]; $order=OrderW::where('order_no',$message['out_trade_no'])->first(); if ($order->is_pay==1){ Log::info('订单号为:'.$message['out_trade_no'].'已经支付'); return true; } if (!$order || $order->status == 1) { Log::info('订单不存在:'.$message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']); return true;//订单不存在 或者 订单已支付 } if ($message['result_code'] === 'FAIL') { //支付失败 Log::warning('[wechat-pay]:' . json_encode($message, true)); return true; } if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态 // 支付成功后的业务逻辑 if($message['result_code'] === 'SUCCESS') { $total_fee=round($message['total_fee']/100,2); DB::connection('mysql_w')->transaction(function()use($order,$message,$total_fee,$rand_no){ //修改订单状态 $order->status = 1; $order->is_help = 1; $order->is_pay = 1; $order->pay_type = 1; $order->pay_money = $total_fee; $order->rand_no = $rand_no; $order->pay_open_id = $message['openid']; $order->self_pay = 1; $order->wechat_pay_no = $message['transaction_id']; $order->pay_at = $message['time_end']; $order->save(); //店铺信息 Log::info($message['out_trade_no']); Log::info($total_fee); $store=Store::where('id',$order->store_id)->lockForUpdate()->first(); $start_money= $store->pending_amount; $store->pending_amount += $total_fee; $store->save(); //规格销量 $details=OrderDetailW::where('order_no',$order->order_no)->get(); foreach($details as $key=>$val){ $where=[]; $where['goods_id']=$val->goods_id; $where['size']=$val->size; $where['type']=$val->type; $sku=StoreGoodsSku::where($where)->lockForUpdate()->first(); $sku->sale_num += $val->num; $sku->save(); //商品销量 $goods=StoreGoods::where('id',$val->goods_id)->lockForUpdate()->first(); $goods->sale_num += $val->num; $goods->save(); } //用户vip变更 $vipLog=VipLogW::where('user_id',$order->user_id) ->where('store_id',$order->store_id)->first(); /*张奇新增**/ // UserW::where('id',$order->user_id)->update(['is_vip'=>1]); if(empty($vipLog) && $order->total>=2){ VipLogW::create([ 'user_id'=>$order->user_id, 'store_id'=>$store->id, 'order_no'=>$message['out_trade_no'] ]); OrderW::where('order_no',$message['out_trade_no'])->update([ 'vip'=>1 ]); } //支付日志 Amount::create([ 'user_id' => $order->user_id, 'store_id' => $order->store_id, 'order_no' => $message['out_trade_no'], 'transaction_id'=> $message['transaction_id'], 'money' => $total_fee, 'type' => 1, //支付 'status' => 1, //成功 'service_fee' => 0, 'start_money' => $start_money, 'end_money' => $store->pending_amount, 'start_amount' => $store->available_amount,//支付前可用金额 'end_amount' => $store->available_amount,//支付后可用金额 'remark' => '收款', ]); },5); //减库存 DB::connection('mysql')->transaction(function()use($order) { event(new RetailOutboundEvent($order->order_no)); },5); } return true; }else{ Log::info('通信失败'); return $fail('通信失败,请稍后再通知我'); } }); return $response; } public function GetPay(){ // 根据返回的订单号查询订单数据 // $message=['appid' => 'wx5224793b7dc7f7b7', // 'bank_type' => 'OTHERS', // 'cash_fee' => '153700', // 'fee_type' => 'CNY', // 'is_subscribe' => 'Y', // 'mch_id' => '1603992271', // 'nonce_str' => '60628be3521c8', // 'openid' => 'ogTajwF_jHXaMTNcXLy1zT_XW-VU', // 'out_trade_no' => '2657_DX21033010243392320927494', // 'result_code' => 'SUCCESS', // 'return_code' => 'SUCCESS', // 'sign' => '8CC558CED364CB2384F188EBEF5D3255', // 'sub_mch_id' => '1607799110', // 'time_end' => '20210330102440', // 'total_fee' => '153700', // 'trade_type' => 'JSAPI', // 'transaction_id' => '4200000896202103308732225144',]; // Log::error($message); Log::error('主动回调'); $rand_no=explode('_',$message['out_trade_no'])[0]; $message['out_trade_no']=explode('_',$message['out_trade_no'])[1]; $order=OrderW::where('order_no',$message['out_trade_no'])->first(); if (!$order || $order->status == 1) { Log::info('订单不存在:'.$message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']); return true;//订单不存在 或者 订单已支付 } if ($message['result_code'] === 'FAIL') { //支付失败 Log::warning('[wechat-pay]:' . json_encode($message, true)); return true; } if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态 // 支付成功后的业务逻辑 if($message['result_code'] === 'SUCCESS') { $total_fee=round($message['total_fee']/100,2); DB::connection('mysql_w')->transaction(function()use($order,$message,$total_fee,$rand_no){ //修改订单状态 $order->status = 1; $order->is_pay = 1; $order->pay_type = 1; $order->pay_money = $total_fee; $order->rand_no = $rand_no; $order->pay_open_id = $message['openid']; $order->self_pay = 1; $order->wechat_pay_no = $message['transaction_id']; $order->pay_at = $message['time_end']; $order->save(); //店铺信息 $store=Store::where('id',$order->store_id)->lockForUpdate()->first(); $start_money= $store->pending_amount; Log::error($store->pending_amount); Log::error($total_fee); $store->pending_amount += $total_fee; $store->save(); //规格销量 $details=OrderDetailW::where('order_no',$order->order_no)->get(); foreach($details as $key=>$val){ $where=[]; $where['goods_id']=$val->goods_id; $where['size']=$val->size; $where['type']=$val->type; $sku=StoreGoodsSku::where($where)->lockForUpdate()->first(); $sku->sale_num += $val->num; $sku->save(); //商品销量 $goods=StoreGoods::where('id',$val->goods_id)->lockForUpdate()->first(); $goods->sale_num += $val->num; $goods->save(); } //用户vip变更 $vipLog=VipLogW::where('user_id',$order->user_id) ->where('store_id',$order->store_id)->first(); /*张奇新增**/ UserW::where('id',$order->user_id)->update(['is_vip'=>1]); if(empty($vipLog) && $order->total>=2){ VipLogW::create([ 'user_id'=>$order->user_id, 'store_id'=>$store->id, 'order_no'=>$message['out_trade_no'] ]); OrderW::where('order_no',$message['out_trade_no'])->update([ 'vip'=>1 ]); } //支付日志 Amount::create([ 'user_id' => $order->user_id, 'store_id' => $order->store_id, 'order_no' => $message['out_trade_no'], 'transaction_id'=> $message['transaction_id'], 'money' => $total_fee, 'type' => 1, //支付 'status' => 1, //成功 'service_fee' => 0, 'start_money' => $start_money, 'end_money' => $store->pending_amount, 'start_amount' => $store->available_amount,//支付前可用金额 'end_amount' => $store->available_amount,//支付后可用金额 'remark' => '收款', ]); },5); //减库存 DB::connection('mysql')->transaction(function()use($order) { event(new RetailOutboundEvent($order->order_no)); },5); } return true; } } }