PayController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @author 段誉
  32. * @date 2023/2/24 17:54
  33. */
  34. public function payWay()
  35. {
  36. $params = (new PayValidate())->goCheck('payway');
  37. $result = PaymentLogic::getPayWay($this->userId, $this->userInfo['terminal'], $params);
  38. if ($result === false) {
  39. return $this->fail(PaymentLogic::getError());
  40. }
  41. return $this->data($result);
  42. }
  43. /**
  44. * @notes 预支付
  45. * @return \think\response\Json
  46. * @author 段誉
  47. * @date 2023/2/28 14:21
  48. */
  49. public function prepay()
  50. {
  51. $params = (new PayValidate())->post()->goCheck();
  52. //订单信息
  53. $order = PaymentLogic::getPayOrderInfo($params);
  54. if (false === $order) {
  55. return $this->fail(PaymentLogic::getError(), $params);
  56. }
  57. //支付流程
  58. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  59. $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl);
  60. if (false === $result) {
  61. return $this->fail(PaymentLogic::getError(), $params);
  62. }
  63. return $this->success('', $result);
  64. }
  65. /**
  66. * @notes 获取支付状态
  67. * @return \think\response\Json
  68. * @author 段誉
  69. * @date 2023/3/1 16:23
  70. */
  71. public function payStatus()
  72. {
  73. $params = (new PayValidate())->goCheck('status', ['user_id' => $this->userId]);
  74. $result = PaymentLogic::getPayStatus($params);
  75. if ($result === false) {
  76. return $this->fail(PaymentLogic::getError());
  77. }
  78. return $this->data($result);
  79. }
  80. /**
  81. * @notes 小程序支付回调
  82. * @return \Psr\Http\Message\ResponseInterface
  83. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  84. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  85. * @throws \ReflectionException
  86. * @throws \Throwable
  87. * @author 段誉
  88. * @date 2023/2/28 14:21
  89. */
  90. public function notifyMnp()
  91. {
  92. return (new WeChatPayService(UserTerminalEnum::WECHAT_MMP))->notify();
  93. }
  94. /**
  95. * @notes 公众号支付回调
  96. * @return \Psr\Http\Message\ResponseInterface
  97. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  98. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  99. * @throws \ReflectionException
  100. * @throws \Throwable
  101. * @author 段誉
  102. * @date 2023/2/28 14:21
  103. */
  104. public function notifyOa()
  105. {
  106. return (new WeChatPayService(UserTerminalEnum::WECHAT_OA))->notify();
  107. }
  108. /**
  109. * @notes 支付宝回调
  110. * @author mjf
  111. * @date 2024/3/18 16:50
  112. */
  113. public function aliNotify()
  114. {
  115. $params = $this->request->post();
  116. $result = (new AliPayService())->notify($params);
  117. if (true === $result) {
  118. echo 'success';
  119. } else {
  120. echo 'fail';
  121. }
  122. }
  123. }