OrderController.php 35 KB

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