1
0

PayController.php 5.3 KB

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