OrderController.php 36 KB

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