RechargePayFrom.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\TinyShop\common\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\helpers\StringHelper;
  6. use common\interfaces\PayHandler;
  7. use addons\TinyShop\common\models\order\Recharge;
  8. /**
  9. * Class RechargePayFrom
  10. * @package addons\TinyShop\common\forms
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class RechargePayFrom extends Model implements PayHandler
  14. {
  15. /**
  16. * @var
  17. */
  18. public $money;
  19. /**
  20. * 充值订单
  21. *
  22. * @var Recharge
  23. */
  24. protected $order;
  25. /**
  26. * @return array
  27. */
  28. public function rules()
  29. {
  30. return [
  31. ['money', 'required'],
  32. ['money', 'number', 'min' => 0.01],
  33. ['money', 'verifyPay'],
  34. ];
  35. }
  36. /**
  37. * @param $attribute
  38. * @throws \yii\web\UnprocessableEntityHttpException
  39. */
  40. public function verifyPay($attribute)
  41. {
  42. $model = new Recharge();
  43. $model = $model->loadDefaultValues();
  44. $model->price = $this->money;
  45. // 充值赠送
  46. if (!empty($rechargeConfig = Yii::$app->tinyShopService->marketingRechargeConfig->getGiveMoney($this->money))) {
  47. $model->give_price = $rechargeConfig->give_price;
  48. $model->give_point = $rechargeConfig->give_point;
  49. $model->give_growth = $rechargeConfig->give_growth;
  50. $model->give_coupon_type_ids = [];
  51. }
  52. $model->member_id = Yii::$app->user->identity->member_id;
  53. $model->order_sn = time() . StringHelper::random(8, true);
  54. $model->out_trade_no = date('YmdHis') . StringHelper::random(8, true);
  55. $model->save();
  56. $this->order = $model;
  57. }
  58. /**
  59. * 支付说明
  60. *
  61. * @return string
  62. */
  63. public function getBody(): string
  64. {
  65. return '在线充值';
  66. }
  67. /**
  68. * 支付详情
  69. *
  70. * @return string
  71. */
  72. public function getDetails(): string
  73. {
  74. return '';
  75. }
  76. /**
  77. * 支付金额
  78. *
  79. * @return float
  80. */
  81. public function getTotalFee(): float
  82. {
  83. return $this->money;
  84. }
  85. /**
  86. * @return int
  87. */
  88. public function getMerchantId(): int
  89. {
  90. return Yii::$app->services->merchant->getNotNullId();
  91. }
  92. /**
  93. * 获取订单号
  94. *
  95. * @return float
  96. */
  97. public function getOrderSn(): string
  98. {
  99. return $this->order->order_sn;
  100. }
  101. /**
  102. * 交易流水号
  103. *
  104. * @return string
  105. */
  106. public function getOutTradeNo()
  107. {
  108. return $this->order->out_trade_no;
  109. }
  110. /**
  111. * 是否查询订单号(避免重复生成)
  112. *
  113. * @return bool
  114. */
  115. public function isQueryOrderSn(): bool
  116. {
  117. return false;
  118. }
  119. }
粤ICP备19079148号