OrderController.php 35 KB

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