Files
alfa-bank-sdk/examples/05_webhooks.php

50 lines
1.5 KiB
PHP

<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Apoloz\AlfaBank\AlfaBank;
use Apoloz\AlfaBank\Webhooks\WebhookFactory;
use Apoloz\AlfaBank\Requests\Webhooks\GetWebhooksRequest;
use Apoloz\AlfaBank\Requests\Webhooks\CreateWebhookRequest;
use Apoloz\AlfaBank\Data\Webhooks\Subscriptions\WebhookSubscriptionRequestData;
/**
* 1. Managing Webhooks (BaaS API)
*/
$baas = AlfaBank::baas('cert.pem', 'key.pem');
$baas->login('id', 'secret');
// List existing webhooks
$response = $baas->send(new GetWebhooksRequest());
$webhooks = $response->dto(); // DataCollection of WebhookSubscriptionData
foreach ($webhooks as $webhook) {
echo "Webhook ID: {$webhook->id}, URL: {$webhook->callbackUri}\n";
}
// Create new webhook
$data = new WebhookSubscriptionRequestData('https://your-app.com/webhooks/alfa', ['transactions']);
$baas->send(new CreateWebhookRequest($data));
/**
* 2. Parsing incoming webhook payload (Webhook Controller logic)
*/
$rawPayload = [
[
'actionType' => 'create',
'eventTime' => '2023-10-27T10:00:00Z',
'object' => 'ul_transaction_default',
'data' => [
'transactionId' => 'TX-12345',
'amount' => ['amount' => 100.50, 'currencyName' => 'RUR'],
'direction' => 'CREDIT',
'paymentPurpose' => 'Test payment'
]
]
];
// Use the factory to parse the payload into a DTO
$webhookDto = WebhookFactory::create($rawPayload);
echo "Received Webhook: {$webhookDto->object} ({$webhookDto->actionType})\n";