OrderController.php 34 KB

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