OrderController.php 33 KB

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