| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace addons\OperatingNote\common\models;
- use addons\OperatingNote\common\enums\NoteEnum;
- use common\enums\AssetsType;
- use common\enums\OrderStatusEnum;
- use common\enums\PaymentTradeOrderEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\StringHelper;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "{{%addons_operating_note_buy}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_id 用户id
- * @property int $note_id 笔记id
- * @property int $view_limit 付费类型
- * @property int $buy_price 付费金额
- * @property int $order_no 订单编号
- * @property int $out_trade_no 外部交易单号
- * @property int $is_pay 是否支付:0.未支付|1.已支付
- * @property int $status
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsOperatingNoteBuy extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_buy}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'note_id', 'view_limit', 'buy_price'], 'required'],
- [['mall_id', 'user_id', 'note_id', 'view_limit', 'is_pay', 'status', 'created_at', 'updated_at'], 'integer'],
- [['order_no', 'out_trade_no'], 'string'],
- [['buy_price'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户id',
- 'note_id' => '笔记id',
- 'view_limit' => '付费类型',
- 'buy_price' => '付费金额',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getContent()
- {
- return $this->hasOne(AddonsOperatingNoteContent::class, ['id' => 'note_id'])->select('*');
- }
- public static function userBuyNote($id, $user_id)
- {
- $state = 0;
- $arr = [];
- $already_buy = self::getOne([['user_id' => $user_id, 'note_id' => $id, 'is_pay' => NoteEnum::STATUS_ONE]]);
- if ($already_buy) {
- $state = 1;//已购买
- return ['state' => $state, 'arr' => $arr];
- }
- $note = AddonsOperatingNoteContent::getOne([['id' => $id]], false, [], false);
- if ($note->view_limit == 0) {
- $state = 2;//会员等级浏览 无需购买
- return ['state' => $state, 'arr' => $arr];
- }
- $model = new self();
- $model->order_no = StringHelper::generateOrderNo('note');
- $model->mall_id = $note['mall_id'];
- $model->user_id = $user_id;
- $model->note_id = $id;
- $model->view_limit = $note->view_limit;
- $model->buy_price = $note->view_limit_content;
- $model->save();
- if ($model->getErrorMessage()) {
- return ['state' => 0, 'arr' => $arr, 'msg' => $model->getErrorMessage()];
- }
- $desc = '购买文章';
- if ($note->view_limit == 1) {//积分直接扣除
- $handle_wallet = [
- 'message_id' => 0,
- 'mall_id' => $note->mall_id,
- 'user_id' => $user_id,
- 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
- 'money' => -$note->view_limit_content,
- 'desc' => $desc,
- 'source_table' => AssetsType::OPERATING_NOTE,
- 'source_table_id' => $model->id,
- 'from_type' => UserWalletEnum::CONSUME,
- 'capital_type' => UserWalletEnum::OPERATING_NOTE_BUY,
- ];
- $state = UserWalletService::handle(0, $handle_wallet);
- if ($state) {
- $note->pay_people_num = $note->pay_people_num + 1;
- $note->pay_score_amount = $note->pay_score_amount + $note->view_limit_content;
- $model->is_pay = OrderStatusEnum::PAY;
- $model->save();
- $note->save();
- $state = 1;
- } else {
- $state = 4;
- }
- } else {
- $state = 3;
- $order_trade_info[] = [
- 'order_no' => $model->order_no,
- 'amount' => $model->buy_price,
- 'title' => $desc,
- 'notify_class' => NoteEnum::NOTIFY_CLASS,
- 'order_type' => PaymentTradeOrderEnum::OPERATING_NOTE_ORDER,
- ];
- //生成支付流水
- $res = \Yii::$app->services->pay->createTrade($note->mall_id, $user_id, $order_trade_info);
- if ($res == false) throw new \Exception('生成支付流水失败');
- $arr['transaction'] = $res;
- $arr['pay_success_page'] = Yii::$app->services->setting->mall->get($note->mall_id, 'order.pay_after_link');// 支付成功后跳转的地址
- }
- return ['state' => $state, 'arr' => $arr];
- }
- }
|