웹훅 설정
알림
웹훅 서버로 작업하려면 하나의 콜백 함수가 필요합니다.
미리 생성한 클래스 사용
Copy
- php
<?php
use Xsolla\SDK\Webhook\WebhookServer;
use Xsolla\SDK\Webhook\Message\Message;
use Xsolla\SDK\Webhook\Message\NotificationTypeDictionary;
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
$callback = function (Message $message) {
switch ($message->getNotificationType()) {
case NotificationTypeDictionary::USER_VALIDATION:
/** @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */
// TODO if user not found, you should throw Xsolla\SDK\Exception\Webhook\InvalidUserException
break;
case NotificationTypeDictionary::PAYMENT:
/** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */
// TODO if the payment delivery fails for some reason, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException
break;
case NotificationTypeDictionary::REFUND:
/** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */
// TODO if you cannot handle the refund, you should throw Xsolla\SDK\Exception\Webhook\XsollaWebhookException
break;
default:
throw new XsollaWebhookException('Notification type not implemented');
}
};
$webhookServer = WebhookServer::create($callback, PROJECT_KEY);
$webhookServer->start();
새 클래스 생성
Copy
- php
<?php
use Xsolla\SDK\Webhook\WebhookRequest;
use Xsolla\SDK\Webhook\Message\Message;
use Xsolla\SDK\Webhook\WebhookAuthenticator;
use Xsolla\SDK\Webhook\WebhookResponse;
use Xsolla\SDK\Exception\Webhook\InvalidUserException;
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
$httpHeaders = array();//TODO fetch HTTP request headers from $_SERVER or
apache_request_headers()
$httpRequestBody = file_get_contents('php://input');
$clientIPv4 = $_SERVER['REMOTE_ADDR'];
$request = new WebhookRequest($httpHeaders, $httpRequestBody, $clientIPv4);
//or $request = WebhookRequest::fromGlobals();
$webhookAuthenticator = new WebhookAuthenticator(PROJECT_KEY);
$webhookAuthenticator->authenticate($request, $authenticateClientIp = true); // throws
Xsolla\SDK\Exception\Webhook\XsollaWebhookException
$requestArray = $request->toArray();
$message = Message::fromArray($requestArray);
switch ($message->getNotificationType()) {
case Message::USER_VALIDATION:
/** @var Xsolla\SDK\Webhook\Message\UserValidationMessage $message */
$userArray = $message->getUser();
$userId = $message->getUserId();
$messageArray = $message->toArray();
// TODO if user not found, you should throw
Xsolla\SDK\Exception\Webhook\InvalidUserException
// throw new InvalidUserException('User not found');
break;
case Message::PAYMENT:
/** @var Xsolla\SDK\Webhook\Message\PaymentMessage $message */
$userArray = $message->getUser();
$paymentArray = $message->getTransaction();
$paymentId = $message->getPaymentId();
$externalPaymentId = $message->getExternalPaymentId();
$paymentDetailsArray = $message->getPaymentDetails();
$customParametersArray = $message->getCustomParameters();
$isDryRun = $message->isDryRun();
$messageArray = $message->toArray();
// TODO if the payment delivery fails for some reason, you should throw
Xsolla\SDK\Exception\Webhook\XsollaWebhookException
break;
case Message::REFUND:
/** @var Xsolla\SDK\Webhook\Message\RefundMessage $message */
$userArray = $message->getUser();
$paymentArray = $message->getTransaction();
$paymentId = $message->getPaymentId();
$externalPaymentId = $message->getExternalPaymentId();
$paymentDetailsArray = $message->getPaymentDetails();
$customParametersArray = $message->getCustomParameters();
$isDryRun = $message->isDryRun();
$refundArray = $message->getRefundDetails();
$messageArray = $message->toArray();
// TODO if you cannot handle the refund, you should throw
Xsolla\SDK\Exception\Webhook\XsollaWebhookException
break;
default:
throw new XsollaWebhookException('Notification type not implemented');
}
WebhookServer 파일을 확장하거나 예제로 사용하여 자신만의 웹훅 서버 클래스를 생성할 수 있습니다.
이 기사가 도움이 되었나요?
의견을 보내 주셔서 감사드립니다!
메시지를 검토한 후 사용자 경험 향상에 사용하겠습니다.오자 또는 기타 텍스트 오류를 찾으셨나요? 텍스트를 선택하고 컨트롤+엔터를 누르세요.