OrderController.php 32 KB

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