PayController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam-段誉
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\controller;
  17. use app\common\enum\UserTerminalEnum;
  18. use app\common\logic\PaymentLogic;
  19. use app\common\service\pay\AliPayService;
  20. use app\common\service\pay\ToutiaoPayService;
  21. use app\common\service\pay\WeChatPayService;
  22. use app\shopapi\validate\PayValidate;
  23. use think\facade\Log;
  24. /**
  25. * 支付
  26. * Class PayController
  27. * @package app\shopapi\controller
  28. */
  29. class PayController extends BaseShopController
  30. {
  31. public array $notNeedLogin = ['notifyMnp', 'notifyOa', 'notifyApp', 'aliNotify', 'toutiaoNotify'];
  32. /**
  33. * @notes 预支付
  34. * @return \think\response\Json
  35. * @throws \Exception
  36. * @author 段誉
  37. * @date 2021/8/3 14:03
  38. */
  39. public function prepay()
  40. {
  41. $params = (new PayValidate())->post()->goCheck();
  42. //订单信息
  43. $order = PaymentLogic::getPayOrderInfo($params,$this->userInfo['terminal']);
  44. outFileLog($order,'prepay','order');
  45. // 输出到runtime/log目录
  46. file_put_contents(
  47. runtime_path() . 'log' . DIRECTORY_SEPARATOR . 'prepay_debug_' . date('Y-m-d') . '.log',
  48. "[" . date('Y-m-d H:i:s') . "] 预支付请求开始: " . json_encode($order, JSON_UNESCAPED_UNICODE) . PHP_EOL,
  49. FILE_APPEND | LOCK_EX
  50. );
  51. if (false === $order) {
  52. return $this->fail(PaymentLogic::getError(), $params);
  53. }
  54. //支付流程
  55. $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal']);
  56. if (false === $result) {
  57. return $this->fail(PaymentLogic::getError(), $params);
  58. }
  59. return $this->success('', $result);
  60. }
  61. /**
  62. * @notes 小程序支付回调
  63. * @return \Symfony\Component\HttpFoundation\Response
  64. * @author 段誉
  65. * @date 2021/8/13 14:17
  66. */
  67. public function notifyMnp()
  68. {
  69. return (new WeChatPayService(UserTerminalEnum::WECHAT_MMP))->notify();
  70. }
  71. /**
  72. * @notes 公众号支付回调
  73. * @return \Symfony\Component\HttpFoundation\Response
  74. * @author 段誉
  75. * @date 2021/8/13 14:17
  76. */
  77. public function notifyOa()
  78. {
  79. return (new WeChatPayService(UserTerminalEnum::WECHAT_OA))->notify();
  80. }
  81. /**
  82. * @notes app支付回调
  83. * @author 段誉
  84. * @date 2021/8/13 14:16
  85. */
  86. public function notifyApp()
  87. {
  88. return (new WeChatPayService(UserTerminalEnum::IOS))->notify();
  89. }
  90. /**
  91. * @notes 支付宝回调
  92. * @return bool
  93. * @author 段誉
  94. * @date 2021/8/13 14:16
  95. */
  96. public function aliNotify()
  97. {
  98. $params = $this->request->post();
  99. $result = (new AliPayService())->notify($params);
  100. if (true === $result) {
  101. echo 'success';
  102. } else {
  103. echo 'fail';
  104. }
  105. }
  106. /**
  107. * @notes 头条支付回调(字节小程序)
  108. * @return mixed
  109. * @author Tab
  110. * @date 2021/11/17 11:40
  111. */
  112. public function toutiaoNotify()
  113. {
  114. $params = $this->request->post();
  115. $result = (new ToutiaoPayService())->notify($params);
  116. return $result;
  117. }
  118. /**
  119. * @notes 支付方式列表
  120. * @return \think\response\Json
  121. * @author 段誉
  122. * @date 2021/8/13 14:33
  123. */
  124. public function payway()
  125. {
  126. $params = (new PayValidate())->goCheck('payway');
  127. $result = PaymentLogic::getPayWay($this->userId, $this->userInfo['terminal'], $params);
  128. if ($result === false) {
  129. return $this->fail(PaymentLogic::getError());
  130. }
  131. return $this->data($result);
  132. }
  133. public function payStatus()
  134. {
  135. $params = (new PayValidate())->goCheck('paystatus');
  136. $result = PaymentLogic::getPayStatus($params);
  137. if ($result === false) {
  138. return $this->fail(PaymentLogic::getError());
  139. }
  140. return $this->data($result);
  141. }
  142. /**
  143. * @notes 获取支付结果
  144. * @return \think\response\Json
  145. * @author 段誉
  146. * @date 2022/4/6 15:23
  147. */
  148. public function payResult()
  149. {
  150. $params = (new PayValidate())->goCheck('payresult');
  151. $result = PaymentLogic::getPayResult($params);
  152. if ($result === false) {
  153. return $this->fail(PaymentLogic::getError());
  154. }
  155. return $this->data($result);
  156. }
  157. }