Checkout.php 31 KB

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