OrderController.php 35 KB

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