PayController.php 5.7 KB

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