OrderController.php 54 KB

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