CostOrderPayDataForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\adopt\models\client;
  8. use app\models\AccountLog;
  9. use app\models\common\CommonOrder;
  10. use app\models\Goods;
  11. use app\models\OrderUnion;
  12. use app\models\User;
  13. use app\modules\client\models\OrderComplete;
  14. use app\plugins\adopt\models\AdoptCostOrder;
  15. use app\plugins\adopt\models\AdoptOrderInfo;
  16. use app\modules\client\models\v1\ShareMoneyForm;
  17. use app\plugins\scanCodePay\models\Order;
  18. use app\plugins\scanCodePay\models\OrderDetail;
  19. use app\utils\Alipay\Alipay;
  20. // use app\utils\CloudPrint;
  21. use app\utils\Alipay\AlipayProfit;
  22. use app\utils\CloudPrint;
  23. use app\utils\Delivery\Delivery;
  24. use app\utils\OrderNo;
  25. use app\utils\Wechat\WechatNewPay;
  26. use app\utils\Wechat\WechatPay;
  27. use ByteDance\ByteDance;
  28. use EasyWeChat\MiniProgram\Application;
  29. use yii\base\BaseObject;
  30. use yii\base\Model;
  31. /**
  32. * @property User $user
  33. * @property Order $order
  34. */
  35. class CostOrderPayDataForm extends Model
  36. {
  37. public $store_id;
  38. public $order_id;
  39. public $order_id_list;
  40. public $pay_type;
  41. public $user;
  42. public $form_id;
  43. public $parent_user_id;
  44. public $condition;
  45. public $_from;
  46. public $is_combine;
  47. /** @var Application $wechat */
  48. private $wechat;
  49. private $wechatPay;
  50. private $order;
  51. const PAY_FROM_APP = 'app';
  52. const PAY_FROM_MINI = 'mini';
  53. const PAY_FROM_H5 = 'h5';
  54. public function rules()
  55. {
  56. return [
  57. [['pay_type', '_from'], 'required'],
  58. [['_from'], 'in', 'range' => ['app', 'mini']],
  59. [['pay_type'], 'in', 'range' => [1, 2, 3, 4]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY',
  60. [['form_id', 'order_id_list', '_from'], 'string'],
  61. ['is_combine', 'default', 'value' => 0],
  62. [['parent_user_id', 'condition', 'order_id', 'is_combine'], 'integer'],
  63. ];
  64. }
  65. public function search()
  66. {
  67. $this->wechatPay = \Yii::$app->controller->wechatPay;
  68. if (!$this->validate()) {
  69. return [
  70. 'code' => 1,
  71. 'msg' => $this->getErrorSummary(false)[0],
  72. ];
  73. }
  74. $this->user->money = doubleval($this->user->money);
  75. if ($this->order_id_list) {
  76. $order_id_list = json_decode($this->order_id_list, true);
  77. if (is_array($order_id_list) && count($order_id_list) == 1) {
  78. $this->order_id = $order_id_list[0];
  79. $this->order_id_list = '';
  80. }
  81. }
  82. $t = \Yii::$app->db->beginTransaction();
  83. $this->order = AdoptCostOrder::findOne([
  84. 'store_id' => $this->store_id,
  85. 'id' => $this->order_id,
  86. ]);
  87. if (!$this->order) {
  88. $t->rollBack();
  89. return [
  90. 'code' => 1,
  91. 'msg' => '订单不存在',
  92. ];
  93. }
  94. if ($this->order->is_delete == 1 || $this->order->trade_status == Order::ORDER_FLOW_CANCEL) {
  95. $t->rollBack();
  96. return [
  97. 'code' => 1,
  98. 'msg' => '订单已取消',
  99. ];
  100. }
  101. // 存储parent_id
  102. CommonOrder::saveParentId($this->parent_user_id);
  103. $goods_names = '';
  104. if ($this->order->order_type == 0){
  105. $goods_names = "代采订单";
  106. }
  107. if ($this->order->order_type == 1){
  108. $goods_names = "保管订单";
  109. }
  110. if (!$this->order->save()) {
  111. $t->rollBack();
  112. return [
  113. 'code' => 1,
  114. 'msg' => '订单保存失败'
  115. ];
  116. }
  117. // 余额抵扣支付
  118. $balance_price = 0;
  119. if ($this->is_combine) {
  120. if ($this->user->money >= $this->order->pay_price) {
  121. $this->pay_type = 3;
  122. } else {
  123. $balance_price = $this->user->money;
  124. $this->order->combine_money = $this->user->money;
  125. $this->order->is_combine_pay = \app\models\Order::IS_COMBINE_PAY;
  126. $this->order->save();
  127. }
  128. } else {
  129. $this->order->combine_money = 0;
  130. $this->order->is_combine_pay = \app\models\Order::NOT_COMBINE_PAY;
  131. $this->order->save();
  132. }
  133. if ($this->pay_type == 1 || $this->pay_type == 4 || $this->pay_type == 5) {
  134. if ($this->order->pay_price == 0) {
  135. $this->order->is_pay = 1;
  136. $this->order->pay_type = 1;
  137. if ($this->pay_type == 4) {
  138. $this->order->pay_type = 4;
  139. }
  140. $this->order->pay_time = time();
  141. if (!$this->order->save()) {
  142. $t->rollBack();
  143. return [
  144. 'code' => 1,
  145. 'msg' => '支付失败'
  146. ];
  147. }
  148. // // 支付完成后,相关操作
  149. $form = new OrderComplete();
  150. $form->order_id = $this->order->id;
  151. $form->order_type = 0;
  152. $form->store_id = get_store_id();
  153. $form->adoptOrderCostNotify();
  154. $t->commit();
  155. return [
  156. 'code' => 0,
  157. 'msg' => '0元支付'
  158. ];
  159. }
  160. if ($this->pay_type == 1) {
  161. if ($this->_from == self::PAY_FROM_APP) {
  162. if (is_profit_sharing()) {
  163. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price);
  164. } else {
  165. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price);
  166. }
  167. } else if ($this->_from == self::PAY_FROM_H5) {
  168. if (is_profit_sharing()) {
  169. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true);
  170. } else {
  171. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true);
  172. }
  173. } else {
  174. if (is_profit_sharing()) {
  175. $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price);
  176. } else {
  177. $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price);
  178. }
  179. }
  180. if (isset($result['code']) && $result['code'] == 1) {
  181. return $result;
  182. }
  183. // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id);
  184. $t->commit();
  185. return [
  186. 'code' => 0,
  187. 'msg' => 'success',
  188. 'data' => (object)$result['data'],
  189. 'res' => $result['res'],
  190. 'body' => $goods_names
  191. ];
  192. }
  193. if ($this->pay_type == 4) {
  194. if (\Yii::$app->params['paySwitch'] == 1) {
  195. $result = AlipayProfit::mini($this->order, $goods_names, $this->user, '', '', false, 0, $balance_price);
  196. } else {
  197. $result = Alipay::mini($this->order, $goods_names, $this->user, '', '', 0, $balance_price);
  198. }
  199. if (isset($result['code']) && $result['code'] == 1) {
  200. return $result;
  201. }
  202. $t->commit();
  203. return [
  204. 'code' => 0,
  205. 'msg' => 'success',
  206. 'data' => $result['data']['trade_no'],
  207. 'body' => $goods_names
  208. ];
  209. }
  210. // 抖音小程序
  211. if ($this->pay_type == 5) {
  212. $result = ByteDance::pay($this->order, '', $goods_names, 0 , $balance_price);
  213. if (isset($result['code']) && $result['code'] == 1) {
  214. return $result;
  215. }
  216. $t->commit();
  217. return [
  218. 'code' => 0,
  219. 'msg' => 'success',
  220. 'data' => $result['data'],
  221. ];
  222. }
  223. }
  224. //货到付款和余额支付数据处理
  225. if ($this->pay_type == 2 || $this->pay_type == 3) {
  226. $order = $this->order;
  227. // 中间页选择货到付款,需要存入支付类型
  228. if ($this->pay_type == 2) {
  229. $order->is_pay = 0;
  230. $order->pay_type = 2;
  231. $order->save();
  232. }
  233. //余额支付 用户余额变动
  234. if ($this->pay_type == 3) {
  235. $user = User::findOne(['id' => $order->user_id]);
  236. if ($user->money < $order->pay_price) {
  237. $t->rollBack();
  238. return [
  239. 'code' => 1,
  240. 'msg' => '支付失败,余额不足',
  241. ];
  242. }
  243. // $user->money -= floatval($order->pay_price);
  244. $res = AccountLog::saveLog($user->id, floatval($order->pay_price), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 1, $order->id, "商城余额支付,订单号为:{$order->order_no}。");
  245. if (!$res) {
  246. $t->rollBack();
  247. return [
  248. 'code' => 1,
  249. 'msg' => '支付失败',
  250. 'res' => $res
  251. ];
  252. }
  253. $order->is_pay = 1;
  254. $order->pay_type = 3;
  255. if ($order->order_type == 1) {
  256. $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND;
  257. } else{
  258. $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM;
  259. }
  260. $order->pay_time = time();
  261. if ($order->user_id != get_user_id()) {
  262. $order->pay_user_id = get_user_id();
  263. }
  264. if ($order->order_type == 0) {
  265. $adopt_order = AdoptOrderInfo::findOne(['order_id' => $order->order_id, 'store_id' => $order->store_id]);
  266. if ($adopt_order->pick_method == AdoptOrderInfo::ORDER_FLOW_NO_PICK) {
  267. $adopt_order->pick_method = AdoptOrderInfo::ORDER_FLOW_OTHER_PICK;
  268. $adopt_order->pick_treatment_method = AdoptOrderInfo::ORDER_FLOW_PICK_STORAGE;
  269. $adopt_order->pick_treatment_method_time = time();
  270. $adopt_order->pick_time = time();
  271. if (!$adopt_order->save()) {
  272. $t->rollBack();
  273. return [
  274. 'code' => 1,
  275. 'msg' => '支付失败'
  276. ];
  277. }
  278. }
  279. }
  280. if (!$order->save()) {
  281. $t->rollBack();
  282. return [
  283. 'code' => 1,
  284. 'msg' => '支付失败'
  285. ];
  286. }
  287. }
  288. // 同城配送订单下单
  289. // if ($order->is_delivery == 1) {
  290. // $detail_goods = \app\models\OrderDetail::find()->where(['order_id' => $order->id])->select('goods_name as name, pic, num, total_price as price')->asArray()->all();
  291. // Delivery::addOrder($detail_goods, $order->order_no);
  292. // }
  293. // 支付完成后,相关操作
  294. $form = new OrderComplete();
  295. $form->order_id = $order->id;
  296. $form->order_type = 0;
  297. $form->store_id = get_store_id();
  298. $form->adoptOrderCostNotify();
  299. // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id);
  300. $t->commit();
  301. return [
  302. 'code' => 0,
  303. 'msg' => '支付成功',
  304. 'data' => ''
  305. ];
  306. }
  307. }
  308. }