OrderController.php 39 KB

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