OrderController.php 55 KB

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