OrderController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\custom\classes\CustomClass;
  7. use bizMall\goods\classes\GoodsClass;
  8. use bizMall\item\classes\ItemClass;
  9. use bizMall\order\classes\OrderClass;
  10. use bizMall\order\classes\OrderForwardClass;
  11. use bizMall\order\classes\OrderGoodsClass;
  12. use bizMall\order\classes\OrderItemClass;
  13. use bizMall\order\services\OrderService;
  14. use bizMall\promote\services\CouponService;
  15. use bizMall\saas\services\RegionService;
  16. use bizMall\shop\classes\ShopExtClass;
  17. use bizMall\user\services\UserService;
  18. use common\components\dateUtil;
  19. use common\components\dict;
  20. use common\components\httpUtil;
  21. use common\components\noticeUtil;
  22. use common\components\orderSn;
  23. use common\components\stringUtil;
  24. use common\services\xhPayToolService;
  25. use Yii;
  26. use common\components\util;
  27. use common\components\lakala\Lakala;
  28. class OrderController extends BaseController
  29. {
  30. //二维码收款使用
  31. public $guestAccess = ['order-relate', 'fast-pay'];
  32. //记账单列表 ssh 20250627
  33. public function actionDebtList()
  34. {
  35. $get = Yii::$app->request->get();
  36. $id = $get['id'] ?? 0;
  37. $where = [];
  38. $where['userId'] = $this->userId;
  39. if (!empty($id)) {
  40. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  41. if (empty($hd)) {
  42. util::fail('无效的花店');
  43. }
  44. $where['hdId'] = $id;
  45. }
  46. $where['debt'] = 1;
  47. $searchTime = $get['searchTime'] ?? '';
  48. if (!empty($searchTime)) {
  49. $startTime = $get['startTime'] ?? '';
  50. $endTime = $get['endTime'] ?? '';
  51. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  52. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  53. }
  54. $respond = OrderClass::getDebtList($where);
  55. util::success($respond);
  56. }
  57. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  58. public function actionFreight()
  59. {
  60. $get = Yii::$app->request->get();
  61. $shop = $this->shop;
  62. $shopLat = isset($shop->lat) ? $shop->lat : '';
  63. $shopLong = isset($shop->long) ? $shop->long : '';
  64. if (empty($shopLat) || empty($shopLong)) {
  65. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  66. }
  67. $userLat = $get['lat'] ?? '';
  68. $userLong = $get['long'] ?? '';
  69. if (empty($userLat) || empty($userLong)) {
  70. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  71. }
  72. $weight = $get['weight'] ?? 0;
  73. if (empty($weight)) {
  74. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  75. }
  76. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  77. util::success($respond);
  78. }
  79. //购买花材 ssh 20220511
  80. public function actionBuyItem()
  81. {
  82. $post = Yii::$app->request->post();
  83. $post['sjId'] = $this->sjId;
  84. $post['shopId'] = $this->shopId;
  85. $post['payWay'] = $this->isWx == true ? 0 : 1;
  86. $post['mainId'] = $this->mainId ?? 0;
  87. $post['userId'] = $this->userId ?? 0;
  88. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  89. $mainId = $this->mainId;
  90. if (getenv('YII_ENV') == 'production') {
  91. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  92. util::fail('暂时无法访问');
  93. }
  94. } else {
  95. if (in_array($mainId, [0, 1])) {
  96. util::fail('暂时无法访问');
  97. }
  98. }
  99. $hdId = $post['hdId'] ?? 0;
  100. $hd = HdClass::getById($hdId, true);
  101. if (empty($hd)) {
  102. util::fail('没有找到花店');
  103. }
  104. if ($hd->shopId != $this->shopId) {
  105. util::fail('不是你的花店');
  106. }
  107. $post['hdName'] = $hd->name ?? '';
  108. $customId = $hd->customId ?? 0;
  109. $custom = CustomClass::getById($customId, true);
  110. if (empty($custom)) {
  111. util::fail('没有找到客户');
  112. }
  113. $customName = $custom->name ?? '';
  114. $post['customId'] = $customId;
  115. $post['customName'] = $customName;
  116. $post['customNamePy'] = stringUtil::py($customName);
  117. $now = time();
  118. //订单30分钟后过期
  119. $expireTime = $now + 1800;
  120. $post['deadline'] = $expireTime;
  121. //商城
  122. $post['fromType'] = dict::getDict('fromType', 'mall');
  123. $productJson = $post['product'] ?? '';
  124. if (empty($productJson) && empty($groupId)) {
  125. util::fail('请选择商品');
  126. }
  127. $productList = json_decode($productJson, true);
  128. if (empty($productList)) {
  129. util::fail('请选择商品');
  130. }
  131. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  132. $productInfoList = productClass::getByIds($ids, null, 'id');
  133. if (!empty($productInfoList)) {
  134. $presellData = [];
  135. $nowTime = strtotime(date("Y-m-d"));
  136. foreach ($productInfoList as $currentInfo) {
  137. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  138. util::fail('只能选择同一家的商品哦');
  139. }
  140. $currentName = $currentInfo['name'] ?? '';
  141. $presell = $currentInfo['presell'] ?? 0;
  142. $presellData[] = $presell;
  143. if ($presell == 1) {
  144. if (isset($post['reachDate']) == false || empty($post['reachDate'])) {
  145. util::fail('请选择配送或取货日期');
  146. }
  147. $sendTimeWant = $post['reachDate'];
  148. if (strtotime($sendTimeWant) < $nowTime) {
  149. util::fail('不能选择过去时间');
  150. }
  151. $hasDateStr = $currentInfo['presellDate'] ?? [];
  152. $hasDate = explode(',', trim($hasDateStr));
  153. if (empty($hasDate)) {
  154. util::fail('预售日期有问题,请提醒门店');
  155. }
  156. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  157. util::fail("有预售花材在{$sendTimeWant}没到货");
  158. }
  159. $post['presell'] = 1;
  160. }
  161. }
  162. if (count($productInfoList) != count($ids)) {
  163. util::fail('存在无效商品');
  164. }
  165. $presellData = array_unique($presellData);
  166. if (count($presellData) > 1) {
  167. util::fail('预售和非预售花材不能一起下单');
  168. }
  169. } else {
  170. util::fail('花材不存在');
  171. }
  172. $connection = Yii::$app->db;
  173. $transaction = $connection->beginTransaction();
  174. try {
  175. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  176. $post['deadline'] = time() + $orderValidTime;
  177. $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  178. $post['needPrint'] = dict::getDict('needPrint', 'need');
  179. $user = $this->user;
  180. $post['bookMobile'] = $user->mobile ?? '';
  181. $post['bookName'] = $user->name ?? '';
  182. $hasPay = 0;
  183. $modifyPrice = 0;
  184. //各个等级对应的price addPrice字段
  185. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  186. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  187. foreach ($productList as $eleKey => $element) {
  188. $level = 0;
  189. $productId = $element['productId'];
  190. $current = $productInfoList[$productId];
  191. if (empty($current)) {
  192. util::fail('有商品信息没有找到');
  193. }
  194. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($current, $level, $priceMap, $addPriceMap);
  195. $num = $element['num'] ?? 0;
  196. $currentTotal = bcmul($price, $num, 2);
  197. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  198. $productList[$eleKey]['unitPrice'] = $price;
  199. }
  200. $post['product'] = $productList;
  201. $post['modifyPrice'] = $modifyPrice;
  202. print_r($post);
  203. die;
  204. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  205. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  206. if (isset($return->shopId)) {
  207. if ($return->shopId == 7610) {
  208. if ($return->actPrice < 200) {
  209. if ($return->sendType == 0) {
  210. util::fail('满200元支持送货上门');
  211. }
  212. }
  213. }
  214. }
  215. if ($return->sendType == 2) {
  216. if (in_array($return->shopId, [2, 726, 1490, 13626, 7856, 7688])) {
  217. util::fail('暂时不能使用跑腿');
  218. }
  219. }
  220. $transaction->commit();
  221. $orderSn = $return->orderSn ?? '';
  222. $orderPrice = $return->orderPrice ?? 0;
  223. $id = $return->id ?? 0;
  224. $getPayType = dict::getDict('getPayType');
  225. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  226. } catch (\Exception $e) {
  227. $transaction->rollBack();
  228. Yii::error("失败原因:" . $e->getMessage());
  229. util::fail('下单失败');
  230. }
  231. }
  232. //商城下单操作 ssh 2019.12.3
  233. public function actionCreateOrder()
  234. {
  235. $post = Yii::$app->request->post();
  236. $goodsId = $post['goodsId'] ?? 0;
  237. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  238. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  239. if (empty($goodsId)) {
  240. util::fail('请选择商品');
  241. }
  242. $goodsInfo = GoodsClass::getById($goodsId, true);
  243. if (empty($goodsInfo)) {
  244. util::fail('没有找到商品');
  245. }
  246. $stock = $goodsInfo->stock ?? 0;
  247. if ($stock <= 0) {
  248. util::fail('库存不足');
  249. }
  250. //事务处理
  251. $connection = Yii::$app->db;
  252. $transaction = $connection->beginTransaction();
  253. try {
  254. $custom = $this->custom;
  255. if (empty($custom)) {
  256. util::fail('个人信息缺失');
  257. }
  258. $customId = $custom->id ?? 0;
  259. $hd = $this->hd;
  260. $post['customId'] = $customId;
  261. $customName = $custom->name ?? '';
  262. $post['customName'] = $customName;
  263. $post['customNamePy'] = stringUtil::py($customName);
  264. $post['hdId'] = $hd->id ?? 0;
  265. $post['hdName'] = $hd->name ?? '';
  266. $user = $this->user;
  267. $post['bookName'] = $user->name ?? '';
  268. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  269. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  270. $post['bookMobile'] = $bookMobile;
  271. $post['payWay'] = 0;
  272. //计算运费
  273. $sendDistance = 0;
  274. $sendCost = 0;
  275. $shop = $this->shop;
  276. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  277. $lat = $post['lat'] ?? '';
  278. $lng = $post['long'] ?? '';
  279. if (empty($lat) || empty($lng)) {
  280. util::fail('请填写收花地址哦');
  281. }
  282. $address = $post['address'] ?? '';
  283. $floor = $post['floor'] ?? '';
  284. $fullAddress = $address . $floor;
  285. $post['fullAddress'] = $fullAddress;
  286. $shopLat = $shop->lat ?? '';
  287. $shopLong = $shop->long ?? '';
  288. if (empty($shopLat) || empty($shopLong)) {
  289. //util::fail('请完成门店地址');
  290. }
  291. //花束重量默认1.5
  292. $weight = 1.5;
  293. $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  294. $sendCost = $disRespond['fee'] ?? 0;
  295. $sendDistance = $disRespond['distance'] ?? 0;
  296. }
  297. $post['sendDistance'] = $sendDistance;
  298. $post['sendCost'] = $sendCost;
  299. $post['sjId'] = $this->sjId;
  300. $post['shopId'] = $this->shopId;
  301. $mainId = $this->mainId;
  302. $post['mainId'] = $mainId;
  303. $post['userId'] = $this->userId;
  304. //计算总金额
  305. $unitPrice = $goodsInfo->price ?? 0;
  306. if ($unitPrice <= 0) {
  307. util::fail('没有价格,无法下单');
  308. }
  309. $goodsPrice = $unitPrice * $goodsNum;
  310. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  311. //非门店订单
  312. $post['store'] = 0;
  313. $sourceType = 2;
  314. $couponId = !empty($post['couponId']) ? $post['couponId'] : 0;
  315. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  316. $actPrice = $discountData['price'];
  317. $post['modifyPrice'] = $actPrice;
  318. $post['discountType'] = $discountData['discountType'];
  319. $post['discountAmount'] = $discountData['discountAmount'];
  320. //非自取订单考虑配送时间
  321. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  322. $sendType = $post['sendType'] ?? 0;
  323. if ($sendType != 1) {
  324. if (empty($post['reachPeriod'])) {
  325. util::fail('请选择配送时间');
  326. }
  327. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  328. }
  329. $now = time();
  330. $expireTime = $now + 1800;//订单30分钟后过期
  331. $post['deadline'] = $expireTime;
  332. $orderSn = orderSn::getOrderSn();
  333. $post['orderSn'] = $orderSn;
  334. $post['fromType'] = dict::getDict('fromType', 'mall');
  335. $post['prePrice'] = $prePrice;
  336. $post['orderPrice'] = $actPrice;
  337. $post['actPrice'] = $actPrice;
  338. $post['realPrice'] = $actPrice;
  339. $post['mainPay'] = $actPrice;
  340. $post['cash'] = 0;
  341. $post['mainId'] = $mainId;
  342. $post['needPrint'] = dict::getDict('needPrint', 'need');
  343. $hasPay = 0;
  344. $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
  345. $post['product'] = $product;
  346. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  347. $orderId = $return->id;
  348. $orderSn = $return->orderSn;
  349. $transaction->commit();
  350. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
  351. } catch (Exception $e) {
  352. $transaction->rollBack();
  353. Yii::info("失败原因:" . $e->getMessage());
  354. util::fail('下单失败');
  355. }
  356. }
  357. //下单要用到的相关信息 ssh 2019.12.6
  358. public function actionOrderRelate()
  359. {
  360. $regionTree = RegionService::tree();
  361. //门店名称
  362. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  363. $sjName = $shop['merchantName'] ?? '';
  364. $shopName = $shop['shopName'] ?? '';
  365. $default = $shop['default'] ?? 0;
  366. $shop['showName'] = $default == 1 && $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  367. $freight = ['first' => 5, 'add' => 2];
  368. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  369. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  370. util::success($out);
  371. }
  372. //余额支付 ssh 20250410
  373. public function actionBalancePay()
  374. {
  375. $post = Yii::$app->request->post();
  376. $orderSn = $post['orderSn'] ?? '';
  377. //避免重复提交
  378. util::checkRepeatCommit($orderSn, 10);
  379. $connection = Yii::$app->db;
  380. $transaction = $connection->beginTransaction();
  381. try {
  382. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  383. if (empty($order)) {
  384. util::fail('没有找到订单');
  385. }
  386. if ($order->shopId != $this->shopId) {
  387. util::fail('不是你的订单');
  388. }
  389. $payWay = dict::getDict('payWay', 'balancePay');
  390. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  391. $transaction->commit();
  392. util::complete('支付成功');
  393. } catch (\Exception $e) {
  394. $transaction->rollBack();
  395. Yii::error("支付原因:" . $e->getMessage());
  396. util::fail('支付失败');
  397. }
  398. }
  399. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  400. public function actionWxPay()
  401. {
  402. ini_set('date.timezone', 'Asia/Shanghai');
  403. $post = Yii::$app->request->post();
  404. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  405. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  406. $order = OrderService::getByOrderSn($orderSn);
  407. $orderId = $order['id'];
  408. //验证优惠券是否还有效
  409. if (!empty($couponId)) {
  410. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  411. }
  412. //支付前验证订单有效性
  413. OrderService::checkBeforePay($order);
  414. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  415. $shop = $this->shop;
  416. if (empty($shop)) {
  417. util::fail('没有找到门店信息');
  418. }
  419. if (isset($shop->shopName) && !empty($shop->shopName)) {
  420. $name .= '(' . $shop->shopName . ')';
  421. }
  422. $totalFee = $order['mainPay'];
  423. $openId = '';
  424. if (!empty($post['miniOpenId'])) {
  425. $openId = $post['miniOpenId'];
  426. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  427. }
  428. if (empty($openId)) {
  429. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  430. $openId = $this->user['miniOpenId'];
  431. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  432. }
  433. }
  434. if (empty($openId)) {
  435. util::fail('没有找到openId');
  436. }
  437. $now = time();
  438. $expireTime = $now + 1800;
  439. $updateData = [];
  440. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  441. $oldOrderSn = $orderSn;
  442. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  443. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  444. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  445. $params = [
  446. 'appid' => 'OP00002119',
  447. 'serial_no' => '018b08cfddbd',
  448. 'merchant_no' => $shop->lklSjNo,
  449. 'term_no' => $shop->lklScanTermNo,
  450. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  451. 'lklCertificatePath' => $lklCertificatePath,
  452. ];
  453. $laResource = new Lakala($params);
  454. $closeParams = [
  455. 'orderSn' => $oldOrderSn,
  456. ];
  457. $response = $laResource->close($closeParams);
  458. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  459. $msg = $response['msg'] ?? '';
  460. $lklSjNo = $shop->lklSjNo ?? '';
  461. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  462. util::fail('出错了哦,请重新下一单');
  463. }
  464. $orderSn = orderSn::getOrderSn();
  465. $updateData['orderSn'] = $orderSn;
  466. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  467. } else {
  468. //已请求的不能再修改价格
  469. $updateData['modPrice'] = 0;
  470. }
  471. if (empty($order['deadline'])) {
  472. $updateData['deadline'] = $expireTime;
  473. }
  474. if (!empty($updateData)) {
  475. OrderService::updateById($orderId, $updateData);
  476. }
  477. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  478. $sjExtend = WxOpenClass::getMallWxInfo();
  479. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  480. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  481. $params = [
  482. 'appid' => 'OP00002119',
  483. 'serial_no' => '018b08cfddbd',
  484. 'merchant_no' => $shop->lklSjNo,
  485. 'term_no' => $shop->lklScanTermNo,
  486. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  487. 'lklCertificatePath' => $lklCertificatePath,
  488. ];
  489. $laResource = new Lakala($params);
  490. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  491. $wxParams = [
  492. 'orderSn' => $orderSn,
  493. 'amount' => $totalFee,
  494. 'capitalType' => $capitalType,
  495. 'notifyUrl' => $notifyUrl,
  496. 'wxAppId' => $sjExtend['miniAppId'],
  497. 'openId' => $openId,
  498. 'subject' => $name,
  499. 'couponId' => $couponId,
  500. ];
  501. $response = $laResource->driveWxPay($wxParams);
  502. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  503. $errMsg = $response['msg'] ?? '';
  504. noticeUtil::push($errMsg, '15280215347');
  505. util::fail('支付失败');
  506. }
  507. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  508. $appId = $newParams['app_id'] ?? '';
  509. $nonceStr = $newParams['nonce_str'] ?? '';
  510. $paySign = $newParams['pay_sign'] ?? '';
  511. $package = $newParams['package'] ?? '';
  512. $signType = $newParams['sign_type'] ?? '';
  513. $timeStamp = $newParams['time_stamp'] ?? '';
  514. $new = [
  515. 'id' => $orderId,
  516. 'appId' => $appId,
  517. 'nonceStr' => $nonceStr,
  518. 'paySign' => $paySign,
  519. 'package' => $package,
  520. 'signType' => $signType,
  521. 'timeStamp' => $timeStamp,
  522. 'orderSn' => $orderSn,
  523. ];
  524. util::success($new);
  525. }
  526. //快捷付款订单
  527. public function actionFastPay()
  528. {
  529. ini_set('date.timezone', 'Asia/Shanghai');
  530. $post = Yii::$app->request->post();
  531. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  532. $shopId = $this->shopId;
  533. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  534. $gatheringItem = !empty($ext) ? $ext->gatheringItem : 0;
  535. if (empty($gatheringItem)) {
  536. util::fail('请设置付款用的商品');
  537. }
  538. $gatheringItemInfo = ItemClass::getById($gatheringItem, true);
  539. if (empty($gatheringItemInfo)) {
  540. util::fail('付款用的商品没有设置');
  541. }
  542. $ptItemId = $gatheringItemInfo->itemId ?? 0;
  543. $classId = $gatheringItemInfo->classId ?? 0;
  544. $customId = $this->customId;
  545. $custom = $this->custom;
  546. $hdId = $this->hdId;
  547. $hd = $this->hd;
  548. if (!empty($customId) && !empty($hdId)) {
  549. //考虑没有登录时的付款
  550. $shop = $this->shop;
  551. $defaultCustomId = $shop->defaultCustomId ?? 0;
  552. if (empty($defaultCustomId)) {
  553. util::fail('请设置快捷开单的客户');
  554. }
  555. $customId = $defaultCustomId;
  556. $custom = CustomClass::getById($defaultCustomId, true);
  557. if (empty($custom)) {
  558. util::fail('请设置快捷客户');
  559. }
  560. $hdId = $custom->hdId;
  561. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  562. if (empty($hd)) {
  563. util::fail('没有花店信息呢!');
  564. }
  565. }
  566. $post['hdId'] = $hdId;
  567. $post['hdName'] = $hd->name ?? '';
  568. $customName = $custom->name ?? '';
  569. $customPy = $custom->py ?? '';
  570. $post['customName'] = $customName;
  571. $post['customNamePy'] = $customPy;
  572. $post['gatheringItem'] = $gatheringItem;
  573. $post['shopId'] = $shopId;
  574. $post['mainId'] = $this->mainId;
  575. $post['sjId'] = $this->sjId;
  576. $post['store'] = 0;
  577. $post['customId'] = $customId;
  578. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  579. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  580. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  581. $bookMobile = empty($bookMobile) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  582. $post['bookMobile'] = $bookMobile;
  583. $prePrice = round($post['prePrice'], 2);
  584. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  585. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  586. $actPrice = $discountData['price'];
  587. $post['discountType'] = $discountData['discountType'];
  588. $post['discountAmount'] = $discountData['discountAmount'];
  589. $post['orderPrice'] = $actPrice;
  590. $post['actPrice'] = $actPrice;
  591. $post['realPrice'] = $actPrice;
  592. $post['mainPay'] = $actPrice;
  593. $post['modifyPrice'] = $actPrice;
  594. $post['userId'] = $this->userId;
  595. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  596. $now = time();
  597. $expireTime = $now + 300;//订单5分钟后过期
  598. $post['createTime'] = date("Y-m-d H:i:s", $now);
  599. $post['deadline'] = $expireTime;
  600. if ($actPrice <= 0) {
  601. util::fail('请填写付款金额');
  602. }
  603. $post['goodsNum'] = 1;
  604. $product = [[
  605. 'productId' => $gatheringItem,
  606. 'bigNum' => 1,
  607. 'smallNum' => '',
  608. 'num' => 1,
  609. 'unitType' => 0,
  610. 'property' => 1,
  611. 'unitPrice' => $actPrice,
  612. 'itemId' => $classId,
  613. 'classId' => $classId
  614. ]];
  615. $post['product'] = $product;
  616. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  617. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  618. //前端未修改时这边做兼容
  619. if (!empty($post['receiveAddress'])) {
  620. $post['address'] = $post['receiveAddress'];
  621. if (!empty($post['receiveProvince'])) {
  622. $post['province'] = $post['receiveProvince'];
  623. }
  624. if (!empty($post['receiveCity'])) {
  625. $post['city'] = $post['receiveCity'];
  626. }
  627. if (!empty($post['receiveFloor'])) {
  628. $post['floor'] = $post['receiveFloor'];
  629. }
  630. if (!empty($post['latitude'])) {
  631. $post['lat'] = $post['latitude'];
  632. }
  633. if (!empty($post['longitude'])) {
  634. $post['long'] = $post['longitude'];
  635. }
  636. $city = $post['receiveCity'] ?? '';
  637. $address = $post['receiveAddress'];
  638. $floor = $post['receiveFloor'] ?? '';
  639. $post['fullAddress'] = $city . $address . $floor;
  640. }
  641. $hasPay = 0;
  642. $post['sendType'] = 1;
  643. $post['orderType'] = 2;//花材订单
  644. $post['dealPrice'] = 1;
  645. $post['fromType'] = 1;
  646. $order = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  647. $orderId = $order->id ?? 0;
  648. $orderSn = $order->orderSn ?? '';
  649. if ($payWay == 0) {
  650. //微信
  651. $name = '购买商品';
  652. $totalFee = $actPrice;
  653. $user = UserService::getById($this->userId);
  654. //小程序使用miniOpenId
  655. $openId = $user['miniOpenId'];
  656. if (empty($openId)) {
  657. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  658. }
  659. if (empty($openId)) {
  660. util::fail('没有微信信息');
  661. }
  662. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  663. $sjExtend = WxOpenClass::getMallWxInfo();
  664. $shop = $this->shop;
  665. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  666. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  667. $params = [
  668. 'appid' => 'OP00002119',
  669. 'serial_no' => '018b08cfddbd',
  670. 'merchant_no' => $shop->lklSjNo,
  671. 'term_no' => $shop->lklScanTermNo,
  672. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  673. 'lklCertificatePath' => $lklCertificatePath,
  674. ];
  675. $laResource = new Lakala($params);
  676. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  677. $wxParams = [
  678. 'orderSn' => $orderSn,
  679. 'amount' => $totalFee,
  680. 'capitalType' => $capitalType,
  681. 'notifyUrl' => $notifyUrl,
  682. 'wxAppId' => $sjExtend['miniAppId'],
  683. 'openId' => $openId,
  684. 'subject' => $name,
  685. 'couponId' => $couponId,
  686. ];
  687. $response = $laResource->driveWxPay($wxParams);
  688. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  689. $errMsg = $response['msg'] ?? '';
  690. noticeUtil::push($errMsg, '15280215347');
  691. util::fail('支付失败');
  692. }
  693. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  694. $appId = $newParams['app_id'] ?? '';
  695. $nonceStr = $newParams['nonce_str'] ?? '';
  696. $paySign = $newParams['pay_sign'] ?? '';
  697. $package = $newParams['package'] ?? '';
  698. $signType = $newParams['sign_type'] ?? '';
  699. $timeStamp = $newParams['time_stamp'] ?? '';
  700. $new = [
  701. 'id' => $orderId,
  702. 'appId' => $appId,
  703. 'nonceStr' => $nonceStr,
  704. 'paySign' => $paySign,
  705. 'package' => $package,
  706. 'signType' => $signType,
  707. 'timeStamp' => $timeStamp,
  708. 'orderSn' => $orderSn,
  709. ];
  710. util::success($new);
  711. } elseif ($payWay == 1) {
  712. //支付宝
  713. $totalFee = $actPrice;
  714. $subject = '购买花材';
  715. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  716. $shop = $this->shop;
  717. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  718. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  719. $params = [
  720. 'appid' => 'OP00002119',
  721. 'serial_no' => '018b08cfddbd',
  722. 'merchant_no' => $shop->lklSjNo,
  723. 'term_no' => $shop->lklB2BTermNo,
  724. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  725. 'lklCertificatePath' => $lklCertificatePath,
  726. ];
  727. $laResource = new Lakala($params);
  728. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  729. $cashParams = [
  730. 'orderSn' => $orderSn,
  731. 'amount' => $totalFee,
  732. 'capitalType' => $capitalType,
  733. 'notifyUrl' => $notifyUrl,
  734. 'subject' => $subject,
  735. ];
  736. $response = $laResource->cashierPay($cashParams);
  737. if (!isset($response['code']) || $response['code'] != '000000') {
  738. util::fail('支付失败了');
  739. }
  740. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  741. if (empty($url)) {
  742. util::fail('支付失败');
  743. }
  744. util::success(['url' => $url]);
  745. } elseif ($payWay == 2) {
  746. util::fail('无效的支付方式');
  747. //余额支付,验证支付密码是否正确
  748. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  749. $user = UserService::getUserInfo($this->userId);
  750. UserService::validPayPassword($payPassword, $user);
  751. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  752. $callbackParams = [];
  753. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  754. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  755. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  756. } else {
  757. util::fail('无效的支付方式');
  758. }
  759. }
  760. //订单列表 ssh 2019.12.12
  761. public function actionList()
  762. {
  763. $get = Yii::$app->request->get();
  764. $status = $get['status'] ?? 0;
  765. $userId = $this->userId;
  766. $where = ['userId' => $userId];
  767. if (!empty($status)) {
  768. $where['status'] = $status;
  769. }
  770. $list = OrderService::getOrderList($where);
  771. util::success($list);
  772. }
  773. //订单评价
  774. public function actionComment()
  775. {
  776. $post = Yii::$app->request->post();
  777. $id = $post['id'];
  778. $order = OrderClass::getById($id);
  779. if ($order['grade'] > 0) {
  780. util::fail('您已经评过了');
  781. }
  782. OrderService::comment($post);
  783. util::complete('提交成功');
  784. }
  785. //订单详情 ssh 2019.12.16
  786. public function actionDetail()
  787. {
  788. $id = Yii::$app->request->get('id', 0);
  789. $detail = OrderClass::getOrderById($id);
  790. $userId = $this->userId;
  791. if ($detail['userId'] != $userId) {
  792. util::fail('不是你的单,不能查看');
  793. }
  794. $shOrderMap = [];
  795. if ($detail['hasForward'] == 1) {
  796. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  797. }
  798. $detail['shOrderMap'] = $shOrderMap;
  799. util::success($detail);
  800. }
  801. //获取详情 ssh 20220512
  802. public function actionInfo()
  803. {
  804. $sn = Yii::$app->request->get('orderSn', '');
  805. $detail = OrderClass::getOrderBySn($sn);
  806. util::success($detail);
  807. }
  808. }