OrderController.php 32 KB

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