OrderController.php 38 KB

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