'开通商户', 'model_type' => $type, 'model_id' => $id, 'regist' => $regist, 'order_sn' => order_sn(), 'money' => $regist->money ]; //dump($data); if (is_mobile_client()) { $this->setViewPath('', 'mindex'); } return $this->display($data); } public function pay(Request $request) { //发起支付 $pay = new MerchantOrderServices(); return $pay->order($request); } public function notify($type, $method, Request $request) { switch ($method) { case 'return': //return abort(403, '请查看之前的页面'); return view('web.merchant.checkshow', ['order_sn' => $request->input('out_trade_no')]); break; case 'notify': $order_sn = $request->input('out_trade_no'); $this->debugLog('商户推广订单号-------' . $order_sn . '----------开始回调'); $this->debugLog('回调数据'.json_encode($request->all())); //异步回调 //取得这个订单号,然后验证签名 $this->order = MerchantOrder::where('order_sn', $order_sn)->first(); $this->pay_method = $this->order->gateway_method;//支付方法 $this->gateway_id = $this->order->gateway_id;//通道ID //取得通道信息,数组形式 $this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id]; $config = [ 'app_id' => $this->pay_gateway['app_id'], 'ali_public_key' => trim($this->pay_gateway['cert_pub']), 'private_key' => trim($this->pay_gateway['cert_key']), ]; $extend_config = [ 'log' => [ // optional 'file' => to_linux_path(base_path()) . '/paylogs/' . $type . '_pay.log', 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug 'type' => 'daily', // optional, 可选 daily. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天 ], 'return_url' => action('Pay\PayController@returnNotify', ['type' => $type]), 'notify_url' => action('Pay\PayController@notify', ['type' => $type]), 'http' => [ // optional 'timeout' => 5.0, 'connect_timeout' => 5.0, // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html) ], ]; if ($this->pay_gateway['is_dev'] == 1) { $extend_config['mode'] = 'dev';//沙箱 } $this->pay_config = $extend_config + $config; $this->pay_model = Pay::alipay($this->pay_config); $data = $this->pay_model->verify(); $this->debugLog('签名的数据',$data->toArray()); if (empty($data)) { $this->debugLog('验证签名失败'); return false; } // dump($data->trade_status); if (!in_array($data->trade_status, ['TRADE_SUCCESS', 'TRADE_FINISHED'])) { $this->debugLog('支付宝订单关闭,未支付'); return false; exit();//终止 } $result = [ 'order_sn' => $data->out_trade_no, 'pay_order_sn' => $data->trade_no, 'pay_money' => $data->total_amount, 'pay_type' => 'alipay', 'account' => $data->buyer_logon_id, ]; //更新订单 $order = $this->order; //订单已经支付成功,直接success if ($order->pay_status == 1) { return $this->success();; } $this->debugLog('订单开始更新'); \Illuminate\Support\Facades\DB::beginTransaction(); //风控 $this->debugLog('写入风控'); $this->gategoryWind(); $this->debugLog('风控写入完成'); $order->pay_order_sn = $result['pay_order_sn'];//支付流水号 //开始更新订单数据 $order->pay_money = $result['pay_money'];//支付金额 $order->pay_status = 1;//支付成功 $order->status = 1;//订单交易成功 $order->notify_status = 1;//支付回调成功 $order_r = $order->save(); $this->debugLog('订单更新情况:' . $order_r); //找到注册提交的数据 $regist = MerchantRegist::find($order->model_id); //取得推广商户信息 $fid_model = Merchant::where('id', $regist->from_merchant_id)->where('is_checked', 1)->first(); //发起商户注册 $m_r = Merchant::create([ 'from_regist' => 2,//推广注册 'email' => $regist->email, 'next_ratio' => $fid_model->ratio, 'origin_password' => $regist->password, 'password' => ($regist->password), 'name' => $regist->email, 'ip' => $request->getClientIp(), 'app_key' => date('YmdHis') . str_random(6), 'token' => str_random(32), 'draw_type' => 1, 'ratio' => $regist->ratio, 'from_id' => $regist->merchant_id, 'level' => $regist->level, 'qq' => $regist->qq, 'mobile' => $regist->mobile, 'skype' => $regist->skype ]); //更新注册推广 $regist->pay_status = 1; $regist->status = 1; $regist->merchant_id = $m_r->id;//更新最后注册成功的商户id $r_r = $regist->save(); $this->debugLog('写入商户关系线'); //商户新增的关联线 $rel_r = $this->updateMerchantRatio($m_r); $this->debugLog('写入商户关系线完成'); $this->debugLog('写入资金变动'); $this->debugLog('模型'.$order->from_merchant_id); //资金变动 try{ $mlog_r = MerchantMoneyLog::adRegistMerchantMoney($order, $order->merchant_money); }catch (\Exception $exception) { $this->debugLog('资金问题'.$exception->getMessage().'---code'.$exception->getLine()); } $this->debugLog('写入资金变动完成'); //商户钱包 //增加金额 $wallet_r = MerchantWallet::moneyIncr($order->from_merchant_id, $order->merchant_money, 'money'); $this->debugLog('写入资金钱包完成'); if ($r_r && $m_r && $rel_r && $wallet_r && $mlog_r) { \Illuminate\Support\Facades\DB::commit(); $this->debugLog('--------END---------------' . $this->order->order_sn . '-订单交易更新完成------------------------'); } else { $this->debugLog('各个情况如下', ['order_r' => $order_r, 'regist_r' => $r_r, 'rel_r' => $rel_r, 'merchant_r' => $m_r, 'wallter_r' => $wallet_r, 'mlog' => $mlog_r]); $this->debugLog('--------END---------------' . $this->order->order_sn . '-订单交易更新失败------------------------'); } } } public function debugLog($str = '', $arr = []) { $arr = is_array($arr) ? $arr : [$arr]; if ($this->log_name) { Log::channel($this->log_name . 'notify_order')->info($str, $arr); } else { Log::channel('pay_order')->info($str, $arr); } } protected function updateMerchantRatio($merchant) { //取得费率表信息 $ratio = MerchantRatio::where('merchant_id', $merchant->id)->where('is_self', 1)->first(); //判断上级是否有上级 $upObj = Merchant::find($merchant->from_id); $hierarchy = 1;//默认1级,表示顶级 if ($upObj) { if ($upObj->from_id != 0) { $hierarchy = 3; } else { //没有上级,则只有2级别 $hierarchy = 2; } } //层级不一样了,判断谁的层级高一点 //全部删除,重新添加 MerchantRatio::where('merchant_id', $merchant->id)->delete(); //重新添加 //判断层级 if ($hierarchy == 3) { //3级关系级别 return MerchantRatio::addDefault($merchant->from_id, $merchant->id, $merchant->ratio); } elseif ($hierarchy == 2) { //代理级别 if ($merchant->level == 2) { //代理级别 return MerchantRatio::addProxyLevel($merchant->from_id, $merchant->id, $merchant->ratio); } else { //商户级别 return MerchantRatio::add($merchant->from_id, $merchant->level, 2, $merchant->id, $merchant->ratio); } } elseif ($hierarchy == 1) { //普通级别 return MerchantRatio::add(0, $merchant->level, 1, $merchant->id, $merchant->ratio); } } //推广商户佣金写入 public function merchantCommission($data, $mid) { return MerchantCommission::getOrCreate($data, ['order_id' => $this->orderObj->id, 'merchant_id' => $mid]); } //成功完成异步回调返回给支付商 public function success() { $this->pay_model->success()->send(); } //支付风控写入 public function gategoryWind() { $gateway_id = $this->gateway_id;// getOrder获得gateway_id $money = $this->order['money']; //风控 $obj = new GatewayWind(); $has = GatewayWind::where(['gateway_id' => $gateway_id, 'day' => date('Y-m-d')])->lockForUpdate()->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 = $this->pay_gateway['name'] . '(' . $this->pay_gateway['mch_id'] . ')';//通道 $obj->amount_total = $money; $obj->gateway_id = $this->gateway_id; $obj->save(); } } //检测订单是否完成 public function check(Request $request) { $order_sn = $request->input('order_sn'); $order = MerchantOrder::where('order_sn', $order_sn)->first(); if (empty($order)) { $data = [ 'error' => 1, 'msg' => '订单不存在' ]; return response()->json($data); } if ($order->pay_status == 1) { //取得商户信息 $regist = MerchantRegist::find($order->model_id); $admin_url = route('merchant.admin.login'); $mdata = [ 'admin_url' => $admin_url, 'email' => $regist->email, 'password' => $regist->password, 'msg' => '

登录地址:' . $admin_url . '

' . '

账号:' . $regist->email . '

' . '

密码:' . $regist->password . '

请记住账号信息' ]; $data = [ 'error' => 0, 'msg' => '订单支付完成', 'merchant' => $mdata ]; return response()->json($data); } else { $data = [ 'error' => 1, 'msg' => '订单还没支付', ]; return response()->json($data); } } public function checkShow(Request $request) { $order_sn = $request->input('order_sn'); return $this->display(['order_sn' => $order_sn]); } }