32], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'template_id' => 'Template ID', 'order_no' => 'Order No', 'out_trade_no' => 'Out Trade No', 'unit' => 'Unit', 'duration' => 'Duration', 'pay_money' => 'Pay Money', 'order_price' => 'Order Price', 'payment_type' => 'Payment Type', 'pay_status' => 'Pay Status', 'pay_time' => 'Pay Time', 'extra_info' => 'Extra Info', 'expire_time' => 'Expire Time', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } /** * @return ActiveQueryInterface the relational query object. */ public function getTemplate() { return $this->hasOne(DiyTemplate::class, ['id' => 'template_id']); } /** * @return ActiveQueryInterface the relational query object. */ public function getPlatformOrder() { return $this ->hasOne(PlatformOrder::class, ['order_id' => 'id']) ->where(['from_type' => PlatformEnum::FROM_TYPE_TEMPLATE]); } /** * 订单号 * * @return string */ public static function getOrderNo() { return 'dtp' . date('YmdHis') . mt_rand(1000, 9999); } /** * 创建订单 * * @param integer $mall_id * @param DiyTemplate $template * @param array $item * @return array|false */ public static function createOrder( int $mall_id, DiyTemplate $template, array $item ) { $t = Yii::$app->db->beginTransaction(); try { $model = new static(); $model->mall_id = $mall_id; $model->template_id = $template->id; $model->order_no = static::getOrderNo(); $model->unit = $item['duration'] == -1 ? 0 : 1; $model->duration = $item['duration']; $model->pay_money = $item['money']; $model->order_price = $item['money']; $model->save(); // 仅支付金额大于0生成订单 $trade = []; if ($model->pay_money > 0) { // 生成交易流水订单 $order_trade_info[] = [ 'order_no' => $model->order_no, 'amount' => $model->pay_money, 'title'=> $template->title, 'notify_class' => 'common\logic\notify\DiyTemplatePayNotify', 'order_type' => static::ORDER_TYPE, 'return_url' => Yii::$app->request->hostInfo . '/index.php?r=mall/addons/confirm-success&order_id=' . $model->id, ]; $trade = Yii::$app->services->pay->createTrade(0, 0 , $order_trade_info); } $mall_name = Mall::find()->where(['id' => $mall_id])->select('name')->scalar(); // 生成平台总订单 $platform_order = PlatformOrder::setData([ 'order_id' => $model->id, 'order_no' => $model->order_no, 'out_trade_no' => $trade['trade_no'] ?? '', 'title' => $template->title, 'cover' => $template->cover, 'consumer_id' => $mall_id, 'consumer_name' => $mall_name, 'consumer_type' => PlatformEnum::CONSUMER_TYPE_MALL, 'pay_money' => $model->pay_money, 'order_price' => $model->order_price, 'expire_time' => $model->expire_time, 'source_table' => self::briefTableName(), 'from_type' => PlatformEnum::FROM_TYPE_TEMPLATE, ]); $t->commit(); return [ $model, $trade ]; } catch (Exception $e) { $t->rollBack(); return false; } } /** * 设置支付数据 * * @param string $out_trade_no * @param string $extra_info * @return boolean */ public function setPayData(string $out_trade_no, string $extra_info) { $this->extra_info = $extra_info; $this->out_trade_no = $out_trade_no; return $this->save(); } /** * @param integer $mall_id * @param integer $id * @return static|null */ public static function getOrderPayDataById(int $mall_id, int $id) { return static::find() ->where(['id' => $id, 'mall_id' => $mall_id]) ->with(['template' => function ($query) { $query->select(['id', 'title']); }]) ->one(); } /** * 订单号获取 * * @param string $order_no * @return static|null */ public static function getOrderByOrderNo(string $order_no) { return static::find() ->where(['order_no' => $order_no]) ->one(); } /** * 支付完成 * * @param integer $payment_type * @return boolean */ public function paid(int $payment_type = 0) { $this->platformOrder->payment_type = $this->payment_type = $payment_type; $this->platformOrder->pay_status = $this->pay_status = 1; $this->platformOrder->pay_time = $this->pay_time = time(); $this->platformOrder->order_status = 4; $this->platformOrder->settlement_status = 1; return $this->save() && $this->platformOrder->save(); } }