services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic'); if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮'); if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单'); //检查是否存在已购买订单 $order = AreaOrder::getOne([ ['mall_id' => $params['mall_id']], ['user_id' => $params['user_id']], ['level' => $params['level']], ['area_id' => $params['district_id']], ['is_pay' => StatusEnum::ENABLED], ['status' => StatusEnum::ENABLED], ], true); if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']]; //获取最近购买该区代身份的订单 $near_order = AreaOrder::getOne([ ['mall_id' => $params['mall_id']], ['level' => $params['level']], ['area_id' => $params['district_id']], ['is_pay' => StatusEnum::ENABLED], ['status' => StatusEnum::ENABLED], ], true); //处理底价 $price = $configs['area_buy_price']; if (!empty($near_order)) { $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2); } return ['total_pay_money' => $price]; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function created($params) { try { $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic'); if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮'); if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单'); //检查是否存在已购买订单 $order = AreaOrder::getOne([ ['mall_id' => $params['mall_id']], ['user_id' => $params['user_id']], ['level' => $params['level']], ['area_id' => $params['district_id']], ['is_pay' => StatusEnum::ENABLED], ['status' => StatusEnum::ENABLED], ], true); if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']]; //获取最近购买该区代身份的订单 $near_order = AreaOrder::getOne([ ['mall_id' => $params['mall_id']], ['level' => $params['level']], ['area_id' => $params['district_id']], ['is_pay' => StatusEnum::ENABLED], ['status' => StatusEnum::ENABLED], ], true); //处理底价 $price = $configs['area_buy_price']; if (!empty($near_order)) { $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2); } $order_data = [ 'mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'level' => $params['level'], 'area_id' => $params['district_id'], 'price' => $price, 'order_no' => self::generateOrderNo('sa'), ]; $order = AreaOrder::setData($order_data); if (!$order) throw new Exception(AreaOrder::$static_error); $order_trade_info[] = [ 'order_id' => $order->id, 'order_type' => 'Area', 'order_no' => $order->order_no, 'amount' => $order->price, 'title' => '购买区代申请资格', 'notify_class' => 'addons\Area\common\logic\notify\Notify', ]; $res = \Yii::$app->services->pay->createTrade($params['mall_id'],$params['user_id'], $order_trade_info); $res['order_id'] = $order->id; $arr['transaction'] = $res; $payment_expire_time = \Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time'); $arr['surplus_second'] = 30*60;//剩余秒数 if($payment_expire_time){ $arr['surplus_second'] = intval($payment_expire_time) * 60;//剩余秒数 } return $arr; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public static function generateOrderNo($prefix = ''){ // 优化唯一性保证:使用random_int()代替rand(),并结合微调以生成更唯一的ID $randInt = random_int(100000, 999999); // 日期时间戳处理 $dateTimeStr = date('YmdHis'); // 保证前缀不为空字符串且不超过一定长度(例如20个字符) $prefix = trim($prefix, '0'); if (strlen($prefix) > 20) { $prefix = substr($prefix, 0, 20); } // 生成订单号,保证总长度为17(前缀+日期时间戳+随机数) $orderNo = $prefix . $dateTimeStr . str_pad($randInt, 6, '0', STR_PAD_LEFT); return $orderNo; } }