设置Webhook

注:
要与Webhook服务器交互,只需要一个回调函数。

使用预创建的类

Copy
Full screen
Small screen
 1<?php
 2
 3use Xsolla\SDK\Webhook\WebhookServer;
 4use Xsolla\SDK\Webhook\Message\Message;
 5use Xsolla\SDK\Webhook\Message\NotificationTypeDictionary;
 6use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
 7
 8$callback = function (Message $message) {
 9    switch ($message->getNotificationType()) {
10        case NotificationTypeDictionary::USER_VALIDATION:
11            /** @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */
12            // TODO if user not found, you should throw Xsolla\SDK\Exception\Webhook\InvalidUserException
13            break;
14        case NotificationTypeDictionary::PAYMENT:
15            /** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */
16            // TODO if the payment delivery fails for some reason, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException
17            break;
18        case NotificationTypeDictionary::REFUND:
19            /** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */
20            // TODO if you cannot handle the refund, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException
21            break;
22        default:
23            throw new XsollaWebhookException('Notification type not implemented');
24    }
25};
26
27$webhookServer = WebhookServer::create($callback, PROJECT_KEY);
28$webhookServer->start();

创建新类

Copy
Full screen
Small screen
 1<?php
 2
 3use Xsolla\SDK\Webhook\WebhookRequest;
 4use Xsolla\SDK\Webhook\Message\Message;
 5use Xsolla\SDK\Webhook\WebhookAuthenticator;
 6use Xsolla\SDK\Webhook\WebhookResponse;
 7use Xsolla\SDK\Exception\Webhook\InvalidUserException;
 8use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
 9
10$httpHeaders = array();//TODO fetch HTTP request headers from $_SERVER or
11apache_request_headers()
12$httpRequestBody = file_get_contents('php://input');
13$clientIPv4 = $_SERVER['REMOTE_ADDR'];
14$request = new WebhookRequest($httpHeaders, $httpRequestBody, $clientIPv4);
15//or $request = WebhookRequest::fromGlobals();
16
17$webhookAuthenticator = new WebhookAuthenticator(PROJECT_KEY);
18$webhookAuthenticator->authenticate($request, $authenticateClientIp = true); // throws
19Xsolla\SDK\Exception\Webhook\XsollaWebhookException
20
21$requestArray = $request->toArray();
22
23$message = Message::fromArray($requestArray);
24switch ($message->getNotificationType()) {
25case Message::USER_VALIDATION:
26/** @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */
27$userArray = $message->getUser();
28$userId = $message->getUserId();
29$messageArray = $message->toArray();
30// TODO if user not found, you should throw
31Xsolla\SDK\Exception\Webhook\InvalidUserException
32// throw new InvalidUserException('User not found');
33break;
34case Message::PAYMENT:
35/** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */
36$userArray = $message->getUser();
37$paymentArray = $message->getTransaction();
38$paymentId = $message->getPaymentId();
39$externalPaymentId = $message->getExternalPaymentId();
40$paymentDetailsArray = $message->getPaymentDetails();
41$customParametersArray = $message->getCustomParameters();
42$isDryRun = $message->isDryRun();
43$messageArray = $message->toArray();
44// TODO if the payment delivery fails for some reason, you should throw
45Xsolla\SDK\Exception\Webhook\XsollaWebhookException
46break;
47case Message::REFUND:
48/** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */
49$userArray = $message->getUser();
50$paymentArray = $message->getTransaction();
51$paymentId = $message->getPaymentId();
52$externalPaymentId = $message->getExternalPaymentId();
53$paymentDetailsArray = $message->getPaymentDetails();
54$customParametersArray = $message->getCustomParameters();
55$isDryRun = $message->isDryRun();
56$refundArray = $message->getRefundDetails();
57$messageArray = $message->toArray();
58// TODO if you cannot handle the refund, you should throw
59Xsolla\SDK\Exception\Webhook\XsollaWebhookException
60break;
61default:
62throw new XsollaWebhookException('Notification type not implemented');
63}

您可以通过继续编写WebhookServer文件或来创建自己的Webhook服务器类或将它用作参考。

本文对您的有帮助吗?
谢谢!
我们还有其他可改进之处吗? 留言
非常抱歉
请说明为何本文没有帮助到您。 留言
感谢您的反馈!
我们会查看您的留言并运用它改进用户体验。
上次更新时间: 2025年8月29日

发现了错别字或其他内容错误? 请选择文本,然后按Ctrl+Enter。

报告问题
我们非常重视内容质量。您的反馈将帮助我们做得更好。
请留下邮箱以便我们后续跟进
感谢您的反馈!
无法发送您的反馈
请稍后重试或发送邮件至doc_feedback@xsolla.com与我们联系。