OrderController.php 33 KB

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