Checkout.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\order\PaySuccess;
  15. use app\api\service\User as UserService;
  16. use app\api\service\Cart as CartService;
  17. use app\api\service\order\Checkout as CheckoutService;
  18. use app\api\validate\order\Checkout as CheckoutValidate;
  19. use app\common\enum\order\PayType as OrderPayTypeEnum;
  20. use app\common\library\paypal\PayPal;
  21. use cores\exception\BaseException;
  22. use think\response\Json;
  23. /**
  24. * 订单结算控制器
  25. * Class Checkout
  26. * @package app\api\controller
  27. */
  28. class Checkout extends Controller
  29. {
  30. // 结算台验证器
  31. /* @var CheckoutValidate $validate */
  32. private $validate;
  33. /**
  34. * 结算台订单信息
  35. * @param string $mode
  36. * @return Json
  37. * @throws BaseException
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function order(string $mode = 'buyNow'): Json
  43. {
  44. if ($mode === 'buyNow') {
  45. return $this->buyNow();
  46. } elseif ($mode === 'cart') {
  47. return $this->cart();
  48. }
  49. return $this->renderError('结算模式不合法');
  50. }
  51. /**
  52. * 订单提交
  53. * @param string $mode
  54. * @return Json
  55. * @throws BaseException
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function submit(string $mode = 'buyNow'): Json
  61. {
  62. return $this->order($mode);
  63. }
  64. /**
  65. * 订单确认-立即购买
  66. * @return Json
  67. * @throws BaseException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. private function buyNow(): Json
  73. {
  74. // 实例化结算台服务
  75. $Checkout = new CheckoutService;
  76. // 订单结算api参数
  77. $params = $Checkout->setParam($this->getParam([
  78. 'goodsId' => 0,
  79. 'goodsSkuId' => '',
  80. 'goodsNum' => 0,
  81. ]));
  82. // 表单验证
  83. if (!$this->getValidate()->scene('buyNow')->check($params)) {
  84. return $this->renderError($this->getValidate()->getError(), ['isCreated' => false]);
  85. }
  86. // 立即购买:获取订单商品列表
  87. $model = new OrderModel;
  88. $goodsList = $model->getOrderGoodsListByNow(
  89. (int)$params['goodsId'],
  90. (string)$params['goodsSkuId'],
  91. (int)$params['goodsNum']
  92. );
  93. // 获取订单确认信息
  94. $orderInfo = $Checkout->onCheckout($goodsList);
  95. if ($this->request->isGet()) {
  96. return $this->renderSuccess([
  97. 'order' => $orderInfo,
  98. 'personal' => $Checkout->getPersonal(),
  99. 'setting' => $Checkout->getSetting(),
  100. ]);
  101. }
  102. // 验证订单是否存在错误
  103. if ($Checkout->hasError()) {
  104. return $this->renderError($Checkout->getError(), ['is_created' => false]);
  105. }
  106. // 创建订单
  107. if (!$Checkout->createOrder($orderInfo)) {
  108. return $this->renderError($Checkout->getError() ?: '订单创建失败', ['is_created' => false]);
  109. }
  110. // 构建微信支付请求
  111. $payment = $model->onOrderPayment($Checkout->model, $params['payType']);
  112. // 返回结算信息
  113. return $this->renderSuccess([
  114. 'orderId' => $Checkout->model['order_id'], // 订单id
  115. 'payType' => $params['payType'], // 支付方式
  116. 'payment' => $payment // 微信支付参数
  117. ]);
  118. }
  119. /**
  120. * 订单确认-购物车结算
  121. * @return Json
  122. * @throws BaseException
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. */
  127. private function cart(): Json
  128. {
  129. // 实例化结算台服务
  130. $Checkout = new CheckoutService;
  131. // 订单结算api参数
  132. $params = $Checkout->setParam($this->getParam());
  133. // 购物车ID集
  134. $cartIds = $this->getCartIds();
  135. // 商品结算信息
  136. $CartModel = new CartService;
  137. // 购物车商品列表
  138. $goodsList = $CartModel->getOrderGoodsList($cartIds);
  139. if (empty($goodsList)) {
  140. return $this->renderError('请选择商品结算');
  141. }
  142. // 获取订单结算信息
  143. $orderInfo = $Checkout->onCheckout($goodsList);
  144. if ($this->request->isGet()) {
  145. return $this->renderSuccess([
  146. 'order' => $orderInfo,
  147. 'personal' => $Checkout->getPersonal(),
  148. 'setting' => $Checkout->getSetting(),
  149. ]);
  150. }
  151. // 验证订单是否存在错误
  152. if ($Checkout->hasError()) {
  153. return $this->renderError($Checkout->getError(), ['is_created' => false]);
  154. }
  155. // 创建订单
  156. if (!$Checkout->createOrder($orderInfo)) {
  157. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  158. }
  159. // 移出购物车中已下单的商品
  160. $CartModel->clear($cartIds);
  161. // 构建微信支付请求
  162. $payment = $Checkout->onOrderPayment();
  163. // 返回状态
  164. return $this->renderSuccess([
  165. 'orderId' => $Checkout->model['order_id'], // 订单id
  166. 'payType' => $params['payType'], // 支付方式
  167. 'payment' => $payment // 微信支付参数
  168. ]);
  169. }
  170. /**
  171. * returnUrl回来时继续调用执行扣款接口paypal
  172. * @return Json
  173. */
  174. public function payPayExecutePay($orderNo)
  175. {
  176. if (empty($orderNo)){
  177. return $this->renderError('something wrong!');
  178. }
  179. //dd($orderNo);
  180. //$paymentId = 'PAYID-MXUYFIQ9KF846046B114514M';
  181. $paymentId = $this->request->param('paymentId');
  182. //$token = $this->request->param('token');
  183. //$PayerID = $this->request->param('PayerID');
  184. $conf = config('paypal');
  185. //$orderNo = '';
  186. $pp = new PayPal($conf);
  187. $flag = $pp->executePayment($paymentId);
  188. if ($flag) {
  189. $orderModel = new PaySuccess($orderNo);
  190. $status = $orderModel->onPaySuccess(OrderPayTypeEnum::PAYPAL, ['transaction_id' => $paymentId]);
  191. if (!$status) {
  192. return $this->renderError('Something Wrong!');
  193. }
  194. }
  195. return $this->renderSuccess([], 'Successful!');
  196. }
  197. /**
  198. * 获取结算台验证器
  199. * @return CheckoutValidate
  200. */
  201. private function getValidate(): CheckoutValidate
  202. {
  203. if (!$this->validate) {
  204. $this->validate = new CheckoutValidate;
  205. }
  206. return $this->validate;
  207. }
  208. /**
  209. * 获取购物车ID集
  210. * @return false|string[]
  211. */
  212. private function getCartIds()
  213. {
  214. $cartIds = $this->request->param('cartIds');
  215. return explode(',', $cartIds);
  216. }
  217. /**
  218. * 订单结算提交的参数
  219. * @param array $define
  220. * @return array
  221. */
  222. private function getParam(array $define = []): array
  223. {
  224. return array_merge($define, $this->request->param());
  225. }
  226. }