OrderController.php 53 KB

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