Checkout.php 6.9 KB

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