Configurar webhooks

Nota
Para operar con el servidor de webhook, solamente necesita una función de devolución de llamada.

Utilice una clase creada previamente

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();

Crear una nueva clase

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}

Puede crear su propia clase de servidor de webhook ampliando el archivo WebhookServer o usándolo como ejemplo.

¿Te ha resultado útil este artículo?
¡Gracias!
¿Hay algo en lo que podamos mejorar? Mensaje
Lo sentimos
Por favor, cuéntanos por qué no te ha resultado útil este artículo. Mensaje
¡Gracias por tu mensaje!
Nos ayudará a mejorar tu experiencia.
Última actualización: 4 de Julio de 2025

¿Has encontrado una errata u otro error de texto? Selecciona el texto y pulsa Ctrl+Intro.

Informar de un problema
Nos esforzamos por ofrecer contenido de calidad. Tus comentarios nos ayudan a mejorar.
Déjanos tu correo electrónico para que te podamos responder
¡Gracias por tu mensaje!
No hemos podido enviar sus comentarios
Vuelva a intentarlo más tarde o escríbanos a [email protected].
OSZAR »