OrderController.php 55 KB

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