HigoRechargeOrders.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\HiGo\common\models;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use Exception;
  7. /**
  8. * This is the model class for table "{{%recharge_orders}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property string $order_no
  13. * @property int $user_id
  14. * @property int $price_setting_method
  15. * @property float $pay_price 充值金额
  16. * @property float $hi_bei 嗨贝数额
  17. * @property int $pay_type 支付方式 1.线上支付
  18. * @property int $is_pay 是否支付 0--未支付 1--支付
  19. * @property int $pay_at
  20. * @property int $status
  21. * @property int $created_at 创建时间
  22. * @property int $deleted_at 删除时间
  23. * @property int $updated_at 修改时间
  24. * @property int $give_score 赠送积分
  25. */
  26. class HigoRechargeOrders extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * 支付方式: 线上支付
  30. */
  31. const PAY_TYPE_ON_LINE = 1;
  32. //充值方式:
  33. const RECHARGE_TYPE_USER = 0;
  34. const RECHARGE_TYPE_ADMIN = 1;
  35. const IS_PAY = 1;
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_higo_recharge_orders}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['mall_id', 'user_id', 'pay_price', 'pay_type'], 'required'],
  50. [['mall_id', 'user_id', 'pay_type', 'is_pay', 'pay_at', 'status', 'created_at', 'deleted_at', 'updated_at', 'price_setting_method'], 'integer'],
  51. [['pay_price', 'hi_bei'], 'number'],
  52. [['order_no'], 'string', 'max' => 32],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => 'Mall ID',
  63. 'order_no' => 'Order No',
  64. 'user_id' => 'User ID',
  65. 'pay_price' => 'Pay Price',
  66. 'hi_bei' => 'Hi Bei',
  67. 'price_setting_method' => 'Price Setting Method',
  68. 'pay_type' => 'Pay Type',
  69. 'is_pay' => 'Is Pay',
  70. 'pay_at' => 'Pay At',
  71. 'is_delete' => 'Is Delete',
  72. 'created_at' => 'Created At',
  73. 'deleted_at' => 'Deleted At',
  74. 'updated_at' => 'Updated At',
  75. ];
  76. }
  77. public static function createOrder($params)
  78. {
  79. try {
  80. $recharge = Yii::$app->services->setting->addons->get($params['mall_id'], HiGoEnum::ADDONS_NAME, 'recharge');
  81. $hi_bei = 0;
  82. if ($params['price_setting_method'] == 1) {
  83. $hi_bei = bcmul($params['pay_price'], bcdiv($recharge['price_setting_ratio'], 100, 10), 10);
  84. }
  85. if ($params['price_setting_method'] == 0) {
  86. $price_ladder = isset($recharge['price_ladder']) ? json_decode($recharge['price_ladder'], true) : [];
  87. foreach ($price_ladder as $item) {
  88. if ($item['uuid'] == $params['uuid']) {
  89. $hi_bei = $item['give'];
  90. break;
  91. }
  92. }
  93. }
  94. if ($hi_bei <= 0) {
  95. throw new Exception('系统充值设置异常,请稍后再试');
  96. }
  97. if ($hi_bei != $params['hi_bei']) {
  98. throw new Exception('系统充值设置异常,请联系系统管理员处理');
  99. }
  100. $wallet_text = HiGoEnum::getHigoWalletText($params['mall_id']);
  101. $hi_bei_name = $wallet_text[HiGoEnum::WALLET_TYPE_HI_BEI];
  102. $model = new self();
  103. $model->mall_id = $params['mall_id'];
  104. $model->pay_price = $params['pay_price'];
  105. $model->hi_bei = $params['hi_bei'];
  106. $model->order_no = HiGoEnum::generateOrderNo('HIRE');
  107. $model->user_id = $params['user_id'];
  108. $model->price_setting_method = $params['price_setting_method'];
  109. $model->status = StatusEnum::ENABLED;
  110. $model->pay_type = self::PAY_TYPE_ON_LINE;
  111. $model->save();
  112. $order_trade_info[] = [
  113. 'order_no' => $model->order_no,
  114. 'amount' => $model->pay_price,
  115. 'title' => '充值' . $hi_bei_name,
  116. 'notify_class' => 'addons\HiGo\common\logic\notify\RechargeNotify',
  117. 'order_type' => HiGoEnum::ADDONS_NAME . '_Recharge'
  118. ];
  119. //生成支付流水
  120. return \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
  121. } catch (Exception $e) {
  122. self::$static_error = $e->getMessage();
  123. return false;
  124. }
  125. }
  126. public static function check($params): bool
  127. {
  128. $order_no_arr = $params['order_no_arr'];
  129. $payment_order_list = $params['payment_order_list'];
  130. $counts = self::find()->where(['order_no' => $order_no_arr, 'is_pay' => 0])->count();
  131. if ($counts != count($payment_order_list)) {
  132. self::$static_error = '充值订单不一致';
  133. return false;
  134. }
  135. return true;
  136. }
  137. }