Checkout.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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\controller;
  13. use app\api\model\Order as OrderModel;
  14. use app\api\service\User as UserService;
  15. use app\api\service\Cart as CartService;
  16. use app\api\service\order\Checkout as CheckoutService;
  17. use app\api\validate\order\Checkout as CheckoutValidate;
  18. use cores\exception\BaseException;
  19. use think\response\Json;
  20. /**
  21. * 订单结算控制器
  22. * Class Checkout
  23. * @package app\api\controller
  24. */
  25. class Checkout extends Controller
  26. {
  27. // 结算台验证器
  28. private ?CheckoutValidate $validate = null;
  29. /**
  30. * 结算台订单信息
  31. * @param string $mode
  32. * @return Json
  33. * @throws BaseException
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function order(string $mode = 'buyNow'): Json
  39. {
  40. if ($mode === 'buyNow') {
  41. return $this->buyNow();
  42. } elseif ($mode === 'cart') {
  43. return $this->cart();
  44. }
  45. return $this->renderError('结算模式不合法');
  46. }
  47. /**
  48. * 订单提交
  49. * @param string $mode
  50. * @return Json
  51. * @throws BaseException
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function submit(string $mode = 'buyNow'): Json
  57. {
  58. return $this->order($mode);
  59. }
  60. /**
  61. * 订单确认-立即购买
  62. * @return Json
  63. * @throws BaseException
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. private function buyNow(): Json
  69. {
  70. // 实例化结算台服务
  71. $Checkout = new CheckoutService;
  72. // 订单结算api参数
  73. $params = $Checkout->setParam($this->getParam([
  74. 'goodsId' => 0,
  75. 'goodsSkuId' => '',
  76. 'goodsNum' => 0,
  77. ]));
  78. // 表单验证
  79. if (!$this->getValidate()->scene('buyNow')->check($params)) {
  80. return $this->renderError($this->getValidate()->getError(), ['isCreated' => false]);
  81. }
  82. // 立即购买:获取订单商品列表
  83. $model = new OrderModel;
  84. $goodsList = $model->getOrderGoodsListByNow(
  85. (int)$params['goodsId'],
  86. (string)$params['goodsSkuId'],
  87. (int)$params['goodsNum']
  88. );
  89. // 获取订单确认信息
  90. $orderInfo = $Checkout->onCheckout($goodsList);
  91. if ($this->request->isGet()) {
  92. return $this->renderSuccess([
  93. 'order' => $orderInfo,
  94. 'personal' => $Checkout->getPersonal(),
  95. 'setting' => $Checkout->getSetting(),
  96. ]);
  97. }
  98. // 验证订单是否存在错误
  99. if ($Checkout->hasError()) {
  100. return $this->renderError($Checkout->getError(), ['is_created' => false]);
  101. }
  102. // 创建订单
  103. if (!$Checkout->createOrder($orderInfo)) {
  104. return $this->renderError($Checkout->getError() ?: '订单创建失败', ['is_created' => false]);
  105. }
  106. // 构建微信支付请求
  107. $payment = $model->onOrderPayment($Checkout->model, $params['payType']);
  108. // 返回结算信息
  109. return $this->renderSuccess([
  110. 'orderId' => $Checkout->model['order_id'], // 订单id
  111. 'payType' => $params['payType'], // 支付方式
  112. 'payment' => $payment // 微信支付参数
  113. ]);
  114. }
  115. /**
  116. * 订单确认-购物车结算
  117. * @return Json
  118. * @throws BaseException
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. private function cart(): Json
  124. {
  125. // 实例化结算台服务
  126. $Checkout = new CheckoutService;
  127. // 订单结算api参数
  128. $params = $Checkout->setParam($this->getParam());
  129. // 购物车ID集
  130. $cartIds = $this->getCartIds();
  131. // 商品结算信息
  132. $CartModel = new CartService;
  133. // 购物车商品列表
  134. $goodsList = $CartModel->getOrderGoodsList($cartIds);
  135. // 获取订单结算信息
  136. $orderInfo = $Checkout->onCheckout($goodsList);
  137. if ($this->request->isGet()) {
  138. return $this->renderSuccess([
  139. 'order' => $orderInfo,
  140. 'personal' => $Checkout->getPersonal(),
  141. 'setting' => $Checkout->getSetting(),
  142. ]);
  143. }
  144. // 验证订单是否存在错误
  145. if ($Checkout->hasError()) {
  146. return $this->renderError($Checkout->getError(), ['is_created' => false]);
  147. }
  148. // 创建订单
  149. if (!$Checkout->createOrder($orderInfo)) {
  150. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  151. }
  152. // 移出购物车中已下单的商品
  153. $CartModel->clear($cartIds);
  154. // 构建微信支付请求
  155. $payment = $Checkout->onOrderPayment();
  156. // 返回状态
  157. return $this->renderSuccess([
  158. 'orderId' => $Checkout->model['order_id'], // 订单id
  159. 'payType' => $params['payType'], // 支付方式
  160. 'payment' => $payment // 微信支付参数
  161. ]);
  162. }
  163. /**
  164. * 获取结算台验证器
  165. * @return CheckoutValidate
  166. */
  167. private function getValidate(): CheckoutValidate
  168. {
  169. if (is_null($this->validate)) {
  170. $this->validate = new CheckoutValidate;
  171. }
  172. return $this->validate;
  173. }
  174. /**
  175. * 获取购物车ID集
  176. * @return false|string[]
  177. */
  178. private function getCartIds()
  179. {
  180. $cartIds = $this->request->param('cartIds');
  181. return explode(',', $cartIds);
  182. }
  183. /**
  184. * 订单结算提交的参数
  185. * @param array $define
  186. * @return array
  187. */
  188. private function getParam(array $define = []): array
  189. {
  190. return array_merge($define, $this->request->param());
  191. }
  192. }