| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace common\models\diy;
- use common\enums\platform\PlatformEnum;
- use common\models\diy\DiyTemplate;
- use common\models\mall\Mall;
- use common\models\platform\PlatformOrder;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%diy_template_order}}".
- *
- * @property int $id
- * @property int $mall_id Mall id
- * @property int $template_id 模板ID
- * @property string $order_no 订单号
- * @property string|null $out_trade_no 外部订单号
- * @property int $unit 周期单位[0=永久,1=月,2=年]
- * @property int $duration 周期时长
- * @property float $pay_money 支付金额
- * @property float $order_price 订单金额
- * @property int|null $payment_type 支付方式[1=微信,2=支付宝]
- * @property int $pay_status 支付状态 0 未支付 1 已支付
- * @property int|null $pay_time 支付时间
- * @property string|null $extra_info
- * @property int|null $expire_time 过期时间
- * @property int|null $created_at
- * @property int|null $updated_at
- */
- class DiyTemplateOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * 订单类型
- * @var string
- */
- const ORDER_TYPE = 'diy_template_order';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%diy_template_order}}';
- }
- /**
- * @return string
- */
- public static function briefTableName()
- {
- return 'diy_template_order';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'template_id', 'order_no', 'unit', 'duration', 'pay_money', 'order_price'], 'required'],
- [['mall_id', 'template_id', 'unit', 'duration', 'payment_type', 'pay_status', 'pay_time', 'expire_time', 'created_at', 'updated_at'], 'integer'],
- [['pay_money', 'order_price'], 'number'],
- [['extra_info'], 'string'],
- [['order_no', 'out_trade_no'], 'string', 'max' => 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();
- }
- }
|