IntegralRechargeSubmitForm.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\IntegralRecharge;
  9. use app\models\IntegralRechargeOrder;
  10. use app\models\User;
  11. use app\modules\admin\models\AlipayThirdForm;
  12. use app\modules\client\models\ApiModel;
  13. use app\modules\client\models\v1\order\OrderPayDataForm;
  14. use app\utils\Alipay\Alipay;
  15. use app\utils\Alipay\AlipayProfit;
  16. use app\utils\OrderNo;
  17. use app\utils\Wechat\WechatNewPay;
  18. use app\utils\Wechat\WechatPay;
  19. use EasyWeChat\Factory;
  20. use app\models\Store;
  21. /**
  22. * @property User $user
  23. * @property IntegralRechargeOrder $order
  24. */
  25. class IntegralRechargeSubmitForm extends ApiModel
  26. {
  27. public $store_id;
  28. public $pay_price;
  29. public $send_integral;
  30. public $pay_type;
  31. public $_from;
  32. public $recharge_id;
  33. /* @var Factory $wechatPay */
  34. public $wechatPay;
  35. public $order;
  36. public $user;
  37. public function rules()
  38. {
  39. return [
  40. [['pay_price', 'pay_type', 'recharge_id'], 'required'],
  41. [['send_integral'], 'number'],
  42. [['_from'], 'string'],
  43. [['pay_type'], 'in', 'range' => [1, 4]],
  44. [['_from'], 'in', 'range' => ['app', 'mini', 'h5']],
  45. ];
  46. }
  47. public function save()
  48. {
  49. $this->wechatPay = self::getWechatPay();
  50. if (!$this->validate()) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $this->getErrorSummary(false)[0],
  54. ];
  55. }
  56. $order = new IntegralRechargeOrder();
  57. $order->store_id = $this->store_id;
  58. $order->user_id = $this->user->id;
  59. if ($this->send_integral != 0) {
  60. $exists = IntegralRecharge::findOne($this->recharge_id);
  61. if (!$exists) {
  62. return [
  63. 'code' => 1,
  64. 'msg' => '充值失败,请重新充值'
  65. ];
  66. }
  67. }
  68. // 是否是平台小程序订单
  69. if (is_platform()) {
  70. $order->is_platform = 1;
  71. }
  72. $order->pay_price = $this->pay_price;
  73. $order->send_integral = $this->send_integral;
  74. $order->order_no = OrderNo::getOrderNo(OrderNo::INTEGRAL_RECHARGE);
  75. $order->is_pay = 0;
  76. $order->pay_type = $this->pay_type;
  77. $order->is_delete = 0;
  78. $order->recharge_id = $this->recharge_id;
  79. $order->created_at = time();
  80. if ($order->save()) {
  81. $this->order = $order;
  82. $body = "积分套餐充值";
  83. if ($this->pay_type == 1) {
  84. if ($this->_from == OrderPayDataForm::PAY_FROM_APP) {
  85. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->store_id) && self_mini() === false) {
  86. $result = WechatPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, true);
  87. } else {
  88. if (is_profit_pay()) {
  89. $result = WechatNewPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, true);
  90. } else {
  91. $result = WechatPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, true);
  92. }
  93. }
  94. } else {
  95. $is_h5 = false;
  96. if (is_h5()) {
  97. $is_h5 = true;
  98. }
  99. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->store_id) && self_mini() === false) {
  100. $result = WechatPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, false, 0, 0, $is_h5);
  101. } else {
  102. if (is_profit_pay()) {
  103. $result = WechatNewPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, false, 0, 0, $is_h5);
  104. } else {
  105. $result = WechatPay::orderUnify($order, OrderNo::ORDER_RECHARGE, $body, 0, false, 0, 0, $is_h5);
  106. }
  107. }
  108. }
  109. if (isset($result['code']) && $result['code'] == 1) {
  110. return $result;
  111. }
  112. return [
  113. 'code' => 0,
  114. 'msg' => 'success',
  115. 'data' => (object)$result['data'],
  116. 'res' => $result['res'],
  117. 'body' => $body,
  118. ];
  119. } elseif ($this->pay_type == 4) {
  120. $user = get_user();
  121. $open_user_id = $user->alipay_open_id;
  122. $form = new AlipayThirdForm();
  123. $form->method = "alipay.open.auth.token.app";
  124. $form->bind_store_id = get_store_id();
  125. if (!$open_user_id) {
  126. return [
  127. 'code' => 0,
  128. 'msg' => '参数获取错误'
  129. ];
  130. }
  131. if (!empty(get_saas_user())) {
  132. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) {
  133. $res = Alipay::mini($this->order, '充值', get_saas_user(), '', '', 0, 0);
  134. } else {
  135. if (is_profit_pay('ali')) {
  136. $res = AlipayProfit::mini($this->order, '充值', get_saas_user(), '', '', false, 0, 0);
  137. } else {
  138. $res = Alipay::mini($this->order, '充值', get_saas_user(), '', '', 0, 0);
  139. }
  140. }
  141. if (isset($res['code']) && $res['code'] == 1) {
  142. return $res;
  143. }
  144. if (isset($res['data'])) {
  145. $res['data'] = array_merge($res['data'], [
  146. 'is_finish' => 1
  147. ]);
  148. }
  149. return $res;
  150. }
  151. }
  152. } else {
  153. return [
  154. 'code' => 1,
  155. 'msg' => $this->getErrorSummary(false)[0],
  156. ];
  157. }
  158. }
  159. }