OrderController.php 30 KB

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