OrderController.php 53 KB

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