PaymentController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Api\Notify;
  4. use App\Master\Framework\Library\Easywechat\PayService;
  5. use App\Utils\LogUtil;
  6. use Hyperf\HttpMessage\Stream\SwooleStream;
  7. use Hyperf\HttpServer\Contract\ResponseInterface;
  8. class PaymentController
  9. {
  10. // 日志模块名称
  11. const LOG_MODULE = 'Notify/PaymentController';
  12. /**
  13. * 微信支付回调
  14. *
  15. * @param ResponseInterface $response
  16. * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
  17. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  18. * @throws \ReflectionException
  19. * @throws \Throwable
  20. */
  21. public function wechat(ResponseInterface $response)
  22. {
  23. LogUtil::info("=== 支付回调开始 ===", self::LOG_MODULE, __FUNCTION__);
  24. $pay = new PayService();
  25. $server = $pay->getServer();
  26. $server->handlePaid(function ($message){
  27. // $message 为微信推送的通知结果,详看微信官方文档
  28. LogUtil::info("通知结果", self::LOG_MODULE, 'wechat',$message);
  29. // 微信支付订单号 $message['transaction_id']
  30. // 商户订单号 $message['out_trade_no']
  31. // 商户号 $message['mchid']
  32. // 具体看微信官方文档...
  33. // 进行业务处理,如存数据库等...
  34. });
  35. // 处理结果通知
  36. try {
  37. LogUtil::info("=== 支付回调结束 ===", self::LOG_MODULE, __FUNCTION__);
  38. return $server->serve();
  39. } catch (\Exception $exception){
  40. LogUtil::info("=== 支付回调结束 ===", self::LOG_MODULE, __FUNCTION__,$exception);
  41. // 抛出异常
  42. return $response->withStatus(500)->withBody(new SwooleStream(json_encode([
  43. 'code' => 'FAIL',
  44. 'message' => '失败'
  45. ])));
  46. }
  47. }
  48. }