middleware(function ($request, $next) { $this->input_status = $request->status; $this->order_id = $request->order_id; $this->orderObj = Order::find($this->order_id); if ($this->orderObj->merchant_id != 0) { $this->serverObj = $this->orderObj->merchants; } view()->share('order_id', $this->order_id); view()->share('order', $this->orderObj); return $next($request); }); } public function handle($order_id, $type, Request $request) { return $this->$type($request); } public function handlePost($order_id, $type, Request $request) { return $this->$type($request); } public function __call($method, $parameters) { return response()->json(['error' => 1, 'msg' => '该方法不存在']); } //更新支付订单 public function pay(Request $request) { $this->setViewPath('', 'pay'); $data = [ 'pay_type' => config('adconfig.pay_type') ]; return $this->display($data); } public function payUpdate(Request $request) { //更新订单支付,支付类型,支付时间,支付状态,订单状态,写入更新操作日志 if ($request->input('pay_time') == '' || $request->input('pay_price') == '') { return $this->msg(['error' => 1, 'msg' => '请填写必要参数']); } //更新订单 $order = $this->orderObj; $order->pay_ok_at = $request->input('pay_time');//支付时间 $order->pay_money = $request->input('pay_price');//支付金额 $order->pay_type = $request->input('pay_type'); // $pay_rate=($request->input('pay_price')*$this->serverObj->ratio)/1000; if($order->pay_status==1) { return $this->msg(['error' => 1, 'msg' => '已经支付']); } $order->pay_status = 1; //最后金额计算,这里提交订单的时候就计算了,所以注释掉 // $order->total_money=$request->input('pay_price')-$pay_rate; //$order->ratio=$this->serverObj->ratio; //手续费 //$order->pay_rate=$pay_rate; //原价 //本站也回调更新 $order->notify_status = 1;// $order->status = 2;//订单状态:已支付,待回调 $r = $order->save(); if ($r) { if ($order->merchant_id != 0) { //后台确认支付的,需要判断是否有代理,如果有代理需要给佣金 //判断是否有股东佣金 if ($order->top_money != 0 || $order->next_ratio != 0) { //如果有,再来判断订单的商户上级商户是否还有上级 $top_id = Merchant::find($this->serverObj->from_id); //3级 if ($top_id->from_id != 0) { //代理上面的股东 //给股东算佣金 $merchant_data = [ 'merchant_id' => $order->merchant_id, 'from_merchant_id' => $top_id->from_id, 'gateway_id' => $order->gateway_id, 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_money' => $order->order_money, 'pay_money' => $order->pay_money, 'money' => $order->top_money, 'ratio' => $order->top_ratio,//返点 'name' => $this->serverObj->name . '的订单佣金', 'order_at' => $order->order_at, 'pay_ok_at' => $order->pay_ok_at, 'status' => '2',//佣金状态,改为处理中,结算时是按后台结算 ]; $this->merchantCommission($merchant_data, $top_id->from_id);//股东商户佣金写入 //推广上级佣金写入 $merchant_data = [ 'merchant_id' => $order->merchant_id, 'from_merchant_id' => $this->serverObj->from_id, 'gateway_id' => $order->gateway_id, 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_money' => $order->order_money, 'pay_money' => $order->pay_money, 'money' => $order->next_money, 'ratio' => $order->next_ratio,//返点 'name' => $this->serverObj->name . '的订单佣金', 'order_at' => $order->order_at, 'pay_ok_at' => $order->pay_ok_at, 'status' => '2',//佣金状态,改为处理中,结算时是按后台结算 ]; $this->merchantCommission($merchant_data, $this->serverObj->from_id);//推广商户佣金写入 } else { //推广上级佣金写入 $merchant_data = [ 'merchant_id' => $order->merchant_id, 'from_merchant_id' => $this->serverObj->from_id, 'gateway_id' => $order->gateway_id, 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_money' => $order->order_money, 'pay_money' => $order->pay_money, 'money' => $order->next_money, 'ratio' => $order->next_ratio,//返点 'name' => $this->serverObj->name . '的订单佣金', 'order_at' => $order->order_at, 'pay_ok_at' => $order->pay_ok_at, 'status' => '2',//佣金状态,改为处理中,结算时是按后台结算 ]; $this->merchantCommission($merchant_data, $this->serverObj->from_id);//推广商户佣金写入 } //2级的时候 } $ysytem_pate = $order->system_ratio * $order->pay_money / 1000; //写入平台佣金 $sys_data = [ 'merchant_id' => $order->merchant_id, 'gateway_id' => $order->gateway_id, 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_money' => $order->order_money, 'pay_money' => $order->pay_money, 'payfor_money' => $order->total_money, 'money' => $ysytem_pate, 'ratio' => $order->system_ratio,//系统佣金比率 'name' => $order->merchant_name . '的订单佣金', 'order_at' => $order->order_at, 'pay_ok_at' => $order->pay_ok_at, 'gateway_money' => $order->gateway_money,//通道扣除费用金额 'gateway_ratio' => $order->gateway_ratio,//通道扣点 'final_money' => $order->final_money,//最后盈利金额 'final_ratio' => $order->final_ratio,//最后盈利扣点 'status' => '1', ]; $this->debugLog('写入平台佣金:', $sys_data); $this->systemCommission($sys_data);//平台佣金 //资金变动 $this->addMerchantMoneyLog($order->total_money, $order->pay_money); } //写入日志 $this->orderHandleLog(['from_status' => 0, 'to_status' => 2, 'name' => '完成订单支付操作'], $this->order_id); //回调队列 //队列回调加入 NotifyOrderJob::dispatch($order,1)->onQueue('order_notify'); //风控 $gateway=Gateway::find($order->gateway_id); $this->gategoryWind($gateway,$order->pay_money); return $this->msg(['error' => 0, 'msg' => '操作成功']); } return $this->msg(['error' => 1, 'msg' => '操作失败']); } //支付风控写入 public function gategoryWind($gateway,$money) { $gateway_id=$gateway->id; $gateway_name=$gateway->name.'('.$gateway->mch_id.')'; //风控 $obj = new GatewayWind(); $has = GatewayWind::where(['gateway_id' => $gateway_id, 'day' => date('Y-m-d')])->first(); if ($has) { $now_money = $money + $has->amount_total; $has->increment('amount_total', $money);//增加金额 //判断金额是否大于最大的限额,如果是,则更新通道并且关闭 if ($now_money >= $has->max_total) { //更新通道为关闭,并且时系统设置 Gateway::where('id', $gateway_id)->update(['is_checked' => 0, 'is_system_close' => 1]); write_gateway();//写入文件 } } else { $obj->day = date('Y-m-d'); $obj->gateway_name =$gateway_name;//通道 $obj->amount_total = $money; $obj->gateway_id = $gateway_id; $obj->save(); $this->debugLog('风控完成写入成功'); } } //推广商户佣金写入 public function merchantCommission($data, $mid) { return MerchantCommission::getOrCreate($data, ['order_id' => $this->orderObj->id, 'merchant_id' => $mid]); } //商户钱包写入 public function addMerchantWallet($trans_money, $money) { return MerchantWallet::addWallte($this->orderObj->merchant_id, $trans_money, $money); } //资金变动交易写入 public function addMerchantMoneyLog($money, $pay_money) { DB::beginTransaction(); $r = MerchantMoneyLog::addMoney($this->orderObj, $money); if ($r) { //插入钱包变动 $w_r=$this->addMerchantWallet($pay_money, $money); if($w_r) { DB::commit(); }else{ DB::rollBack(); } }else { DB::rollBack(); } } public function msg($arr) { return response()->json($arr); } //系统佣金写入 public function systemCommission($data) { return SystemCommission::getOrCreate($data, ['order_id' => $this->orderObj->id]); } public function notifyPost(Request $request) { //发送curl过去 $order_info = $this->orderObj->infos; if ($order_info) { $notify_url = $order_info->notify_url;//回调地址 //签名计算 $data = [ 'app_id' => $this->serverObj->app_key, 'token' => $this->serverObj->token, 'money' => $this->orderObj->order_money, 'return_url' => $order_info->return_url, 'notify_url' => $order_info->notify_url, 'out_trade_sn' => $this->orderObj->out_trade_sn ]; //dump($data); //回调携带参数回去 //返回付款金额,原来充值金额,本站订单号,支付流水号,原网站订单号,附加消息原样返回 $notify_data = [ 'pay_money' => $this->orderObj->pay_money, 'money' => $this->orderObj->order_money, 'order_sn' => $this->orderObj->order_sn, 'out_trade_sn' => $this->orderObj->out_trade_sn, 'pay_order_sn' => $this->orderObj->pay_order_sn, // 'sign' => PayServices::sign($data, 1), 'sign' => md5($this->serverObj->token . $order_info->sign . 'pay_money=' . $this->orderObj->pay_money . 'pay_status=' . $this->orderObj->pay_status), 'attch' => $order_info->attach, 'pay_status' => $this->orderObj->pay_status, 'return_url' => $order_info->return_url, 'notify_url' => $order_info->notify_url ]; //dd($notify_data); Log::channel('pay_order')->info('总后台手动回调信息', $notify_data); if (!$notify_url) { return response(['error' => 1, 'msg' => '回调失败,缺少回调地址', 'back' => '']); } //发送curl $r = Curl::to($notify_url)->withData($notify_data)->post(); //返回success表示回调成功 //dump(gettype($r)); if ($r == 'success') { DB::beginTransaction(); //更新订单 $this->orderObj->out_notify_status = 1; $this->orderObj->status = 1; $or = $this->orderObj->save(); // $order_info->notify_data = json_encode($notify_data); $order_info->notify_status = $r; $lr = $order_info->save(); if ($lr && $or) { //写入日志 $this->orderHandleLog(['from_status' => 2, 'to_status' => 3, 'name' => '外站回调成功'], $this->order_id); DB::commit(); return response(['error' => 0, 'msg' => '回调成功', 'back' => $r]); } DB::rollBack(); $this->orderHandleLog(['from_status' => 2, 'to_status' => 3, 'name' => '外站回调失败'], $this->order_id); return response(['error' => 1, 'msg' => '对方回调成功,本站更新失败', 'back' => $r]); } else { return response(['error' => 1, 'msg' => '回调失败,外部没有返回success', 'back' => $r, 'sign_data' => $data, 'url' => $notify_url, 'notify_data' => $notify_data]); } } return response(['error' => 1, 'msg' => '回调失败,缺少回调地址', 'back' => '']); } //结算该笔订单 public function payFor() { $this->setViewPath('', 'payfor'); $status = config('adconfig.payfor_status'); unset($status[0]); $data = [ 'status' => $status ]; return $this->display($data); } //结算提交 public function PayForUpdate(Request $request) { if ($request->input('pay_time') == '') { return $this->msg(['error' => 1, 'msg' => '请填写必要参数']); } $status = $request->input('status'); DB::beginTransaction(); //更新订单 $order = $this->orderObj; $order->success_status = $status;//结算状态 $order->success_at = $request->input('pay_time');//结算时间 $order->status = 1; $r = $order->save(); //如果结算为失败则不添加到结算日志里面 //写入结算日志 $pay_for_data = [ 'status' => $status, 'order_sn' => $order->order_sn, 'money' => $order->total_money, 'mark' => $request->input('mark'), 'success_at' => $request->input('pay_time'), 'out_trade_sn' => $order->out_trade_sn ]; $log_r = $this->payForLog($pay_for_data); if ($log_r && $r) { DB::commit(); $this->orderHandleLog(['from_status' => 3, 'to_status' => 1, 'name' => '结算操作'], $this->order_id); return response(['error' => 0, 'msg' => '结算操作成功', 'back' => '']); } DB::rollBack(); return response(['error' => 1, 'msg' => '结算操作失败', 'back' => '']); } //结算日志 public function payForLog($config) { $log = new PayForLog(); $log->order_id = $this->order_id; foreach ($config as $k => $v) { $log->$k = $v; } $log->admin_id = admin('id'); $log->admin_name = admin('account'); $r = $log->save(); if ($r) { return true; } return false; } //订单操作日志 public function orderHandleLog($config) { $log = new OrderHandleLog(); foreach ($config as $k => $v) { $log->$k = $v; } $log->order_id = $this->order_id; $log->handle_user_id = admin('id');// $log->handle_user_name = admin('account');// $log->handle_user_type = 'admin';// $r = $log->save(); $this->insertLog('操作订单,行为是' . $config['name']); if ($r) { return true; } return false; } }