BalanceForm.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\purchase;
  8. use app\models\Admin;
  9. use app\models\Option;
  10. use app\models\RechargeReOrder;
  11. use app\models\Store;
  12. use app\modules\admin\models\PlatformForm;
  13. use yii\base\Model;
  14. use app\models\PurchaseReOrder;
  15. use app\models\User;
  16. use app\modules\client\models\ApiModel;
  17. use app\modules\client\models\v1\order\OrderPayDataForm;
  18. use app\utils\Alipay\Alipay;
  19. use app\utils\OrderNo;
  20. use app\utils\Wechat\WechatNewPay;
  21. use app\utils\Wechat\WechatPay;
  22. use EasyWeChat\Factory;
  23. use app\utils\Wechat\WechatBusinessPay;
  24. use app\models\StoreCloud;
  25. use app\utils\Wechat\WechatAlliancePay;
  26. class BalanceForm extends Model
  27. {
  28. public $saas_id;
  29. public $store_id;
  30. public $pay_price;
  31. public $send_price = 0;
  32. public $pay_type;
  33. public $_from;
  34. public $order;
  35. public $user;
  36. public function rules()
  37. {
  38. return [
  39. [['send_price'], 'number'],
  40. [['_from'], 'string'],
  41. [['pay_type'],'in','range'=>[1, 4]],
  42. [['_from'], 'in', 'range' => ['app', 'mini', 'h5']],
  43. [['saas_id', 'user', 'order', 'pay_price','pay_type'], 'safe'],
  44. ];
  45. }
  46. public function payData()
  47. {
  48. if (!$this->validate()) {
  49. return [
  50. 'code' => 1,
  51. 'msg' => $this->getErrorSummary(false)[0],
  52. ];
  53. }
  54. $orderType = OrderNo::ORDER_PURCHASE_RE;
  55. $order = new PurchaseReOrder();
  56. $order->store_id = $this->store_id;
  57. $order->user_id = $this->user->id;
  58. $order->saas_id = $this->saas_id;
  59. // 是否是平台小程序订单
  60. if (is_platform()) {
  61. $order->is_platform = 1;
  62. }
  63. $order->pay_price = $this->pay_price;
  64. $order->send_price = $this->send_price;
  65. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_PURCHASE_RE);
  66. $order->is_pay = 0;
  67. $order->pay_type = $this->pay_type;
  68. $order->is_delete = 0;
  69. $order->created_at = time();
  70. // var_dump($this);die;
  71. if ($order->save()) {
  72. $this->order = $order;
  73. $body = "充值";
  74. if ($this->pay_type == 1) {
  75. // $result = WechatAlliancePay::orderUnify($this->order,$orderType, $body, 0, false, 0);
  76. $result = WechatBusinessPay::orderUnify($this->order, $orderType, $body, 0, false, 0);
  77. if (isset($result['code']) && $result['code'] == 1) {
  78. return $result;
  79. }
  80. return [
  81. 'code' => 0,
  82. 'msg' => 'success',
  83. 'data' => (object)$result['data'],
  84. 'res' => $result['res'],
  85. 'body' => $body,
  86. ];
  87. }
  88. $goods_names = $body;
  89. $res = Alipay::mini($this->order, $goods_names, $this->user, '', '', 0, 0);
  90. if ($res['code'] != 0) {
  91. return $res;
  92. }
  93. return [
  94. 'code' => 0,
  95. 'msg' => 'success',
  96. 'data' => $res['data'],
  97. 'res' => $res['data'],
  98. 'body' => $body,
  99. ];
  100. } else {
  101. return [
  102. 'code' => 1,
  103. 'msg' => $this->getErrorSummary(false)[0],
  104. ];
  105. }
  106. }
  107. public function payDataLeague()
  108. {
  109. if (!$this->validate()) {
  110. return [
  111. 'code' => 1,
  112. 'msg' => $this->getErrorSummary(false)[0],
  113. ];
  114. }
  115. $orderType = OrderNo::ORDER_PURCHASE_RE;
  116. $order = new RechargeReOrder();
  117. $order->user_id = $this->user->id;
  118. $order->saas_id = $this->saas_id;
  119. // 是否是平台小程序订单
  120. if (is_platform()) {
  121. $order->is_platform = 1;
  122. }
  123. $order->pay_price = $this->pay_price;
  124. // $store_recharge_profit_model = Option::findOne(['name' => 'store_recharge_profit']);
  125. // $profit = $store_recharge_profit_model->value;
  126. $admin = Admin::findOne(['saas_user_id' => $this->saas_id, 'is_delete' => 0, 'type' => 'store']);
  127. $store = Store::findOne($admin->type_id);
  128. $order->store_id = $store->id;
  129. $order->league_price = $this->pay_price;
  130. $order->send_price = $this->send_price;
  131. $order->order_no = OrderNo::getOrderNo(OrderNo::LEAGUE_PRICE);
  132. $order->is_pay = 0;
  133. $order->pay_type = $this->pay_type;
  134. $order->is_delete = 0;
  135. $order->created_at = time();
  136. // var_dump($this);die;
  137. if ($order->save()) {
  138. $this->order = $order;
  139. $body = "充值联盟券";
  140. if ($this->pay_type == 1) {
  141. // $result = WechatAlliancePay::orderUnify($this->order,$orderType, $body, 0, false, 0);
  142. $result = WechatBusinessPay::orderUnify($this->order, $orderType, $body, 0, false, 0);
  143. if (isset($result['code']) && $result['code'] == 1) {
  144. return $result;
  145. }
  146. return [
  147. 'code' => 0,
  148. 'msg' => 'success',
  149. 'data' => (object)$result['data'],
  150. 'res' => $result['res'],
  151. 'body' => $body,
  152. ];
  153. }
  154. $goods_names = $body;
  155. $res = Alipay::mini($this->order, $goods_names, $this->user, '', '', 0, 0);
  156. if ($res['code'] != 0) {
  157. return $res;
  158. }
  159. return [
  160. 'code' => 0,
  161. 'msg' => 'success',
  162. 'data' => $res['data'],
  163. 'res' => $res['data'],
  164. 'body' => $body,
  165. ];
  166. } else {
  167. return [
  168. 'code' => 1,
  169. 'msg' => $this->getErrorSummary(false)[0],
  170. ];
  171. }
  172. }
  173. //商户充值
  174. public function storeSubmitRecharge(){
  175. $order = $this->order;
  176. $store_id = $order->store_id;
  177. $item = \app\models\StoreCloud::findOne(['store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1]);
  178. // $storeCloud = get_saas_purchase_store_cloud();
  179. if (!$item) {
  180. return [
  181. 'code' => 11,
  182. 'msg' => '商户未找到',
  183. ];
  184. }
  185. $form = new PlatformForm();
  186. $form->attributes = [
  187. 'id' => $item->cloud_store_id,
  188. 'price' => $order->pay_price,
  189. ];
  190. $res = $form->storeSubmitRecharge();
  191. \Yii::error([__METHOD__, 'storeSubmitRecharge', $res, $this->order, $item]);
  192. return $res;
  193. }
  194. }