OrderController.php 33 KB

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