PayController.php 4.0 KB

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