OrderController.php 54 KB

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