OrderController.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\wx\classes\WxMessageClass;
  6. use bizHd\custom\classes\HdClass;
  7. use bizHd\express\classes\HdDeliveryOrderClass;
  8. use bizHd\hb\classes\HbClass;
  9. use bizHd\order\classes\ScanPayClass;
  10. use bizHd\wx\classes\WxOpenClass;
  11. use bizMall\custom\classes\CustomClass;
  12. use bizMall\goods\classes\GoodsClass;
  13. use bizMall\order\classes\OrderClass;
  14. use bizMall\order\classes\OrderForwardClass;
  15. use bizMall\order\classes\OrderItemClass;
  16. use bizMall\order\services\OrderService;
  17. use bizMall\promote\services\CouponService;
  18. use bizMall\saas\services\RegionService;
  19. use bizMall\shop\classes\ShopExtClass;
  20. use bizMall\user\services\UserService;
  21. use common\components\dateUtil;
  22. use common\components\delivery\util\DeliveryQuoteUtil;
  23. use common\components\dict;
  24. use common\components\imgUtil;
  25. use common\components\noticeUtil;
  26. use common\components\orderSn;
  27. use common\components\stringUtil;
  28. use Yii;
  29. use common\components\util;
  30. use common\components\lakala\Lakala;
  31. use common\components\IntraCityExpress;
  32. class OrderController extends BaseController
  33. {
  34. //二维码收款使用
  35. public $guestAccess = ['order-relate', 'fast-pay'];
  36. public function actionInform()
  37. {
  38. $get = Yii::$app->request->get();
  39. $hdId = $get['hdId'] ?? 0;
  40. $account = $get['account'] ?? 0;
  41. $page = $get['page'] ?? '';
  42. $userId = $this->userId;
  43. noticeUtil::push("零售客户下单报错 hdId:{$hdId} account:{$account} userId:{$userId} page:{$page}", '15280215347');
  44. }
  45. //记账单列表 ssh 20250627
  46. public function actionDebtList()
  47. {
  48. $get = Yii::$app->request->get();
  49. $id = $get['id'] ?? 0;
  50. $where = [];
  51. $where['userId'] = $this->userId;
  52. if (!empty($id)) {
  53. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  54. if (empty($hd)) {
  55. util::fail('无效的花店');
  56. }
  57. $where['hdId'] = $id;
  58. }
  59. $where['debt'] = 1;
  60. $searchTime = $get['searchTime'] ?? '';
  61. if (!empty($searchTime)) {
  62. $startTime = $get['startTime'] ?? '';
  63. $endTime = $get['endTime'] ?? '';
  64. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  65. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  66. }
  67. $respond = OrderClass::getDebtList($where);
  68. util::success($respond);
  69. }
  70. //新计算运费
  71. public function actionNewFreight()
  72. {
  73. $post = Yii::$app->request->post();
  74. $hasMap = dict::getDict('hasMap');
  75. if ($hasMap == 0) {
  76. util::fail('暂不支持跑腿功能');
  77. }
  78. $mainData = $this->main;
  79. $wxStoreId = $mainData->wxStoreId ?? '';
  80. if (empty($wxStoreId)) {
  81. util::fail('商家还没有开通跑腿');
  82. }
  83. $cargoName = $post['cargo_name']; // 商品名称
  84. $cargo = [
  85. 'cargo_name' => $cargoName, // 商品名称
  86. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  87. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  88. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  89. 'cargo_num' => intval($post['package_num'])
  90. ];
  91. $originalLng = doubleval($post['user_lng']);
  92. $originalLat = doubleval($post['user_lat']);
  93. $userName = !empty($post['user_name']) ? $post['user_name'] : '游客';
  94. $orderData = [
  95. 'wx_store_id' => $wxStoreId,
  96. 'user_name' => $userName,
  97. 'user_phone' => $post['user_phone'],
  98. 'user_lng' => $originalLng,
  99. 'user_lat' => $originalLat,
  100. 'user_address' => $post['user_address'],
  101. 'cargo' => $cargo,
  102. ];
  103. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  104. $result = IntraCityExpress::preAddOrder($orderData);
  105. if ($result['errcode'] === 0) {
  106. util::success($result, "查询成功");
  107. } else {
  108. Yii::error("运费查询失败:" . json_encode($result));
  109. $errMsg = $result['errmsg'] ?? '';
  110. $errCode = $result['errcode'] ?? '';
  111. noticeUtil::push("运费查询失败,原因:" . $errMsg . ",编号:" . $errCode, '15280215347');
  112. util::fail('运费查询失败,请确认是否超过20公斤,编号896');
  113. }
  114. }
  115. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  116. public function actionFreight()
  117. {
  118. $get = Yii::$app->request->get();
  119. $shop = $this->shop;
  120. $shopLat = isset($shop->lat) ? $shop->lat : '';
  121. $shopLong = isset($shop->long) ? $shop->long : '';
  122. // 判断地图功能是否正常。0 表示地图定位不可用,全部经纬度数据失效
  123. if (dict::getDict('hasMap') == 0) {
  124. $shopLat = '';
  125. $shopLong = '';
  126. }
  127. if (empty($shopLat) || empty($shopLong)) {
  128. util::success([
  129. 'distance' => 0,
  130. 'showDistance' => 0,
  131. 'fee' => 0,
  132. 'hasMap' => dict::getDict('hasMap'),
  133. 'shop' => 'fail' //门店引起的失败
  134. ]);
  135. }
  136. $userLat = $get['lat'] ?? '';
  137. $userLong = $get['long'] ?? '';
  138. if (empty($userLat) || empty($userLong)) {
  139. util::success([
  140. 'distance' => 0,
  141. 'showDistance' => 0,
  142. 'fee' => 0,
  143. 'hasMap' => dict::getDict('hasMap'),
  144. 'client' => 'fail' //用户端定位失败
  145. ]);
  146. }
  147. $weight = $get['weight'] ?? 0;
  148. if (empty($weight)) {
  149. util::success([
  150. 'distance' => 0,
  151. 'showDistance' => 0,
  152. 'fee' => 0,
  153. 'hasMap' => dict::getDict('hasMap'),
  154. 'weight' => 'fail' //重量不对
  155. ]);
  156. }
  157. //data: {distance: "321", showDistance: "0.3", fee: "12.00"}
  158. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  159. util::success($respond);
  160. }
  161. // 零售商的限购花材信息(与批发端 PurchaseController::actionLimitBuyInfo 对应,按花店客户与 hd_limit_buy Redis)
  162. public function actionLimitBuyInfo()
  163. {
  164. $post = Yii::$app->request->post();
  165. $list = $post['list'] ?? [];
  166. if (empty($list) || !is_array($list)) {
  167. util::fail('请选择花材');
  168. }
  169. $hd = $this->hd;
  170. if (empty($hd)) {
  171. util::fail('没有找到花店');
  172. }
  173. if ($hd->shopId != $this->shopId) {
  174. util::fail('不是你的花店');
  175. }
  176. $hdCustomId = $hd->customId ?? 0;
  177. if (empty($hdCustomId)) {
  178. util::fail('没有找到客户');
  179. }
  180. $respond = \bizHd\product\classes\ProductClass::getLimitBuyInfoByList($list, $hdCustomId);
  181. util::success($respond);
  182. }
  183. //购买花材 ssh 20220511
  184. public function actionBuyItem()
  185. {
  186. $post = Yii::$app->request->post();
  187. $post['sjId'] = $this->sjId;
  188. $post['shopId'] = $this->shopId;
  189. $post['payWay'] = 0;
  190. $post['mainId'] = $this->mainId ?? 0;
  191. $post['userId'] = $this->userId ?? 0;
  192. //避免重复提交
  193. $userId = $this->userId;
  194. util::checkRepeatCommit($userId, 3);
  195. $shop = $this->shop;
  196. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  197. if ($openShop == 0) {
  198. util::fail('已休店');
  199. }
  200. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  201. $mainId = $this->mainId;
  202. if (getenv('YII_ENV') == 'production') {
  203. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  204. util::fail('暂时无法访问');
  205. }
  206. } else {
  207. if (in_array($mainId, [0, 1])) {
  208. util::fail('暂时无法访问');
  209. }
  210. }
  211. $hdId = $post['hdId'] ?? 0;
  212. $hd = HdClass::getById($hdId, true);
  213. if (empty($hd)) {
  214. util::fail('没有找到花店');
  215. }
  216. if ($hd->shopId != $this->shopId) {
  217. util::fail('不是你的花店');
  218. }
  219. $post['hdName'] = $hd->name ?? '';
  220. $hdCustomId = $hd->customId ?? 0;
  221. $custom = CustomClass::getById($hdCustomId, true);
  222. if (empty($custom)) {
  223. util::fail('没有找到客户');
  224. }
  225. $customName = $custom->name ?? '';
  226. $post['customId'] = $hdCustomId;
  227. $post['customName'] = $customName;
  228. $post['customNamePy'] = stringUtil::py($customName);
  229. $now = time();
  230. //订单30分钟后过期
  231. $expireTime = $now + 1800;
  232. $post['deadline'] = $expireTime;
  233. //商城
  234. $post['fromType'] = dict::getDict('fromType', 'mall');
  235. $productJson = $post['product'] ?? '';
  236. if (empty($productJson) && empty($groupId)) {
  237. util::fail('请选择商品');
  238. }
  239. $productList = json_decode($productJson, true);
  240. if (empty($productList)) {
  241. util::fail('请选择商品');
  242. }
  243. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  244. $productInfoList = productClass::getByIds($ids, null, 'id');
  245. if (!empty($productInfoList)) {
  246. $presellData = [];
  247. $nowTime = strtotime(date("Y-m-d"));
  248. foreach ($productInfoList as $currentInfo) {
  249. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  250. util::fail('只能选一家的商品,请清空重新选呢');
  251. }
  252. $presell = $currentInfo['presell'] ?? 0;
  253. $presellData[] = $presell;
  254. if ($presell == 1) {
  255. if (empty($post['reachDate'])) {
  256. util::fail('请选择配送或取货日期');
  257. }
  258. $sendTimeWant = $post['reachDate'];
  259. if (strtotime($sendTimeWant) < $nowTime) {
  260. util::fail('不能选择过去时间');
  261. }
  262. $hasDateStr = $currentInfo['presellDate'] ?? [];
  263. $hasDate = explode(',', trim($hasDateStr));
  264. if (empty($hasDate)) {
  265. util::fail('预售日期有问题,请提醒门店');
  266. }
  267. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  268. util::fail("有预售花材在{$sendTimeWant}没到货");
  269. }
  270. $post['presell'] = 1;
  271. }
  272. }
  273. if (count($productInfoList) != count($ids)) {
  274. util::fail('存在无效商品');
  275. }
  276. $presellData = array_unique($presellData);
  277. if (count($presellData) > 1) {
  278. util::fail('预售和非预售花材不能一起下单');
  279. }
  280. } else {
  281. util::fail('花材不存在');
  282. }
  283. $connection = Yii::$app->db;
  284. $transaction = $connection->beginTransaction();
  285. $transactionFinished = false;
  286. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  287. if ($transactionFinished) {
  288. return;
  289. }
  290. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  291. if ($transaction->isActive) {
  292. $transaction->rollBack();
  293. }
  294. });
  295. try {
  296. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
  297. $post['deadline'] = time() + $orderValidTime;
  298. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  299. $post['needPrint'] = dict::getDict('needPrint', 'need');
  300. $user = $this->user;
  301. $post['bookMobile'] = $user->mobile ?? '';
  302. $post['bookName'] = $user->name ?? '';
  303. $hasPay = 0;
  304. $modifyPrice = 0;
  305. //各个等级对应的price addPrice字段
  306. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  307. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  308. $totalWeight = 0;
  309. $totalNum = 0;
  310. $totalReachDiscount = 0;
  311. foreach ($productList as $eleKey => $element) {
  312. $level = 0;
  313. $productId = $element['productId'];
  314. $product = $productInfoList[$productId];
  315. if (empty($product)) {
  316. util::fail('有商品信息没有找到');
  317. }
  318. $num = $element['num'] ?? 0; // 此次购买数量
  319. $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
  320. $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
  321. $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum); // 已购买总数量
  322. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
  323. $reachNum = $product['reachNum'] ?? 0;
  324. $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
  325. if ($reachNum > 0 && $num >= $reachNum && $reachNumDiscount > 0) {
  326. $currentReachDiscount = bcmul($reachNumDiscount, $num, 2);
  327. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  328. }
  329. $currentTotal = bcmul($price, $num, 2);
  330. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  331. $productList[$eleKey]['unitPrice'] = $price;
  332. $weight = $product['weight'] ?? 0;
  333. $currentWeight = bcmul($num, $weight, 2);
  334. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  335. $totalNum = bcadd($totalNum, $num);
  336. if ($product['limitBuy'] > 0) {
  337. $product['productId'] = $productId;
  338. \bizHd\product\classes\ProductClass::handleLimitBuy($product, $hdCustomId, floatval($num));
  339. }
  340. }
  341. $post['product'] = $productList;
  342. $post['reachDiscountPrice'] = $totalReachDiscount;
  343. //不能前端传运费过来,只能通过跑腿接口计算出来
  344. unset($post['sendCost']);
  345. $mapSet = ShopClass::hasIntraCity($this->shop);
  346. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  347. if (isset($post['sendType']) && $post['sendType'] == 2 && $openIntraCity != 0) {
  348. if ($openIntraCity == 2) {
  349. util::fail('不能使用跑腿');
  350. }
  351. $cargoName = '花材'; // 商品名称
  352. $cargo = [
  353. 'cargo_name' => $cargoName, // 商品名称
  354. 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  355. 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
  356. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  357. 'cargo_num' => $totalNum
  358. ];
  359. $customName = $custom->name;
  360. $customMobile = $custom->mobile;
  361. if (empty($post['long']) || empty($post['lat'])) {
  362. util::fail('位置错误');
  363. }
  364. // $originalLng = doubleval($post['long']);
  365. // $originalLat = doubleval($post['lat']);
  366. // $city = $post['city'] ?? '';
  367. // $dist = $post['dist'] ?? '';
  368. // $address = $post['address'] ?? '';
  369. // $showAddress = $post['showAddress'] ?? '';
  370. // $userAddress = $city . $dist . $address . "({$showAddress})";
  371. // $main = $this->main;
  372. $shop = $this->shop;
  373. $shopLat = $shop->lat ?? '';
  374. $shopLong = $shop->long ?? '';
  375. if (empty($shopLat) || empty($shopLong)) {
  376. util::fail('商家门店地址定位未设置,编号263');
  377. }
  378. // $wxStoreId = $main->wxStoreId ?? 0;
  379. // if (empty($wxStoreId)) {
  380. // util::fail('商家还没有开通跑腿功能');
  381. // }
  382. // $orderData = [
  383. // 'wx_store_id' => $wxStoreId,
  384. // 'user_name' => $customName,
  385. // 'user_phone' => $customMobile,
  386. // 'user_lng' => $originalLng,
  387. // 'user_lat' => $originalLat,
  388. // 'user_address' => $userAddress,
  389. // 'cargo' => $cargo,
  390. // ];
  391. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  392. // $result = IntraCityExpress::preAddOrder($orderData);
  393. // $errCode = $result['errcode'] ?? 1;
  394. // if ($errCode != 0) {
  395. // $errMsg = $result['errmsg'] ?? '';
  396. // util::fail('获取运费错误:' . $errMsg);
  397. // }
  398. // $fee = $result['est_fee'] ?? 0;
  399. // $distance = $result['distance'] ?? 0;
  400. // $sendCost = bcdiv($fee, 100, 2);
  401. $distance = 0;
  402. $sendCost = 0;
  403. //生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  404. $prefix = 'PT-XSD-' . $this->mainId . '-';
  405. $orderSn = $prefix . round(microtime(true) * 1000);
  406. $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  407. // 这儿要调用跑腿接口计算运费和距离
  408. try {
  409. if (empty($post['deliveryPlatform'])) {
  410. util::fail('请选择跑腿');
  411. }
  412. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  413. 'productList' => $productList,
  414. 'deliveryPlatform' => $post['deliveryPlatform'],
  415. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  416. 'ghsInfo' => $ghsInfo,
  417. 'custom' => $this->user, // $custom 数据有问题,要是 user
  418. 'order' => [
  419. 'orderSn' => $orderSn,
  420. 'goodsType' => 1, //商品类型:0花束 1花材
  421. 'itemTotalAmount' => $modifyPrice,
  422. 'remark' => $post['remark'] ?? '',
  423. ],
  424. 'mainId' => $this->mainId,
  425. 'productCount' => $totalNum,
  426. ]);
  427. $sendCost = $quoteResult['sendCost'];
  428. $distance = $quoteResult['sendDistance'];
  429. } catch (\Exception $e) {
  430. util::fail($e->getMessage());
  431. }
  432. $post['sendCost'] = $sendCost;
  433. $post['sendDistance'] = $distance;
  434. //noticeUtil::push("零售订单(花材),获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance}m 姓名:{$customName} 手机号 {$customMobile} 金额:{$modifyPrice} 数量:{$totalNum}", '15280215347');
  435. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  436. }
  437. //红包使用,多有处,关键词 use_hb_action
  438. $hbId = $post['hbId'] ?? 0;
  439. $hb = null;
  440. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  441. $hb = HbClass::getById($hbId, true);
  442. if (!empty($hb)) {
  443. if ($hb->customId != $hdCustomId) {
  444. util::fail('不是你的红包');
  445. }
  446. if ($hb->amount != $post['hbAmount']) {
  447. util::fail('红包金额不相等'); //红包数据可能被串改
  448. }
  449. if ($hb->status == 1) {
  450. util::fail('红包已使用');
  451. }
  452. if ($hb->status == -1) {
  453. util::fail('红包已作废');
  454. }
  455. if ($hb->endTime < time()) {
  456. $hb->status = -1;
  457. $hb->save();
  458. util::fail('红包已失效');
  459. }
  460. if ($hb->beginTime > time()) {
  461. util::fail('红包可使用期未到');
  462. }
  463. if (bccomp($modifyPrice, $hb->minConsume) == -1) {
  464. util::fail("不满足最低消费金额{$hb->minConsume}元");
  465. }
  466. if ($this->shop->rechargeWeal == 3) {
  467. if (bccomp($custom->balance, $modifyPrice) == -1) {
  468. //不要限制提交
  469. //util::fail('此红包非余额支付不可用(您的余额不足)');
  470. }
  471. }
  472. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  473. } else {
  474. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  475. util::fail('红包无效');
  476. }
  477. }
  478. $post['bigNum'] = $totalNum;
  479. $post['modifyPrice'] = $modifyPrice;
  480. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  481. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  482. if (!empty($hb)) {
  483. $hb->status = 1;
  484. $hb->orderId = $return->id;
  485. $hb->save();
  486. }
  487. if (isset($return->shopId)) {
  488. if ($return->shopId == 7610) {
  489. if ($return->actPrice < 200) {
  490. if ($return->sendType == 0) {
  491. util::fail('满200元支持送货上门');
  492. }
  493. }
  494. }
  495. }
  496. if ($return->sendType == 2) {
  497. $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
  498. if ($ext->thirdSend == 1) {
  499. util::fail('本店暂不能使用跑腿呢');
  500. }
  501. }
  502. $transaction->commit();
  503. $transactionFinished = true;
  504. \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
  505. $orderSn = $return->orderSn ?? '';
  506. $orderPrice = $return->orderPrice ?? 0;
  507. $id = $return->id ?? 0;
  508. $getPayType = dict::getDict('getPayType');
  509. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  510. } catch (\Exception $e) {
  511. $transaction->rollBack();
  512. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  513. $transactionFinished = true;
  514. Yii::error("失败原因:" . $e->getMessage());
  515. util::fail('下单失败');
  516. }
  517. }
  518. //商城下单操作 ssh 2019.12.3
  519. public function actionCreateOrder()
  520. {
  521. $post = Yii::$app->request->post();
  522. //避免重复提交
  523. $userId = $this->userId;
  524. util::checkRepeatCommit($userId, 3);
  525. $shop = $this->shop;
  526. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  527. if ($openShop == 0) {
  528. util::fail('已休店');
  529. }
  530. $goodsId = $post['goodsId'] ?? 0;
  531. $specGoodsId = (int)($post['specGoodsId'] ?? 0);
  532. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  533. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  534. if (empty($goodsId)) {
  535. util::fail('请选择商品');
  536. }
  537. $mainId = $this->mainId;
  538. $goodsInfo = GoodsClass::getById($goodsId, true);
  539. if (empty($goodsInfo)) {
  540. util::fail('没有找到商品');
  541. }
  542. if ($goodsInfo->mainId != $mainId) {
  543. util::fail('不是你的商品');
  544. }
  545. if ($specGoodsId > 0) {
  546. $specGoods = GoodsClass::getById($specGoodsId, true);
  547. if (empty($specGoods) || $specGoods->mainId != $mainId || $specGoods->masterId != $goodsId || $specGoods->delStatus != 0 || $specGoods->status != 1) {
  548. util::fail('规格不可用');
  549. }
  550. $goodsInfo = $specGoods;
  551. } elseif ((int)($goodsInfo->masterId ?? 0) === 0) {
  552. $hasSpecs = GoodsClass::getByCondition(['masterId'=>$goodsId, 'delStatus'=>0], true);
  553. if (!empty($hasSpecs)) util::fail('请选择规格');
  554. }
  555. $stock = $goodsInfo->stock ?? 0;
  556. if ($stock <= 0) {
  557. util::fail('库存不足');
  558. }
  559. //事务处理
  560. $connection = Yii::$app->db;
  561. $transaction = $connection->beginTransaction();
  562. try {
  563. $custom = $this->custom;
  564. if (empty($custom)) {
  565. util::fail('个人信息缺失');
  566. }
  567. $customId = $custom->id ?? 0;
  568. $hd = $this->hd;
  569. $post['customId'] = $customId;
  570. $customName = $custom->name ?? '';
  571. $post['customName'] = $customName;
  572. $post['customNamePy'] = stringUtil::py($customName);
  573. $post['hdId'] = $hd->id ?? 0;
  574. $post['hdName'] = $hd->name ?? '';
  575. $user = $this->user;
  576. $post['bookName'] = $user->name ?? '';
  577. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  578. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  579. $post['bookMobile'] = $bookMobile;
  580. $post['payWay'] = 0;
  581. $shop = $this->shop;
  582. $params = [];
  583. $goodsData = $goodsInfo->attributes;
  584. $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, $params);
  585. $goodsInfo->priceType = $ret['priceType'] ?? 0;
  586. if ($goodsInfo->priceType == 0) {
  587. util::fail('没有价格,不能下单哈');
  588. }
  589. $unitPrice = $ret['price'] ?? 0;
  590. if ($unitPrice <= 0) {
  591. util::fail('没有价格,不能下单呢');
  592. }
  593. //计算总金额
  594. $goodsPrice = $unitPrice * $goodsNum;
  595. //计算运费
  596. $sendDistance = 0;
  597. $sendCost = 0;
  598. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  599. $receiveUserName = $post['receiveUserName'] ?? '';
  600. if (empty($receiveUserName)) {
  601. util::fail('请填写收花人名字');
  602. }
  603. $receiveMobile = $post['receiveMobile'] ?? '';
  604. if (empty($receiveMobile)) {
  605. util::fail('请填写收花人手机号');
  606. }
  607. if (!stringUtil::isMobile($receiveMobile)) {
  608. util::fail('收花人手机号错误');
  609. }
  610. }
  611. $mapSet = ShopClass::hasIntraCity($shop);
  612. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  613. if ($sendType == dict::getDict('sendType', 'thirdSend') && $openIntraCity != 0) {
  614. // if (dict::getDict('hasMap') == 0) {
  615. // util::fail('不能使用跑腿,请选其它方式');
  616. // }
  617. // $lat = $post['lat'] ?? '';
  618. // $lng = $post['long'] ?? '';
  619. // if ((empty($lat) || empty($lng))) { // 当 hasMap 为1时,经纬数据是必要的
  620. // util::fail('请填写正确地址');
  621. // }
  622. // $address = $post['address'] ?? '';
  623. // $floor = $post['floor'] ?? '';
  624. // $fullAddress = $address . $floor;
  625. // $post['fullAddress'] = $fullAddress;
  626. // $shopLat = $shop->lat ?? '';
  627. // $shopLong = $shop->long ?? '';
  628. // if (empty($shopLat) || empty($shopLong)) {
  629. // util::fail('商家门店地址定位未设置,编号8966');
  630. // }
  631. // $weight = $goodsInfo->weight ?? 0;
  632. // if ($weight <= 0) {
  633. // util::fail('商品没有重量');
  634. // }
  635. // $totalWeight = bcmul($weight, $goodsNum, 2);
  636. // $cargoName = '鲜花'; // 商品名称
  637. // $cargo = [
  638. // 'cargo_name' => $cargoName, // 商品名称
  639. // 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  640. // 'cargo_price' => intval($goodsPrice * 100), // 单位:分 -- 把元转换为分
  641. // 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  642. // 'cargo_num' => $goodsNum
  643. // ];
  644. // $customName = $custom->name;
  645. // $customMobile = $custom->mobile;
  646. // if (empty($post['long']) || empty($post['lat'])) {
  647. // util::fail('位置错误');
  648. // }
  649. // $originalLng = doubleval($post['long']);
  650. // $originalLat = doubleval($post['lat']);
  651. // $city = $post['city'] ?? '';
  652. // $dist = $post['dist'] ?? '';
  653. // $address = $post['address'] ?? '';
  654. // $showAddress = $post['showAddress'] ?? '';
  655. // $userAddress = $city . $dist . $address . "({$showAddress})";
  656. // $main = $this->main;
  657. // $shopLat = $shop->lat ?? '';
  658. // $shopLong = $shop->long ?? '';
  659. // if (empty($shopLat) || empty($shopLong)) {
  660. // util::fail('商家门店地址定位未设置,编号6880');
  661. // }
  662. // $wxStoreId = $main->wxStoreId ?? 0;
  663. // if (empty($wxStoreId)) {
  664. // util::fail('商家还没有开通跑腿功能');
  665. // }
  666. // $orderData = [
  667. // 'wx_store_id' => $wxStoreId,
  668. // 'user_name' => $customName,
  669. // 'user_phone' => $customMobile,
  670. // 'user_lng' => $originalLng,
  671. // 'user_lat' => $originalLat,
  672. // 'user_address' => $userAddress,
  673. // 'cargo' => $cargo,
  674. // ];
  675. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  676. // $result = IntraCityExpress::preAddOrder($orderData);
  677. // $errCode = $result['errcode'] ?? 1;
  678. // if ($errCode != 0) {
  679. // $errMsg = $result['errmsg'] ?? '';
  680. // util::fail('获取运费错误:' . $errMsg);
  681. // }
  682. // $fee = $result['est_fee'] ?? 0;
  683. // $sendDistance = $result['distance'] ?? 0;
  684. // $sendCost = bcdiv($fee, 100, 2);
  685. // //免运费的不收运费
  686. // if ($goodsInfo->freightType == 1) {
  687. // $sendCost = 0;
  688. // }
  689. // noticeUtil::push("零售订单!获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$sendDistance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$goodsPrice} 数量:{$goodsNum}", '15280215347');
  690. if ($openIntraCity == 2) {
  691. util::fail('不能使用跑腿');
  692. }
  693. $cutomInfo = [
  694. 'name' => $post['receiveUserName'],
  695. 'mobile' => $post['receiveMobile'],
  696. 'fullAddress' => $post['address'],
  697. 'floor' => $post['floor'],
  698. 'dist' => $post[''] ?? '',
  699. 'lat' => $post['lat'],
  700. 'long' => $post['long'],
  701. 'address' => $post['address'],
  702. 'city' => $post['city'],
  703. ];
  704. $cutomInfo = (object)$cutomInfo;
  705. // 生成随机花束订单号
  706. $prefix = 'KD_CS-' . $this->mainId . '-';
  707. $orderSn = $prefix . round(microtime(true) * 1000);
  708. // 使用 DeliveryQuoteUtil 获取配送报价
  709. try {
  710. if ($goodsInfo->weight > 5) {
  711. $goodsInfo->weight = 5;
  712. }
  713. if (empty($post['deliveryPlatform'])) {
  714. util::fail('请选择跑腿');
  715. }
  716. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  717. 'productList' => [['bigNum' => $goodsNum, 'weight' => $goodsInfo->weight]],
  718. 'deliveryPlatform' => $post['deliveryPlatform'],
  719. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  720. 'ghsInfo' => ['mainId' => $mainId, 'shopId' => $this->shopId],
  721. 'custom' => $cutomInfo,
  722. 'order' => [
  723. 'orderSn' => $orderSn,
  724. 'goodsType' => 0, //商品类型:0花束 1花材
  725. 'itemTotalAmount' => $goodsPrice,
  726. 'remark' => $post['remark'] ?? '',
  727. 'freightType' => $goodsInfo->freightType,
  728. ],
  729. 'mainId' => $this->mainId,
  730. 'productCount' => $goodsNum,
  731. ]);
  732. $sendCost = $quoteResult['sendCost'];
  733. $sendDistance = $quoteResult['sendDistance'];
  734. noticeUtil::push("零售订单(花束),获取运费:{$sendCost} 重量:{$goodsInfo->weight} 距离:{$sendDistance}m 姓名:{$customName} 金额:{$goodsPrice} 数量:{$goodsNum}");
  735. } catch (\Exception $e) {
  736. util::fail($e->getMessage());
  737. }
  738. }
  739. $post['sendDistance'] = $sendDistance;
  740. $post['sendCost'] = $sendCost;
  741. $post['sjId'] = $this->sjId;
  742. $post['shopId'] = $this->shopId;
  743. $post['mainId'] = $mainId;
  744. $post['userId'] = $this->userId;
  745. if ($goodsInfo->priceType == 0) {
  746. util::fail('没有价格,不能下单');
  747. }
  748. $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  749. //非门店订单
  750. $post['store'] = 0;
  751. //红包使用,多有处,关键词 use_hb_action
  752. $hbId = $post['hbId'] ?? 0;
  753. $hb = null;
  754. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  755. $hb = HbClass::getById($hbId, true);
  756. if (!empty($hb)) {
  757. if ($hb->customId != $customId) {
  758. util::fail('不是你的红包');
  759. }
  760. if ($hb->amount != $post['hbAmount']) {
  761. util::fail('红包金额不相等'); //红包数据可能被串改
  762. }
  763. if ($hb->status == 1) {
  764. util::fail('红包已使用');
  765. }
  766. if ($hb->status == -1) {
  767. util::fail('红包已作废');
  768. }
  769. if ($hb->endTime < time()) {
  770. $hb->status = -1;
  771. $hb->save();
  772. util::fail('红包已失效');
  773. }
  774. if ($hb->beginTime > time()) {
  775. util::fail('红包可使用期未到');
  776. }
  777. if ($modifyPrice < $hb->minConsume) {
  778. util::fail("不满足最低消费金额{$hb->minConsume}元");
  779. }
  780. if ($this->shop->rechargeWeal == 3) {
  781. if ($custom->balance < $modifyPrice) {
  782. util::fail('此红包非余额支付不可用(您的余额不足)');
  783. }
  784. }
  785. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  786. } else {
  787. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  788. util::fail('红包无效');
  789. }
  790. }
  791. $post['modifyPrice'] = $modifyPrice;
  792. //非自取订单考虑配送时间
  793. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  794. $sendType = $post['sendType'] ?? 0;
  795. if ($sendType != 1) {
  796. if (empty($post['reachPeriod'])) {
  797. util::fail('请选择配送时间哈');
  798. }
  799. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  800. }
  801. $now = time();
  802. $expireTime = $now + 1800;//订单30分钟后过期
  803. $post['deadline'] = $expireTime;
  804. $orderSn = orderSn::getOrderSn();
  805. $post['orderSn'] = $orderSn;
  806. $post['fromType'] = dict::getDict('fromType', 'mall');
  807. $post['cash'] = 0;
  808. $post['mainId'] = $mainId;
  809. $post['needPrint'] = dict::getDict('needPrint', 'need');
  810. $hasPay = 0;
  811. $saleGoodsId = $specGoodsId > 0 ? $specGoodsId : $goodsId;
  812. $product = [['productId' => $saleGoodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
  813. $post['product'] = $product;
  814. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  815. //红包保存订单id
  816. if (!empty($hb)) {
  817. $hb->status = 1;
  818. $hb->orderId = $return->id;
  819. $hb->save();
  820. }
  821. $orderId = $return->id;
  822. $orderSn = $return->orderSn;
  823. $actPrice = $return->actPrice ?? 0;
  824. $transaction->commit();
  825. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => 0, 'id' => $orderId]);
  826. } catch (Exception $e) {
  827. $transaction->rollBack();
  828. Yii::info("失败原因:" . $e->getMessage());
  829. util::fail('下单失败');
  830. }
  831. }
  832. /**
  833. * 花束+花材合并结算下单
  834. * 复用 OrderService::createHdOrder,不改动 buy-item / create-order
  835. */
  836. public function actionCreateMixOrder()
  837. {
  838. $post = Yii::$app->request->post();
  839. $post['sjId'] = $this->sjId;
  840. $post['shopId'] = $this->shopId;
  841. $post['payWay'] = 0;
  842. $post['mainId'] = $this->mainId ?? 0;
  843. $post['userId'] = $this->userId ?? 0;
  844. $userId = $this->userId;
  845. util::checkRepeatCommit($userId, 3);
  846. $shop = $this->shop;
  847. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  848. if ($openShop == 0) {
  849. util::fail('已休店');
  850. }
  851. $mainId = $this->mainId;
  852. if (getenv('YII_ENV') == 'production') {
  853. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  854. util::fail('暂时无法访问');
  855. }
  856. }
  857. $hdId = $post['hdId'] ?? 0;
  858. $hd = HdClass::getById($hdId, true);
  859. if (empty($hd)) {
  860. util::fail('没有找到花店');
  861. }
  862. if ($hd->shopId != $this->shopId) {
  863. util::fail('不是你的花店');
  864. }
  865. $post['hdName'] = $hd->name ?? '';
  866. // 合并结算沿用花材购物车的客户身份(花店客户)
  867. $hdCustomId = $hd->customId ?? 0;
  868. $custom = CustomClass::getById($hdCustomId, true);
  869. if (empty($custom)) {
  870. util::fail('没有找到客户');
  871. }
  872. $customName = $custom->name ?? '';
  873. $post['customId'] = $hdCustomId;
  874. $post['customName'] = $customName;
  875. $post['customNamePy'] = stringUtil::py($customName);
  876. $productJson = $post['product'] ?? '';
  877. if (empty($productJson)) {
  878. util::fail('请选择商品');
  879. }
  880. $productList = json_decode($productJson, true);
  881. if (empty($productList) || !is_array($productList)) {
  882. util::fail('请选择商品');
  883. }
  884. $itemRows = [];
  885. $goodsRows = [];
  886. foreach ($productList as $idx => $row) {
  887. $property = intval($row['property'] ?? -1);
  888. if ($property === 1) {
  889. $itemRows[$idx] = $row;
  890. } elseif ($property === 0) {
  891. $goodsRows[$idx] = $row;
  892. } else {
  893. util::fail('商品类型错误');
  894. }
  895. }
  896. if (empty($itemRows) && empty($goodsRows)) {
  897. util::fail('请选择商品');
  898. }
  899. // ---- 花材校验与定价 ----
  900. $productInfoList = [];
  901. if (!empty($itemRows)) {
  902. $ids = array_unique(array_filter(array_map(function ($r) {
  903. return intval($r['productId'] ?? 0);
  904. }, $itemRows)));
  905. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  906. if (empty($productInfoList)) {
  907. util::fail('花材不存在');
  908. }
  909. $presellData = [];
  910. $nowTime = strtotime(date("Y-m-d"));
  911. foreach ($productInfoList as $currentInfo) {
  912. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  913. util::fail('只能选一家的商品,请清空重新选呢');
  914. }
  915. $presell = $currentInfo['presell'] ?? 0;
  916. $presellData[] = $presell;
  917. if ($presell == 1) {
  918. if (empty($post['reachDate'])) {
  919. util::fail('请选择配送或取货日期');
  920. }
  921. $sendTimeWant = $post['reachDate'];
  922. if (strtotime($sendTimeWant) < $nowTime) {
  923. util::fail('不能选择过去时间');
  924. }
  925. $hasDateStr = $currentInfo['presellDate'] ?? [];
  926. $hasDate = explode(',', trim($hasDateStr));
  927. if (empty($hasDate)) {
  928. util::fail('预售日期有问题,请提醒门店');
  929. }
  930. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  931. util::fail("有预售花材在{$sendTimeWant}没到货");
  932. }
  933. $post['presell'] = 1;
  934. }
  935. }
  936. if (count($productInfoList) != count($ids)) {
  937. util::fail('存在无效花材');
  938. }
  939. $presellData = array_unique($presellData);
  940. if (count($presellData) > 1) {
  941. util::fail('预售和非预售花材不能一起下单');
  942. }
  943. }
  944. $connection = Yii::$app->db;
  945. $transaction = $connection->beginTransaction();
  946. $transactionFinished = false;
  947. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  948. if ($transactionFinished) {
  949. return;
  950. }
  951. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  952. if ($transaction->isActive) {
  953. $transaction->rollBack();
  954. }
  955. });
  956. try {
  957. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
  958. $post['deadline'] = time() + $orderValidTime;
  959. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  960. $post['needPrint'] = dict::getDict('needPrint', 'need');
  961. $post['fromType'] = dict::getDict('fromType', 'mall');
  962. $post['store'] = 0;
  963. $user = $this->user;
  964. $post['bookMobile'] = $user->mobile ?? '';
  965. $post['bookName'] = $user->name ?? '';
  966. $hasPay = 0;
  967. $modifyPrice = 0;
  968. $totalWeight = 0;
  969. $totalNum = 0;
  970. $itemBigNum = 0;
  971. $totalReachDiscount = 0;
  972. $resolvedProduct = [];
  973. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  974. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  975. foreach ($productList as $eleKey => $element) {
  976. $property = intval($element['property'] ?? -1);
  977. if ($property === 1) {
  978. $level = 0;
  979. $productId = $element['productId'];
  980. $product = $productInfoList[$productId] ?? null;
  981. if (empty($product)) {
  982. util::fail('有花材信息没有找到');
  983. }
  984. $num = $element['num'] ?? 0;
  985. $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
  986. $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
  987. $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum);
  988. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
  989. $reachNum = $product['reachNum'] ?? 0;
  990. $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
  991. if ($reachNum > 0 && $num >= $reachNum && $reachNumDiscount > 0) {
  992. $currentReachDiscount = bcmul($reachNumDiscount, $num, 2);
  993. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  994. }
  995. $currentTotal = bcmul($price, $num, 2);
  996. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  997. $weight = $product['weight'] ?? 0;
  998. $currentWeight = bcmul($num, $weight, 2);
  999. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  1000. $totalNum = bcadd($totalNum, $num);
  1001. $itemBigNum = bcadd($itemBigNum, $num);
  1002. if (($product['limitBuy'] ?? 0) > 0) {
  1003. $product['productId'] = $productId;
  1004. \bizHd\product\classes\ProductClass::handleLimitBuy($product, $hdCustomId, floatval($num));
  1005. }
  1006. $resolvedProduct[] = [
  1007. 'productId' => $productId,
  1008. 'num' => $num,
  1009. 'itemId' => $element['itemId'] ?? 0,
  1010. 'unitType' => $element['unitType'] ?? 0,
  1011. 'property' => 1,
  1012. 'unitPrice' => $price,
  1013. ];
  1014. } else {
  1015. $goodsId = intval($element['goodsId'] ?? 0);
  1016. $specGoodsId = intval($element['specGoodsId'] ?? 0);
  1017. $saleGoodsId = intval($element['productId'] ?? 0);
  1018. $goodsNum = isset($element['num']) && $element['num'] > 0 ? $element['num'] : 1;
  1019. if ($goodsId <= 0 && $saleGoodsId > 0) {
  1020. $probe = GoodsClass::getById($saleGoodsId, true);
  1021. if (!empty($probe)) {
  1022. $masterId = intval($probe->masterId ?? 0);
  1023. if ($masterId > 0) {
  1024. $goodsId = $masterId;
  1025. $specGoodsId = $saleGoodsId;
  1026. } else {
  1027. $goodsId = $saleGoodsId;
  1028. }
  1029. }
  1030. }
  1031. if ($goodsId <= 0) {
  1032. util::fail('请选择花束商品');
  1033. }
  1034. $goodsInfo = GoodsClass::getById($goodsId, true);
  1035. if (empty($goodsInfo)) {
  1036. util::fail('没有找到花束商品');
  1037. }
  1038. if ($goodsInfo->mainId != $mainId) {
  1039. util::fail('不是你的商品');
  1040. }
  1041. if ($specGoodsId > 0) {
  1042. $specGoods = GoodsClass::getById($specGoodsId, true);
  1043. if (empty($specGoods) || $specGoods->mainId != $mainId || $specGoods->masterId != $goodsId || $specGoods->delStatus != 0 || $specGoods->status != 1) {
  1044. util::fail('规格不可用');
  1045. }
  1046. $goodsInfo = $specGoods;
  1047. $saleGoodsId = $specGoodsId;
  1048. } else {
  1049. if ((int)($goodsInfo->masterId ?? 0) === 0) {
  1050. $hasSpecs = GoodsClass::getByCondition(['masterId' => $goodsId, 'delStatus' => 0], true);
  1051. if (!empty($hasSpecs)) {
  1052. util::fail('请选择规格');
  1053. }
  1054. }
  1055. $saleGoodsId = $goodsId;
  1056. }
  1057. $stock = $goodsInfo->stock ?? 0;
  1058. $stockSet = $goodsInfo->stockSet ?? 0;
  1059. if ($stockSet == 1 && $stock <= 0) {
  1060. util::fail('花束库存不足');
  1061. }
  1062. $goodsData = $goodsInfo->attributes;
  1063. $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, []);
  1064. $priceType = $ret['priceType'] ?? 0;
  1065. if ($priceType == 0) {
  1066. util::fail('花束没有价格,不能下单');
  1067. }
  1068. $unitPrice = $ret['price'] ?? 0;
  1069. if ($unitPrice <= 0) {
  1070. util::fail('花束没有价格,不能下单');
  1071. }
  1072. $currentTotal = bcmul($unitPrice, $goodsNum, 2);
  1073. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  1074. $weight = $goodsInfo->weight ?? 0;
  1075. if ($weight > 5) {
  1076. $weight = 5;
  1077. }
  1078. $currentWeight = bcmul($weight, $goodsNum, 2);
  1079. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  1080. $totalNum = bcadd($totalNum, $goodsNum);
  1081. $resolvedProduct[] = [
  1082. 'productId' => $saleGoodsId,
  1083. 'unitType' => 0,
  1084. 'property' => 0,
  1085. 'unitPrice' => $unitPrice,
  1086. 'num' => $goodsNum,
  1087. ];
  1088. }
  1089. }
  1090. $post['product'] = $resolvedProduct;
  1091. $post['reachDiscountPrice'] = $totalReachDiscount;
  1092. $goodsOnlyPrice = $modifyPrice;
  1093. unset($post['sendCost'], $post['packCost'], $post['packingFee'], $post['unMeetSendCost']);
  1094. $sendType = intval($post['sendType'] ?? 0);
  1095. $customInfo = array_merge(
  1096. is_array($custom) ? $custom : ($custom ? $custom->attributes : []),
  1097. is_array($hd) ? $hd : ($hd ? $hd->attributes : [])
  1098. );
  1099. $additionalCosts = \bizHd\order\classes\PsMethodClass::calcFee(
  1100. $this->shopId,
  1101. $this->mainId,
  1102. $sendType,
  1103. $goodsOnlyPrice,
  1104. $itemBigNum,
  1105. $hdCustomId,
  1106. $customInfo
  1107. );
  1108. $unMeetSendCost = $additionalCosts['sendCost'] ?? 0;
  1109. $packingFee = $additionalCosts['packCost'] ?? 0;
  1110. $mapSet = ShopClass::hasIntraCity($this->shop);
  1111. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  1112. $hasItem = !empty($itemRows);
  1113. // 含花材(含混合)按花材报价 goodsType=1;纯花束按 0
  1114. $quoteGoodsType = $hasItem ? 1 : 0;
  1115. if (isset($post['sendType']) && $post['sendType'] == 2 && $openIntraCity != 0) {
  1116. if ($openIntraCity == 2) {
  1117. util::fail('不能使用跑腿');
  1118. }
  1119. if (empty($post['long']) || empty($post['lat'])) {
  1120. util::fail('位置错误');
  1121. }
  1122. $shopLat = $shop->lat ?? '';
  1123. $shopLong = $shop->long ?? '';
  1124. if (empty($shopLat) || empty($shopLong)) {
  1125. util::fail('商家门店地址定位未设置,编号263');
  1126. }
  1127. $distance = 0;
  1128. $sendCost = 0;
  1129. $prefix = 'PT-MIX-' . $this->mainId . '-';
  1130. $orderSn = $prefix . round(microtime(true) * 1000);
  1131. $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  1132. try {
  1133. if (empty($post['deliveryPlatform'])) {
  1134. util::fail('请选择跑腿');
  1135. }
  1136. $quoteCustom = $this->user;
  1137. if ($quoteGoodsType === 0) {
  1138. $quoteCustom = (object)[
  1139. 'name' => $post['receiveUserName'] ?? '',
  1140. 'mobile' => $post['receiveMobile'] ?? '',
  1141. 'fullAddress' => $post['address'] ?? '',
  1142. 'floor' => $post['floor'] ?? '',
  1143. 'dist' => '',
  1144. 'lat' => $post['lat'],
  1145. 'long' => $post['long'],
  1146. 'address' => $post['address'] ?? '',
  1147. 'city' => $post['city'] ?? '',
  1148. ];
  1149. }
  1150. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  1151. 'productList' => $resolvedProduct,
  1152. 'deliveryPlatform' => $post['deliveryPlatform'],
  1153. 'deliveryBracketContent' => $post['deliveryBracketContent'] ?? '',
  1154. 'ghsInfo' => $ghsInfo,
  1155. 'custom' => $quoteCustom,
  1156. 'order' => [
  1157. 'orderSn' => $orderSn,
  1158. 'goodsType' => $quoteGoodsType,
  1159. 'itemTotalAmount' => $modifyPrice,
  1160. 'remark' => $post['remark'] ?? '',
  1161. ],
  1162. 'mainId' => $this->mainId,
  1163. 'productCount' => $totalNum,
  1164. ]);
  1165. $sendCost = $quoteResult['sendCost'];
  1166. $distance = $quoteResult['sendDistance'];
  1167. } catch (\Exception $e) {
  1168. util::fail($e->getMessage());
  1169. }
  1170. $post['sendCost'] = $sendCost;
  1171. $post['sendDistance'] = $distance;
  1172. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  1173. }
  1174. if ($unMeetSendCost > 0) {
  1175. $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $unMeetSendCost, 2);
  1176. $modifyPrice = bcadd($modifyPrice, $unMeetSendCost, 2);
  1177. }
  1178. if ($packingFee > 0) {
  1179. $post['packingFee'] = $packingFee;
  1180. $modifyPrice = bcadd($modifyPrice, $packingFee, 2);
  1181. }
  1182. // 红包(归属花店客户,与 buy-item 一致)
  1183. $hbId = $post['hbId'] ?? 0;
  1184. $hb = null;
  1185. if ($hbId > 0) {
  1186. $hb = HbClass::getById($hbId, true);
  1187. if (!empty($hb)) {
  1188. if ($hb->customId != $hdCustomId) {
  1189. util::fail('不是你的红包');
  1190. }
  1191. if ($hb->amount != $post['hbAmount']) {
  1192. util::fail('红包金额不相等');
  1193. }
  1194. if ($hb->status == 1) {
  1195. util::fail('红包已使用');
  1196. }
  1197. if ($hb->status == -1) {
  1198. util::fail('红包已作废');
  1199. }
  1200. if ($hb->endTime < time()) {
  1201. $hb->status = -1;
  1202. $hb->save();
  1203. util::fail('红包已失效');
  1204. }
  1205. if ($hb->beginTime > time()) {
  1206. util::fail('红包可使用期未到');
  1207. }
  1208. if (bccomp($modifyPrice, $hb->minConsume) == -1) {
  1209. util::fail("不满足最低消费金额{$hb->minConsume}元");
  1210. }
  1211. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);
  1212. } else {
  1213. util::fail('红包无效');
  1214. }
  1215. }
  1216. $psMethodError = \bizHd\order\classes\PsMethodClass::checkLimit(
  1217. $this->shopId,
  1218. $this->mainId,
  1219. $sendType,
  1220. $goodsOnlyPrice,
  1221. $itemBigNum,
  1222. $packingFee,
  1223. $unMeetSendCost,
  1224. $hdCustomId,
  1225. $customInfo
  1226. );
  1227. if ($psMethodError !== '') {
  1228. util::fail($psMethodError);
  1229. }
  1230. $sendType = $post['sendType'] ?? 0;
  1231. if ($sendType != 1) {
  1232. if (empty($post['reachPeriod'])) {
  1233. util::fail('请选择配送时间哈');
  1234. }
  1235. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  1236. }
  1237. $post['bigNum'] = $totalNum;
  1238. $post['modifyPrice'] = $modifyPrice;
  1239. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  1240. $orderSn = $return->orderSn ?? '';
  1241. if (!empty($orderSn) && (float)$unMeetSendCost > 0) {
  1242. $unMeetSendKey = 'hd_ps_order_unmeet_send_' . $orderSn;
  1243. Yii::$app->redis->executeCommand('SETEX', [$unMeetSendKey, 864000, (string)$unMeetSendCost]);
  1244. }
  1245. if (!empty($hb)) {
  1246. $hb->status = 1;
  1247. $hb->orderId = $return->id;
  1248. $hb->save();
  1249. }
  1250. if ($return->sendType == 2) {
  1251. $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
  1252. if (!empty($ext) && $ext->thirdSend == 1) {
  1253. util::fail('本店暂不能使用跑腿呢');
  1254. }
  1255. }
  1256. $transaction->commit();
  1257. $transactionFinished = true;
  1258. \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
  1259. $orderSn = $return->orderSn ?? '';
  1260. $orderPrice = $return->orderPrice ?? ($return->actPrice ?? 0);
  1261. $id = $return->id ?? 0;
  1262. $getPayType = dict::getDict('getPayType');
  1263. util::success([
  1264. 'orderSn' => $orderSn,
  1265. 'totalPrice' => $orderPrice,
  1266. 'couponId' => 0,
  1267. 'id' => $id,
  1268. 'getPayType' => $getPayType,
  1269. ]);
  1270. } catch (\Exception $e) {
  1271. $transaction->rollBack();
  1272. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  1273. $transactionFinished = true;
  1274. Yii::error("合并下单失败:" . $e->getMessage());
  1275. util::fail('下单失败');
  1276. }
  1277. }
  1278. //下单要用到的相关信息 ssh 2019.12.6
  1279. public function actionOrderRelate()
  1280. {
  1281. $regionTree = RegionService::tree();
  1282. //门店名称
  1283. $shop = !empty($this->shop) ? $this->shop->attributes : [];
  1284. $sjName = $shop['merchantName'] ?? '';
  1285. $shopName = $shop['shopName'] ?? '';
  1286. $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  1287. $avatar = $shop['avatar'] ?? '';
  1288. $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1289. $shop['smallAvatar'] = $smallAvatar;
  1290. $freight = ['first' => 5, 'add' => 2];
  1291. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1292. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  1293. util::success($out);
  1294. }
  1295. //余额支付 ssh 20250410
  1296. public function actionBalancePay()
  1297. {
  1298. $post = Yii::$app->request->post();
  1299. $orderSn = $post['orderSn'] ?? '';
  1300. //避免重复提交
  1301. util::checkRepeatCommit($orderSn, 5);
  1302. $connection = Yii::$app->db;
  1303. $transaction = $connection->beginTransaction();
  1304. try {
  1305. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  1306. if (empty($order)) {
  1307. util::fail('没有找到订单');
  1308. }
  1309. if ($order->shopId != $this->shopId) {
  1310. util::fail('不是你的订单');
  1311. }
  1312. //支付前验证订单有效性
  1313. OrderService::checkBeforePay($order);
  1314. $payWay = dict::getDict('payWay', 'balancePay');
  1315. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  1316. $transaction->commit();
  1317. //解决重复通知,重要
  1318. $cacheKey = 'mall_order_balance_pay_' . $orderSn;
  1319. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  1320. if (empty($has)) {
  1321. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  1322. //打印小票和语音播报
  1323. $order = \bizHd\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn], true);
  1324. if (!empty($order)) {
  1325. \bizHd\order\classes\OrderClass::onlinePrint($order);
  1326. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  1327. HdDeliveryOrderClass::payAfter($order);
  1328. $shopId = $order->shopId ?? 0;
  1329. $shop = ShopClass::getById($shopId, true);
  1330. if (in_array($order->fromType, [1, 3])) {
  1331. //您有新的收入提醒
  1332. WxMessageClass::gatheringIncomeInform($shop, $order);
  1333. }
  1334. if ($order->fromType == 2) {
  1335. //来自商城的新订单微信通知
  1336. WxMessageClass::hdNewOrderInform($shop, $order);
  1337. \bizHd\order\classes\PsMethodClass::markUnMeetFeePaidFromOrder($order);
  1338. }
  1339. }
  1340. }
  1341. util::complete('支付成功');
  1342. } catch (\Exception $e) {
  1343. $transaction->rollBack();
  1344. Yii::error("支付原因:" . $e->getMessage());
  1345. util::fail('支付失败');
  1346. }
  1347. }
  1348. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1349. public function actionWxPay()
  1350. {
  1351. ini_set('date.timezone', 'Asia/Shanghai');
  1352. $post = Yii::$app->request->post();
  1353. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  1354. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1355. $order = OrderService::getByOrderSn($orderSn);
  1356. $orderId = $order['id'];
  1357. //验证优惠券是否还有效
  1358. if (!empty($couponId)) {
  1359. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  1360. }
  1361. //支付前验证订单有效性
  1362. OrderService::checkBeforePay($order);
  1363. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  1364. $shop = $this->shop;
  1365. if (empty($shop)) {
  1366. util::fail('没有找到门店信息');
  1367. }
  1368. if (isset($shop->shopName) && !empty($shop->shopName)) {
  1369. $name .= '(' . $shop->shopName . ')';
  1370. }
  1371. $totalFee = $order['mainPay'];
  1372. $openId = '';
  1373. if (!empty($post['miniOpenId'])) {
  1374. $openId = $post['miniOpenId'];
  1375. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  1376. }
  1377. if (empty($openId)) {
  1378. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  1379. $openId = $this->user['miniOpenId'];
  1380. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  1381. }
  1382. }
  1383. if (empty($openId)) {
  1384. util::fail('没有找到openId');
  1385. }
  1386. $now = time();
  1387. $expireTime = $now + 1800;
  1388. $updateData = [];
  1389. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1390. $oldOrderSn = $orderSn;
  1391. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1392. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1393. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1394. $params = [
  1395. 'appid' => 'OP00002119',
  1396. 'serial_no' => '018b08cfddbd',
  1397. 'merchant_no' => $shop->lklSjNo,
  1398. 'term_no' => $shop->lklScanTermNo,
  1399. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1400. 'lklCertificatePath' => $lklCertificatePath,
  1401. ];
  1402. $laResource = new Lakala($params);
  1403. $closeParams = [
  1404. 'orderSn' => $oldOrderSn,
  1405. ];
  1406. $response = $laResource->close($closeParams);
  1407. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1408. $msg = $response['msg'] ?? '';
  1409. $lklSjNo = $shop->lklSjNo ?? '';
  1410. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  1411. util::fail('出错了哦,请重新下一单');
  1412. }
  1413. $orderSn = orderSn::getOrderSn();
  1414. $updateData['orderSn'] = $orderSn;
  1415. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  1416. } else {
  1417. //已请求的不能再修改价格
  1418. $updateData['modPrice'] = 0;
  1419. }
  1420. if (empty($order['deadline'])) {
  1421. $updateData['deadline'] = $expireTime;
  1422. }
  1423. if (!empty($updateData)) {
  1424. OrderService::updateById($orderId, $updateData);
  1425. }
  1426. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1427. $sjExtend = WxOpenClass::getMallWxInfo();
  1428. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1429. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1430. $params = [
  1431. 'appid' => 'OP00002119',
  1432. 'serial_no' => '018b08cfddbd',
  1433. 'merchant_no' => $shop->lklSjNo,
  1434. 'term_no' => $shop->lklScanTermNo,
  1435. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1436. 'lklCertificatePath' => $lklCertificatePath,
  1437. ];
  1438. $laResource = new Lakala($params);
  1439. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1440. $wxParams = [
  1441. 'orderSn' => $orderSn,
  1442. 'amount' => $totalFee,
  1443. 'capitalType' => $capitalType,
  1444. 'notifyUrl' => $notifyUrl,
  1445. 'wxAppId' => $sjExtend['miniAppId'],
  1446. 'openId' => $openId,
  1447. 'subject' => $name,
  1448. 'couponId' => $couponId,
  1449. ];
  1450. $response = $laResource->driveWxPay($wxParams);
  1451. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1452. $errMsg = $response['msg'] ?? '';
  1453. noticeUtil::push($errMsg, '15280215347');
  1454. util::fail('支付失败');
  1455. }
  1456. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1457. $appId = $newParams['app_id'] ?? '';
  1458. $nonceStr = $newParams['nonce_str'] ?? '';
  1459. $paySign = $newParams['pay_sign'] ?? '';
  1460. $package = $newParams['package'] ?? '';
  1461. $signType = $newParams['sign_type'] ?? '';
  1462. $timeStamp = $newParams['time_stamp'] ?? '';
  1463. $new = [
  1464. 'id' => $orderId,
  1465. 'appId' => $appId,
  1466. 'nonceStr' => $nonceStr,
  1467. 'paySign' => $paySign,
  1468. 'package' => $package,
  1469. 'signType' => $signType,
  1470. 'timeStamp' => $timeStamp,
  1471. 'orderSn' => $orderSn,
  1472. ];
  1473. util::success($new);
  1474. }
  1475. //快捷付款订单
  1476. public function actionFastPay()
  1477. {
  1478. ini_set('date.timezone', 'Asia/Shanghai');
  1479. $post = Yii::$app->request->post();
  1480. $payWay = $post['payWay'] ?? 0;
  1481. $shopId = $this->shopId;
  1482. $shop = $this->shop;
  1483. if (empty($shop)) {
  1484. util::fail('没有门店信息');
  1485. }
  1486. $customId = $this->customId;
  1487. $custom = $this->custom;
  1488. $hdId = $this->hdId;
  1489. $hd = $this->hd;
  1490. if (empty($customId) || empty($hdId)) {
  1491. //考虑没有登录时的付款
  1492. $defaultCustomId = $shop->defaultCustomId ?? 0;
  1493. if (empty($defaultCustomId)) {
  1494. util::fail('请设置快捷开单的客户');
  1495. }
  1496. $customId = $defaultCustomId;
  1497. $custom = CustomClass::getById($defaultCustomId, true);
  1498. if (empty($custom)) {
  1499. util::fail('无效的快捷客户');
  1500. }
  1501. $hdId = $custom->hdId;
  1502. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1503. if (empty($hd)) {
  1504. util::fail('没有花店信息呢!');
  1505. }
  1506. }
  1507. $post['hdId'] = $hdId;
  1508. $post['hdName'] = $hd->name ?? '';
  1509. $customName = $custom->name ?? '';
  1510. $customPy = $custom->py ?? '';
  1511. $post['customName'] = $customName;
  1512. $post['customNamePy'] = $customPy;
  1513. $post['shopId'] = $shopId;
  1514. $post['mainId'] = $this->mainId;
  1515. $post['customId'] = $customId;
  1516. $prePrice = round($post['prePrice'], 2);
  1517. $actPrice = $prePrice;
  1518. if ($actPrice <= 0) {
  1519. util::fail('请填写付款金额');
  1520. }
  1521. $post['actPrice'] = $actPrice;
  1522. $post['userId'] = $this->userId;
  1523. $now = time();
  1524. $expireTime = $now + 300;//订单5分钟后过期
  1525. $post['createTime'] = date("Y-m-d H:i:s", $now);
  1526. $post['deadline'] = date("Y-m-d H:i:s", $expireTime);
  1527. $order = ScanPayClass::addOrder($post);
  1528. $orderId = $order->id ?? 0;
  1529. $orderSn = $order->orderSn ?? '';
  1530. $capitalType = dict::getDict('capitalType', 'scanPay', 'id');
  1531. if ($payWay == 0) {
  1532. //微信
  1533. $name = '购买鲜花,扫码付款';
  1534. $totalFee = $actPrice;
  1535. $user = UserService::getById($this->userId);
  1536. //小程序使用miniOpenId
  1537. $openId = $user['miniOpenId'];
  1538. if (empty($openId)) {
  1539. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  1540. }
  1541. if (empty($openId)) {
  1542. util::fail('没有微信信息');
  1543. }
  1544. $sjExtend = WxOpenClass::getMallWxInfo();
  1545. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1546. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1547. $params = [
  1548. 'appid' => 'OP00002119',
  1549. 'serial_no' => '018b08cfddbd',
  1550. 'merchant_no' => $shop->lklSjNo,
  1551. 'term_no' => $shop->lklScanTermNo,
  1552. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1553. 'lklCertificatePath' => $lklCertificatePath,
  1554. ];
  1555. $laResource = new Lakala($params);
  1556. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1557. $wxParams = [
  1558. 'orderSn' => $orderSn,
  1559. 'amount' => $totalFee,
  1560. 'capitalType' => $capitalType,
  1561. 'notifyUrl' => $notifyUrl,
  1562. 'wxAppId' => $sjExtend['miniAppId'],
  1563. 'openId' => $openId,
  1564. 'subject' => $name,
  1565. 'couponId' => 0,
  1566. ];
  1567. $response = $laResource->driveWxPay($wxParams);
  1568. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1569. $errMsg = $response['msg'] ?? '';
  1570. noticeUtil::push($errMsg, '15280215347');
  1571. util::fail('支付失败');
  1572. }
  1573. $newParams = $response['resp_data']['acc_resp_fields'] ?? [];
  1574. $appId = $newParams['app_id'] ?? '';
  1575. $nonceStr = $newParams['nonce_str'] ?? '';
  1576. $paySign = $newParams['pay_sign'] ?? '';
  1577. $package = $newParams['package'] ?? '';
  1578. $signType = $newParams['sign_type'] ?? '';
  1579. $timeStamp = $newParams['time_stamp'] ?? '';
  1580. $new = [
  1581. 'id' => $orderId,
  1582. 'appId' => $appId,
  1583. 'nonceStr' => $nonceStr,
  1584. 'paySign' => $paySign,
  1585. 'package' => $package,
  1586. 'signType' => $signType,
  1587. 'timeStamp' => $timeStamp,
  1588. 'orderSn' => $orderSn,
  1589. ];
  1590. util::success($new);
  1591. } elseif ($payWay == 1) {
  1592. //支付宝
  1593. $totalFee = $actPrice;
  1594. $subject = '购买鲜花,扫码付款';
  1595. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1596. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1597. $params = [
  1598. 'appid' => 'OP00002119',
  1599. 'serial_no' => '018b08cfddbd',
  1600. 'merchant_no' => $shop->lklSjNo,
  1601. 'term_no' => $shop->lklB2BTermNo,
  1602. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1603. 'lklCertificatePath' => $lklCertificatePath,
  1604. ];
  1605. $laResource = new Lakala($params);
  1606. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  1607. $cashParams = [
  1608. 'orderSn' => $orderSn,
  1609. 'amount' => $totalFee,
  1610. 'capitalType' => $capitalType,
  1611. 'notifyUrl' => $notifyUrl,
  1612. 'subject' => $subject,
  1613. ];
  1614. $response = $laResource->cashierPay($cashParams);
  1615. if (!isset($response['code']) || $response['code'] != '000000') {
  1616. util::fail('支付失败了');
  1617. }
  1618. $url = $response['resp_data']['counter_url'] ?? '';
  1619. if (empty($url)) {
  1620. util::fail('支付失败');
  1621. }
  1622. util::success(['url' => $url]);
  1623. } elseif ($payWay == 2) {
  1624. //余额支付
  1625. util::fail('暂不支持');
  1626. } else {
  1627. util::fail('无效的支付方式');
  1628. }
  1629. }
  1630. //订单列表 ssh 2019.12.12
  1631. public function actionList()
  1632. {
  1633. $get = Yii::$app->request->get();
  1634. $status = $get['status'] ?? 0;
  1635. $hdId = $get['hdId'] ?? ($get['id'] ?? 0);
  1636. $userId = $this->userId;
  1637. $where = ['userId' => $userId];
  1638. if (!empty($status)) {
  1639. $where['status'] = $status;
  1640. }
  1641. if (!empty($hdId)) {
  1642. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1643. if (empty($hd)) {
  1644. util::fail('无效的花店');
  1645. }
  1646. if ($hd['userId'] != $userId) {
  1647. util::fail('不是你的花店');
  1648. }
  1649. $where['hdId'] = $hdId;
  1650. }
  1651. $list = OrderService::getOrderList($where);
  1652. util::success($list);
  1653. }
  1654. //订单评价
  1655. public function actionComment()
  1656. {
  1657. $post = Yii::$app->request->post();
  1658. $id = $post['id'];
  1659. $order = OrderClass::getById($id);
  1660. if ($order['grade'] > 0) {
  1661. util::fail('您已经评过了');
  1662. }
  1663. OrderService::comment($post);
  1664. util::complete('提交成功');
  1665. }
  1666. //订单详情 ssh 2019.12.16
  1667. public function actionDetail()
  1668. {
  1669. $id = Yii::$app->request->get('id', 0);
  1670. $detail = OrderClass::getOrderById($id);
  1671. $userId = $this->userId;
  1672. if ($detail['userId'] != $userId) {
  1673. util::fail('不是你的单,不能查看');
  1674. }
  1675. $shOrderMap = [];
  1676. if ($detail['hasForward'] == 1) {
  1677. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1678. }
  1679. $detail['shOrderMap'] = $shOrderMap;
  1680. //是否显示余额支付按钮 ssh 20260120
  1681. $hd = $this->hd;
  1682. $balance = $hd->balance ?? 0;
  1683. $showYePay = $balance > 0 ? 1 : 0;
  1684. $detail['showYePay'] = $showYePay;
  1685. $detail['balance'] = $balance;
  1686. $detail['rechargeWeal'] = $this->shop->rechargeWeal; //是否开启余额支付
  1687. util::success($detail);
  1688. }
  1689. //获取详情 ssh 20220512
  1690. public function actionInfo()
  1691. {
  1692. $sn = Yii::$app->request->get('orderSn', '');
  1693. $detail = OrderClass::getOrderBySn($sn);
  1694. util::success($detail);
  1695. }
  1696. }