| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace common\models\addons;
- use common\enums\addons\AddonsOrderEnum;
- use common\enums\AliPayPayTypeEnum;
- use common\enums\PayTypeEnum;
- use common\enums\platform\PlatformEnum;
- use common\enums\WechatPayTypeEnum;
- use common\models\common\Addons;
- use common\models\mall\Mall;
- use common\models\order\Order;
- use common\models\platform\PlatformOrder;
- use common\models\platform\PlatformOrderStatisticsDay;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_order}}".
- *
- * @property int $id 订单id
- * @property int $mall_id 商城ID
- * @property string $order_no 订单编号
- * @property string|null $out_trade_no 系统支付流水号
- * @property string $addons_name 应用名或标识
- * @property int $unit 周期单位[0=永久,1=月,2=年]
- * @property int $duration 周期时长
- * @property int $version 版本号
- * @property float|null $pay_money 订单实付金额
- * @property float|null $order_price 订单价格
- * @property int|null $order_status 订单状态
- * @property int|null $pay_status 订单付款状态[0=未支付,1=已支付]
- * @property int|null $payment_type 支付方式[1=微信,2=支付宝]
- * @property int|null $pay_time 订单付款时间
- * @property int|null $expire_time 订单过期时间
- * @property int|null $recommend_uid 推荐代理uid
- * @property int|null $operator_id 操作人ID
- * @property int|null $operator_username 操作人用户名称
- * @property string|null $extra_info 额外信息
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 更新时间
- * @property int|null $close_time 关闭时间
- */
- class AddonsOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_order}}';
- }
- /**
- * 无前缀表
- * @Author: lun
- * @DateTime: 2022/3/21 0021 10:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return string
- */
- public static function briefTableName()
- {
- return 'addons_order';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'unit', 'duration','close_time', 'order_status', 'pay_status', 'payment_type', 'pay_time', 'expire_time', 'operator_id', 'recommend_uid', 'status', 'created_at', 'updated_at'], 'integer'],
- [['pay_money', 'order_price'], 'number'],
- [['extra_info', 'version', 'operator_username'], 'string'],
- [['order_no', 'out_trade_no', 'addons_name'], 'string', 'max' => 100],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '订单id',
- 'mall_id' => '商城ID',
- 'order_no' => '订单编号',
- 'out_trade_no' => '系统支付流水号',
- 'addons_name' => '插件名或标识',
- 'unit' => '周期单位[0=永久,1=月,2=年]',
- 'duration' => '周期时长',
- 'version' => '版本号',
- 'pay_money' => '订单实付金额',
- 'order_price' => '订单价格',
- 'order_status' => '订单状态',
- 'pay_status' => '订单付款状态[0=未支付,1=已支付]',
- 'payment_type' => '支付方式[1=微信,2=支付宝]',
- 'pay_time' => '订单付款时间',
- 'expire_time' => '订单过期时间',
- 'operator_id' => '操作人ID',
- 'operator_username' => '操作人用户名称',
- 'recommend_uid' => '推荐代理uid',
- 'extra_info' => '额外信息',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'close_time' => '关闭时间',
- ];
- }
- /**关联模型 */
- public function getAddons()
- {
- return $this->hasOne(Addons::class, ['name' => 'addons_name']);
- }
- public function getMall()
- {
- return $this->hasOne(Mall::class, ['id' => 'mall_id']);
- }
- /**
- * 创建订单
- * @Author: lun
- * @DateTime: 2022/2/26 0026 18:08
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $addons
- * @param $pay_setting
- * @return array|bool
- */
- public static function createOrder($mall_id, $addons, $pay_setting)
- {
- try {
- $time = time();
- // 创建订单
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->order_no = Order::generateOrderNo('a');
- $model->addons_name = $addons['name'];
- $model->unit = $pay_setting['unit'];
- $model->duration = $pay_setting['duration'];
- $model->version = $addons['version'];
- $model->pay_money = $pay_setting['money'];
- $model->order_price = $pay_setting['money'];
- $model->expire_time = $time + AddonsOrderEnum::EXPIRE_TIME * 60;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- $order = $model->toArray();
- // 生成交易流水订单
- $order_trade_info[] = [
- 'order_no' => $order['order_no'],
- 'amount' => $order['pay_money'],
- 'title'=> $addons['title'],
- 'notify_class' => 'common\logic\notify\AddonsPayNotify',
- 'order_type' => 'order',
- 'return_url' => Yii::$app->request->hostInfo . '/index.php?r=mall/addons/confirm-success&order_id=' . $order['id'],
- ];
- $trade = \Yii::$app->services->pay->createTrade(0, 0 , $order_trade_info);
- if($trade == false) throw new \Exception('生成支付流水失败');
- // 生成平台总订单
- $platform_order = PlatformOrder::setData([
- 'order_id' => $order['id'],
- 'order_no' => $order['order_no'],
- 'out_trade_no' => $trade['trade_no'],
- 'title' => $addons['title'],
- 'cover' => $addons['cover'] ?? '',
- 'consumer_id' => $mall_id,
- 'consumer_name' => $addons['consumer_name'],
- 'consumer_type' => PlatformEnum::CONSUMER_TYPE_MALL,
- 'pay_money' => $order['pay_money'],
- 'order_price' => $order['order_price'],
- 'expire_time' => $order['expire_time'],
- 'source_table' => self::briefTableName(),
- 'from_type' => PlatformEnum::FROM_TYPE_ADDONS_PAY,
- ]);
- if ($platform_order == false) throw new \Exception('平台订单生成失败:' . PlatformOrder::getStaticError());
- // 订单统计
- $res = PlatformOrderStatisticsDay::setData([
- 'order_num' => 1,
- 'order_money' => $order['order_price'],
- ]);
- if ($res == false) throw new \Exception(PlatformOrderStatisticsDay::getStaticError());
- // 判断是否是免费应用
- if ($model->pay_money == 0) {
- // 修改交易流水
- $trade_order = Yii::$app->services->pay->getByTradeNo($trade['trade_no']);
- // 修改订单交易流水
- $model->out_trade_no = $trade['trade_no'];
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 触发支付回调
- $orders = Yii::$app->services->pay->changePaymentTradeOrder($trade_order,PayTypeEnum::FREE);
- Yii::$app->services->pay->notify($orders);
- }
- // 返回信息
- return ['order' => [
- 'order_id' => $order['id'],
- 'order_no' => $order['order_no'],
- 'title' => $addons['title'],
- 'addons_name' => $addons['name'],
- 'unit' => $order['unit'],
- 'duration' => $order['duration'],
- 'pay_money' => $order['pay_money'],
- ], 'trade' => $trade];
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/3/3 0003 19:49
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return array|bool
- */
- public static function setData($params)
- {
- try {
- $model = self::findOne(['id' => $params['id']]);
- if (empty($model)) throw new Exception('查无此订单');
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model->toArray();
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取支付方式
- * @Author: lun
- * @DateTime: 2022/2/28 0028 9:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $payment_str
- * @param $trade
- * @return array
- * @throws \Exception
- */
- public static function getPaymentArr($payment_str, $trade)
- {
- $payment_arr = [];
- $payment_map = PayTypeEnum::getMap();
- $payment_type = Json::decode($payment_str, true);
- foreach ($payment_type as $type) {
- switch ($type) {
- case PayTypeEnum::WECHAT :
- if (empty(Yii::$app->services->setting->get('wechat.status'))) break;
- // 生成微信支付二维码地址
- $payment_arr[] = [
- 'payment_url' => Yii::$app->services->pay->wechat($trade, WechatPayTypeEnum::NATIVE),
- 'payment_way' => 'scan',// 扫码支付
- 'payment_name' => $payment_map[$type],
- ];
- break;
- case PayTypeEnum::ALI :
- if (empty(Yii::$app->services->setting->get('alipay.status'))) break;
- // 生成支付宝支付地址
- $alipay = Yii::$app->services->pay->alipay($trade, AliPayPayTypeEnum::PC);
- if (empty($alipay['config'])) throw new \Exception('支付宝扫码地址获取失败');
- $payment_arr[] = [
- 'payment_url' => $alipay['config'],
- 'payment_way' => 'navigate',// 链接跳转
- 'payment_name' => $payment_map[$type],
- ];
- break;
- }
- }
- return $payment_arr;
- }
- }
|