input('order_no'); $order=OrderW::where('order_no',$order_no)->lockForUpdate()->first(); $store=Store::where('id',$order->store_id)->first(); if(empty($store)){ return $this->error('450001','','店铺不存在或者已被删除'); } if($store->status==1){ 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','','支付金额有误'); } //修改订单状态 $order->status = 1; $order->is_help = 1; $order->is_pay = 1; $order->pay_type = 1; $order->save(); return response()->json([ 'error_code'=>200, 'msg'=>'成功', ]); } public function pay(Request $request) { Log::info($request); $order_no=$request->input('order_no'); $tradeType=$request->input('type','APP');//支付场景,APP/JSAPI $is_assit=$request->input('is_assit',0); if(!in_array($tradeType,['APP','JSAPI'])){ return $this->error('450001','支付场景有误'); } try{ DB::transaction(function()use($order_no, $is_assit ,&$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("订单正在支付中"); } if($is_assit){ $order->self_pay=2; }else{ $order->self_pay=1; } $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($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','支付金额有误'); } $options = $this->options($order->store_id,$tradeType); $payment = Factory::payment($options); $jssdk = $payment->jssdk; Log::info('订单支付金额:'.$order->account); $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)); } $attributes = [ 'time_expire' => $expire, // 'trade_type' => 'JSAPI', 'trade_type' => $tradeType, // 支付方式,小程序支付使用JSAPI、APP 'body' => '订单支付', // 订单说明 'out_trade_no' => mt_rand('1000','9999').'_'.$order_no, // 自定义订单号 'total_fee' => $order->account*100, // 单位:分 // 'total_fee' => 1, // 单位:分 'profit_sharing' => 'Y' //分账 ]; if($tradeType=='JSAPI'){ if($is_assit){ //助手支付 $openid=Assit::where('agent_id',Auth::user()->id)->value('openid'); if(empty($openid)){ return $this->error('450001','参数有误,请重新登录助手帐号'); } }else{ $openid=User::where('id',Auth::user()->id)->value('openid'); } $attributes['openid']=$openid; // 当前用户的openId } Log::info([$options,$attributes,$order->account*100]); $result = $payment->order->unify($attributes); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { Log::info($result); $order_info['order']=$order_no; $order_info['price']=$order->account; Cache::put('prepay_id:'.$result['prepay_id'],$order_info,3600*2); $prepayId = $result['prepay_id']; if($tradeType == 'JSAPI'){ $config = $jssdk->sdkConfig($prepayId, false); }else{ $config = $jssdk->appConfig($prepayId); } Log::info($config); return response()->json([ 'error_code'=>200, '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'=>4011, 'code'=>4011, 'msg'=> $result['return_msg'], ]); } Log::info('订单支付失败:'.$order->user_id.'/'.$order_no.'/'.$order->account.'/'.$result['err_code_des']); return response()->json([ 'error_code'=>4011, 'code'=>4011, 'msg'=> $result['err_code_des'], ]); } public function getOrderNo(Request $request){ $prepay_id=$request->input('prepay_id'); $order_info=Cache::get('prepay_id:'.$prepay_id); if($order_info){ Cache::forget('prepay_id:'.$prepay_id); } return $this->success($order_info); } protected function options($id,$type) { if($type=='APP'){ $arr= [ 'app_id' => config('wechat.daweiboshi_app.default.app_id'), 'key' => config('wechat.payment.test.key'), 'mch_id' => config('wechat.payment.test.mch_id'), 'notify_url' => url('api/payment/app_notify'), ]; }else{ $arr= [ 'app_id' => config('wechat.payment.test.app_id'), 'key' => config('wechat.payment.test.key'), 'mch_id' => config('wechat.payment.test.mch_id'), 'notify_url' => url('api/payment/h5_notify'), ]; } if($id){ $arr['sub_mch_id'] = Store::where('id',$id)->value('sub_mchid'); } return $arr; } public function h5_notify(Request $request) { Log::info('h5支付完成开始微信回调'); $options = $this->options(null,'JSAPI'); $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'].'已经支付'); if($order->wechat_pay_no != $message['transaction_id']){ OrderOnceW::create([ 'order_no'=>$message['out_trade_no'], 'open_id'=>$message['openid'], 'wechat_no'=>$message['transaction_id'], 'result_code'=>$message['result_code'], 'type'=> 1, ]); } 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') { //处理订单支付完整后的信息 $this->settlement($order,$message,$rand_no); } Log::info('微信支付回调结束'); return true; }else{ Log::info('通信失败'); return $fail('通信失败,请稍后再通知我'); } }); return $response; } public function app_notify(Request $request) { Log::info('app支付完成开始微信回调'); $options = $this->options(null,'APP'); $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'].'已经支付'); if($order->wechat_pay_no != $message['transaction_id']){ OrderOnceW::create([ 'order_no'=>$message['out_trade_no'], 'open_id'=>$message['openid'], 'wechat_no'=>$message['transaction_id'], 'result_code'=>$message['result_code'], 'type'=> 1, ]); } 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') { //处理订单支付完整后的信息 $this->settlement($order,$message,$rand_no); } Log::info('微信支付回调结束'); return true; }else{ Log::info('通信失败'); return $fail('通信失败,请稍后再通知我'); } }); return $response; } private function settlement($order,$message,$rand_no){ $total_fee=round($message['total_fee']/100,2); DB::connection('mysql_w')->transaction(function()use($order,$message,$total_fee,$rand_no){ // $basic=ZbsBasic::whereIn('keys',['round_start_time','round_end_time','season'])->pluck('value','keys')->toArray(); // $start_time=$basic['round_start_time']; // $end_time=$basic['round_end_time']; // $season=$basic['season']; // Log::info('0000000-00000'.$start_time.'///'.$end_time.'///'.$season); // $sto=Store::where('id',$order->store_id)->first(); // $userzs = UserZ::where('dwbs_id', $sto->user_id)->get(); // // $order->is_zbs = 0; $integral=$order->total*2; $zbs=0; //不在争霸赛期间 // // if(count($userzs) > 0) { //争霸赛存在该用户 //// if(!empty($userz)) { //争霸赛存在该用户 // $now = Carbon::now()->timestamp; // $pay_time=Carbon::parse($message['time_end'])->timestamp; // Log::info('<<<<<>>>>>'.$now.'///'.$start_time.'///'.$end_time.'///'.$pay_time); // if ($pay_time >= $start_time && $pay_time <= $end_time) { //在争霸赛期间 // $uids = \Illuminate\Support\Facades\Cache::remember('uids', Carbon::now()->addHours(12), function () use ($season) { // return ZbsEnroll::where('season', $season)->where('is_refund', 0)->where('status', 0)->pluck('uid')->toArray(); // }); // // foreach($userzs as $key =>$val){ // if (in_array($val->id, $uids)) {//报名了争霸赛 // //标记争霸赛订单 // $order->is_zbs = 1; // //学分变更 // $integral=0; // $zbs=1; //在争霸赛期间 // } // } // } // } $inte=IntegralW::where('order_no',$order->order_no)->where('store_id',$order->store_id)->where('type',1)->first(); if(empty($inte)){ IntegralW::create([ 'store_id'=>$order->store_id, 'order_no'=>$order->order_no, 'num'=>$order->total, 'integral'=>$integral, 'integral_double'=>$integral, 'type'=>1, 'remark'=>'下单成功', 'is_zbs'=>$zbs, ]); } //修改订单状态 $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->integral += $integral; $store->cycle_inte += $integral; $store->total += $order->total; $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(); //减微店代理库存 $arr_test=Store::getTest(); if(in_array($order->store_id,$arr_test)){ //判断是不是减库存代理 Log::info('【'.$store->name.'的店铺-'.$goods->main_attr.'-'.$val->type.'-'.$val->size); $storeInventory=InventoryW::where(['store_id'=>$order->store_id,'goods_id'=>$val->goods_id,'sku_id'=>$val->sku_id])->lockForUpdate()->first(); $storeInve_num=!empty($storeInventory)?$storeInventory->num:0; if($val->num > $storeInve_num){ Log::info('【'.$store->name.'的店铺-'.$goods->main_attr.'-'.$val->type.'-'.$val->size.'】库存不足'); } Log::info($val->num .'---'. $storeInve_num); $storeInventory->num-=$val->num; $storeInventory->save(); } } //用户vip变更 $vipLog=VipLogW::where('user_id',$order->user_id) ->where('store_id',$order->store_id)->first(); if(empty($vipLog) && $order->total>=2){ VipLogW::create([ 'user_id'=>$order->user_id, 'store_id'=>$store->id, 'order_no'=>$message['out_trade_no'], 'type'=>2,//购买两套,添加vip 'remark'=>'购买自动添加vip' ]); 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); //算争霸赛学分 // ZbsOrder::create([ // 'order_id'=>$order->id, // 'status'=>0, // 'is_cancel'=>0, // ]); //代下单通知客户 $user=UserW::where('id',$order->user_id)->first(); $agent_id=Store::where('id',$order->store_id)->value('user_id'); $agent = User::where('id',$agent_id)->first(); if($user->openId){ $data['openid']=$user->openId; $data['data']=[ 'first' =>'代理【'.$agent->nickname .'】已帮您下单成功,请及时查看订单', 'keyword1' => $order->order_no, 'keyword2' => number_format($order->account, 2), 'keyword3' => $order->op_name, 'keyword4' => $agent->mobile, 'remark' => '点击链接进入系统,查看详情', ]; event(new HelpOrderEvent($data)); } //代下单通知代理 //新订单通知 // $store=Store::where('id',$order->store_id)->first(); // $user=User::where('id',$store->user_id)->first(); if($agent->openid){ $data['openid']=$agent->openid; $data['data']=[ 'first' => '您有新的客户订单,请及时处理', 'keyword1' => $user->nickname, 'keyword2' => number_format($order->account, 2), 'keyword3' => $order->order_no, 'keyword4' => $order->created_at ]; event(new NewOrder($data)); } } 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); //代下单通知客户 $user=UserW::where('id',$order->user_id)->first(); $agent_id=Store::where('id',$order->store_id)->value('user_id'); $agent = User::where('id',$agent_id)->first(); if($user->openId){ $data['openid']=$user->openId; $data['data']=[ 'first' =>'代理【'.$agent->nickname .'】已帮您下单成功,请及时查看订单', 'keyword1' => $order->order_no, 'keyword2' => number_format($order->account, 2), 'keyword3' => $order->op_name, 'keyword4' => $agent->mobile, 'remark' => '点击链接进入系统,查看详情', ]; event(new HelpOrderEvent($data)); } //代下单通知代理 //新订单通知 // $store=Store::where('id',$order->store_id)->first(); // $user=User::where('id',$store->user_id)->first(); if($agent->openid){ $data['openid']=$agent->openid; $data['data']=[ 'first' => '您有新的客户订单,请及时处理', 'keyword1' => $user->nickname, 'keyword2' => number_format($order->account, 2), 'keyword3' => $order->order_no, 'keyword4' => $order->created_at ]; event(new NewOrder($data)); } } return true; } } public function getc(){ $arr=['DD21052507413880022425375','DD21052815445331406169875', 'DD21072617031211447413371', 'DD21072409242307226852652', 'DX21062619411395558884771', 'DX21060322150044423898976', 'DX21072722274881359336010', 'DX21072722054667762245663', 'DX21061221143924883861697', 'DX21081212124361685786288', 'DX21080516405831422592274', 'DX21081222191558554151936', 'DD21070114344465217624191', 'DX21062612370251221614394', 'DD21081810585174889697435', 'DD21081717423715556697940', 'DX21092320373487075958695', 'DD21091008310834739743203', 'DX21091516461172167273109', 'DX21091516485490330788346', 'DX21091421560651633921802', 'DX21091521223258511559791', 'DX21092320335761762613987']; $orders=OrderW::with(['store:id,name,phone','user:id,nickname,phone']) ->whereIn('order_no',$arr) ->select('order_no','status','store_id','user_id','signature','is_finish','express_info','express_time','created_at') ->orderBy('store_id') ->get(); return $this->success($orders); } }