1
0

PayController.php 4.3 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\PayEnum;
  17. use app\common\enum\user\UserTerminalEnum;
  18. use app\common\logic\PaymentLogic;
  19. use app\common\service\pay\AliPayService;
  20. use app\common\service\pay\WeChatPayService;
  21. use think\facade\Log;
  22. /**
  23. * 支付
  24. * Class PayController
  25. * @package app\api\controller
  26. */
  27. class PayController extends BaseApiController
  28. {
  29. public array $notNeedLogin = ['notifyMnp', 'notifyOa', 'aliNotify'];
  30. /**
  31. * @notes 支付方式
  32. * @return \think\response\Json
  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. */
  47. public function prepay()
  48. {
  49. $params = (new PayValidate())->post()->goCheck();
  50. //订单信息
  51. $order = PaymentLogic::getPayOrderInfo($params);
  52. if (false === $order) {
  53. return $this->fail(PaymentLogic::getError(), $params);
  54. }
  55. //支付流程
  56. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  57. if ($params['pay_way'] == PayEnum::WECHAT_NATIVE_PAY) {
  58. //微信二维码预支付
  59. $result = PaymentLogic::pay(2, $params['from'], $order, 4, $redirectUrl);
  60. } else {
  61. $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl);
  62. }
  63. if (false === $result) {
  64. return $this->fail(PaymentLogic::getError(), $params);
  65. }
  66. $result['sn'] = $order['sn'];
  67. $result['order_id'] = $order['id'];
  68. return $this->success('', $result);
  69. }
  70. /**
  71. * @notes 获取支付状态
  72. * @return \think\response\Json
  73. */
  74. public function payStatus()
  75. {
  76. $params = (new PayValidate())->goCheck('status', ['user_id' => $this->userId]);
  77. $result = PaymentLogic::getPayStatus($params);
  78. if ($result === false) {
  79. return $this->fail(PaymentLogic::getError());
  80. }
  81. return $this->data($result);
  82. }
  83. /**
  84. * @notes 小程序支付回调
  85. * @return \Psr\Http\Message\ResponseInterface
  86. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  87. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  88. * @throws \ReflectionException
  89. * @throws \Throwable
  90. */
  91. public function notifyMnp()
  92. {
  93. Log::write(json_encode($this->request->param(), JSON_UNESCAPED_UNICODE));
  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. */
  104. public function notifyOa()
  105. {
  106. return (new WeChatPayService(UserTerminalEnum::WECHAT_OA))->notify();
  107. }
  108. /**
  109. * @notes 支付宝回调
  110. * @author mjf
  111. */
  112. public function aliNotify()
  113. {
  114. $params = $this->request->post();
  115. $result = (new AliPayService())->notify($params);
  116. if (true === $result) {
  117. echo 'success';
  118. } else {
  119. echo 'fail';
  120. }
  121. }
  122. }