OrderController.php 52 KB

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