웹훅 설정
알림
웹훅 서버로 작업하려면 하나의 콜백 함수가 필요합니다.
미리 생성한 클래스 사용
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();
새 클래스 생성
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}
WebhookServer 파일을 확장하거나 예제로 사용하여 자신만의 웹훅 서버 클래스를 생성할 수 있습니다.
이 기사가 도움이 되었나요?
의견을 보내 주셔서 감사드립니다!
메시지를 검토한 후 사용자 경험 향상에 사용하겠습니다.오자 또는 기타 텍스트 오류를 찾으셨나요? 텍스트를 선택하고 컨트롤+엔터를 누르세요.