OrderPayFrom.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\TinyShop\common\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\interfaces\PayHandler;
  6. use addons\TinyShop\common\enums\OrderStatusEnum;
  7. use addons\TinyShop\common\models\order\OrderProduct;
  8. use addons\TinyShop\common\models\order\Order;
  9. /**
  10. * Class OrderPayFrom
  11. * @package addons\TinyShop\common\forms
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class OrderPayFrom extends Model implements PayHandler
  15. {
  16. /**
  17. * @var
  18. */
  19. public $order_id;
  20. /**
  21. * @var Order
  22. */
  23. protected $order;
  24. /**
  25. * @return array
  26. */
  27. public function rules()
  28. {
  29. return [
  30. ['order_id', 'required'],
  31. ['order_id', 'integer', 'min' => 0],
  32. ['order_id', 'verifyPay'],
  33. ];
  34. }
  35. /**
  36. * @param $attribute
  37. * @throws \yii\web\UnprocessableEntityHttpException
  38. */
  39. public function verifyPay($attribute)
  40. {
  41. empty($this->order) && $this->order = Yii::$app->tinyShopService->order->findById($this->order_id);
  42. if (!$this->order) {
  43. $this->addError($attribute, '找不到订单');
  44. return;
  45. }
  46. if ($this->order['order_status'] != OrderStatusEnum::NOT_PAY) {
  47. $this->addError($attribute, '订单已完成');
  48. return;
  49. }
  50. // 支付前验证库存
  51. Yii::$app->tinyShopService->productSku->decrRepertory($this->order, $this->order->product, null, false);
  52. }
  53. /**
  54. * 支付说明
  55. *
  56. * @return string
  57. */
  58. public function getBody(): string
  59. {
  60. return '订单支付';
  61. }
  62. /**
  63. * 支付详情
  64. *
  65. * @return string
  66. */
  67. public function getDetails(): string
  68. {
  69. return '';
  70. }
  71. /**
  72. * 支付金额
  73. *
  74. * @return float
  75. */
  76. public function getTotalFee(): float
  77. {
  78. return $this->order['pay_money'];
  79. }
  80. /**
  81. * 获取订单号
  82. *
  83. * @return float
  84. */
  85. public function getOrderSn(): string
  86. {
  87. return $this->order['order_sn'];
  88. }
  89. /**
  90. * 交易流水号
  91. *
  92. * @return string
  93. */
  94. public function getOutTradeNo()
  95. {
  96. return $this->order['out_trade_no'];
  97. }
  98. /**
  99. * @return int
  100. */
  101. public function getMerchantId(): int
  102. {
  103. return $this->order['merchant_id'];
  104. }
  105. /**
  106. * 是否查询订单号(避免重复生成)
  107. *
  108. * @return bool
  109. */
  110. public function isQueryOrderSn(): bool
  111. {
  112. return true;
  113. }
  114. }
粤ICP备19079148号