PayController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\api\validate\PayValidate;
  16. use app\common\enum\user\UserTerminalEnum;
  17. use app\common\logic\PaymentLogic;
  18. use app\common\service\pay\AliPayService;
  19. use app\common\service\pay\WeChatPayService;
  20. /**
  21. * 支付
  22. * Class PayController
  23. * @package app\api\controller
  24. */
  25. class PayController extends BaseApiController
  26. {
  27. public array $notNeedLogin = ['notifyMnp', 'notifyOa', 'aliNotify'];
  28. /**
  29. * @notes 支付方式
  30. * @return \think\response\Json
  31. */
  32. public function payWay()
  33. {
  34. $params = (new PayValidate())->goCheck('payway');
  35. $result = PaymentLogic::getPayWay($this->userId, $this->userInfo['terminal'], $params);
  36. if ($result === false) {
  37. return $this->fail(PaymentLogic::getError());
  38. }
  39. return $this->data($result);
  40. }
  41. /**
  42. * @notes 预支付
  43. * @return \think\response\Json
  44. */
  45. public function prepay()
  46. {
  47. $params = (new PayValidate())->post()->goCheck();
  48. //订单信息
  49. $order = PaymentLogic::getPayOrderInfo($params);
  50. if (false === $order) {
  51. return $this->fail(PaymentLogic::getError(), $params);
  52. }
  53. //支付流程
  54. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  55. $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl);
  56. if (false === $result) {
  57. return $this->fail(PaymentLogic::getError(), $params);
  58. }
  59. return $this->success('', $result);
  60. }
  61. /**
  62. * @notes 获取支付状态
  63. * @return \think\response\Json
  64. */
  65. public function payStatus()
  66. {
  67. $params = (new PayValidate())->goCheck('status', ['user_id' => $this->userId]);
  68. $result = PaymentLogic::getPayStatus($params);
  69. if ($result === false) {
  70. return $this->fail(PaymentLogic::getError());
  71. }
  72. return $this->data($result);
  73. }
  74. /**
  75. * @notes 小程序支付回调
  76. * @return \Psr\Http\Message\ResponseInterface
  77. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  78. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  79. * @throws \ReflectionException
  80. * @throws \Throwable
  81. */
  82. public function notifyMnp()
  83. {
  84. return (new WeChatPayService(UserTerminalEnum::WECHAT_MMP))->notify();
  85. }
  86. /**
  87. * @notes 公众号支付回调
  88. * @return \Psr\Http\Message\ResponseInterface
  89. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  90. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  91. * @throws \ReflectionException
  92. * @throws \Throwable
  93. */
  94. public function notifyOa()
  95. {
  96. return (new WeChatPayService(UserTerminalEnum::WECHAT_OA))->notify();
  97. }
  98. /**
  99. * @notes 支付宝回调
  100. * @author mjf
  101. */
  102. public function aliNotify()
  103. {
  104. $params = $this->request->post();
  105. $result = (new AliPayService())->notify($params);
  106. if (true === $result) {
  107. echo 'success';
  108. } else {
  109. echo 'fail';
  110. }
  111. }
  112. }