OrderController.php 54 KB

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