belongsTo('App\Models\Merchant', 'merchant_id', 'id')->withDefault([ 'name' => '本站' ]); } public function users() { return $this->belongsTo('App\Models\User', 'local_user_id', 'id')->withDefault(); } public function infos() { return $this->belongsTo('App\Models\OrderInfo', 'id', 'order_id')->withDefault(); } public function handle_logs() { return $this->hasMany('App\Models\OrderHandleLog', 'order_id', 'id'); } public function pay_for_logs() { return $this->hasMany('App\Models\PayForLog', 'order_id', 'id'); } public function pay_logs() { return $this->belongsTo('App\Models\PayLog', 'order_sn', 'order_sn')->withDefault(); } //订单查看 public function getPayMethodNameAttribute() { $id_arr = config('adconfig.pay_client'); return $id_arr[$this->gateway_method]; } //订单查看 public function getOrderShowUrlAttribute() { if ($this->id) { return admin_url('Order', 'show', ['id' => $this->id]); } } public function getOrderShowUrl2Attribute() { if ($this->id) { return admin_merchant_url('Order', 'show', ['id' => $this->id]); } } //支付通道 public function getPayTypeNameAttribute() { return config('adconfig.pay_type')[$this->pay_type]; } //订单状态 public function getOrderStatusNameAttribute() { return config('adconfig.order_status')[$this->status]; } //支付状态 public function getPayStatusNameAttribute() { return config('adconfig.pay_status')[$this->pay_status]; } //本站回调状态 public function getNotifyStatusNameAttribute() { return config('adconfig.notify_status')[$this->notify_status]; } //外站回调状态 public function getOutNotifyStatusNameAttribute() { return config('adconfig.pay_type')[$this->out_notify_status]; } //结算回调 public function getPayForStatusNameAttribute() { return config('adconfig.payfor_status')[$this->success_status]; } //该商户已经支付成功结算总额 public static function getMerchantTotalMoney($merchant_id, $pay_status = 1, $where = []) { $data = [ 'merchant_id' => $merchant_id, 'pay_status' => $pay_status, ]; $data = $where + $data; return self::where($data)->sum('total_money'); } public static function getMerchantTotalToDayMoney($merchant_id, $pay_status = 1) { $data = [ 'merchant_id' => $merchant_id, 'pay_status' => $pay_status, ]; $data = $data; $day = DateServices::today(); return self::where($data)->whereRaw('order_at >= ? and order_at <= ?', [$day['start_at'], $day['end_at']])->sum('total_money'); } //自动取消未支付超过15分 public static function autoCancleNopay( $where) { //删除订单记录,移动到新的表里面 $where['pay_status'] = 0; $where['status']=0; $total=self::getSearchModel(new self(), $where)->count(); self::getSearchModel(new self(), $where)->update([ 'status'=>3 ]); $arr_id = []; $up_count = $total; return $up_count; } public static function autoDeleteNopay($limit = 20, $where) { //删除订单记录,移动到新的表里面 $where['pay_status'] = 0; $list = self::getSearchModel(new self(), $where)->limit($limit)->get()->toArray(); $arr_id = []; $up_count = 0; if (count($list) > 0) { $data = []; $id_arr = []; foreach ($list as $k => $v) { $id_arr[] = $v['id']; //插入到失败表 unset($v['id']); $data[] = $v; } if ($id_arr) { self::whereIn('id', $id_arr)->forceDelete();//删除 //删除订单附加信息 OrderInfo::autoDel($id_arr); } if (count($data) > 0) { //插入 $up_count = OrderDel::insert($data); } } return $up_count; } //更新盈利 public static function updateFinalMoney($limit = 20, $where) { //更新已经支付的 $where['pay_status'] = 1; $list = self::getSearchModel(new self(), $where)->where('gateway_ratio', 0)->limit($limit)->get()->toArray(); $id_arr = []; if (count($list) > 0) { foreach ($list as $k => $v) { //找到自己,并且更新 $obj = Self::find($v['id']); //取得通道费率,当前费率是0.6 $obj->gateway_ratio = 6; $obj->gateway_money = ($v['pay_money'] * $obj->gateway_ratio) / 1000; $obj->final_money = ($v['system_ratio'] * $v['pay_money']) / 1000 - $obj->gateway_money;//盈利金额=支付金额*系统费率/1000 - 通道成本 $obj->final_ratio = $v['system_ratio'] - $obj->gateway_ratio; $obj->real_pate_money = ($v['pay_money'] * $v['ratio']) / 1000;//真实扣费金额 $r = $obj->save(); $id_arr[] = $r; } } return count($id_arr); } //某个商户订单结算总额 public static function merchantTotalMoney($merchant_id) { return self::where(['pay_status' => 1, 'merchant_id' => $merchant_id])->sum('total_money'); } //通道类型 public function gateways() { return $this->belongsTo('App\Models\Gateway', 'gateway_id', 'id')->withDefault(['name'=>'']); } public static function notifyMerchant($order_id,$type='id') { if($type=='id') { $order=self::find($order_id); }else { $order=$order_id; } if(empty($order)) { return (['error' => 1, 'msg' => '订单不存在', 'back' => '']); } if(($order->pay_status!=1)) { return (['error' => 1, 'msg' => '订单未支付', 'back' => '']); } if($order->out_notify_status==1 && $order->status==1) { return (['error' => 0, 'msg' => '订单已经回调成功', 'back' => '']); } //发送curl过去 $order_info = $order->infos; if ($order_info) { $notify_url = $order_info->notify_url;//回调地址 if (!$notify_url) { return (['error' => 1, 'msg' => '回调失败,缺少回调地址', 'back' => '']); } $notify_data = self::backParam($order_info,$order); //发送curl Log::channel('pay_order')->info('order文件中回调信息', $notify_data); $r = Curl::to($notify_url)->withData($notify_data)->post(); //返回success表示回调成功 //dump(gettype($r)); if (trim($r) == 'success') { DB::beginTransaction(); //更新订单 $order->out_notify_status = 1; $order->status = 1; $or = $order->save(); // $order_info->notify_data = json_encode($notify_data); $order_info->notify_status = 'success'; $lr = $order_info->save(); if ($lr && $or) { //写入日志 DB::commit(); return (['error' => 0, 'msg' => '回调成功', 'back' => $r]); } DB::rollBack(); return (['error' => 1, 'msg' => '对方回调成功,本站更新失败', 'back' => $r]); } else { $order_info->notify_data = json_encode($r); $order->notify_number += 1; $order->save(); $order_info->save(); return (['error' => 1, 'msg' => '回调失败,外部没有返回success', 'back' => $r, 'sign_data' => '$data', 'url' => $notify_url, 'notify_data' => $notify_data]); } } return (['error' => 1, 'msg' => '回调失败,缺少回调地址', 'back' => '']); } /** * 返回数据的签名参数 */ public static function backParam($order_info, $order) { $order = $order; //商户信息 $merchant = get_merchant($order_info->app_id);//取得商户 $notify_data = [ 'pay_money' => $order->pay_money, 'money' => $order->order_money, 'order_sn' => $order->order_sn, 'out_trade_sn' => $order->out_trade_sn, 'pay_order_sn' =>$order->pay_order_sn, 'sign' => md5($merchant['token'] . $order_info->sign . 'pay_money=' . $order->pay_money . 'pay_status=' . $order->pay_status), // 'attach' => $order_info->attach, 'attch' => $order_info->attach, 'pay_status' => $order->pay_status, 'return_url' => $order_info->return_url, 'notify_url' => $order_info->notify_url ]; return $notify_data; } public function getOrderStatusName2Attribute() { $config = config('adconfig.order_status'); $config_color= config('adconfig.order_status_color'); return ''.$config[$this->status].''; } }