OrderController.php 33 KB

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