AddonsOperatingNoteBuy.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace addons\OperatingNote\common\models;
  3. use addons\OperatingNote\common\enums\NoteEnum;
  4. use common\enums\AssetsType;
  5. use common\enums\OrderStatusEnum;
  6. use common\enums\PaymentTradeOrderEnum;
  7. use common\enums\UserWalletEnum;
  8. use common\helpers\StringHelper;
  9. use services\common\UserWalletService;
  10. use Yii;
  11. /**
  12. * This is the model class for table "{{%addons_operating_note_buy}}".
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商城ID
  16. * @property int $user_id 用户id
  17. * @property int $note_id 笔记id
  18. * @property int $view_limit 付费类型
  19. * @property int $buy_price 付费金额
  20. * @property int $order_no 订单编号
  21. * @property int $out_trade_no 外部交易单号
  22. * @property int $is_pay 是否支付:0.未支付|1.已支付
  23. * @property int $status
  24. * @property int $created_at
  25. * @property int $updated_at
  26. */
  27. class AddonsOperatingNoteBuy extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%addons_operating_note_buy}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'user_id', 'note_id', 'view_limit', 'buy_price'], 'required'],
  43. [['mall_id', 'user_id', 'note_id', 'view_limit', 'is_pay', 'status', 'created_at', 'updated_at'], 'integer'],
  44. [['order_no', 'out_trade_no'], 'string'],
  45. [['buy_price'], 'number'],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'mall_id' => '商城ID',
  56. 'user_id' => '用户id',
  57. 'note_id' => '笔记id',
  58. 'view_limit' => '付费类型',
  59. 'buy_price' => '付费金额',
  60. 'status' => 'Status',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. ];
  64. }
  65. public function getContent()
  66. {
  67. return $this->hasOne(AddonsOperatingNoteContent::class, ['id' => 'note_id'])->select('*');
  68. }
  69. public static function userBuyNote($id, $user_id)
  70. {
  71. $state = 0;
  72. $arr = [];
  73. $already_buy = self::getOne([['user_id' => $user_id, 'note_id' => $id, 'is_pay' => NoteEnum::STATUS_ONE]]);
  74. if ($already_buy) {
  75. $state = 1;//已购买
  76. return ['state' => $state, 'arr' => $arr];
  77. }
  78. $note = AddonsOperatingNoteContent::getOne([['id' => $id]], false, [], false);
  79. if ($note->view_limit == 0) {
  80. $state = 2;//会员等级浏览 无需购买
  81. return ['state' => $state, 'arr' => $arr];
  82. }
  83. $model = new self();
  84. $model->order_no = StringHelper::generateOrderNo('note');
  85. $model->mall_id = $note['mall_id'];
  86. $model->user_id = $user_id;
  87. $model->note_id = $id;
  88. $model->view_limit = $note->view_limit;
  89. $model->buy_price = $note->view_limit_content;
  90. $model->save();
  91. if ($model->getErrorMessage()) {
  92. return ['state' => 0, 'arr' => $arr, 'msg' => $model->getErrorMessage()];
  93. }
  94. $desc = '购买文章';
  95. if ($note->view_limit == 1) {//积分直接扣除
  96. $handle_wallet = [
  97. 'message_id' => 0,
  98. 'mall_id' => $note->mall_id,
  99. 'user_id' => $user_id,
  100. 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
  101. 'money' => -$note->view_limit_content,
  102. 'desc' => $desc,
  103. 'source_table' => AssetsType::OPERATING_NOTE,
  104. 'source_table_id' => $model->id,
  105. 'from_type' => UserWalletEnum::CONSUME,
  106. 'capital_type' => UserWalletEnum::OPERATING_NOTE_BUY,
  107. ];
  108. $state = UserWalletService::handle(0, $handle_wallet);
  109. if ($state) {
  110. $note->pay_people_num = $note->pay_people_num + 1;
  111. $note->pay_score_amount = $note->pay_score_amount + $note->view_limit_content;
  112. $model->is_pay = OrderStatusEnum::PAY;
  113. $model->save();
  114. $note->save();
  115. $state = 1;
  116. } else {
  117. $state = 4;
  118. }
  119. } else {
  120. $state = 3;
  121. $order_trade_info[] = [
  122. 'order_no' => $model->order_no,
  123. 'amount' => $model->buy_price,
  124. 'title' => $desc,
  125. 'notify_class' => NoteEnum::NOTIFY_CLASS,
  126. 'order_type' => PaymentTradeOrderEnum::OPERATING_NOTE_ORDER,
  127. ];
  128. //生成支付流水
  129. $res = \Yii::$app->services->pay->createTrade($note->mall_id, $user_id, $order_trade_info);
  130. if ($res == false) throw new \Exception('生成支付流水失败');
  131. $arr['transaction'] = $res;
  132. $arr['pay_success_page'] = Yii::$app->services->setting->mall->get($note->mall_id, 'order.pay_after_link');// 支付成功后跳转的地址
  133. }
  134. return ['state' => $state, 'arr' => $arr];
  135. }
  136. }