Set up webhooks
Note
To work with the webhook server, you only need one callback function.
Use pre-created class
Copy
- php
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();
Create new class
Copy
- php
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}
You can create your own webhook server class by extending the WebhookServer file or using it as an example.
Was this article helpful?
Thank you for your feedback!
We’ll review your message and use it to help us improve your experience.Found a typo or other text error? Select the text and press Ctrl+Enter.