OrderController.php 38 KB

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