PayController.php 4.2 KB

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