123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- declare(strict_types=1);
- namespace App\Controller\Api\Notify;
- use App\Master\Framework\Library\Easywechat\PayService;
- use App\Utils\LogUtil;
- use Hyperf\HttpMessage\Stream\SwooleStream;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- class PaymentController
- {
- // 日志模块名称
- const LOG_MODULE = 'Notify/PaymentController';
- /**
- * 微信支付回调
- *
- * @param ResponseInterface $response
- * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \ReflectionException
- * @throws \Throwable
- */
- public function wechat(ResponseInterface $response)
- {
- LogUtil::info("=== 支付回调开始 ===", self::LOG_MODULE, __FUNCTION__);
- $pay = new PayService();
- $server = $pay->getServer();
- $server->handlePaid(function ($message){
- // $message 为微信推送的通知结果,详看微信官方文档
- LogUtil::info("通知结果", self::LOG_MODULE, 'wechat',$message);
- // 微信支付订单号 $message['transaction_id']
- // 商户订单号 $message['out_trade_no']
- // 商户号 $message['mchid']
- // 具体看微信官方文档...
- // 进行业务处理,如存数据库等...
- });
- // 处理结果通知
- try {
- LogUtil::info("=== 支付回调结束 ===", self::LOG_MODULE, __FUNCTION__);
- return $server->serve();
- } catch (\Exception $exception){
- LogUtil::info("=== 支付回调结束 ===", self::LOG_MODULE, __FUNCTION__,$exception);
- // 抛出异常
- return $response->withStatus(500)->withBody(new SwooleStream(json_encode([
- 'code' => 'FAIL',
- 'message' => '失败'
- ])));
- }
- }
- }
|