Checkout.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\index\service\order;
  13. use app\index\model\Order as OrderModel;
  14. use app\index\model\User as UserModel;
  15. use app\index\model\Goods as GoodsModel;
  16. use app\index\model\Setting as SettingModel;
  17. use app\index\model\UserAddress;
  18. use app\index\model\UserCoupon as UserCouponModel;
  19. use app\index\service\User as UserService;
  20. use app\index\service\Payment as PaymentService;
  21. use app\index\service\user\Grade as UserGradeService;
  22. use app\index\service\coupon\GoodsDeduct as GoodsDeductService;
  23. use app\index\service\points\GoodsDeduct as PointsDeductService;
  24. use app\index\service\order\source\checkout\Factory as CheckoutFactory;
  25. use app\common\enum\order\PayStatus as PayStatusEnum;
  26. use app\common\enum\Setting as SettingEnum;
  27. use app\common\enum\order\PayType as OrderPayTypeEnum;
  28. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  29. use app\common\enum\order\OrderSource as OrderSourceEnum;
  30. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  31. use app\common\service\BaseService;
  32. use app\common\service\delivery\Express as ExpressService;
  33. use app\common\service\goods\source\Factory as StockFactory;
  34. use app\common\library\helper;
  35. use cores\exception\BaseException;
  36. /**
  37. * 订单结算台服务类
  38. * Class Checkout
  39. * @package app\api\service\order
  40. */
  41. class Checkout extends BaseService
  42. {
  43. /* $model OrderModel 订单模型 */
  44. public $model;
  45. /* @var UserModel $user 当前用户信息 */
  46. private $user;
  47. // 订单结算商品列表
  48. private $goodsList = [];
  49. /**
  50. * 订单结算api参数
  51. * @var array
  52. */
  53. private $param = [
  54. 'delivery' => null, // 配送方式
  55. 'couponId' => 0, // 用户的优惠券ID
  56. 'isUsePoints' => 0, // 是否使用积分抵扣
  57. 'remark' => '', // 买家留言
  58. 'payType' => OrderPayTypeEnum::BALANCE, // 支付方式
  59. ];
  60. /**
  61. * 订单结算的规则
  62. * @var array
  63. */
  64. private $checkoutRule = [
  65. 'isUserGrade' => true, // 会员等级折扣
  66. 'isCoupon' => true, // 优惠券抵扣
  67. 'isUsePoints' => true, // 是否使用积分抵扣
  68. ];
  69. /**
  70. * 订单来源
  71. * @var array
  72. */
  73. private $orderSource = [
  74. 'source' => OrderSourceEnum::MAIN,
  75. 'source_id' => 0,
  76. ];
  77. /**
  78. * 订单结算数据
  79. * @var array
  80. */
  81. private $orderData = [];
  82. /**
  83. * 构造函数
  84. * Checkout constructor.
  85. * @throws BaseException
  86. */
  87. public function __construct()
  88. {
  89. parent::__construct();
  90. $this->user = UserService::getCurrentLoginUser();
  91. $this->model = new OrderModel;
  92. $this->storeId = $this->getStoreId();
  93. }
  94. /**
  95. * 设置结算台请求的参数
  96. * @param $param
  97. * @return array
  98. */
  99. public function setParam($param): array
  100. {
  101. $this->param = array_merge($this->param, $param);
  102. return $this->getParam();
  103. }
  104. /**
  105. * 获取结算台请求的参数
  106. * @return array
  107. */
  108. public function getParam(): array
  109. {
  110. return $this->param;
  111. }
  112. /**
  113. * 订单结算的规则
  114. * @param $data
  115. * @return $this
  116. */
  117. public function setCheckoutRule($data): Checkout
  118. {
  119. $this->checkoutRule = array_merge($this->checkoutRule, $data);
  120. return $this;
  121. }
  122. /**
  123. * 设置订单来源(普通订单)
  124. * @param $data
  125. * @return $this
  126. */
  127. public function setOrderSource($data): Checkout
  128. {
  129. $this->orderSource = array_merge($this->orderSource, $data);
  130. return $this;
  131. }
  132. /**
  133. * 订单确认-结算台
  134. * @param $goodsList
  135. * @return array
  136. * @throws BaseException
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public function onCheckout($goodsList): array
  142. {
  143. // 订单确认-立即购买
  144. $this->goodsList = $goodsList;
  145. return $this->checkout();
  146. }
  147. /**
  148. * 订单结算台
  149. * @return array
  150. * @throws BaseException
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. private function checkout(): array
  156. {
  157. // 整理订单数据
  158. $this->orderData = $this->getOrderData();
  159. // 验证商品状态, 是否允许购买
  160. $this->validateGoodsList();
  161. // 订单商品总数量
  162. $orderTotalNum = (int)helper::getArrayColumnSum($this->goodsList, 'total_num');
  163. // 设置订单商品会员折扣价
  164. $this->setOrderGoodsGradeMoney();
  165. // 设置订单商品总金额(不含优惠折扣)
  166. $this->setOrderTotalPrice();
  167. // 当前用户可用的优惠券列表
  168. $couponList = $this->getUserCouponList((float)$this->orderData['orderTotalPrice']);
  169. // 计算优惠券抵扣
  170. $this->setOrderCouponMoney($couponList, (int)$this->param['couponId']);
  171. // 计算可用积分抵扣
  172. $this->setOrderPoints();
  173. // 计算订单商品的实际付款金额
  174. $this->setOrderGoodsPayPrice();
  175. // 设置默认配送方式
  176. if (!$this->param['delivery']) {
  177. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  178. $this->param['delivery'] = current($deliveryType);
  179. }
  180. // 处理配送方式
  181. if ($this->param['delivery'] == DeliveryTypeEnum::EXPRESS) {
  182. $this->setOrderExpress();
  183. }
  184. // 计算订单最终金额
  185. $this->setOrderPayPrice();
  186. // 计算订单积分赠送数量
  187. $this->setOrderPointsBonus();
  188. // 返回订单数据
  189. return array_merge([
  190. 'goodsList' => $this->goodsList, // 商品信息
  191. 'orderTotalNum' => $orderTotalNum, // 商品总数量
  192. 'couponList' => array_values($couponList), // 优惠券列表
  193. 'hasError' => $this->hasError(),
  194. 'errorMsg' => $this->getError(),
  195. ], $this->orderData);
  196. }
  197. /**
  198. * 计算订单可用积分抵扣
  199. * @return void
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\DbException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. */
  204. private function setOrderPoints(): void
  205. {
  206. // 设置默认的商品积分抵扣信息
  207. $this->setDefaultGoodsPoints();
  208. // 积分设置
  209. $setting = SettingModel::getItem('points');
  210. // 条件:后台开启下单使用积分抵扣
  211. if (!$setting['is_shopping_discount'] || !$this->checkoutRule['isUsePoints']) {
  212. return;
  213. }
  214. // 条件:订单金额满足[?]元
  215. if (helper::bccomp($setting['discount']['full_order_price'], $this->orderData['orderTotalPrice']) === 1) {
  216. return;
  217. }
  218. // 计算订单商品最多可抵扣的积分数量
  219. $this->setOrderGoodsMaxPointsNum();
  220. // 订单最多可抵扣的积分总数量
  221. $maxPointsNumCount = (int)helper::getArrayColumnSum($this->goodsList, 'max_points_num');
  222. // 实际可抵扣的积分数量
  223. $actualPointsNum = min($maxPointsNumCount, $this->user['points']);
  224. if ($actualPointsNum < 1) {
  225. return;
  226. }
  227. // 计算订单商品实际抵扣的积分数量和金额
  228. $GoodsDeduct = new PointsDeductService($this->goodsList);
  229. $GoodsDeduct->setGoodsPoints($maxPointsNumCount, $actualPointsNum);
  230. // 积分抵扣总金额
  231. $orderPointsMoney = helper::getArrayColumnSum($this->goodsList, 'points_money');
  232. $this->orderData['pointsMoney'] = helper::number2($orderPointsMoney);
  233. // 积分抵扣总数量
  234. $this->orderData['pointsNum'] = $actualPointsNum;
  235. // 允许积分抵扣
  236. $this->orderData['isAllowPoints'] = true;
  237. }
  238. /**
  239. * 计算订单商品最多可抵扣的积分数量
  240. * @return void
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. private function setOrderGoodsMaxPointsNum(): void
  246. {
  247. // 积分设置
  248. $setting = SettingModel::getItem('points');
  249. foreach ($this->goodsList as &$goods) {
  250. // 商品不允许积分抵扣
  251. if (!$goods['is_points_discount']) continue;
  252. // 积分抵扣比例
  253. $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100);
  254. // 最多可抵扣的金额
  255. $totalPayPrice = helper::bcsub($goods['total_price'], $goods['coupon_money']);
  256. $maxPointsMoney = helper::bcmul($totalPayPrice, $deductionRatio);
  257. // 最多可抵扣的积分数量
  258. $goods['max_points_num'] = helper::bcdiv($maxPointsMoney, $setting['discount']['discount_ratio'], 0);
  259. }
  260. }
  261. /**
  262. * 设置默认的商品积分抵扣信息
  263. * @return void
  264. */
  265. private function setDefaultGoodsPoints(): void
  266. {
  267. foreach ($this->goodsList as &$goods) {
  268. // 最多可抵扣的积分数量
  269. $goods['max_points_num'] = 0;
  270. // 实际抵扣的积分数量
  271. $goods['pointsNum'] = 0;
  272. // 实际抵扣的金额
  273. $goods['points_money'] = 0.00;
  274. }
  275. }
  276. /**
  277. * 整理订单数据(结算台初始化)
  278. * @return array
  279. * @throws \think\db\exception\DataNotFoundException
  280. * @throws \think\db\exception\DbException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. */
  283. private function getOrderData(): array
  284. {
  285. // 系统支持的配送方式 (后台设置)
  286. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  287. if (!empty($this->param['address_id'])){
  288. $addr = UserAddress::detail(intval($this->param['address_id']));
  289. }else{
  290. $addr = $this->user['address_default'];
  291. }
  292. return [
  293. // 当前配送类型
  294. 'delivery' => $this->param['delivery'] > 0 ? $this->param['delivery'] : $deliveryType[0],
  295. // 默认地址
  296. 'address' => $addr,//$this->user['address_default'],
  297. // 是否存在收货地址
  298. 'existAddress' => true,//$this->user['address_id'] > 0
  299. // 配送费用
  300. 'expressPrice' => 0.00,
  301. // 当前用户收货城市是否存在配送规则中
  302. 'isIntraRegion' => true,
  303. // 是否允许使用积分抵扣
  304. 'isAllowPoints' => false,
  305. // 是否使用积分抵扣
  306. 'isUsePoints' => $this->param['isUsePoints'],
  307. // 积分抵扣金额
  308. 'pointsMoney' => 0.00,
  309. // 赠送的积分数量
  310. 'pointsBonus' => 0,
  311. // 支付方式
  312. 'payType' => $this->param['payType'],
  313. // 系统设置 TODO: 废弃
  314. 'setting' => $this->getSetting(),
  315. ];
  316. }
  317. /**
  318. * 获取订单页面中使用到的系统设置
  319. * @return array
  320. * @throws \think\db\exception\DataNotFoundException
  321. * @throws \think\db\exception\DbException
  322. * @throws \think\db\exception\ModelNotFoundException
  323. */
  324. public function getSetting(): array
  325. {
  326. // 系统支持的配送方式 (后台设置)
  327. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  328. // 积分设置
  329. $pointsSetting = SettingModel::getItem(SettingEnum::POINTS);
  330. return [
  331. 'deliveryType' => $deliveryType, // 支持的配送方式
  332. 'points_name' => $pointsSetting['points_name'], // 积分名称
  333. 'points_describe' => $pointsSetting['describe'], // 积分说明
  334. ];
  335. }
  336. // 获取订单结算时的个人信息
  337. public function getPersonal(): array
  338. {
  339. return [
  340. 'user_id' => $this->user['user_id'],
  341. 'balance' => $this->user['balance'],
  342. 'points' => $this->user['points'],
  343. 'address_id' => $this->user['address_id'],
  344. ];
  345. }
  346. /**
  347. * 当前用户可用的优惠券列表
  348. * @param float $orderTotalPrice 总金额
  349. * @return array|mixed
  350. * @throws \think\db\exception\DbException
  351. */
  352. private function getUserCouponList(float $orderTotalPrice)
  353. {
  354. // 是否开启优惠券折扣
  355. if (!$this->checkoutRule['isCoupon']) {
  356. return [];
  357. }
  358. // 整理当前订单所有商品ID集
  359. $orderGoodsIds = helper::getArrayColumn($this->goodsList, 'goods_id');
  360. // 当前用户可用的优惠券列表
  361. $couponList = UserCouponModel::getUserCouponList($this->user['user_id'], $orderTotalPrice);
  362. // 判断当前优惠券是否满足订单使用条件 (优惠券适用范围)
  363. return UserCouponModel::couponListApplyRange($couponList, $orderGoodsIds);
  364. }
  365. /**
  366. * 验证订单商品的状态
  367. * @return void
  368. */
  369. private function validateGoodsList(): void
  370. {
  371. $Checkout = CheckoutFactory::getFactory(
  372. $this->user,
  373. $this->goodsList,
  374. $this->orderSource['source']
  375. );
  376. $status = $Checkout->validateGoodsList();
  377. $status == false && $this->setError($Checkout->getError());
  378. }
  379. /**
  380. * 设置订单的商品总金额(不含优惠折扣)
  381. */
  382. private function setOrderTotalPrice()
  383. {
  384. // 订单商品的总金额(不含优惠券折扣)
  385. $this->orderData['orderTotalPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_price'));
  386. }
  387. /**
  388. * 设置订单的实际支付金额(含配送费)
  389. */
  390. private function setOrderPayPrice()
  391. {
  392. // 订单金额(含优惠折扣)
  393. $this->orderData['orderPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_pay_price'));
  394. // 订单实付款金额(订单金额 + 运费)
  395. $this->orderData['orderPayPrice'] = helper::number2(helper::bcadd($this->orderData['orderPrice'], $this->orderData['expressPrice']));
  396. }
  397. /**
  398. * 计算订单积分赠送数量
  399. * @return void
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\DbException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. */
  404. private function setOrderPointsBonus(): void
  405. {
  406. // 初始化商品积分赠送数量
  407. foreach ($this->goodsList as &$goods) {
  408. $goods['points_bonus'] = 0;
  409. }
  410. // 积分设置
  411. $setting = SettingModel::getItem('points');
  412. // 条件:后台开启开启购物送积分
  413. if (!$setting['is_shopping_gift']) {
  414. return;
  415. }
  416. // 设置商品积分赠送数量
  417. foreach ($this->goodsList as &$goods) {
  418. // 积分赠送比例
  419. $ratio = helper::bcdiv($setting['gift_ratio'], 100);
  420. // 计算抵扣积分数量
  421. $goods['points_bonus'] = !$goods['is_points_gift'] ? 0 : helper::bcmul($goods['total_pay_price'], $ratio, 0);
  422. }
  423. // 订单积分赠送数量
  424. $this->orderData['pointsBonus'] = (int)helper::getArrayColumnSum($this->goodsList, 'points_bonus');
  425. }
  426. /**
  427. * 计算订单商品的实际付款金额
  428. * @return void
  429. */
  430. private function setOrderGoodsPayPrice(): void
  431. {
  432. // 商品总价 - 优惠抵扣
  433. foreach ($this->goodsList as &$goods) {
  434. // 减去优惠券抵扣金额
  435. $value = helper::bcsub($goods['total_price'], $goods['coupon_money']);
  436. // 减去积分抵扣金额
  437. if ($this->orderData['isAllowPoints'] && $this->orderData['isUsePoints']) {
  438. $value = helper::bcsub($value, $goods['points_money']);
  439. }
  440. $goods['total_pay_price'] = helper::number2($value);
  441. }
  442. }
  443. /**
  444. * 设置订单商品会员折扣价
  445. * @return void
  446. * @throws BaseException
  447. */
  448. private function setOrderGoodsGradeMoney(): void
  449. {
  450. // 设置默认数据
  451. helper::setDataAttribute($this->goodsList, [
  452. // 标记参与会员折扣
  453. 'is_user_grade' => false,
  454. // 会员等级抵扣的金额
  455. 'grade_ratio' => 0,
  456. // 会员折扣的商品单价
  457. 'grade_goods_price' => 0.00,
  458. // 会员折扣的总额差
  459. 'grade_total_money' => 0.00,
  460. ], true);
  461. // 是否开启会员等级折扣
  462. if (!$this->checkoutRule['isUserGrade']) {
  463. return;
  464. }
  465. // 获取当前登录用户的会员等级信息
  466. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  467. // 判断商品是否参与会员折扣
  468. if (empty($gradeInfo)) {
  469. return;
  470. }
  471. // 计算抵扣金额
  472. foreach ($this->goodsList as &$goods) {
  473. // 判断商品是否参与会员折扣
  474. if (!$goods['is_enable_grade']) {
  475. continue;
  476. }
  477. // 折扣比例
  478. $discountRatio = $gradeInfo['equity']['discount'];
  479. // 商品单独设置了会员折扣
  480. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$this->user['grade_id']])) {
  481. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  482. }
  483. if (empty($discountRatio)) {
  484. continue;
  485. }
  486. // 会员折扣后的商品总金额
  487. $gradeTotalPrice = UserGradeService::getDiscountPrice($goods['total_price'], $discountRatio);
  488. helper::setDataAttribute($goods, [
  489. 'is_user_grade' => true,
  490. 'grade_ratio' => $discountRatio,
  491. 'grade_goods_price' => UserGradeService::getDiscountPrice($goods['goods_price'], $discountRatio),
  492. 'grade_total_money' => helper::bcsub($goods['total_price'], $gradeTotalPrice),
  493. 'total_price' => $gradeTotalPrice,
  494. ], false);
  495. }
  496. }
  497. /**
  498. * 设置订单优惠券抵扣信息
  499. * @param array $couponList 当前用户可用的优惠券列表
  500. * @param int $userCouponId 当前选择的优惠券ID
  501. * @return void
  502. * @throws BaseException
  503. */
  504. private function setOrderCouponMoney(array $couponList, int $userCouponId): void
  505. {
  506. // 设置默认数据:订单信息
  507. helper::setDataAttribute($this->orderData, [
  508. 'couponId' => 0, // 用户的优惠券ID
  509. 'couponMoney' => 0, // 优惠券抵扣金额
  510. ], false);
  511. // 设置默认数据:订单商品列表
  512. helper::setDataAttribute($this->goodsList, [
  513. 'coupon_money' => 0, // 优惠券抵扣金额
  514. ], true);
  515. // 验证选择的优惠券ID是否合法
  516. if (!$this->verifyOrderCouponId($userCouponId, $couponList)) {
  517. return;
  518. }
  519. // 获取优惠券信息
  520. $couponInfo = $this->getCouponInfo($userCouponId, $couponList);
  521. // 计算订单商品优惠券抵扣金额
  522. $goodsListTemp = helper::getArrayColumns($this->goodsList, ['goods_id', 'goods_sku_id', 'total_price']);
  523. $CouponMoney = new GoodsDeductService;
  524. $rangeGoodsList = $CouponMoney->setGoodsList($goodsListTemp)
  525. ->setCouponInfo($couponInfo)
  526. ->setGoodsCouponMoney()
  527. ->getRangeGoodsList();
  528. // 分配订单商品优惠券抵扣金额
  529. foreach ($this->goodsList as &$goods) {
  530. $goodsKey = "{$goods['goods_id']}-{$goods['goods_sku_id']}";
  531. if (isset($rangeGoodsList[$goodsKey])) {
  532. $goods['coupon_money'] = helper::bcdiv($rangeGoodsList[$goodsKey]['coupon_money'], 100);
  533. }
  534. }
  535. // 记录订单优惠券信息
  536. $this->orderData['couponId'] = $userCouponId;
  537. $this->orderData['couponMoney'] = helper::number2(helper::bcdiv($CouponMoney->getActualReducedMoney(), 100));
  538. }
  539. /**
  540. * 验证用户选择的优惠券ID是否合法
  541. * @param int $userCouponId
  542. * @param $couponList
  543. * @return bool
  544. * @throws BaseException
  545. */
  546. private function verifyOrderCouponId(int $userCouponId, $couponList): bool
  547. {
  548. // 是否开启优惠券折扣
  549. if (!$this->checkoutRule['isCoupon']) {
  550. return false;
  551. }
  552. // 如果没有可用的优惠券,直接返回
  553. if ($userCouponId <= 0 || empty($couponList)) {
  554. return false;
  555. }
  556. // 判断优惠券是否存在
  557. $couponInfo = $this->getCouponInfo($userCouponId, $couponList);
  558. if (!$couponInfo) {
  559. throwError('未找到优惠券信息');
  560. }
  561. // 判断优惠券适用范围是否合法
  562. if (!$couponInfo['is_apply']) {
  563. throwError($couponInfo['not_apply_info']);
  564. }
  565. return true;
  566. }
  567. /**
  568. * 查找指定的优惠券信息
  569. * @param int $userCouponId 优惠券ID
  570. * @param array $couponList 优惠券列表
  571. * @return false|mixed
  572. */
  573. private function getCouponInfo(int $userCouponId, array $couponList)
  574. {
  575. return helper::getArrayItemByColumn($couponList, 'user_coupon_id', $userCouponId);
  576. }
  577. /**
  578. * 订单配送-快递配送
  579. * @return void
  580. * @throws \think\db\exception\DataNotFoundException
  581. * @throws \think\db\exception\DbException
  582. * @throws \think\db\exception\ModelNotFoundException
  583. */
  584. private function setOrderExpress(): void
  585. {
  586. // 设置默认数据:配送费用
  587. helper::setDataAttribute($this->goodsList, [
  588. 'expressPrice' => 0,
  589. ], true);
  590. // 当前用户收货城市id
  591. $cityId = $this->user['address_default'] ? (int)$this->user['address_default']['city_id'] : 0;
  592. // 初始化配送服务类
  593. $ExpressService = new ExpressService($cityId, $this->goodsList);
  594. // 验证商品是否在配送范围
  595. $isIntraRegion = $ExpressService->isIntraRegion();
  596. if ($cityId > 0 && $isIntraRegion == false) {
  597. $notInRuleGoodsName = $ExpressService->getNotInRuleGoodsName();
  598. $this->setError("很抱歉,您的收货地址不在商品 [{$notInRuleGoodsName}] 的配送范围内");
  599. }
  600. // 订单总运费金额
  601. $this->orderData['isIntraRegion'] = $isIntraRegion;
  602. $this->orderData['expressPrice'] = $ExpressService->getDeliveryFee();
  603. }
  604. /**
  605. * 创建新订单
  606. * @param array $order 订单信息
  607. * @return bool
  608. */
  609. public function createOrder(array $order): bool
  610. {
  611. // 表单验证
  612. if (!$this->validateOrderForm($order)) {
  613. return false;
  614. }
  615. // 创建新的订单
  616. $status = $this->model->transaction(function () use ($order) {
  617. // 创建订单事件
  618. return $this->createOrderEvent($order);
  619. });
  620. // 余额支付标记订单已支付
  621. if ($status && $order['payType'] == OrderPayTypeEnum::BALANCE) {
  622. return $this->model->onPaymentByBalance($this->model['order_no']);
  623. }
  624. return $status;
  625. }
  626. /**
  627. * 创建订单事件
  628. * @param $order
  629. * @return bool
  630. * @throws BaseException
  631. * @throws \think\db\exception\DataNotFoundException
  632. * @throws \think\db\exception\DbException
  633. * @throws \think\db\exception\ModelNotFoundException
  634. */
  635. private function createOrderEvent($order): bool
  636. {
  637. // 新增订单记录
  638. $status = $this->add($order, $this->param['remark']);
  639. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  640. // 记录收货地址
  641. $this->saveOrderAddress($order['address']);
  642. }
  643. // 保存订单商品信息
  644. $this->saveOrderGoods($order);
  645. // 更新商品库存 (针对下单减库存的商品)
  646. $this->updateGoodsStockNum($order);
  647. // 设置优惠券使用状态
  648. $order['couponId'] > 0 && UserCouponModel::setIsUse((int)$order['couponId']);
  649. // 积分抵扣情况下扣除用户积分
  650. if ($order['isAllowPoints'] && $order['isUsePoints'] && $order['pointsNum'] > 0) {
  651. $describe = "用户消费:{$this->model['order_no']}";
  652. UserModel::setIncPoints($this->user['user_id'], -$order['pointsNum'], $describe);
  653. }
  654. // 获取订单详情
  655. $detail = OrderModel::getUserOrderDetail((int)$this->model['order_id']);
  656. return $status;
  657. }
  658. /**
  659. * 构建支付请求的参数
  660. * @return array
  661. * @throws BaseException
  662. * @throws \think\db\exception\DataNotFoundException
  663. * @throws \think\db\exception\DbException
  664. * @throws \think\db\exception\ModelNotFoundException
  665. */
  666. public function onOrderPayment(): array
  667. {
  668. return PaymentService::orderPayment($this->model, $this->param['payType']);
  669. }
  670. /**
  671. * 表单验证 (订单提交)
  672. * @param array $order 订单信息
  673. * @return bool
  674. */
  675. private function validateOrderForm(array $order): bool
  676. {
  677. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  678. if (empty($order['address'])) {
  679. $this->error = '您还没有选择配送地址';
  680. return false;
  681. }
  682. }
  683. // 余额支付时判断用户余额是否足够
  684. if ($order['payType'] == OrderPayTypeEnum::BALANCE) {
  685. if ($this->user['balance'] < $order['orderPayPrice']) {
  686. $this->error = '您的余额不足,无法使用余额支付';
  687. return false;
  688. }
  689. }
  690. return true;
  691. }
  692. /**
  693. * 当前订单是否存在和使用积分抵扣
  694. * @param $order
  695. * @return bool
  696. */
  697. private function isExistPointsDeduction($order): bool
  698. {
  699. return $order['isAllowPoints'] && $order['isUsePoints'];
  700. }
  701. /**
  702. * 新增订单记录
  703. * @param $order
  704. * @param string $remark
  705. * @return bool|false
  706. */
  707. private function add($order, string $remark = ''): bool
  708. {
  709. // 当前订单是否存在和使用积分抵扣
  710. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  711. // 订单数据
  712. $data = [
  713. 'user_id' => $this->user['user_id'],
  714. 'order_no' => $this->model->orderNo(),
  715. 'total_price' => $order['orderTotalPrice'],
  716. 'order_price' => $order['orderPrice'],
  717. 'coupon_id' => $order['couponId'],
  718. 'coupon_money' => $order['couponMoney'],
  719. 'points_money' => $isExistPointsDeduction ? $order['pointsMoney'] : 0.00,
  720. 'points_num' => $isExistPointsDeduction ? $order['pointsNum'] : 0,
  721. 'pay_price' => $order['orderPayPrice'],
  722. 'delivery_type' => $order['delivery'],
  723. 'pay_type' => $order['payType'],
  724. 'pay_status' => PayStatusEnum::PENDING,
  725. 'buyer_remark' => trim($remark),
  726. 'order_source' => $this->orderSource['source'],
  727. 'order_source_id' => $this->orderSource['source_id'],
  728. 'points_bonus' => $order['pointsBonus'],
  729. 'order_status' => OrderStatusEnum::NORMAL,
  730. 'platform' => getPlatform(),
  731. 'store_id' => $this->storeId,
  732. ];
  733. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  734. $data['express_price'] = $order['expressPrice'];
  735. }
  736. // 保存订单记录
  737. return $this->model->save($data);
  738. }
  739. /**
  740. * 保存订单商品信息
  741. * @param $order
  742. * @return void
  743. */
  744. private function saveOrderGoods($order): void
  745. {
  746. // 当前订单是否存在和使用积分抵扣
  747. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  748. // 订单商品列表
  749. $goodsList = [];
  750. foreach ($order['goodsList'] as $goods) {
  751. /* @var GoodsModel $goods */
  752. $item = [
  753. 'user_id' => $this->user['user_id'],
  754. 'store_id' => $this->storeId,
  755. 'goods_id' => $goods['goods_id'],
  756. 'goods_name' => $goods['goods_name'],
  757. 'goods_no' => $goods['goods_no'] ?: '',
  758. 'image_id' => (int)current($goods['goods_images'])['file_id'],
  759. 'deduct_stock_type' => $goods['deduct_stock_type'],
  760. 'spec_type' => $goods['spec_type'],
  761. 'goods_sku_id' => $goods['skuInfo']['goods_sku_id'],
  762. 'goods_props' => $goods['skuInfo']['goods_props'] ?: '',
  763. 'content' => $goods['content'] ?? '',
  764. 'goods_sku_no' => $goods['skuInfo']['goods_sku_no'] ?: '',
  765. 'goods_price' => $goods['skuInfo']['goods_price'],
  766. 'line_price' => $goods['skuInfo']['line_price'],
  767. 'goods_weight' => $goods['skuInfo']['goods_weight'],
  768. 'is_user_grade' => (int)$goods['is_user_grade'],
  769. 'grade_ratio' => $goods['grade_ratio'],
  770. 'grade_goods_price' => $goods['grade_goods_price'],
  771. 'grade_total_money' => $goods['grade_total_money'],
  772. 'coupon_money' => $goods['coupon_money'],
  773. 'points_money' => $isExistPointsDeduction ? $goods['points_money'] : 0.00,
  774. 'points_num' => $isExistPointsDeduction ? $goods['points_num'] : 0,
  775. 'points_bonus' => $goods['points_bonus'],
  776. 'total_num' => $goods['total_num'],
  777. 'total_price' => $goods['total_price'],
  778. 'total_pay_price' => $goods['total_pay_price']
  779. ];
  780. // 记录订单商品来源id
  781. $item['goods_source_id'] = isset($goods['goods_source_id']) ? $goods['goods_source_id'] : 0;
  782. $goodsList[] = $item;
  783. }
  784. $this->model->goods()->saveAll($goodsList) !== false;
  785. }
  786. /**
  787. * 更新商品库存 (针对下单减库存的商品)
  788. * @param $order
  789. * @return void
  790. */
  791. private function updateGoodsStockNum($order): void
  792. {
  793. StockFactory::getFactory($this->model['order_source'])->updateGoodsStock($order['goodsList']);
  794. }
  795. /**
  796. * 记录收货地址
  797. * @param $address
  798. * @return void
  799. */
  800. private function saveOrderAddress($address): void
  801. {
  802. $this->model->address()->save([
  803. 'user_id' => $this->user['user_id'],
  804. 'store_id' => $this->storeId,
  805. 'name' => $address['name'],
  806. 'last_name' => $address['last_name'],
  807. 'zip_code' => $address['zip_code'],
  808. 'phone' => $address['phone'],
  809. 'province_id' => $address['province_id'],
  810. 'city_id' => $address['city_id'],
  811. 'region_id' => $address['region_id'],
  812. 'detail' => $address['detail'],
  813. ]);
  814. }
  815. }