OrderController.php 53 KB

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