DiyTemplateOrder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace common\models\diy;
  3. use common\enums\platform\PlatformEnum;
  4. use common\models\diy\DiyTemplate;
  5. use common\models\mall\Mall;
  6. use common\models\platform\PlatformOrder;
  7. use Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%diy_template_order}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id Mall id
  14. * @property int $template_id 模板ID
  15. * @property string $order_no 订单号
  16. * @property string|null $out_trade_no 外部订单号
  17. * @property int $unit 周期单位[0=永久,1=月,2=年]
  18. * @property int $duration 周期时长
  19. * @property float $pay_money 支付金额
  20. * @property float $order_price 订单金额
  21. * @property int|null $payment_type 支付方式[1=微信,2=支付宝]
  22. * @property int $pay_status 支付状态 0 未支付 1 已支付
  23. * @property int|null $pay_time 支付时间
  24. * @property string|null $extra_info
  25. * @property int|null $expire_time 过期时间
  26. * @property int|null $created_at
  27. * @property int|null $updated_at
  28. */
  29. class DiyTemplateOrder extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * 订单类型
  33. * @var string
  34. */
  35. const ORDER_TYPE = 'diy_template_order';
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%diy_template_order}}';
  42. }
  43. /**
  44. * @return string
  45. */
  46. public static function briefTableName()
  47. {
  48. return 'diy_template_order';
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function rules()
  54. {
  55. return [
  56. [['mall_id', 'template_id', 'order_no', 'unit', 'duration', 'pay_money', 'order_price'], 'required'],
  57. [['mall_id', 'template_id', 'unit', 'duration', 'payment_type', 'pay_status', 'pay_time', 'expire_time', 'created_at', 'updated_at'], 'integer'],
  58. [['pay_money', 'order_price'], 'number'],
  59. [['extra_info'], 'string'],
  60. [['order_no', 'out_trade_no'], 'string', 'max' => 32],
  61. ];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function attributeLabels()
  67. {
  68. return [
  69. 'id' => 'ID',
  70. 'mall_id' => 'Mall ID',
  71. 'template_id' => 'Template ID',
  72. 'order_no' => 'Order No',
  73. 'out_trade_no' => 'Out Trade No',
  74. 'unit' => 'Unit',
  75. 'duration' => 'Duration',
  76. 'pay_money' => 'Pay Money',
  77. 'order_price' => 'Order Price',
  78. 'payment_type' => 'Payment Type',
  79. 'pay_status' => 'Pay Status',
  80. 'pay_time' => 'Pay Time',
  81. 'extra_info' => 'Extra Info',
  82. 'expire_time' => 'Expire Time',
  83. 'created_at' => 'Created At',
  84. 'updated_at' => 'Updated At',
  85. ];
  86. }
  87. /**
  88. * @return ActiveQueryInterface the relational query object.
  89. */
  90. public function getTemplate()
  91. {
  92. return $this->hasOne(DiyTemplate::class, ['id' => 'template_id']);
  93. }
  94. /**
  95. * @return ActiveQueryInterface the relational query object.
  96. */
  97. public function getPlatformOrder()
  98. {
  99. return $this
  100. ->hasOne(PlatformOrder::class, ['order_id' => 'id'])
  101. ->where(['from_type' => PlatformEnum::FROM_TYPE_TEMPLATE]);
  102. }
  103. /**
  104. * 订单号
  105. *
  106. * @return string
  107. */
  108. public static function getOrderNo()
  109. {
  110. return 'dtp' . date('YmdHis') . mt_rand(1000, 9999);
  111. }
  112. /**
  113. * 创建订单
  114. *
  115. * @param integer $mall_id
  116. * @param DiyTemplate $template
  117. * @param array $item
  118. * @return array|false
  119. */
  120. public static function createOrder(
  121. int $mall_id,
  122. DiyTemplate $template,
  123. array $item
  124. )
  125. {
  126. $t = Yii::$app->db->beginTransaction();
  127. try {
  128. $model = new static();
  129. $model->mall_id = $mall_id;
  130. $model->template_id = $template->id;
  131. $model->order_no = static::getOrderNo();
  132. $model->unit = $item['duration'] == -1 ? 0 : 1;
  133. $model->duration = $item['duration'];
  134. $model->pay_money = $item['money'];
  135. $model->order_price = $item['money'];
  136. $model->save();
  137. // 仅支付金额大于0生成订单
  138. $trade = [];
  139. if ($model->pay_money > 0) {
  140. // 生成交易流水订单
  141. $order_trade_info[] = [
  142. 'order_no' => $model->order_no,
  143. 'amount' => $model->pay_money,
  144. 'title'=> $template->title,
  145. 'notify_class' => 'common\logic\notify\DiyTemplatePayNotify',
  146. 'order_type' => static::ORDER_TYPE,
  147. 'return_url' => Yii::$app->request->hostInfo . '/index.php?r=mall/addons/confirm-success&order_id=' . $model->id,
  148. ];
  149. $trade = Yii::$app->services->pay->createTrade(0, 0 , $order_trade_info);
  150. }
  151. $mall_name = Mall::find()->where(['id' => $mall_id])->select('name')->scalar();
  152. // 生成平台总订单
  153. $platform_order = PlatformOrder::setData([
  154. 'order_id' => $model->id,
  155. 'order_no' => $model->order_no,
  156. 'out_trade_no' => $trade['trade_no'] ?? '',
  157. 'title' => $template->title,
  158. 'cover' => $template->cover,
  159. 'consumer_id' => $mall_id,
  160. 'consumer_name' => $mall_name,
  161. 'consumer_type' => PlatformEnum::CONSUMER_TYPE_MALL,
  162. 'pay_money' => $model->pay_money,
  163. 'order_price' => $model->order_price,
  164. 'expire_time' => $model->expire_time,
  165. 'source_table' => self::briefTableName(),
  166. 'from_type' => PlatformEnum::FROM_TYPE_TEMPLATE,
  167. ]);
  168. $t->commit();
  169. return [
  170. $model,
  171. $trade
  172. ];
  173. } catch (Exception $e) {
  174. $t->rollBack();
  175. return false;
  176. }
  177. }
  178. /**
  179. * 设置支付数据
  180. *
  181. * @param string $out_trade_no
  182. * @param string $extra_info
  183. * @return boolean
  184. */
  185. public function setPayData(string $out_trade_no, string $extra_info)
  186. {
  187. $this->extra_info = $extra_info;
  188. $this->out_trade_no = $out_trade_no;
  189. return $this->save();
  190. }
  191. /**
  192. * @param integer $mall_id
  193. * @param integer $id
  194. * @return static|null
  195. */
  196. public static function getOrderPayDataById(int $mall_id, int $id)
  197. {
  198. return static::find()
  199. ->where(['id' => $id, 'mall_id' => $mall_id])
  200. ->with(['template' => function ($query) {
  201. $query->select(['id', 'title']);
  202. }])
  203. ->one();
  204. }
  205. /**
  206. * 订单号获取
  207. *
  208. * @param string $order_no
  209. * @return static|null
  210. */
  211. public static function getOrderByOrderNo(string $order_no)
  212. {
  213. return static::find()
  214. ->where(['order_no' => $order_no])
  215. ->one();
  216. }
  217. /**
  218. * 支付完成
  219. *
  220. * @param integer $payment_type
  221. * @return boolean
  222. */
  223. public function paid(int $payment_type = 0)
  224. {
  225. $this->platformOrder->payment_type = $this->payment_type = $payment_type;
  226. $this->platformOrder->pay_status = $this->pay_status = 1;
  227. $this->platformOrder->pay_time = $this->pay_time = time();
  228. $this->platformOrder->order_status = 4;
  229. $this->platformOrder->settlement_status = 1;
  230. return $this->save() && $this->platformOrder->save();
  231. }
  232. }