OrderController.php 31 KB

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