OrderController.php 52 KB

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