OrderController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. $openId = '';
  370. if (isset($post['miniOpenId']) && !empty($post['miniOpenId'])) {
  371. $openId = $post['miniOpenId'];
  372. }
  373. if (empty($openId)) {
  374. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  375. $openId = $this->user['miniOpenId'];
  376. noticeUtil::push('零售买花时,通过数据库获取了openId:' . $openId, '15280215347');
  377. }
  378. }
  379. if (empty($openId)) {
  380. util::fail('没有找到openId');
  381. }
  382. $now = time();
  383. $expireTime = $now + 1800;
  384. $updateData = [];
  385. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  386. $oldOrderSn = $orderSn;
  387. $orderSn = orderSn::getOrderSn();
  388. $updateData['orderSn'] = $orderSn;
  389. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  390. } else {
  391. //已请求的不能再修改价格
  392. $updateData['modPrice'] = 0;
  393. }
  394. if (empty($order['deadline'])) {
  395. $updateData['deadline'] = $expireTime;
  396. }
  397. if (!empty($updateData)) {
  398. OrderService::updateById($orderId, $updateData);
  399. }
  400. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  401. $sjExtend = WxOpenClass::getMallWxInfo();
  402. $shop = $this->shop;
  403. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  404. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  405. $params = [
  406. 'appid' => 'OP00002119',
  407. 'serial_no' => '018b08cfddbd',
  408. 'merchant_no' => $shop->lklSjNo,
  409. 'term_no' => $shop->lklScanTermNo,
  410. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  411. 'lklCertificatePath' => $lklCertificatePath,
  412. ];
  413. $laResource = new Lakala($params);
  414. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  415. $wxParams = [
  416. 'orderSn' => $orderSn,
  417. 'amount' => $totalFee,
  418. 'capitalType' => $capitalType,
  419. 'notifyUrl' => $notifyUrl,
  420. 'wxAppId' => $sjExtend['miniAppId'],
  421. 'openId' => $openId,
  422. 'subject' => $name,
  423. 'couponId' => $couponId,
  424. ];
  425. $response = $laResource->driveWxPay($wxParams);
  426. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  427. $errMsg = $response['msg'] ?? '';
  428. noticeUtil::push($errMsg, '15280215347');
  429. util::fail('支付失败');
  430. }
  431. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  432. $appId = $newParams['app_id'] ?? '';
  433. $nonceStr = $newParams['nonce_str'] ?? '';
  434. $paySign = $newParams['pay_sign'] ?? '';
  435. $package = $newParams['package'] ?? '';
  436. $signType = $newParams['sign_type'] ?? '';
  437. $timeStamp = $newParams['time_stamp'] ?? '';
  438. $new = [
  439. 'id' => $orderId,
  440. 'appId' => $appId,
  441. 'nonceStr' => $nonceStr,
  442. 'paySign' => $paySign,
  443. 'package' => $package,
  444. 'signType' => $signType,
  445. 'timeStamp' => $timeStamp,
  446. 'orderSn' => $orderSn,
  447. ];
  448. util::success($new);
  449. }
  450. //快捷付款订单
  451. public function actionFastPay()
  452. {
  453. ini_set('date.timezone', 'Asia/Shanghai');
  454. $post = Yii::$app->request->post();
  455. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  456. $shopId = $this->shopId;
  457. $post['shopId'] = $shopId;
  458. $post['mainId'] = $this->mainId;
  459. $post['sjId'] = $this->sjId;
  460. $post['store'] = 0;
  461. $post['customId'] = $this->customId;
  462. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  463. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  464. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  465. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  466. $post['bookMobile'] = $bookMobile;
  467. $prePrice = round($post['prePrice'], 2);
  468. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  469. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  470. $actPrice = $discountData['price'];
  471. $post['discountType'] = $discountData['discountType'];
  472. $post['discountAmount'] = $discountData['discountAmount'];
  473. $post['orderPrice'] = $actPrice;
  474. $post['actPrice'] = $actPrice;
  475. $post['realPrice'] = $actPrice;
  476. $post['mainPay'] = $actPrice;
  477. $post['payStyle'] = 0;
  478. $post['sjId'] = $this->sjId;
  479. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  480. $now = time();
  481. $expireTime = $now + 300;//订单5分钟后过期
  482. $post['createTime'] = date("Y-m-d H:i:s", $now);
  483. $post['deadline'] = $expireTime;
  484. if ($actPrice <= 0) {
  485. util::fail('请填写正确的金额');
  486. }
  487. //微信支付订单提交不能修改价格
  488. $post['modPrice'] = $this->isWx ? 0 : 1;
  489. $post['goodsNum'] = 1;
  490. $post['fromType'] = $post['fromType'] ?? 1;
  491. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  492. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  493. //前端未修改时这边做兼容
  494. if (isset($post['receiveAddress']) && !empty($post['receiveAddress'])) {
  495. $post['address'] = $post['receiveAddress'];
  496. if (isset($post['receiveProvince']) && !empty($post['receiveProvince'])) {
  497. $post['province'] = $post['receiveProvince'];
  498. }
  499. if (isset($post['receiveCity']) && !empty($post['receiveCity'])) {
  500. $post['city'] = $post['receiveCity'];
  501. }
  502. if (isset($post['receiveFloor']) && !empty($post['receiveFloor'])) {
  503. $post['floor'] = $post['receiveFloor'];
  504. }
  505. if (isset($post['latitude']) && !empty($post['latitude'])) {
  506. $post['lat'] = $post['latitude'];
  507. }
  508. if (isset($post['longitude']) && !empty($post['longitude'])) {
  509. $post['long'] = $post['longitude'];
  510. }
  511. $city = $post['receiveCity'] ?? '';
  512. $address = $post['receiveAddress'] ?? '';
  513. $floor = $post['receiveFloor'] ?? '';
  514. $post['fullAddress'] = $city . $address . $floor;
  515. }
  516. $order = OrderClass::addOrder($post);
  517. $orderId = $order['id'];
  518. $orderSn = $order['orderSn'];
  519. $sjId = $this->sjId;
  520. if ($payWay == 0) {
  521. $orderId = $order['id'];
  522. $name = '购买商品';
  523. $totalFee = $actPrice;
  524. $user = UserService::getById($this->userId);
  525. //小程序使用miniOpenId
  526. $openId = $user['miniOpenId'];
  527. if (empty($openId)) {
  528. $openId = isset($post['miniOpenId']) && !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  529. }
  530. if (empty($openId)) {
  531. util::fail('没有微信信息');
  532. }
  533. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  534. $sjExtend = WxOpenClass::getMallWxInfo();
  535. $shop = $this->shop;
  536. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  537. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  538. $params = [
  539. 'appid' => 'OP00002119',
  540. 'serial_no' => '018b08cfddbd',
  541. 'merchant_no' => $shop->lklSjNo,
  542. 'term_no' => $shop->lklScanTermNo,
  543. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  544. 'lklCertificatePath' => $lklCertificatePath,
  545. ];
  546. $laResource = new Lakala($params);
  547. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  548. $wxParams = [
  549. 'orderSn' => $orderSn,
  550. 'amount' => $totalFee,
  551. 'capitalType' => $capitalType,
  552. 'notifyUrl' => $notifyUrl,
  553. 'wxAppId' => $sjExtend['miniAppId'],
  554. 'openId' => $openId,
  555. 'subject' => $name,
  556. 'couponId' => $couponId,
  557. ];
  558. $response = $laResource->driveWxPay($wxParams);
  559. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  560. $errMsg = $response['msg'] ?? '';
  561. noticeUtil::push($errMsg, '15280215347');
  562. util::fail('支付失败');
  563. }
  564. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  565. $appId = $newParams['app_id'] ?? '';
  566. $nonceStr = $newParams['nonce_str'] ?? '';
  567. $paySign = $newParams['pay_sign'] ?? '';
  568. $package = $newParams['package'] ?? '';
  569. $signType = $newParams['sign_type'] ?? '';
  570. $timeStamp = $newParams['time_stamp'] ?? '';
  571. $new = [
  572. 'id' => $orderId,
  573. 'appId' => $appId,
  574. 'nonceStr' => $nonceStr,
  575. 'paySign' => $paySign,
  576. 'package' => $package,
  577. 'signType' => $signType,
  578. 'timeStamp' => $timeStamp,
  579. 'orderSn' => $orderSn,
  580. ];
  581. util::success($new);
  582. } elseif ($payWay == 1) {
  583. $totalFee = $order['realPrice'];
  584. $subject = '购买花材';
  585. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  586. $shop = $this->shop;
  587. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  588. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  589. $params = [
  590. 'appid' => 'OP00002119',
  591. 'serial_no' => '018b08cfddbd',
  592. 'merchant_no' => $shop->lklSjNo,
  593. 'term_no' => $shop->lklScanTermNo,
  594. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  595. 'lklCertificatePath' => $lklCertificatePath,
  596. ];
  597. $laResource = new Lakala($params);
  598. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  599. $cashParams = [
  600. 'orderSn' => $orderSn,
  601. 'amount' => $totalFee,
  602. 'capitalType' => $capitalType,
  603. 'notifyUrl' => $notifyUrl,
  604. 'subject' => $subject,
  605. ];
  606. $response = $laResource->cashierPay($cashParams);
  607. if (isset($response['code']) == false || $response['code'] != '000000') {
  608. util::fail('支付失败了');
  609. }
  610. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  611. if (empty($url)) {
  612. util::fail('支付失败');
  613. }
  614. util::success(['url' => $url]);
  615. } elseif ($payWay == 2) {
  616. util::fail('无效的支付方式');
  617. //余额支付,验证支付密码是否正确
  618. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  619. $user = UserService::getUserInfo($this->userId);
  620. UserService::validPayPassword($payPassword, $user);
  621. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  622. $callbackParams = [];
  623. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  624. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  625. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  626. } else {
  627. util::fail('无效的支付方式');
  628. }
  629. }
  630. //订单列表 ssh 2019.12.12
  631. public function actionList()
  632. {
  633. $userId = $this->userId;
  634. if (empty($userId)) {
  635. util::success(['list' => [], 'moreData' => 0, 'totalNum' => 0, 'totalPage' => 0]);
  636. }
  637. $where = ['userId' => $userId];
  638. $list = OrderService::getOrderList($where);
  639. util::success($list);
  640. }
  641. //订单评价
  642. public function actionComment()
  643. {
  644. $post = Yii::$app->request->post();
  645. $id = $post['id'];
  646. $order = OrderClass::getById($id);
  647. if ($order['grade'] > 0) {
  648. util::fail('您已经评过了');
  649. }
  650. OrderService::comment($post);
  651. util::complete('提交成功');
  652. }
  653. //订单详情 ssh 2019.12.16
  654. public function actionDetail()
  655. {
  656. $id = Yii::$app->request->get('id', 0);
  657. $detail = OrderClass::getOrderById($id);
  658. util::success($detail);
  659. }
  660. //获取详情 ssh 20220512
  661. public function actionInfo()
  662. {
  663. $sn = Yii::$app->request->get('orderSn', '');
  664. $detail = OrderClass::getOrderBySn($sn);
  665. util::success($detail);
  666. }
  667. }