OrderController.php 32 KB

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