Checkout.php 6.8 KB

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