OrderController.php 33 KB

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