Checkout.php 7.0 KB

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