1
0

PayController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if ($params['from'] == 'retention_money') {
  52. $order = PaymentLogic::getPayRetentionMoneyOrderInfo($params);//质保金缴费单
  53. } else {
  54. $order = PaymentLogic::getPayOrderInfo($params);
  55. }
  56. if (false === $order) {
  57. return $this->fail(PaymentLogic::getError(), $params);
  58. }
  59. //支付流程
  60. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  61. if ($params['pay_way'] == PayEnum::WECHAT_NATIVE_PAY) {
  62. //微信二维码预支付
  63. $result = PaymentLogic::pay(2, $params['from'], $order, 4, $redirectUrl);
  64. } else {
  65. $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl);
  66. }
  67. if (false === $result) {
  68. return $this->fail(PaymentLogic::getError(), $params);
  69. }
  70. $result['sn'] = $order['sn'];
  71. $result['order_id'] = $order['id'];
  72. return $this->success('', $result);
  73. }
  74. /**
  75. * @notes 获取支付状态
  76. * @return \think\response\Json
  77. */
  78. public function payStatus()
  79. {
  80. $params = (new PayValidate())->goCheck('status', ['user_id' => $this->userId]);
  81. $result = PaymentLogic::getPayStatus($params);
  82. if ($result === false) {
  83. return $this->fail(PaymentLogic::getError());
  84. }
  85. return $this->data($result);
  86. }
  87. /**
  88. * @notes 小程序支付回调
  89. * @return \Psr\Http\Message\ResponseInterface
  90. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  91. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  92. * @throws \ReflectionException
  93. * @throws \Throwable
  94. */
  95. public function notifyMnp()
  96. {
  97. Log::write(json_encode($this->request->param(), JSON_UNESCAPED_UNICODE));
  98. return (new WeChatPayService(UserTerminalEnum::WECHAT_MMP))->notify();
  99. }
  100. /**
  101. * @notes 公众号支付回调
  102. * @return \Psr\Http\Message\ResponseInterface
  103. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  104. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  105. * @throws \ReflectionException
  106. * @throws \Throwable
  107. */
  108. public function notifyOa()
  109. {
  110. return (new WeChatPayService(UserTerminalEnum::WECHAT_OA))->notify();
  111. }
  112. /**
  113. * @notes 支付宝回调
  114. * @author mjf
  115. */
  116. public function aliNotify()
  117. {
  118. $params = $this->request->post();
  119. $result = (new AliPayService())->notify($params);
  120. if (true === $result) {
  121. echo 'success';
  122. } else {
  123. echo 'fail';
  124. }
  125. }
  126. /**
  127. * @notes 工单分享代付
  128. * @return \think\response\Json
  129. */
  130. public function sharePrepay()
  131. {
  132. $params = (new PayValidate())->post()->goCheck();
  133. //订单信息
  134. $order = PaymentLogic::getPayOrderInfo($params);
  135. if (false === $order) {
  136. return $this->fail(PaymentLogic::getError(), $params);
  137. }
  138. //支付流程
  139. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  140. $result = PaymentLogic::userSharePay($params['pay_way'], $params['from'], $order, ['openid'=>$params['openid']??''], $redirectUrl);
  141. if (false === $result) {
  142. return $this->fail(PaymentLogic::getError(), $params);
  143. }
  144. $result['sn'] = $order['sn'];
  145. return $this->success('', $result);
  146. }
  147. }