LevelPayDataForm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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\Level;
  9. use app\models\LevelOrder;
  10. use app\models\Order;
  11. use app\models\ReOrder;
  12. use app\models\SaasUser;
  13. use app\models\UserShareMoney;
  14. use app\modules\client\models\ApiModel;
  15. use app\models\AccountLog;
  16. use app\models\common\CommonOrder;
  17. use app\models\User;
  18. use app\modules\client\models\OrderComplete;
  19. use app\utils\Alipay\Alipay;
  20. use app\utils\Alipay\AlipayProfit;
  21. use app\utils\CloudPrint;
  22. use app\utils\OrderNo;
  23. use app\utils\Wechat\WechatNewPay;
  24. use app\utils\Wechat\WechatPay;
  25. use yii\base\BaseObject;
  26. use yii\base\Model;
  27. use yii\helpers\Json;
  28. use app\utils\Delivery\Delivery;
  29. use app\models\Store;
  30. /**
  31. * @property User $user
  32. * @property LevelOrder $order
  33. */
  34. class LevelPayDataForm extends ApiModel
  35. {
  36. public $store_id;
  37. public $order_id;
  38. public $order_id_list;
  39. public $pay_type;
  40. public $user;
  41. public $form_id;
  42. public $parent_user_id;
  43. public $condition;
  44. public $_from;
  45. public $is_combine;
  46. public $session_key;
  47. /** @var Wechat $wechat */
  48. private $wechat;
  49. private $wechatPay;
  50. private $order;
  51. const PAY_FROM_APP = 'app';
  52. const PAY_FROM_OFFICIAL = 'official';
  53. const PAY_FROM_H5 = 'h5';
  54. const PAY_FROM_MINI = 'mini';
  55. public function rules()
  56. {
  57. return [
  58. [['pay_type', '_from'], 'required'],
  59. [['_from'], 'in', 'range' => ['app', 'mini', 'official', self::PAY_FROM_H5]],
  60. [['pay_type'], 'in', 'range' => [1, 2, 3, 4, 5, Order::PAY_TYPE_WX_B2B]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY',
  61. [['_from'], 'string'],
  62. ['is_combine', 'default', 'value' => 0],
  63. [[ 'order_id', 'is_combine'], 'integer'],
  64. [['session_key'], 'safe']
  65. ];
  66. }
  67. public function search()
  68. {
  69. $this->wechatPay = \Yii::$app->controller->wechatPay;
  70. if (!$this->validate()) {
  71. return [
  72. 'code' => 1,
  73. 'msg' => $this->getErrorSummary(false)[0],
  74. ];
  75. }
  76. $this->user->money = doubleval($this->user->money);
  77. $t = \Yii::$app->db->beginTransaction();
  78. $this->order = LevelOrder::findOne([
  79. 'store_id' => $this->store_id,
  80. 'id' => $this->order_id,
  81. ]);
  82. if (!$this->order) {
  83. $t->rollBack();
  84. return [
  85. 'code' => 1,
  86. 'msg' => '订单不存在',
  87. ];
  88. }
  89. if ($this->order->is_delete == 1 || $this->order->is_pay == 1) {
  90. $t->rollBack();
  91. return [
  92. 'code' => 1,
  93. 'msg' => '订单已取消或已支付',
  94. ];
  95. }
  96. $goods_names = '会员购买';
  97. // 余额抵扣支付
  98. $balance_price = 0;
  99. if ($this->is_combine) {
  100. if ($this->user->money >= $this->order->pay_price) {
  101. $this->pay_type = 3;
  102. } else {
  103. $balance_price = $this->user->money;
  104. $this->order->combine_money = $this->user->money;
  105. $this->order->is_combine_pay = 1;
  106. $this->order->save();
  107. }
  108. } else {
  109. $this->order->combine_money = 0;
  110. $this->order->is_combine_pay = 0;
  111. $this->order->save();
  112. }
  113. // 供应链系统下单时是否使用平台商户号,1使用,0未使用
  114. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->store_id)) {
  115. $this->order->is_use_platform_mch = 1;
  116. $this->order->save();
  117. }
  118. if ($this->pay_type == 1 || $this->pay_type == 4) {
  119. if ($this->order->pay_price == 0) {
  120. $this->order->is_pay = 1;
  121. $this->order->pay_type = 1;
  122. if ($this->pay_type == 4) {
  123. $this->order->pay_type = 4;
  124. }
  125. $this->order->pay_time = time();
  126. if (!$this->order->save()) {
  127. $t->rollBack();
  128. return [
  129. 'code' => 1,
  130. 'msg' => '支付失败'
  131. ];
  132. }
  133. $t->commit();
  134. return [
  135. 'code' => 0,
  136. 'msg' => '0元支付'
  137. ];
  138. }
  139. if ($this->pay_type == 1) {
  140. // if ($this->_from == self::PAY_FROM_APP) {
  141. // if (is_profit_pay()) {
  142. // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price);
  143. // } else {
  144. // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price);
  145. // }
  146. // } else {
  147. // if (is_profit_pay()) {
  148. // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price);
  149. // } else {
  150. // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price);
  151. // }
  152. // }
  153. if ($this->_from == self::PAY_FROM_APP) {
  154. $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']);
  155. if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
  156. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
  157. } else {
  158. if (is_profit_pay()) {
  159. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
  160. } else {
  161. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
  162. }
  163. }
  164. } else if ($this->_from == self::PAY_FROM_H5) {
  165. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) {
  166. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
  167. } else {
  168. if (is_profit_pay()) {
  169. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
  170. } else {
  171. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
  172. }
  173. }
  174. } else if ($this->_from == self::PAY_FROM_OFFICIAL) {
  175. $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']);
  176. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && !$self_mini) {
  177. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
  178. } else {
  179. if (is_profit_pay()) {
  180. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
  181. } else {
  182. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
  183. }
  184. }
  185. } else {
  186. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) {
  187. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
  188. } else {
  189. if (is_profit_pay() && Store::hasIncoming($this->order->store_id)) {
  190. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
  191. } else {
  192. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
  193. }
  194. }
  195. }
  196. if (isset($result['code']) && $result['code'] == 1) {
  197. return $result;
  198. }
  199. CloudPrint::doPrint($this->order->id, 0, $this->order->store_id);
  200. $t->commit();
  201. return [
  202. 'code' => 0,
  203. 'msg' => 'success',
  204. 'data' => (object)$result['data'],
  205. 'res' => $result['res'],
  206. 'body' => $goods_names
  207. ];
  208. }
  209. if ($this->pay_type == 4) {
  210. if (is_profit_pay('ali')) {
  211. $result = AlipayProfit::mini($this->order, $goods_names, $this->user, '', '', false, 0, $balance_price);
  212. } else {
  213. $result = Alipay::mini($this->order, $goods_names, $this->user, '', '', 0, $balance_price);
  214. }
  215. if (isset($result['code']) && $result['code'] == 1) {
  216. return $result;
  217. }
  218. $t->commit();
  219. return [
  220. 'code' => 0,
  221. 'msg' => 'success',
  222. 'data' => $result['data']['trade_no'],
  223. 'body' => $goods_names
  224. ];
  225. }
  226. }
  227. if ($this->pay_type == Order::PAY_TYPE_WX_B2B) {
  228. return \app\modules\admin\models\UserAuditForm::pay($this->session_key, $this->order,OrderNo::ORDER_RECHARGE, $goods_names);
  229. }
  230. //货到付款和余额支付数据处理
  231. if ($this->pay_type == 2 || $this->pay_type == 3) {
  232. $order = $this->order;
  233. // 中间页选择货到付款,需要存入支付类型
  234. if ($this->pay_type == 2) {
  235. $order->pay_type = 2;
  236. $order->save();
  237. }
  238. //余额支付 用户余额变动
  239. if ($this->pay_type == 3) {
  240. $user = User::findOne(['id' => $order->user_id]);
  241. if ($user->money < $order->pay_price) {
  242. $t->rollBack();
  243. return [
  244. 'code' => 1,
  245. 'msg' => '支付失败,余额不足',
  246. ];
  247. }
  248. // $user->money -= floatval($order->pay_price);
  249. $res = AccountLog::saveLog($user->id, floatval($order->pay_price), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 1, $order->id, "购买会员余额支付,订单号为:{$order->order_no}。");
  250. if (!$res) {
  251. $t->rollBack();
  252. return [
  253. 'code' => 1,
  254. 'msg' => '支付失败'
  255. ];
  256. }
  257. $order->is_pay = 1;
  258. $order->pay_type = 3;
  259. $order->pay_time = time();
  260. if (!$order->save()) {
  261. $t->rollBack();
  262. return [
  263. 'code' => 1,
  264. 'msg' => '支付失败'
  265. ];
  266. } else {
  267. //会员升级
  268. $user = User::findOne($order->user_id);
  269. $user->level = $order->after_level;
  270. $user->save();
  271. $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]);
  272. if ($level) {
  273. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  274. $user_name = $user->nickname;
  275. if ($saas_user) {
  276. $user_name = $saas_user->name;
  277. }
  278. if ($user->old_parent_id) {
  279. $parent = User::findOne($user->old_parent_id);//上级
  280. //检测一级是否存在且佣金是否设置
  281. if ($parent && $level->firstProfit) {
  282. //发放一级佣金
  283. //...
  284. $parent->total_price += $level->firstProfit;
  285. $parent->price += $level->firstProfit;
  286. $parent->save();
  287. UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金');
  288. if ($parent->old_parent_id) {
  289. $parent_1 = User::findOne($parent->old_parent_id);//上级
  290. //检测二级是否存在且佣金是否设置
  291. if ($parent_1 && $level->secondProfit) {
  292. //发放二级佣金
  293. $parent_1->total_price += $level->secondProfit;
  294. $parent_1->price += $level->secondProfit;
  295. $parent_1->save();
  296. UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 2, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金');
  297. //检测三级是否存在且佣金是否设置
  298. $parent_2 = User::findOne($parent_1->old_parent_id);//上级
  299. if ($parent_2 && $level->thirdProfit) {
  300. //发放三级佣金
  301. $parent_2->total_price += $level->thirdProfit;
  302. $parent_2->price += $level->thirdProfit;
  303. $parent_2->save();
  304. UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 3, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金');
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. $t->commit();
  314. return [
  315. 'code' => 0,
  316. 'msg' => '下单成功',
  317. 'data' => ''
  318. ];
  319. }
  320. }
  321. }