-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.php
More file actions
127 lines (108 loc) · 4.26 KB
/
webhook.php
File metadata and controls
127 lines (108 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace MyPlugin\ExampleWebhookImplementation;
/**
* Webhook Management Example
*
* This script demonstrates the Webhook Management functionality:
* This script demonstrates the Webhook Management functionality:
* - Installing a Webhook (URL + Listener).
* - Listing Webhook URLs and Listeners.
* - Updating a Webhook URL.
* - Uninstalling a Webhook.
*
* USAGE:
* php webhook.php
*/
use PostFinanceCheckout\PluginCore\Sdk\SdkV1\WebhookManagementGateway;
use PostFinanceCheckout\PluginCore\Sdk\SdkV1\WebhookSignatureGateway;
use PostFinanceCheckout\PluginCore\Transaction\State as TransactionState;
use PostFinanceCheckout\PluginCore\Webhook\Enum\WebhookListener;
use PostFinanceCheckout\PluginCore\Webhook\WebhookConfig;
use PostFinanceCheckout\PluginCore\Webhook\WebhookService;
error_reporting(E_ALL & ~E_DEPRECATED);
/** @var array $common */
$common = require __DIR__ . '/../../../examples/Common/bootstrap.php';
$spaceId = $common['spaceId'];
$sdkProvider = $common['sdkProvider'];
$logger = $common['logger'];
$settings = $common['settings'];
// Webhook example doesn't need persistence or argLoader typically, but they are available.
// Setup the required webhook management and signature services.
$managementGateway = new WebhookManagementGateway($sdkProvider, $logger);
$signatureGateway = new WebhookSignatureGateway($sdkProvider, $logger);
$webhookService = new WebhookService(
$managementGateway,
$signatureGateway,
$logger
);
echo "Starting Webhook Management Demo in Space $spaceId...\n\n";
// Install the webhook configuration (URL and Listener) in the portal.
echo "--- STEP 1: Installing Webhook ---\n";
// Use uniqid to ensure URL is unique (SDK constraint)
$uniqueId = uniqid();
$config = new WebhookConfig(
url: 'https://example.com/webhook/callback/' . $uniqueId,
name: 'Demo Webhook ' . $uniqueId,
entity: WebhookListener::TRANSACTION, // Enum
eventStates: [TransactionState::AUTHORIZED->value] // Array of states
);
try {
$webhookService->installWebhook((int)$spaceId, $config);
echo "SUCCESS: Webhook installed.\n";
} catch (\Exception $e) {
exit("FAILED: " . $e->getMessage() . "\n");
}
// List the existing webhook configurations for the space.
echo "\n--- STEP 2: Listing Webhooks ---\n";
try {
$urls = $webhookService->listUrls((int)$spaceId);
echo "Found " . count($urls) . " Webhook URL(s).\n";
// Find our recently created URL and Listener
$myUrl = null;
foreach ($urls as $url) {
if ($url->name === $config->name) {
$myUrl = $url;
echo "URL Found: ID=" . $url->id . ", Name=" . $url->name . ", URL=" . $url->url . "\n";
break;
}
}
$myListener = null;
if ($myUrl) {
// Fetch listeners specifically for this URL
$listeners = $webhookService->getWebhookListeners((int)$spaceId, $myUrl->id);
foreach ($listeners as $listener) {
// In the installWebhook method, we use the same name for the listener and URL
if ($listener->name === $config->name) {
$myListener = $listener;
echo "Listener Found: ID=" . $listener->id . ", Name=" . $listener->name . "\n";
break;
}
}
}
} catch (\Exception $e) {
echo "FAILED: " . $e->getMessage() . "\n";
}
// Update the URL of an existing webhook configuration.
if ($myUrl) {
echo "\n--- STEP 3: Updating Webhook URL ---\n";
try {
$newUrl = 'https://example.com/webhook/v2/' . uniqid(); // Ensure uniqueness
$webhookService->updateWebhookUrl((int)$spaceId, $myUrl->id, $newUrl);
echo "SUCCESS: Webhook URL updated to: $newUrl\n";
} catch (\Exception $e) {
echo "FAILED to update URL: " . $e->getMessage() . "\n";
}
}
// Uninstall the webhook by removing its listeners.
// This ensures that the system stops sending events to the callback URL.
if ($myUrl) {
echo "\n--- STEP 4: Uninstalling (Cleanup) ---\n";
try {
// Remove all listeners associated with the URL.
$webhookService->deleteWebhookListenersForUrl((int)$spaceId, $myUrl->id);
echo "SUCCESS: Webhook Listeners removed for URL ID " . $myUrl->id . ".\n";
} catch (\Exception $e) {
echo "FAILED to uninstall: " . $e->getMessage() . "\n";
}
}
echo "\nDone.\n";