'余额支付', 1 => '微信支付', 2 => '支付宝支付', 3 => '混合支付', 4 => '公益豆支付', ]; private $order_type = [ 1 => '线上订单', 2 => '闲置订单', ]; static private $server = null; private function __construct() { } /** * 创建对象 * @return PayServer */ static function creatServer() { if (empty(self::$server)) self::$server = new PayServer(); return self::$server; } /** * 获取支付类型 * @param $pay_type * @param $balance_money * @return string */ public function getPayArr($pay_type, $balance_money = 0) { $pay_name = empty($this->pay_arr[$pay_type]) ? '' : $this->pay_arr[$pay_type]; if ($pay_type != 0 && $pay_type != 4 && $balance_money > 0) { $pay_name .= '+余额支付'; } return $pay_name; } /** * 创建支付信息 * @param $order * @param $balance_money * @param $pay_type * @param $money_type * @param $order_type * @return PayOrder|\Illuminate\Database\Eloquent\Model * @throws \Exception */ function createPayInfo($order, $balance_money, $pay_type, $money_type = 1, $order_type = 1) { if (empty($balance_money) || $balance_money <= 0) $balance_money = 0; $mobile_money = $order->{'total_money'} - $balance_money; if ($mobile_money <= 0) $mobile_money = 0; $pay_info = PayOrder::where('order_id', $order->{'id'})->where('m_id', $order->{'m_id'})->where('pay_type', $pay_type)->where('balance_money', $balance_money)->where('mobile_money', $mobile_money)->where('status', 0)->first(); if (!empty($pay_info)) { return $pay_info; } if ($order_type == 1) { $pay_info = [ 'order_id' => $order->{'id'}, 'm_id' => $order->{'m_id'}, 'balance_money' => $balance_money, 'mobile_money' => $mobile_money, 'pay_type' => $pay_type, 'money_type' => $money_type, 'order_type' => $order_type, 'status' => 0, 'water_id' => 0, 'pay_time' => date('Y-m-d H:i:s'), 'pay_sn' => '', 'pay_info' => '', // 'pay_code' => $order->{'order_sn'}, 'pay_code' => IndentNumServer::creatServer()->getIndentNum(4), ]; } else { $pay_info = [ 'order_id' => $order->{'id'}, 'm_id' => $order->{'buy_id'}, 'balance_money' => $balance_money ?: 0, 'mobile_money' => $order->{'total_money'} - $balance_money, 'pay_type' => $pay_type, 'money_type' => $money_type, 'order_type' => $order_type, 'status' => 0, 'water_id' => 0, 'pay_time' => date('Y-m-d H:i:s'), 'pay_sn' => '', 'pay_info' => '', 'pay_code' => $order->{'used_sn'}, ]; } DB::beginTransaction(); PayOrder::where('order_id', $order->{'id'})->where('m_id', $order->{'m_id'})->update(['status' => 2]); $pay_info = PayOrder::create($pay_info); if ($order_type == 1) OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, '订单创建支付信息'); DB::commit(); return $pay_info; } /** * 余额支付 * @param PayOrder $pay_info * @return bool * @throws \Exception */ function balancePay(PayOrder $pay_info) { DB::beginTransaction(); if ($pay_info->{'balance_money'} > 0) { $pay_info = $this->deductionBalance($pay_info); if (empty($pay_info)) { DB::rollBack(); return false; } } $pay_info->update(['status' => 1, 'pay_sn' => IndentNumServer::creatServer()->getIndentNum(4)]); if ($pay_info->{'order_type'} == 1) { //线上加入队列处理 OrderSubAccountJob::dispatch($pay_info)->onConnection('redis')->onQueue('order_sub'); OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, '余额支付完成'); } elseif ($pay_info->{'order_type'} == 2) { //闲置订单支付流程 UsedServer::creatServer()->payOrder($pay_info); } DB::commit(); return $pay_info; } /** * 公益豆支付 * @param PayOrder $pay_info * @param PayOrder $claim_shop_id 扣款店铺 * @return \App\Models\MoneyDetail|PayOrder|false|\Illuminate\Database\Eloquent\Model * @throws \Exception */ function benefitPay(PayOrder $pay_info, $claim_shop_id) { DB::beginTransaction(); $pay_info = $this->benefitBalance($pay_info, $claim_shop_id); if (empty($pay_info)) { DB::rollBack(); return false; } $pay_info->update(['status' => 1]); //加入队列处理 OrderSubAccountJob::dispatch($pay_info)->onConnection('redis')->onQueue('order_sub'); OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, '公益豆支付完成'); DB::commit(); return $pay_info; } /** * 回调信息 * @param $pay_code * @param $pay_sn * @param $pay_money * @param $notify_info * @return bool * @throws \Exception */ function NotifyPay($pay_code, $pay_sn, $pay_money, $notify_info) { $pay_info = PayOrder::where('pay_code', $pay_code)->where('status', 0)->first(); if (empty($pay_info)) { $pay_info->update(['pay_sn' => $pay_sn, 'pay_info' => serialize($notify_info)]); ErrorRecord::create(['msg' => '订单支付状态错误', 'data' => serialize($notify_info)]); return false; } $is_test = env('TEST_SERVE', false); if ($pay_info->{'mobile_money'} != $pay_money && !$is_test) { $pay_info->update(['pay_sn' => $pay_sn, 'pay_info' => serialize($notify_info)]); ErrorRecord::create(['msg' => '支付金额不匹配', 'data' => serialize($notify_info)]); return false; } DB::beginTransaction(); if ($pay_info->{'balance_money'} > 0) { $pay_info = $this->deductionBalance($pay_info); if (empty($pay_info)) { ErrorRecord::create(['msg' => '订单余额支付错误', 'data' => serialize($notify_info)]); DB::rollBack(); return false; } } $pay_info->update(['pay_sn' => $pay_sn, 'pay_info' => serialize($notify_info), 'status' => 1]); // event(new OrderSubAccountEvent($pay_info)); if ($pay_info->{'order_type'} == 1) { //线上加入队列处理 OrderSubAccountJob::dispatch($pay_info)->onConnection('redis')->onQueue('order_sub'); OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, '在线支付回调完成'); } elseif ($pay_info->{'order_type'} == 2) { //闲置订单支付流程 UsedServer::creatServer()->payOrder($pay_info); } DB::commit(); return true; } /** * 余额支付 * @param PayOrder $pay_info * @return \App\Models\MoneyDetail|false|\Illuminate\Database\Eloquent\Model */ function deductionBalance(PayOrder $pay_info) { $water_id = MoneyDetailServer::creatServer()->write(1, 2, $pay_info->{'balance_money'}, 0, $pay_info->{'m_id'}, $pay_info->{'pay_code'} . '流水支付完成', $pay_info->{'id'}); if (empty($water_id)) return false; $pay_info->update(['water_id' => $pay_info->{'id'}]); return $pay_info; } /** * 公益豆 * @param PayOrder $pay_info * @param PayOrder $claim_shop_id 扣款店铺ID(公益豆支付) * @return \App\Models\MoneyDetail|false|\Illuminate\Database\Eloquent\Model */ function benefitBalance(PayOrder $pay_info, $claim_shop_id = 0) { $water_id = MoneyDetailServer::creatServer()->write(3, 4, $pay_info->{'balance_money'}, 0, $pay_info->{'m_id'}, $pay_info->{'pay_code'} . '流水支付完成', $pay_info->{'id'}, $claim_shop_id); if (empty($water_id)) return false; $pay_info->update(['water_id' => $pay_info->{'id'}]); return $pay_info; } /** * 订单商家分账 * @param PayOrder $pay_info * @return false */ function OrderSubAccount(PayOrder $pay_info) { $item_num = PayItem::where('pay_id', $pay_info->{'id'})->count(); if ($item_num > 0) { return false; } $order = Order::where('id', $pay_info->{'order_id'})->select(['id', 'parent_id', 'child_num', 'total_money', 'shop_id', 'order_sn'])->first(); DB::beginTransaction(); //更新订单支付信息 $pay_name = $this->getPayArr($pay_info->{'pay_type'}, $pay_info->{'balance_money'}); Order::where('id', '=', $pay_info->{'order_id'}, 'or')->where('parent_id', '=', $pay_info->{'order_id'}, 'or')->update(['pay_time' => date('Y-m-d H:i:s'), 'pay_code' => $pay_info->{'pay_sn'}, 'pay_id' => $pay_info->{'id'}, 'pay_type' => $pay_info->{'pay_type'}, 'pay_name' => $pay_name]); OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, $order->{'order_sn'} . '订单进行分账'); //订单分账 if ($order->{'shop_id'}) { if ($pay_info->{'balance_money'} > 0) { $this->balanceSubAccount($pay_info, 0, $pay_info->{'balance_money'}, $order->{'shop_id'}, $order->{'id'}, 1, $pay_info->{'money_type'}); } if ($pay_info->{'mobile_money'} > 0) { $this->balanceSubAccount($pay_info, $pay_info->{'pay_type'}, $pay_info->{'mobile_money'}, $order->{'shop_id'}, $order->{'id'}, $pay_info->{'pay_type'}, $pay_info->{'money_type'}); } OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, $order->{'order_sn'} . '订单分账完成'); } else { $order_list = Order::where('parent_id', $pay_info->{'order_id'})->where('id', '<>', $pay_info->{'order_id'})->select(['id', 'parent_id', 'child_num', 'total_money', 'shop_id', 'order_sn'])->get(); foreach ($order_list as $item) { $proportion = round($item->{'total_money'} / $order->{'total_money'}, 6); if ($pay_info->{'balance_money'} > 0) { $sub_money = $proportion * $pay_info->{'balance_money'}; $this->balanceSubAccount($pay_info, 0, $sub_money, $item->{'shop_id'}, $item->{'id'}, 1, $pay_info->{'money_type'}); } if ($pay_info->{'mobile_money'} > 0) { $sub_money = $proportion * $pay_info->{'mobile_money'}; $this->balanceSubAccount($pay_info, $pay_info->{'pay_type'}, $sub_money, $item->{'shop_id'}, $item->{'id'}, $pay_info->{'pay_type'}, $pay_info->{'money_type'}); } OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, $order->{'order_sn'} . '订单分账至' . $item->{'order_sn'}); } OrderServer::creatServer()->OrderLog($pay_info->{'order_id'}, $order->{'order_sn'} . '订单分账完成'); } DB::commit(); } /** * 生成分账明细 * @param PayOrder $pay_info * @param $pay_type * @param $money * @param $shop_id * @param $order_id * @param int $type * @param int $money_type */ function balanceSubAccount(PayOrder $pay_info, $pay_type, $money, $shop_id, $order_id, $type = 1, $money_type = 1) { $item = [ 'm_id' => $pay_info->{'m_id'}, 'pay_id' => $pay_info->{'id'}, 'pay_sn' => $pay_info->{'pay_sn'}, 'pay_type' => $pay_type, 'money' => $money, 'type' => $type, 'shop_id' => $shop_id, 'money_type' => $money_type, 'order_id' => $order_id, ]; PayItem::create($item); } }