OrderController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\custom\classes\CustomClass;
  7. use bizMall\goods\classes\GoodsClass;
  8. use bizMall\order\classes\OrderClass;
  9. use bizMall\order\classes\OrderForwardClass;
  10. use bizMall\order\classes\OrderGoodsClass;
  11. use bizMall\order\classes\OrderItemClass;
  12. use bizMall\order\services\OrderService;
  13. use bizMall\promote\services\CouponService;
  14. use bizMall\saas\services\RegionService;
  15. use bizMall\user\services\UserService;
  16. use common\components\dateUtil;
  17. use common\components\dict;
  18. use common\components\httpUtil;
  19. use common\components\noticeUtil;
  20. use common\components\orderSn;
  21. use common\components\stringUtil;
  22. use common\services\xhPayToolService;
  23. use Yii;
  24. use common\components\util;
  25. use common\components\lakala\Lakala;
  26. class OrderController extends BaseController
  27. {
  28. //二维码收款使用
  29. public $guestAccess = ['order-relate', 'fast-pay'];
  30. //记账单列表 ssh 20250627
  31. public function actionDebtList()
  32. {
  33. $get = Yii::$app->request->get();
  34. $id = $get['id'] ?? 0;
  35. $where = [];
  36. $where['userId'] = $this->userId;
  37. if (!empty($id)) {
  38. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  39. if (empty($hd)) {
  40. util::fail('无效的花店');
  41. }
  42. $where['hdId'] = $id;
  43. }
  44. $where['debt'] = 1;
  45. $searchTime = $get['searchTime'] ?? '';
  46. if (!empty($searchTime)) {
  47. $startTime = $get['startTime'] ?? '';
  48. $endTime = $get['endTime'] ?? '';
  49. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  50. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  51. }
  52. $respond = OrderClass::getDebtList($where);
  53. util::success($respond);
  54. }
  55. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  56. public function actionFreight()
  57. {
  58. $get = Yii::$app->request->get();
  59. $shop = $this->shop;
  60. $shopLat = isset($shop->lat) ? $shop->lat : '';
  61. $shopLong = isset($shop->long) ? $shop->long : '';
  62. if (empty($shopLat) || empty($shopLong)) {
  63. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  64. }
  65. $userLat = $get['lat'] ?? '';
  66. $userLong = $get['long'] ?? '';
  67. if (empty($userLat) || empty($userLong)) {
  68. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  69. }
  70. $weight = $get['weight'] ?? 0;
  71. if (empty($weight)) {
  72. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  73. }
  74. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  75. util::success($respond);
  76. }
  77. //购买花材 ssh 20220511
  78. public function actionBuyItem()
  79. {
  80. $post = Yii::$app->request->post();
  81. $post['sjId'] = $this->sjId;
  82. $post['shopId'] = $this->shopId;
  83. $post['payWay'] = $this->isWx == true ? 0 : 1;
  84. $post['mainId'] = $this->mainId ?? 0;
  85. $post['userId'] = $this->userId ?? 0;
  86. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  87. $mainId = $this->mainId;
  88. if (getenv('YII_ENV') == 'production') {
  89. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  90. util::fail('暂时无法访问');
  91. }
  92. } else {
  93. if (in_array($mainId, [0, 1])) {
  94. util::fail('暂时无法访问');
  95. }
  96. }
  97. $hdId = $post['hdId'] ?? 0;
  98. $hd = HdClass::getById($hdId, true);
  99. if (empty($hd)) {
  100. util::fail('没有找到花店');
  101. }
  102. if ($hd->shopId != $this->shopId) {
  103. util::fail('不是你的花店');
  104. }
  105. $post['hdName'] = $hd->name ?? '';
  106. $customId = $hd->customId ?? 0;
  107. $custom = CustomClass::getById($customId, true);
  108. if (empty($custom)) {
  109. util::fail('没有找到客户');
  110. }
  111. $customName = $custom->name ?? '';
  112. $post['customId'] = $customId;
  113. $post['customName'] = $customName;
  114. $post['customNamePy'] = stringUtil::py($customName);
  115. $now = time();
  116. //订单30分钟后过期
  117. $expireTime = $now + 1800;
  118. $post['deadline'] = $expireTime;
  119. //商城
  120. $post['fromType'] = dict::getDict('fromType', 'mall');
  121. $productJson = $post['product'] ?? '';
  122. if (empty($productJson) && empty($groupId)) {
  123. util::fail('请选择商品');
  124. }
  125. $productList = json_decode($productJson, true);
  126. if (empty($productList)) {
  127. util::fail('请选择商品');
  128. }
  129. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  130. $productInfoList = productClass::getByIds($ids, null, 'id');
  131. if (!empty($productInfoList)) {
  132. $presellData = [];
  133. $nowTime = strtotime(date("Y-m-d"));
  134. foreach ($productInfoList as $currentInfo) {
  135. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  136. util::fail('只能选择同一家的商品哦');
  137. }
  138. $currentName = $currentInfo['name'] ?? '';
  139. $presell = $currentInfo['presell'] ?? 0;
  140. $presellData[] = $presell;
  141. if ($presell == 1) {
  142. if (isset($post['reachDate']) == false || empty($post['reachDate'])) {
  143. util::fail('请选择配送或取货日期');
  144. }
  145. $sendTimeWant = $post['reachDate'];
  146. if (strtotime($sendTimeWant) < $nowTime) {
  147. util::fail('不能选择过去时间');
  148. }
  149. $hasDateStr = $currentInfo['presellDate'] ?? [];
  150. $hasDate = explode(',', trim($hasDateStr));
  151. if (empty($hasDate)) {
  152. util::fail('预售日期有问题,请提醒门店');
  153. }
  154. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  155. util::fail("有预售花材在{$sendTimeWant}没到货");
  156. }
  157. $post['presell'] = 1;
  158. }
  159. }
  160. if (count($productInfoList) != count($ids)) {
  161. util::fail('存在无效商品');
  162. }
  163. $presellData = array_unique($presellData);
  164. if (count($presellData) > 1) {
  165. util::fail('预售和非预售花材不能一起下单');
  166. }
  167. } else {
  168. util::fail('花材不存在');
  169. }
  170. $connection = Yii::$app->db;
  171. $transaction = $connection->beginTransaction();
  172. try {
  173. $post['product'] = $productList;
  174. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  175. $post['deadline'] = time() + $orderValidTime;
  176. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  177. $post['needPrint'] = dict::getDict('needPrint', 'need');
  178. $user = $this->user;
  179. $post['bookMobile'] = $user->mobile ?? '';
  180. $post['bookName'] = $user->name ?? '';
  181. $return = OrderService::createOrder($post, $custom);
  182. if (isset($return->shopId)) {
  183. if ($return->shopId == 7610) {
  184. if ($return->actPrice < 200) {
  185. if ($return->sendType == 0) {
  186. util::fail('满200元支持送货上门');
  187. }
  188. }
  189. }
  190. }
  191. if ($return->sendType == 2) {
  192. if (in_array($return->shopId, [2, 726, 1490, 13626, 7856, 7688])) {
  193. util::fail('暂时不能使用跑腿');
  194. }
  195. }
  196. $transaction->commit();
  197. $orderSn = $return->orderSn ?? '';
  198. $orderPrice = $return->orderPrice ?? 0;
  199. $id = $return->id ?? 0;
  200. $getPayType = dict::getDict('getPayType');
  201. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  202. } catch (\Exception $e) {
  203. $transaction->rollBack();
  204. Yii::error("失败原因:" . $e->getMessage());
  205. util::fail('下单失败');
  206. }
  207. }
  208. //商城下单操作 ssh 2019.12.3
  209. public function actionCreateOrder()
  210. {
  211. $post = Yii::$app->request->post();
  212. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  213. $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0;
  214. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  215. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  216. if (empty($goodsId)) {
  217. util::fail('请选择商品');
  218. }
  219. $goodsInfo = GoodsClass::getGoodsInfo($goodsId);
  220. if (empty($goodsInfo)) {
  221. util::fail('没有找到商品');
  222. }
  223. $stock = $goodsInfo['stock'] && is_numeric($goodsInfo['stock']) ? $goodsInfo['stock'] : 0;
  224. $stockSet = $goodsInfo['stockSet'] && is_numeric($goodsInfo['stockSet']) ? $goodsInfo['stockSet'] : 0;
  225. if ($stock <= 0 && $stockSet == 0) {
  226. util::fail('库存不足');
  227. }
  228. //事务处理
  229. $connection = Yii::$app->db;
  230. $transaction = $connection->beginTransaction();
  231. try {
  232. $custom = CustomClass::getByCondition(['shopId' => $this->shopId, 'userId' => $this->userId], true);
  233. $customId = $custom->id ?? 0;
  234. if (!empty($custom)) {
  235. $post['customId'] = $customId;
  236. $customName = $custom->name ?? '';
  237. $post['customName'] = $customName;
  238. $post['customNamePy'] = stringUtil::py($customName);
  239. $hdId = $custom->hdId ?? 0;
  240. $hd = HdClass::getById($hdId, true);
  241. if (!empty($hd)) {
  242. $post['hdId'] = $hd->id ?? 0;
  243. $post['hdName'] = $hd->name ?? '';
  244. }
  245. }
  246. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  247. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  248. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  249. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  250. $post['bookMobile'] = $bookMobile;
  251. $post['payWay'] = $this->isWx == true ? 0 : 1;
  252. //计算运费
  253. $sendDistance = 0;
  254. $sendCost = 0;
  255. $shop = $this->shop;
  256. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  257. $lat = $post['lat'] ?? '';
  258. $lng = $post['long'] ?? '';
  259. if (empty($lat) || empty($lng)) {
  260. util::fail('请填写收花地址哦');
  261. }
  262. $address = $post['address'] ?? '';
  263. $floor = $post['floor'] ?? '';
  264. $fullAddress = $address . $floor;
  265. $post['fullAddress'] = $fullAddress;
  266. $shopLat = $shop->lat ?? '';
  267. $shopLong = $shop->long ?? '';
  268. if (empty($shopLat) || empty($shopLong)) {
  269. //util::fail('请完成门店地址');
  270. }
  271. //花束重量默认1.5
  272. $weight = 1.5;
  273. $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  274. $sendCost = $disRespond['fee'] ?? 0;
  275. $sendDistance = $disRespond['distance'] ?? 0;
  276. }
  277. $post['sendDistance'] = $sendDistance;
  278. $post['sendCost'] = $sendCost;
  279. $post['sjId'] = $this->sjId;
  280. $post['shopId'] = $this->shopId;
  281. $mainId = $this->mainId;
  282. $post['mainId'] = $mainId;
  283. $post['userId'] = $this->userId;
  284. //计算总金额
  285. $unitPrice = $goodsInfo['price'];
  286. $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
  287. if (!empty($goodsStyleId)) {
  288. if (empty($goodsStyleList)) {
  289. util::fail('商品款式没有找到');
  290. }
  291. $styleIdList = array_keys($goodsStyleList);
  292. if (in_array($goodsStyleId, $styleIdList) == false) {
  293. util::fail('商品款式没有找到!');
  294. }
  295. $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
  296. }
  297. $goodsPrice = $unitPrice * $goodsNum;
  298. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  299. //非门店订单
  300. $post['store'] = 0;
  301. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  302. $sourceType = $this->isWx ? 0 : 1;
  303. if (httpUtil::isMiniProgram()) {
  304. $sourceType = 2;
  305. }
  306. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  307. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  308. $actPrice = $discountData['price'];
  309. $post['discountType'] = $discountData['discountType'];
  310. $post['discountAmount'] = $discountData['discountAmount'];
  311. //非自取订单考虑配送时间
  312. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  313. $sendType = $post['sendType'] ?? 0;
  314. if ($sendType != 1) {
  315. if (isset($post['reachPeriod']) == false || empty($post['reachPeriod'])) {
  316. util::fail('请选择配送时间');
  317. }
  318. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  319. }
  320. $now = time();
  321. $expireTime = $now + 1800;//订单30分钟后过期
  322. $post['deadline'] = $expireTime;
  323. $orderSn = orderSn::getOrderSn();
  324. $post['orderSn'] = $orderSn;
  325. $post['fromType'] = dict::getDict('fromType', 'mall');
  326. $post['prePrice'] = $prePrice;
  327. $post['orderPrice'] = $actPrice;
  328. $post['actPrice'] = $actPrice;
  329. $post['realPrice'] = $actPrice;
  330. $post['mainPay'] = $actPrice;
  331. $post['cash'] = 0;
  332. $post['mainId'] = $mainId;
  333. $post['needPrint'] = dict::getDict('needPrint', 'need');
  334. $order = OrderClass::addOrder($post);
  335. $orderId = $order['id'];
  336. $orderSn = $order['orderSn'];
  337. $currentCover = isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '';
  338. $flower = $goodsInfo['flower'] ?? 0;
  339. $price = bcmul($goodsNum, $unitPrice, 2);
  340. $data = [
  341. 'orderId' => $orderId,
  342. 'goodsId' => $goodsId,
  343. 'flower' => $flower,
  344. 'userId' => $this->userId,
  345. 'sjId' => $this->sjId,
  346. 'mainId' => $mainId,
  347. 'name' => $goodsInfo['name'] ?? '',
  348. 'cover' => $currentCover,
  349. 'unitPrice' => $unitPrice,
  350. 'num' => $goodsNum,
  351. 'orderSn' => $orderSn,
  352. 'price' => $price,
  353. 'createTime' => date("Y-m-d H:i:s"),
  354. ];
  355. OrderGoodsClass::addData($data);
  356. $transaction->commit();
  357. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
  358. } catch (Exception $e) {
  359. $transaction->rollBack();
  360. Yii::info("失败原因:" . $e->getMessage());
  361. util::fail('下单失败');
  362. }
  363. }
  364. //下单要用到的相关信息 ssh 2019.12.6
  365. public function actionOrderRelate()
  366. {
  367. $regionTree = RegionService::tree();
  368. //门店名称
  369. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  370. $sjName = $shop['merchantName'] ?? '';
  371. $shopName = $shop['shopName'] ?? '';
  372. $default = $shop['default'] ?? 0;
  373. $shop['showName'] = $default == 1 && $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  374. $freight = ['first' => 5, 'add' => 2];
  375. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  376. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  377. util::success($out);
  378. }
  379. //余额支付 ssh 20250410
  380. public function actionBalancePay()
  381. {
  382. $post = Yii::$app->request->post();
  383. $orderSn = $post['orderSn'] ?? '';
  384. //避免重复提交
  385. util::checkRepeatCommit($orderSn, 10);
  386. $connection = Yii::$app->db;
  387. $transaction = $connection->beginTransaction();
  388. try {
  389. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  390. if (empty($order)) {
  391. util::fail('没有找到订单');
  392. }
  393. if ($order->shopId != $this->shopId) {
  394. util::fail('不是你的订单');
  395. }
  396. $payWay = dict::getDict('payWay', 'balancePay');
  397. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  398. $transaction->commit();
  399. util::complete('支付成功');
  400. } catch (\Exception $e) {
  401. $transaction->rollBack();
  402. Yii::error("支付原因:" . $e->getMessage());
  403. util::fail('支付失败');
  404. }
  405. }
  406. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  407. public function actionWxPay()
  408. {
  409. ini_set('date.timezone', 'Asia/Shanghai');
  410. $post = Yii::$app->request->post();
  411. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  412. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  413. $order = OrderService::getByOrderSn($orderSn);
  414. $orderId = $order['id'];
  415. //验证优惠券是否还有效
  416. if (!empty($couponId)) {
  417. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  418. }
  419. //支付前验证订单有效性
  420. OrderService::checkBeforePay($order);
  421. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  422. $shop = $this->shop;
  423. if (empty($shop)) {
  424. util::fail('没有找到门店');
  425. }
  426. if (!empty($shop->shopName)) {
  427. $name .= '(' . $shop->shopName . ')';
  428. }
  429. $totalFee = $order['mainPay'];
  430. $openId = '';
  431. if (!empty($post['miniOpenId'])) {
  432. $openId = $post['miniOpenId'];
  433. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  434. }
  435. if (empty($openId)) {
  436. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  437. $openId = $this->user['miniOpenId'];
  438. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  439. }
  440. }
  441. if (empty($openId)) {
  442. util::fail('没有找到openId');
  443. }
  444. $now = time();
  445. $expireTime = $now + 1800;
  446. $updateData = [];
  447. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  448. $oldOrderSn = $orderSn;
  449. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  450. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  451. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  452. $params = [
  453. 'appid' => 'OP00002119',
  454. 'serial_no' => '018b08cfddbd',
  455. 'merchant_no' => $shop->lklSjNo,
  456. 'term_no' => $shop->lklScanTermNo,
  457. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  458. 'lklCertificatePath' => $lklCertificatePath,
  459. ];
  460. $laResource = new Lakala($params);
  461. $closeParams = [
  462. 'orderSn' => $oldOrderSn,
  463. ];
  464. $response = $laResource->close($closeParams);
  465. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  466. $msg = $response['msg'] ?? '';
  467. $lklSjNo = $shop->lklSjNo ?? '';
  468. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  469. util::fail('出错了哦,请重新下一单');
  470. }
  471. $orderSn = orderSn::getOrderSn();
  472. $updateData['orderSn'] = $orderSn;
  473. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  474. } else {
  475. //已请求的不能再修改价格
  476. $updateData['modPrice'] = 0;
  477. }
  478. if (empty($order['deadline'])) {
  479. $updateData['deadline'] = $expireTime;
  480. }
  481. if (!empty($updateData)) {
  482. OrderService::updateById($orderId, $updateData);
  483. }
  484. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  485. $sjExtend = WxOpenClass::getMallWxInfo();
  486. $shop = $this->shop;
  487. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  488. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  489. $params = [
  490. 'appid' => 'OP00002119',
  491. 'serial_no' => '018b08cfddbd',
  492. 'merchant_no' => $shop->lklSjNo,
  493. 'term_no' => $shop->lklScanTermNo,
  494. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  495. 'lklCertificatePath' => $lklCertificatePath,
  496. ];
  497. $laResource = new Lakala($params);
  498. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  499. $wxParams = [
  500. 'orderSn' => $orderSn,
  501. 'amount' => $totalFee,
  502. 'capitalType' => $capitalType,
  503. 'notifyUrl' => $notifyUrl,
  504. 'wxAppId' => $sjExtend['miniAppId'],
  505. 'openId' => $openId,
  506. 'subject' => $name,
  507. 'couponId' => $couponId,
  508. ];
  509. $response = $laResource->driveWxPay($wxParams);
  510. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  511. $errMsg = $response['msg'] ?? '';
  512. noticeUtil::push($errMsg, '15280215347');
  513. util::fail('支付失败');
  514. }
  515. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  516. $appId = $newParams['app_id'] ?? '';
  517. $nonceStr = $newParams['nonce_str'] ?? '';
  518. $paySign = $newParams['pay_sign'] ?? '';
  519. $package = $newParams['package'] ?? '';
  520. $signType = $newParams['sign_type'] ?? '';
  521. $timeStamp = $newParams['time_stamp'] ?? '';
  522. $new = [
  523. 'id' => $orderId,
  524. 'appId' => $appId,
  525. 'nonceStr' => $nonceStr,
  526. 'paySign' => $paySign,
  527. 'package' => $package,
  528. 'signType' => $signType,
  529. 'timeStamp' => $timeStamp,
  530. 'orderSn' => $orderSn,
  531. ];
  532. util::success($new);
  533. }
  534. //快捷付款订单
  535. public function actionFastPay()
  536. {
  537. ini_set('date.timezone', 'Asia/Shanghai');
  538. $post = Yii::$app->request->post();
  539. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  540. $shopId = $this->shopId;
  541. $post['shopId'] = $shopId;
  542. $post['mainId'] = $this->mainId;
  543. $post['sjId'] = $this->sjId;
  544. $post['store'] = 0;
  545. $post['customId'] = $this->customId;
  546. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  547. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  548. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  549. $bookMobile = empty($bookMobile) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  550. $post['bookMobile'] = $bookMobile;
  551. $prePrice = round($post['prePrice'], 2);
  552. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  553. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  554. $actPrice = $discountData['price'];
  555. $post['discountType'] = $discountData['discountType'];
  556. $post['discountAmount'] = $discountData['discountAmount'];
  557. $post['orderPrice'] = $actPrice;
  558. $post['actPrice'] = $actPrice;
  559. $post['realPrice'] = $actPrice;
  560. $post['mainPay'] = $actPrice;
  561. $post['payStyle'] = 0;
  562. $post['userId'] = $this->userId;
  563. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  564. $now = time();
  565. $expireTime = $now + 300;//订单5分钟后过期
  566. $post['createTime'] = date("Y-m-d H:i:s", $now);
  567. $post['deadline'] = $expireTime;
  568. if ($actPrice <= 0) {
  569. util::fail('请填写正确的金额');
  570. }
  571. //微信支付订单提交不能修改价格
  572. $post['modPrice'] = $this->isWx ? 0 : 1;
  573. $post['goodsNum'] = 1;
  574. $post['fromType'] = $post['fromType'] ?? 1;
  575. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  576. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  577. //前端未修改时这边做兼容
  578. if (!empty($post['receiveAddress'])) {
  579. $post['address'] = $post['receiveAddress'];
  580. if (!empty($post['receiveProvince'])) {
  581. $post['province'] = $post['receiveProvince'];
  582. }
  583. if (!empty($post['receiveCity'])) {
  584. $post['city'] = $post['receiveCity'];
  585. }
  586. if (!empty($post['receiveFloor'])) {
  587. $post['floor'] = $post['receiveFloor'];
  588. }
  589. if (!empty($post['latitude'])) {
  590. $post['lat'] = $post['latitude'];
  591. }
  592. if (!empty($post['longitude'])) {
  593. $post['long'] = $post['longitude'];
  594. }
  595. $city = $post['receiveCity'] ?? '';
  596. $address = $post['receiveAddress'];
  597. $floor = $post['receiveFloor'] ?? '';
  598. $post['fullAddress'] = $city . $address . $floor;
  599. }
  600. $order = OrderClass::addOrder($post);
  601. $orderId = $order['id'] ?? 0;
  602. $orderSn = $order['orderSn'] ?? '';
  603. if ($payWay == 0) {
  604. $name = '购买商品';
  605. $totalFee = $actPrice;
  606. $user = UserService::getById($this->userId);
  607. //小程序使用miniOpenId
  608. $openId = $user['miniOpenId'];
  609. if (empty($openId)) {
  610. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  611. }
  612. if (empty($openId)) {
  613. util::fail('没有微信信息');
  614. }
  615. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  616. $sjExtend = WxOpenClass::getMallWxInfo();
  617. $shop = $this->shop;
  618. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  619. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  620. $params = [
  621. 'appid' => 'OP00002119',
  622. 'serial_no' => '018b08cfddbd',
  623. 'merchant_no' => $shop->lklSjNo,
  624. 'term_no' => $shop->lklScanTermNo,
  625. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  626. 'lklCertificatePath' => $lklCertificatePath,
  627. ];
  628. $laResource = new Lakala($params);
  629. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  630. $wxParams = [
  631. 'orderSn' => $orderSn,
  632. 'amount' => $totalFee,
  633. 'capitalType' => $capitalType,
  634. 'notifyUrl' => $notifyUrl,
  635. 'wxAppId' => $sjExtend['miniAppId'],
  636. 'openId' => $openId,
  637. 'subject' => $name,
  638. 'couponId' => $couponId,
  639. ];
  640. $response = $laResource->driveWxPay($wxParams);
  641. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  642. $errMsg = $response['msg'] ?? '';
  643. noticeUtil::push($errMsg, '15280215347');
  644. util::fail('支付失败');
  645. }
  646. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  647. $appId = $newParams['app_id'] ?? '';
  648. $nonceStr = $newParams['nonce_str'] ?? '';
  649. $paySign = $newParams['pay_sign'] ?? '';
  650. $package = $newParams['package'] ?? '';
  651. $signType = $newParams['sign_type'] ?? '';
  652. $timeStamp = $newParams['time_stamp'] ?? '';
  653. $new = [
  654. 'id' => $orderId,
  655. 'appId' => $appId,
  656. 'nonceStr' => $nonceStr,
  657. 'paySign' => $paySign,
  658. 'package' => $package,
  659. 'signType' => $signType,
  660. 'timeStamp' => $timeStamp,
  661. 'orderSn' => $orderSn,
  662. ];
  663. util::success($new);
  664. } elseif ($payWay == 1) {
  665. $totalFee = $order['realPrice'];
  666. $subject = '购买花材';
  667. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  668. $shop = $this->shop;
  669. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  670. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  671. $params = [
  672. 'appid' => 'OP00002119',
  673. 'serial_no' => '018b08cfddbd',
  674. 'merchant_no' => $shop->lklSjNo,
  675. 'term_no' => $shop->lklB2BTermNo,
  676. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  677. 'lklCertificatePath' => $lklCertificatePath,
  678. ];
  679. $laResource = new Lakala($params);
  680. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  681. $cashParams = [
  682. 'orderSn' => $orderSn,
  683. 'amount' => $totalFee,
  684. 'capitalType' => $capitalType,
  685. 'notifyUrl' => $notifyUrl,
  686. 'subject' => $subject,
  687. ];
  688. $response = $laResource->cashierPay($cashParams);
  689. if (!isset($response['code']) || $response['code'] != '000000') {
  690. util::fail('支付失败了');
  691. }
  692. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  693. if (empty($url)) {
  694. util::fail('支付失败');
  695. }
  696. util::success(['url' => $url]);
  697. } elseif ($payWay == 2) {
  698. util::fail('无效的支付方式');
  699. //余额支付,验证支付密码是否正确
  700. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  701. $user = UserService::getUserInfo($this->userId);
  702. UserService::validPayPassword($payPassword, $user);
  703. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  704. $callbackParams = [];
  705. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  706. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  707. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  708. } else {
  709. util::fail('无效的支付方式');
  710. }
  711. }
  712. //订单列表 ssh 2019.12.12
  713. public function actionList()
  714. {
  715. $get = Yii::$app->request->get();
  716. $status = $get['status'] ?? 0;
  717. $userId = $this->userId;
  718. $where = ['userId' => $userId];
  719. if (!empty($status)) {
  720. $where['status'] = $status;
  721. }
  722. $list = OrderService::getOrderList($where);
  723. util::success($list);
  724. }
  725. //订单评价
  726. public function actionComment()
  727. {
  728. $post = Yii::$app->request->post();
  729. $id = $post['id'];
  730. $order = OrderClass::getById($id);
  731. if ($order['grade'] > 0) {
  732. util::fail('您已经评过了');
  733. }
  734. OrderService::comment($post);
  735. util::complete('提交成功');
  736. }
  737. //订单详情 ssh 2019.12.16
  738. public function actionDetail()
  739. {
  740. $id = Yii::$app->request->get('id', 0);
  741. $detail = OrderClass::getOrderById($id);
  742. $shOrderMap = [];
  743. if ($detail['hasForward'] == 1) {
  744. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  745. }
  746. $detail['shOrderMap'] = $shOrderMap;
  747. util::success($detail);
  748. }
  749. //获取详情 ssh 20220512
  750. public function actionInfo()
  751. {
  752. $sn = Yii::$app->request->get('orderSn', '');
  753. $detail = OrderClass::getOrderBySn($sn);
  754. util::success($detail);
  755. }
  756. }