OrderController.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\wx\classes\WxMessageClass;
  6. use bizHd\custom\classes\HdClass;
  7. use bizHd\express\classes\HdDeliveryOrderClass;
  8. use bizHd\hb\classes\HbClass;
  9. use bizHd\order\classes\ScanPayClass;
  10. use bizHd\wx\classes\WxOpenClass;
  11. use bizMall\custom\classes\CustomClass;
  12. use bizMall\goods\classes\GoodsClass;
  13. use bizMall\order\classes\OrderClass;
  14. use bizMall\order\classes\OrderForwardClass;
  15. use bizMall\order\classes\OrderItemClass;
  16. use bizMall\order\services\OrderService;
  17. use bizMall\promote\services\CouponService;
  18. use bizMall\saas\services\RegionService;
  19. use bizMall\shop\classes\ShopExtClass;
  20. use bizMall\user\services\UserService;
  21. use common\components\dateUtil;
  22. use common\components\delivery\util\DeliveryQuoteUtil;
  23. use common\components\dict;
  24. use common\components\imgUtil;
  25. use common\components\noticeUtil;
  26. use common\components\orderSn;
  27. use common\components\stringUtil;
  28. use Yii;
  29. use common\components\util;
  30. use common\components\lakala\Lakala;
  31. use common\components\IntraCityExpress;
  32. class OrderController extends BaseController
  33. {
  34. //二维码收款使用
  35. public $guestAccess = ['order-relate', 'fast-pay'];
  36. public function actionInform()
  37. {
  38. $get = Yii::$app->request->get();
  39. $hdId = $get['hdId'] ?? 0;
  40. $account = $get['account'] ?? 0;
  41. $page = $get['page'] ?? '';
  42. $userId = $this->userId;
  43. noticeUtil::push("零售客户下单报错 hdId:{$hdId} account:{$account} userId:{$userId} page:{$page}", '15280215347');
  44. }
  45. //记账单列表 ssh 20250627
  46. public function actionDebtList()
  47. {
  48. $get = Yii::$app->request->get();
  49. $id = $get['id'] ?? 0;
  50. $where = [];
  51. $where['userId'] = $this->userId;
  52. if (!empty($id)) {
  53. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  54. if (empty($hd)) {
  55. util::fail('无效的花店');
  56. }
  57. $where['hdId'] = $id;
  58. }
  59. $where['debt'] = 1;
  60. $searchTime = $get['searchTime'] ?? '';
  61. if (!empty($searchTime)) {
  62. $startTime = $get['startTime'] ?? '';
  63. $endTime = $get['endTime'] ?? '';
  64. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  65. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  66. }
  67. $respond = OrderClass::getDebtList($where);
  68. util::success($respond);
  69. }
  70. //新计算运费
  71. public function actionNewFreight()
  72. {
  73. $post = Yii::$app->request->post();
  74. $hasMap = dict::getDict('hasMap');
  75. if ($hasMap == 0) {
  76. util::fail('暂不支持跑腿功能');
  77. }
  78. $mainData = $this->main;
  79. $wxStoreId = $mainData->wxStoreId ?? '';
  80. if (empty($wxStoreId)) {
  81. util::fail('商家还没有开通跑腿');
  82. }
  83. $cargoName = $post['cargo_name']; // 商品名称
  84. $cargo = [
  85. 'cargo_name' => $cargoName, // 商品名称
  86. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  87. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  88. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  89. 'cargo_num' => intval($post['package_num'])
  90. ];
  91. $originalLng = doubleval($post['user_lng']);
  92. $originalLat = doubleval($post['user_lat']);
  93. $userName = !empty($post['user_name']) ? $post['user_name'] : '游客';
  94. $orderData = [
  95. 'wx_store_id' => $wxStoreId,
  96. 'user_name' => $userName,
  97. 'user_phone' => $post['user_phone'],
  98. 'user_lng' => $originalLng,
  99. 'user_lat' => $originalLat,
  100. 'user_address' => $post['user_address'],
  101. 'cargo' => $cargo,
  102. ];
  103. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  104. $result = IntraCityExpress::preAddOrder($orderData);
  105. if ($result['errcode'] === 0) {
  106. util::success($result, "查询成功");
  107. } else {
  108. Yii::error("运费查询失败:" . json_encode($result));
  109. $errMsg = $result['errmsg'] ?? '';
  110. $errCode = $result['errcode'] ?? '';
  111. noticeUtil::push("运费查询失败,原因:" . $errMsg . ",编号:" . $errCode, '15280215347');
  112. util::fail('运费查询失败,请确认是否超过20公斤,编号896');
  113. }
  114. }
  115. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  116. public function actionFreight()
  117. {
  118. $get = Yii::$app->request->get();
  119. $shop = $this->shop;
  120. $shopLat = isset($shop->lat) ? $shop->lat : '';
  121. $shopLong = isset($shop->long) ? $shop->long : '';
  122. // 判断地图功能是否正常。0 表示地图定位不可用,全部经纬度数据失效
  123. if (dict::getDict('hasMap') == 0) {
  124. $shopLat = '';
  125. $shopLong = '';
  126. }
  127. if (empty($shopLat) || empty($shopLong)) {
  128. util::success([
  129. 'distance' => 0,
  130. 'showDistance' => 0,
  131. 'fee' => 0,
  132. 'hasMap' => dict::getDict('hasMap'),
  133. 'shop' => 'fail' //门店引起的失败
  134. ]);
  135. }
  136. $userLat = $get['lat'] ?? '';
  137. $userLong = $get['long'] ?? '';
  138. if (empty($userLat) || empty($userLong)) {
  139. util::success([
  140. 'distance' => 0,
  141. 'showDistance' => 0,
  142. 'fee' => 0,
  143. 'hasMap' => dict::getDict('hasMap'),
  144. 'client' => 'fail' //用户端定位失败
  145. ]);
  146. }
  147. $weight = $get['weight'] ?? 0;
  148. if (empty($weight)) {
  149. util::success([
  150. 'distance' => 0,
  151. 'showDistance' => 0,
  152. 'fee' => 0,
  153. 'hasMap' => dict::getDict('hasMap'),
  154. 'weight' => 'fail' //重量不对
  155. ]);
  156. }
  157. //data: {distance: "321", showDistance: "0.3", fee: "12.00"}
  158. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  159. util::success($respond);
  160. }
  161. // 零售商的限购花材信息(与批发端 PurchaseController::actionLimitBuyInfo 对应,按花店客户与 hd_limit_buy Redis)
  162. public function actionLimitBuyInfo()
  163. {
  164. $post = Yii::$app->request->post();
  165. $list = $post['list'] ?? [];
  166. if (empty($list) || !is_array($list)) {
  167. util::fail('请选择花材');
  168. }
  169. $hd = $this->hd;
  170. if (empty($hd)) {
  171. util::fail('没有找到花店');
  172. }
  173. if ($hd->shopId != $this->shopId) {
  174. util::fail('不是你的花店');
  175. }
  176. $hdCustomId = $hd->customId ?? 0;
  177. if (empty($hdCustomId)) {
  178. util::fail('没有找到客户');
  179. }
  180. $respond = \bizHd\product\classes\ProductClass::getLimitBuyInfoByList($list, $hdCustomId);
  181. util::success($respond);
  182. }
  183. //购买花材 ssh 20220511
  184. public function actionBuyItem()
  185. {
  186. $post = Yii::$app->request->post();
  187. $post['sjId'] = $this->sjId;
  188. $post['shopId'] = $this->shopId;
  189. $post['payWay'] = 0;
  190. $post['mainId'] = $this->mainId ?? 0;
  191. $post['userId'] = $this->userId ?? 0;
  192. //避免重复提交
  193. $userId = $this->userId;
  194. util::checkRepeatCommit($userId, 3);
  195. $shop = $this->shop;
  196. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  197. if ($openShop == 0) {
  198. util::fail('已休店');
  199. }
  200. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  201. $mainId = $this->mainId;
  202. if (getenv('YII_ENV') == 'production') {
  203. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  204. util::fail('暂时无法访问');
  205. }
  206. } else {
  207. if (in_array($mainId, [0, 1])) {
  208. util::fail('暂时无法访问');
  209. }
  210. }
  211. $hdId = $post['hdId'] ?? 0;
  212. $hd = HdClass::getById($hdId, true);
  213. if (empty($hd)) {
  214. util::fail('没有找到花店');
  215. }
  216. if ($hd->shopId != $this->shopId) {
  217. util::fail('不是你的花店');
  218. }
  219. $post['hdName'] = $hd->name ?? '';
  220. $hdCustomId = $hd->customId ?? 0;
  221. $custom = CustomClass::getById($hdCustomId, true);
  222. if (empty($custom)) {
  223. util::fail('没有找到客户');
  224. }
  225. $customName = $custom->name ?? '';
  226. $post['customId'] = $hdCustomId;
  227. $post['customName'] = $customName;
  228. $post['customNamePy'] = stringUtil::py($customName);
  229. $now = time();
  230. //订单30分钟后过期
  231. $expireTime = $now + 1800;
  232. $post['deadline'] = $expireTime;
  233. //商城
  234. $post['fromType'] = dict::getDict('fromType', 'mall');
  235. $productJson = $post['product'] ?? '';
  236. if (empty($productJson) && empty($groupId)) {
  237. util::fail('请选择商品');
  238. }
  239. $productList = json_decode($productJson, true);
  240. if (empty($productList)) {
  241. util::fail('请选择商品');
  242. }
  243. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  244. $productInfoList = productClass::getByIds($ids, null, 'id');
  245. if (!empty($productInfoList)) {
  246. $presellData = [];
  247. $nowTime = strtotime(date("Y-m-d"));
  248. foreach ($productInfoList as $currentInfo) {
  249. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  250. util::fail('只能选一家的商品,请清空重新选呢');
  251. }
  252. $presell = $currentInfo['presell'] ?? 0;
  253. $presellData[] = $presell;
  254. if ($presell == 1) {
  255. if (empty($post['reachDate'])) {
  256. util::fail('请选择配送或取货日期');
  257. }
  258. $sendTimeWant = $post['reachDate'];
  259. if (strtotime($sendTimeWant) < $nowTime) {
  260. util::fail('不能选择过去时间');
  261. }
  262. $hasDateStr = $currentInfo['presellDate'] ?? [];
  263. $hasDate = explode(',', trim($hasDateStr));
  264. if (empty($hasDate)) {
  265. util::fail('预售日期有问题,请提醒门店');
  266. }
  267. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  268. util::fail("有预售花材在{$sendTimeWant}没到货");
  269. }
  270. $post['presell'] = 1;
  271. }
  272. }
  273. if (count($productInfoList) != count($ids)) {
  274. util::fail('存在无效商品');
  275. }
  276. $presellData = array_unique($presellData);
  277. if (count($presellData) > 1) {
  278. util::fail('预售和非预售花材不能一起下单');
  279. }
  280. } else {
  281. util::fail('花材不存在');
  282. }
  283. $connection = Yii::$app->db;
  284. $transaction = $connection->beginTransaction();
  285. $transactionFinished = false;
  286. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  287. if ($transactionFinished) {
  288. return;
  289. }
  290. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  291. if ($transaction->isActive) {
  292. $transaction->rollBack();
  293. }
  294. });
  295. try {
  296. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
  297. $post['deadline'] = time() + $orderValidTime;
  298. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  299. $post['needPrint'] = dict::getDict('needPrint', 'need');
  300. $user = $this->user;
  301. $post['bookMobile'] = $user->mobile ?? '';
  302. $post['bookName'] = $user->name ?? '';
  303. $hasPay = 0;
  304. $modifyPrice = 0;
  305. //各个等级对应的price addPrice字段
  306. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  307. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  308. $totalWeight = 0;
  309. $totalNum = 0;
  310. $totalReachDiscount = 0;
  311. foreach ($productList as $eleKey => $element) {
  312. $level = 0;
  313. $productId = $element['productId'];
  314. $product = $productInfoList[$productId];
  315. if (empty($product)) {
  316. util::fail('有商品信息没有找到');
  317. }
  318. $num = $element['num'] ?? 0; // 此次购买数量
  319. $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
  320. $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
  321. $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum); // 已购买总数量
  322. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
  323. $reachNum = $product['reachNum'] ?? 0;
  324. $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
  325. if ($reachNum > 0 && $num >= $reachNum && $reachNumDiscount > 0) {
  326. $currentReachDiscount = bcmul($reachNumDiscount, $num, 2);
  327. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  328. }
  329. $currentTotal = bcmul($price, $num, 2);
  330. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  331. $productList[$eleKey]['unitPrice'] = $price;
  332. $weight = $product['weight'] ?? 0;
  333. $currentWeight = bcmul($num, $weight, 2);
  334. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  335. $totalNum = bcadd($totalNum, $num);
  336. if ($product['limitBuy'] > 0) {
  337. $product['productId'] = $productId;
  338. \bizHd\product\classes\ProductClass::handleLimitBuy($product, $hdCustomId, floatval($num));
  339. }
  340. }
  341. $post['product'] = $productList;
  342. $post['reachDiscountPrice'] = $totalReachDiscount;
  343. //不能前端传运费过来,只能通过跑腿接口计算出来
  344. unset($post['sendCost']);
  345. $mapSet = ShopClass::hasIntraCity($this->shop);
  346. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  347. if (isset($post['sendType']) && $post['sendType'] == 2 && $openIntraCity != 0) {
  348. if ($openIntraCity == 2) {
  349. util::fail('不能使用跑腿');
  350. }
  351. $cargoName = '花材'; // 商品名称
  352. $cargo = [
  353. 'cargo_name' => $cargoName, // 商品名称
  354. 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  355. 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
  356. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  357. 'cargo_num' => $totalNum
  358. ];
  359. $customName = $custom->name;
  360. $customMobile = $custom->mobile;
  361. if (empty($post['long']) || empty($post['lat'])) {
  362. util::fail('位置错误');
  363. }
  364. // $originalLng = doubleval($post['long']);
  365. // $originalLat = doubleval($post['lat']);
  366. // $city = $post['city'] ?? '';
  367. // $dist = $post['dist'] ?? '';
  368. // $address = $post['address'] ?? '';
  369. // $showAddress = $post['showAddress'] ?? '';
  370. // $userAddress = $city . $dist . $address . "({$showAddress})";
  371. // $main = $this->main;
  372. $shop = $this->shop;
  373. $shopLat = $shop->lat ?? '';
  374. $shopLong = $shop->long ?? '';
  375. if (empty($shopLat) || empty($shopLong)) {
  376. util::fail('商家门店地址定位未设置,编号263');
  377. }
  378. // $wxStoreId = $main->wxStoreId ?? 0;
  379. // if (empty($wxStoreId)) {
  380. // util::fail('商家还没有开通跑腿功能');
  381. // }
  382. // $orderData = [
  383. // 'wx_store_id' => $wxStoreId,
  384. // 'user_name' => $customName,
  385. // 'user_phone' => $customMobile,
  386. // 'user_lng' => $originalLng,
  387. // 'user_lat' => $originalLat,
  388. // 'user_address' => $userAddress,
  389. // 'cargo' => $cargo,
  390. // ];
  391. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  392. // $result = IntraCityExpress::preAddOrder($orderData);
  393. // $errCode = $result['errcode'] ?? 1;
  394. // if ($errCode != 0) {
  395. // $errMsg = $result['errmsg'] ?? '';
  396. // util::fail('获取运费错误:' . $errMsg);
  397. // }
  398. // $fee = $result['est_fee'] ?? 0;
  399. // $distance = $result['distance'] ?? 0;
  400. // $sendCost = bcdiv($fee, 100, 2);
  401. $distance = 0;
  402. $sendCost = 0;
  403. //生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  404. $prefix = 'PT-XSD-' . $this->mainId . '-';
  405. $orderSn = $prefix . round(microtime(true) * 1000);
  406. $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  407. // 这儿要调用跑腿接口计算运费和距离
  408. try {
  409. if (empty($post['deliveryPlatform'])) {
  410. util::fail('请选择跑腿');
  411. }
  412. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  413. 'productList' => $productList,
  414. 'deliveryPlatform' => $post['deliveryPlatform'],
  415. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  416. 'ghsInfo' => $ghsInfo,
  417. 'custom' => $this->user, // $custom 数据有问题,要是 user
  418. 'order' => [
  419. 'orderSn' => $orderSn,
  420. 'goodsType' => 1, //商品类型:0花束 1花材
  421. 'itemTotalAmount' => $modifyPrice,
  422. 'remark' => $post['remark'] ?? '',
  423. ],
  424. 'mainId' => $this->mainId,
  425. 'productCount' => $totalNum,
  426. ]);
  427. $sendCost = $quoteResult['sendCost'];
  428. $distance = $quoteResult['sendDistance'];
  429. } catch (\Exception $e) {
  430. util::fail($e->getMessage());
  431. }
  432. $post['sendCost'] = $sendCost;
  433. $post['sendDistance'] = $distance;
  434. //noticeUtil::push("零售订单(花材),获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance}m 姓名:{$customName} 手机号 {$customMobile} 金额:{$modifyPrice} 数量:{$totalNum}", '15280215347');
  435. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  436. }
  437. //红包使用,多有处,关键词 use_hb_action
  438. $hbId = $post['hbId'] ?? 0;
  439. $hb = null;
  440. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  441. $hb = HbClass::getById($hbId, true);
  442. if (!empty($hb)) {
  443. if ($hb->customId != $hdCustomId) {
  444. util::fail('不是你的红包');
  445. }
  446. if ($hb->amount != $post['hbAmount']) {
  447. util::fail('红包金额不相等'); //红包数据可能被串改
  448. }
  449. if ($hb->status == 1) {
  450. util::fail('红包已使用');
  451. }
  452. if ($hb->status == -1) {
  453. util::fail('红包已作废');
  454. }
  455. if ($hb->endTime < time()) {
  456. $hb->status = -1;
  457. $hb->save();
  458. util::fail('红包已失效');
  459. }
  460. if ($hb->beginTime > time()) {
  461. util::fail('红包可使用期未到');
  462. }
  463. if (bccomp($modifyPrice, $hb->minConsume) == -1) {
  464. util::fail("不满足最低消费金额{$hb->minConsume}元");
  465. }
  466. if ($this->shop->rechargeWeal == 3) {
  467. if (bccomp($custom->balance, $modifyPrice) == -1) {
  468. //不要限制提交
  469. //util::fail('此红包非余额支付不可用(您的余额不足)');
  470. }
  471. }
  472. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  473. } else {
  474. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  475. util::fail('红包无效');
  476. }
  477. }
  478. $post['bigNum'] = $totalNum;
  479. $post['modifyPrice'] = $modifyPrice;
  480. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  481. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  482. if (!empty($hb)) {
  483. $hb->status = 1;
  484. $hb->orderId = $return->id;
  485. $hb->save();
  486. }
  487. if (isset($return->shopId)) {
  488. if ($return->shopId == 7610) {
  489. if ($return->actPrice < 200) {
  490. if ($return->sendType == 0) {
  491. util::fail('满200元支持送货上门');
  492. }
  493. }
  494. }
  495. }
  496. if ($return->sendType == 2) {
  497. $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
  498. if ($ext->thirdSend == 1) {
  499. util::fail('本店暂不能使用跑腿呢');
  500. }
  501. }
  502. $transaction->commit();
  503. $transactionFinished = true;
  504. \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
  505. $orderSn = $return->orderSn ?? '';
  506. $orderPrice = $return->orderPrice ?? 0;
  507. $id = $return->id ?? 0;
  508. $getPayType = dict::getDict('getPayType');
  509. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  510. } catch (\Exception $e) {
  511. $transaction->rollBack();
  512. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  513. $transactionFinished = true;
  514. Yii::error("失败原因:" . $e->getMessage());
  515. util::fail('下单失败');
  516. }
  517. }
  518. //商城下单操作 ssh 2019.12.3
  519. public function actionCreateOrder()
  520. {
  521. $post = Yii::$app->request->post();
  522. //避免重复提交
  523. $userId = $this->userId;
  524. util::checkRepeatCommit($userId, 3);
  525. $shop = $this->shop;
  526. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  527. if ($openShop == 0) {
  528. util::fail('已休店');
  529. }
  530. $goodsId = $post['goodsId'] ?? 0;
  531. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  532. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  533. if (empty($goodsId)) {
  534. util::fail('请选择商品');
  535. }
  536. $mainId = $this->mainId;
  537. $goodsInfo = GoodsClass::getById($goodsId, true);
  538. if (empty($goodsInfo)) {
  539. util::fail('没有找到商品');
  540. }
  541. if ($goodsInfo->mainId != $mainId) {
  542. util::fail('不是你的商品');
  543. }
  544. $stock = $goodsInfo->stock ?? 0;
  545. if ($stock <= 0) {
  546. util::fail('库存不足');
  547. }
  548. //事务处理
  549. $connection = Yii::$app->db;
  550. $transaction = $connection->beginTransaction();
  551. try {
  552. $custom = $this->custom;
  553. if (empty($custom)) {
  554. util::fail('个人信息缺失');
  555. }
  556. $customId = $custom->id ?? 0;
  557. $hd = $this->hd;
  558. $post['customId'] = $customId;
  559. $customName = $custom->name ?? '';
  560. $post['customName'] = $customName;
  561. $post['customNamePy'] = stringUtil::py($customName);
  562. $post['hdId'] = $hd->id ?? 0;
  563. $post['hdName'] = $hd->name ?? '';
  564. $user = $this->user;
  565. $post['bookName'] = $user->name ?? '';
  566. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  567. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  568. $post['bookMobile'] = $bookMobile;
  569. $post['payWay'] = 0;
  570. $shop = $this->shop;
  571. $params = [];
  572. $goodsData = $goodsInfo->attributes;
  573. $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, $params);
  574. $goodsInfo->priceType = $ret['priceType'] ?? 0;
  575. if ($goodsInfo->priceType == 0) {
  576. util::fail('没有价格,不能下单哈');
  577. }
  578. $unitPrice = $ret['price'] ?? 0;
  579. if ($unitPrice <= 0) {
  580. util::fail('没有价格,不能下单呢');
  581. }
  582. //计算总金额
  583. $goodsPrice = $unitPrice * $goodsNum;
  584. //计算运费
  585. $sendDistance = 0;
  586. $sendCost = 0;
  587. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  588. $receiveUserName = $post['receiveUserName'] ?? '';
  589. if (empty($receiveUserName)) {
  590. util::fail('请填写收花人名字');
  591. }
  592. $receiveMobile = $post['receiveMobile'] ?? '';
  593. if (empty($receiveMobile)) {
  594. util::fail('请填写收花人手机号');
  595. }
  596. if (!stringUtil::isMobile($receiveMobile)) {
  597. util::fail('收花人手机号错误');
  598. }
  599. }
  600. $mapSet = ShopClass::hasIntraCity($shop);
  601. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  602. if ($sendType == dict::getDict('sendType', 'thirdSend') && $openIntraCity != 0) {
  603. // if (dict::getDict('hasMap') == 0) {
  604. // util::fail('不能使用跑腿,请选其它方式');
  605. // }
  606. // $lat = $post['lat'] ?? '';
  607. // $lng = $post['long'] ?? '';
  608. // if ((empty($lat) || empty($lng))) { // 当 hasMap 为1时,经纬数据是必要的
  609. // util::fail('请填写正确地址');
  610. // }
  611. // $address = $post['address'] ?? '';
  612. // $floor = $post['floor'] ?? '';
  613. // $fullAddress = $address . $floor;
  614. // $post['fullAddress'] = $fullAddress;
  615. // $shopLat = $shop->lat ?? '';
  616. // $shopLong = $shop->long ?? '';
  617. // if (empty($shopLat) || empty($shopLong)) {
  618. // util::fail('商家门店地址定位未设置,编号8966');
  619. // }
  620. // $weight = $goodsInfo->weight ?? 0;
  621. // if ($weight <= 0) {
  622. // util::fail('商品没有重量');
  623. // }
  624. // $totalWeight = bcmul($weight, $goodsNum, 2);
  625. // $cargoName = '鲜花'; // 商品名称
  626. // $cargo = [
  627. // 'cargo_name' => $cargoName, // 商品名称
  628. // 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  629. // 'cargo_price' => intval($goodsPrice * 100), // 单位:分 -- 把元转换为分
  630. // 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  631. // 'cargo_num' => $goodsNum
  632. // ];
  633. // $customName = $custom->name;
  634. // $customMobile = $custom->mobile;
  635. // if (empty($post['long']) || empty($post['lat'])) {
  636. // util::fail('位置错误');
  637. // }
  638. // $originalLng = doubleval($post['long']);
  639. // $originalLat = doubleval($post['lat']);
  640. // $city = $post['city'] ?? '';
  641. // $dist = $post['dist'] ?? '';
  642. // $address = $post['address'] ?? '';
  643. // $showAddress = $post['showAddress'] ?? '';
  644. // $userAddress = $city . $dist . $address . "({$showAddress})";
  645. // $main = $this->main;
  646. // $shopLat = $shop->lat ?? '';
  647. // $shopLong = $shop->long ?? '';
  648. // if (empty($shopLat) || empty($shopLong)) {
  649. // util::fail('商家门店地址定位未设置,编号6880');
  650. // }
  651. // $wxStoreId = $main->wxStoreId ?? 0;
  652. // if (empty($wxStoreId)) {
  653. // util::fail('商家还没有开通跑腿功能');
  654. // }
  655. // $orderData = [
  656. // 'wx_store_id' => $wxStoreId,
  657. // 'user_name' => $customName,
  658. // 'user_phone' => $customMobile,
  659. // 'user_lng' => $originalLng,
  660. // 'user_lat' => $originalLat,
  661. // 'user_address' => $userAddress,
  662. // 'cargo' => $cargo,
  663. // ];
  664. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  665. // $result = IntraCityExpress::preAddOrder($orderData);
  666. // $errCode = $result['errcode'] ?? 1;
  667. // if ($errCode != 0) {
  668. // $errMsg = $result['errmsg'] ?? '';
  669. // util::fail('获取运费错误:' . $errMsg);
  670. // }
  671. // $fee = $result['est_fee'] ?? 0;
  672. // $sendDistance = $result['distance'] ?? 0;
  673. // $sendCost = bcdiv($fee, 100, 2);
  674. // //免运费的不收运费
  675. // if ($goodsInfo->freightType == 1) {
  676. // $sendCost = 0;
  677. // }
  678. // noticeUtil::push("零售订单!获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$sendDistance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$goodsPrice} 数量:{$goodsNum}", '15280215347');
  679. if ($openIntraCity == 2) {
  680. util::fail('不能使用跑腿');
  681. }
  682. $cutomInfo = [
  683. 'name' => $post['receiveUserName'],
  684. 'mobile' => $post['receiveMobile'],
  685. 'fullAddress' => $post['address'],
  686. 'floor' => $post['floor'],
  687. 'dist' => $post[''] ?? '',
  688. 'lat' => $post['lat'],
  689. 'long' => $post['long'],
  690. 'address' => $post['address'],
  691. 'city' => $post['city'],
  692. ];
  693. $cutomInfo = (object)$cutomInfo;
  694. // 生成随机花束订单号
  695. $prefix = 'KD_CS-' . $this->mainId . '-';
  696. $orderSn = $prefix . round(microtime(true) * 1000);
  697. // 使用 DeliveryQuoteUtil 获取配送报价
  698. try {
  699. if ($goodsInfo->weight > 5) {
  700. $goodsInfo->weight = 5;
  701. }
  702. if (empty($post['deliveryPlatform'])) {
  703. util::fail('请选择跑腿');
  704. }
  705. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  706. 'productList' => [['bigNum' => $goodsNum, 'weight' => $goodsInfo->weight]],
  707. 'deliveryPlatform' => $post['deliveryPlatform'],
  708. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  709. 'ghsInfo' => ['mainId' => $mainId, 'shopId' => $this->shopId],
  710. 'custom' => $cutomInfo,
  711. 'order' => [
  712. 'orderSn' => $orderSn,
  713. 'goodsType' => 0, //商品类型:0花束 1花材
  714. 'itemTotalAmount' => $goodsPrice,
  715. 'remark' => $post['remark'] ?? '',
  716. 'freightType' => $goodsInfo->freightType,
  717. ],
  718. 'mainId' => $this->mainId,
  719. 'productCount' => $goodsNum,
  720. ]);
  721. $sendCost = $quoteResult['sendCost'];
  722. $sendDistance = $quoteResult['sendDistance'];
  723. noticeUtil::push("零售订单(花束),获取运费:{$sendCost} 重量:{$goodsInfo->weight} 距离:{$sendDistance}m 姓名:{$customName} 金额:{$goodsPrice} 数量:{$goodsNum}");
  724. } catch (\Exception $e) {
  725. util::fail($e->getMessage());
  726. }
  727. }
  728. $post['sendDistance'] = $sendDistance;
  729. $post['sendCost'] = $sendCost;
  730. $post['sjId'] = $this->sjId;
  731. $post['shopId'] = $this->shopId;
  732. $post['mainId'] = $mainId;
  733. $post['userId'] = $this->userId;
  734. if ($goodsInfo->priceType == 0) {
  735. util::fail('没有价格,不能下单');
  736. }
  737. $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  738. //非门店订单
  739. $post['store'] = 0;
  740. //红包使用,多有处,关键词 use_hb_action
  741. $hbId = $post['hbId'] ?? 0;
  742. $hb = null;
  743. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  744. $hb = HbClass::getById($hbId, true);
  745. if (!empty($hb)) {
  746. if ($hb->customId != $customId) {
  747. util::fail('不是你的红包');
  748. }
  749. if ($hb->amount != $post['hbAmount']) {
  750. util::fail('红包金额不相等'); //红包数据可能被串改
  751. }
  752. if ($hb->status == 1) {
  753. util::fail('红包已使用');
  754. }
  755. if ($hb->status == -1) {
  756. util::fail('红包已作废');
  757. }
  758. if ($hb->endTime < time()) {
  759. $hb->status = -1;
  760. $hb->save();
  761. util::fail('红包已失效');
  762. }
  763. if ($hb->beginTime > time()) {
  764. util::fail('红包可使用期未到');
  765. }
  766. if ($modifyPrice < $hb->minConsume) {
  767. util::fail("不满足最低消费金额{$hb->minConsume}元");
  768. }
  769. if ($this->shop->rechargeWeal == 3) {
  770. if ($custom->balance < $modifyPrice) {
  771. util::fail('此红包非余额支付不可用(您的余额不足)');
  772. }
  773. }
  774. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  775. } else {
  776. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  777. util::fail('红包无效');
  778. }
  779. }
  780. $post['modifyPrice'] = $modifyPrice;
  781. //非自取订单考虑配送时间
  782. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  783. $sendType = $post['sendType'] ?? 0;
  784. if ($sendType != 1) {
  785. if (empty($post['reachPeriod'])) {
  786. util::fail('请选择配送时间哈');
  787. }
  788. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  789. }
  790. $now = time();
  791. $expireTime = $now + 1800;//订单30分钟后过期
  792. $post['deadline'] = $expireTime;
  793. $orderSn = orderSn::getOrderSn();
  794. $post['orderSn'] = $orderSn;
  795. $post['fromType'] = dict::getDict('fromType', 'mall');
  796. $post['cash'] = 0;
  797. $post['mainId'] = $mainId;
  798. $post['needPrint'] = dict::getDict('needPrint', 'need');
  799. $hasPay = 0;
  800. $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
  801. $post['product'] = $product;
  802. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  803. //红包保存订单id
  804. if (!empty($hb)) {
  805. $hb->status = 1;
  806. $hb->orderId = $return->id;
  807. $hb->save();
  808. }
  809. $orderId = $return->id;
  810. $orderSn = $return->orderSn;
  811. $actPrice = $return->actPrice ?? 0;
  812. $transaction->commit();
  813. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => 0, 'id' => $orderId]);
  814. } catch (Exception $e) {
  815. $transaction->rollBack();
  816. Yii::info("失败原因:" . $e->getMessage());
  817. util::fail('下单失败');
  818. }
  819. }
  820. //下单要用到的相关信息 ssh 2019.12.6
  821. public function actionOrderRelate()
  822. {
  823. $regionTree = RegionService::tree();
  824. //门店名称
  825. $shop = !empty($this->shop) ? $this->shop->attributes : [];
  826. $sjName = $shop['merchantName'] ?? '';
  827. $shopName = $shop['shopName'] ?? '';
  828. $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  829. $avatar = $shop['avatar'] ?? '';
  830. $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  831. $shop['smallAvatar'] = $smallAvatar;
  832. $freight = ['first' => 5, 'add' => 2];
  833. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  834. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  835. util::success($out);
  836. }
  837. //余额支付 ssh 20250410
  838. public function actionBalancePay()
  839. {
  840. $post = Yii::$app->request->post();
  841. $orderSn = $post['orderSn'] ?? '';
  842. //避免重复提交
  843. util::checkRepeatCommit($orderSn, 5);
  844. $connection = Yii::$app->db;
  845. $transaction = $connection->beginTransaction();
  846. try {
  847. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  848. if (empty($order)) {
  849. util::fail('没有找到订单');
  850. }
  851. if ($order->shopId != $this->shopId) {
  852. util::fail('不是你的订单');
  853. }
  854. //支付前验证订单有效性
  855. OrderService::checkBeforePay($order);
  856. $payWay = dict::getDict('payWay', 'balancePay');
  857. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  858. $transaction->commit();
  859. //解决重复通知,重要
  860. $cacheKey = 'mall_order_balance_pay_' . $orderSn;
  861. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  862. if (empty($has)) {
  863. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  864. //打印小票和语音播报
  865. $order = \bizHd\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn], true);
  866. if (!empty($order)) {
  867. \bizHd\order\classes\OrderClass::onlinePrint($order);
  868. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  869. HdDeliveryOrderClass::payAfter($order);
  870. $shopId = $order->shopId ?? 0;
  871. $shop = ShopClass::getById($shopId, true);
  872. if (in_array($order->fromType, [1, 3])) {
  873. //您有新的收入提醒
  874. WxMessageClass::gatheringIncomeInform($shop, $order);
  875. }
  876. if ($order->fromType == 2) {
  877. //来自商城的新订单微信通知
  878. WxMessageClass::hdNewOrderInform($shop, $order);
  879. }
  880. }
  881. }
  882. util::complete('支付成功');
  883. } catch (\Exception $e) {
  884. $transaction->rollBack();
  885. Yii::error("支付原因:" . $e->getMessage());
  886. util::fail('支付失败');
  887. }
  888. }
  889. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  890. public function actionWxPay()
  891. {
  892. ini_set('date.timezone', 'Asia/Shanghai');
  893. $post = Yii::$app->request->post();
  894. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  895. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  896. $order = OrderService::getByOrderSn($orderSn);
  897. $orderId = $order['id'];
  898. //验证优惠券是否还有效
  899. if (!empty($couponId)) {
  900. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  901. }
  902. //支付前验证订单有效性
  903. OrderService::checkBeforePay($order);
  904. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  905. $shop = $this->shop;
  906. if (empty($shop)) {
  907. util::fail('没有找到门店信息');
  908. }
  909. if (isset($shop->shopName) && !empty($shop->shopName)) {
  910. $name .= '(' . $shop->shopName . ')';
  911. }
  912. $totalFee = $order['mainPay'];
  913. $openId = '';
  914. if (!empty($post['miniOpenId'])) {
  915. $openId = $post['miniOpenId'];
  916. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  917. }
  918. if (empty($openId)) {
  919. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  920. $openId = $this->user['miniOpenId'];
  921. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  922. }
  923. }
  924. if (empty($openId)) {
  925. util::fail('没有找到openId');
  926. }
  927. $now = time();
  928. $expireTime = $now + 1800;
  929. $updateData = [];
  930. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  931. $oldOrderSn = $orderSn;
  932. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  933. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  934. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  935. $params = [
  936. 'appid' => 'OP00002119',
  937. 'serial_no' => '018b08cfddbd',
  938. 'merchant_no' => $shop->lklSjNo,
  939. 'term_no' => $shop->lklScanTermNo,
  940. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  941. 'lklCertificatePath' => $lklCertificatePath,
  942. ];
  943. $laResource = new Lakala($params);
  944. $closeParams = [
  945. 'orderSn' => $oldOrderSn,
  946. ];
  947. $response = $laResource->close($closeParams);
  948. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  949. $msg = $response['msg'] ?? '';
  950. $lklSjNo = $shop->lklSjNo ?? '';
  951. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  952. util::fail('出错了哦,请重新下一单');
  953. }
  954. $orderSn = orderSn::getOrderSn();
  955. $updateData['orderSn'] = $orderSn;
  956. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  957. } else {
  958. //已请求的不能再修改价格
  959. $updateData['modPrice'] = 0;
  960. }
  961. if (empty($order['deadline'])) {
  962. $updateData['deadline'] = $expireTime;
  963. }
  964. if (!empty($updateData)) {
  965. OrderService::updateById($orderId, $updateData);
  966. }
  967. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  968. $sjExtend = WxOpenClass::getMallWxInfo();
  969. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  970. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  971. $params = [
  972. 'appid' => 'OP00002119',
  973. 'serial_no' => '018b08cfddbd',
  974. 'merchant_no' => $shop->lklSjNo,
  975. 'term_no' => $shop->lklScanTermNo,
  976. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  977. 'lklCertificatePath' => $lklCertificatePath,
  978. ];
  979. $laResource = new Lakala($params);
  980. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  981. $wxParams = [
  982. 'orderSn' => $orderSn,
  983. 'amount' => $totalFee,
  984. 'capitalType' => $capitalType,
  985. 'notifyUrl' => $notifyUrl,
  986. 'wxAppId' => $sjExtend['miniAppId'],
  987. 'openId' => $openId,
  988. 'subject' => $name,
  989. 'couponId' => $couponId,
  990. ];
  991. $response = $laResource->driveWxPay($wxParams);
  992. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  993. $errMsg = $response['msg'] ?? '';
  994. noticeUtil::push($errMsg, '15280215347');
  995. util::fail('支付失败');
  996. }
  997. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  998. $appId = $newParams['app_id'] ?? '';
  999. $nonceStr = $newParams['nonce_str'] ?? '';
  1000. $paySign = $newParams['pay_sign'] ?? '';
  1001. $package = $newParams['package'] ?? '';
  1002. $signType = $newParams['sign_type'] ?? '';
  1003. $timeStamp = $newParams['time_stamp'] ?? '';
  1004. $new = [
  1005. 'id' => $orderId,
  1006. 'appId' => $appId,
  1007. 'nonceStr' => $nonceStr,
  1008. 'paySign' => $paySign,
  1009. 'package' => $package,
  1010. 'signType' => $signType,
  1011. 'timeStamp' => $timeStamp,
  1012. 'orderSn' => $orderSn,
  1013. ];
  1014. util::success($new);
  1015. }
  1016. //快捷付款订单
  1017. public function actionFastPay()
  1018. {
  1019. ini_set('date.timezone', 'Asia/Shanghai');
  1020. $post = Yii::$app->request->post();
  1021. $payWay = $post['payWay'] ?? 0;
  1022. $shopId = $this->shopId;
  1023. $shop = $this->shop;
  1024. if (empty($shop)) {
  1025. util::fail('没有门店信息');
  1026. }
  1027. $customId = $this->customId;
  1028. $custom = $this->custom;
  1029. $hdId = $this->hdId;
  1030. $hd = $this->hd;
  1031. if (empty($customId) || empty($hdId)) {
  1032. //考虑没有登录时的付款
  1033. $defaultCustomId = $shop->defaultCustomId ?? 0;
  1034. if (empty($defaultCustomId)) {
  1035. util::fail('请设置快捷开单的客户');
  1036. }
  1037. $customId = $defaultCustomId;
  1038. $custom = CustomClass::getById($defaultCustomId, true);
  1039. if (empty($custom)) {
  1040. util::fail('无效的快捷客户');
  1041. }
  1042. $hdId = $custom->hdId;
  1043. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1044. if (empty($hd)) {
  1045. util::fail('没有花店信息呢!');
  1046. }
  1047. }
  1048. $post['hdId'] = $hdId;
  1049. $post['hdName'] = $hd->name ?? '';
  1050. $customName = $custom->name ?? '';
  1051. $customPy = $custom->py ?? '';
  1052. $post['customName'] = $customName;
  1053. $post['customNamePy'] = $customPy;
  1054. $post['shopId'] = $shopId;
  1055. $post['mainId'] = $this->mainId;
  1056. $post['customId'] = $customId;
  1057. $prePrice = round($post['prePrice'], 2);
  1058. $actPrice = $prePrice;
  1059. if ($actPrice <= 0) {
  1060. util::fail('请填写付款金额');
  1061. }
  1062. $post['actPrice'] = $actPrice;
  1063. $post['userId'] = $this->userId;
  1064. $now = time();
  1065. $expireTime = $now + 300;//订单5分钟后过期
  1066. $post['createTime'] = date("Y-m-d H:i:s", $now);
  1067. $post['deadline'] = date("Y-m-d H:i:s", $expireTime);
  1068. $order = ScanPayClass::addOrder($post);
  1069. $orderId = $order->id ?? 0;
  1070. $orderSn = $order->orderSn ?? '';
  1071. $capitalType = dict::getDict('capitalType', 'scanPay', 'id');
  1072. if ($payWay == 0) {
  1073. //微信
  1074. $name = '购买鲜花,扫码付款';
  1075. $totalFee = $actPrice;
  1076. $user = UserService::getById($this->userId);
  1077. //小程序使用miniOpenId
  1078. $openId = $user['miniOpenId'];
  1079. if (empty($openId)) {
  1080. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  1081. }
  1082. if (empty($openId)) {
  1083. util::fail('没有微信信息');
  1084. }
  1085. $sjExtend = WxOpenClass::getMallWxInfo();
  1086. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1087. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1088. $params = [
  1089. 'appid' => 'OP00002119',
  1090. 'serial_no' => '018b08cfddbd',
  1091. 'merchant_no' => $shop->lklSjNo,
  1092. 'term_no' => $shop->lklScanTermNo,
  1093. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1094. 'lklCertificatePath' => $lklCertificatePath,
  1095. ];
  1096. $laResource = new Lakala($params);
  1097. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1098. $wxParams = [
  1099. 'orderSn' => $orderSn,
  1100. 'amount' => $totalFee,
  1101. 'capitalType' => $capitalType,
  1102. 'notifyUrl' => $notifyUrl,
  1103. 'wxAppId' => $sjExtend['miniAppId'],
  1104. 'openId' => $openId,
  1105. 'subject' => $name,
  1106. 'couponId' => 0,
  1107. ];
  1108. $response = $laResource->driveWxPay($wxParams);
  1109. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1110. $errMsg = $response['msg'] ?? '';
  1111. noticeUtil::push($errMsg, '15280215347');
  1112. util::fail('支付失败');
  1113. }
  1114. $newParams = $response['resp_data']['acc_resp_fields'] ?? [];
  1115. $appId = $newParams['app_id'] ?? '';
  1116. $nonceStr = $newParams['nonce_str'] ?? '';
  1117. $paySign = $newParams['pay_sign'] ?? '';
  1118. $package = $newParams['package'] ?? '';
  1119. $signType = $newParams['sign_type'] ?? '';
  1120. $timeStamp = $newParams['time_stamp'] ?? '';
  1121. $new = [
  1122. 'id' => $orderId,
  1123. 'appId' => $appId,
  1124. 'nonceStr' => $nonceStr,
  1125. 'paySign' => $paySign,
  1126. 'package' => $package,
  1127. 'signType' => $signType,
  1128. 'timeStamp' => $timeStamp,
  1129. 'orderSn' => $orderSn,
  1130. ];
  1131. util::success($new);
  1132. } elseif ($payWay == 1) {
  1133. //支付宝
  1134. $totalFee = $actPrice;
  1135. $subject = '购买鲜花,扫码付款';
  1136. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1137. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1138. $params = [
  1139. 'appid' => 'OP00002119',
  1140. 'serial_no' => '018b08cfddbd',
  1141. 'merchant_no' => $shop->lklSjNo,
  1142. 'term_no' => $shop->lklB2BTermNo,
  1143. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1144. 'lklCertificatePath' => $lklCertificatePath,
  1145. ];
  1146. $laResource = new Lakala($params);
  1147. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  1148. $cashParams = [
  1149. 'orderSn' => $orderSn,
  1150. 'amount' => $totalFee,
  1151. 'capitalType' => $capitalType,
  1152. 'notifyUrl' => $notifyUrl,
  1153. 'subject' => $subject,
  1154. ];
  1155. $response = $laResource->cashierPay($cashParams);
  1156. if (!isset($response['code']) || $response['code'] != '000000') {
  1157. util::fail('支付失败了');
  1158. }
  1159. $url = $response['resp_data']['counter_url'] ?? '';
  1160. if (empty($url)) {
  1161. util::fail('支付失败');
  1162. }
  1163. util::success(['url' => $url]);
  1164. } elseif ($payWay == 2) {
  1165. //余额支付
  1166. util::fail('暂不支持');
  1167. } else {
  1168. util::fail('无效的支付方式');
  1169. }
  1170. }
  1171. //订单列表 ssh 2019.12.12
  1172. public function actionList()
  1173. {
  1174. $get = Yii::$app->request->get();
  1175. $status = $get['status'] ?? 0;
  1176. $userId = $this->userId;
  1177. $where = ['userId' => $userId];
  1178. if (!empty($status)) {
  1179. $where['status'] = $status;
  1180. }
  1181. $list = OrderService::getOrderList($where);
  1182. util::success($list);
  1183. }
  1184. //订单评价
  1185. public function actionComment()
  1186. {
  1187. $post = Yii::$app->request->post();
  1188. $id = $post['id'];
  1189. $order = OrderClass::getById($id);
  1190. if ($order['grade'] > 0) {
  1191. util::fail('您已经评过了');
  1192. }
  1193. OrderService::comment($post);
  1194. util::complete('提交成功');
  1195. }
  1196. //订单详情 ssh 2019.12.16
  1197. public function actionDetail()
  1198. {
  1199. $id = Yii::$app->request->get('id', 0);
  1200. $detail = OrderClass::getOrderById($id);
  1201. $userId = $this->userId;
  1202. if ($detail['userId'] != $userId) {
  1203. util::fail('不是你的单,不能查看');
  1204. }
  1205. $shOrderMap = [];
  1206. if ($detail['hasForward'] == 1) {
  1207. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1208. }
  1209. $detail['shOrderMap'] = $shOrderMap;
  1210. //是否显示余额支付按钮 ssh 20260120
  1211. $hd = $this->hd;
  1212. $balance = $hd->balance ?? 0;
  1213. $showYePay = $balance > 0 ? 1 : 0;
  1214. $detail['showYePay'] = $showYePay;
  1215. $detail['balance'] = $balance;
  1216. $detail['rechargeWeal'] = $this->shop->rechargeWeal; //是否开启余额支付
  1217. util::success($detail);
  1218. }
  1219. //获取详情 ssh 20220512
  1220. public function actionInfo()
  1221. {
  1222. $sn = Yii::$app->request->get('orderSn', '');
  1223. $detail = OrderClass::getOrderBySn($sn);
  1224. util::success($detail);
  1225. }
  1226. }