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