forked from coinbase/coinbase-commerce-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookExample.php
More file actions
executable file
·47 lines (40 loc) · 1.52 KB
/
WebhookExample.php
File metadata and controls
executable file
·47 lines (40 loc) · 1.52 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
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use CoinbaseCommerce\PaymentLink\PaymentLinkWebhook;
// Webhook secret from your subscription response
$webhookSecret = getenv('COINBASE_WEBHOOK_SECRET');
// Get the raw request body and headers
$payload = file_get_contents('php://input');
$signatureHeader = $_SERVER['HTTP_X_HOOK0_SIGNATURE'] ?? '';
// Convert $_SERVER headers to lowercase format
$headers = [];
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headerName = strtolower(str_replace('_', '-', substr($key, 5)));
$headers[$headerName] = $value;
}
}
$headers['content-type'] = $_SERVER['CONTENT_TYPE'] ?? 'application/json';
try {
$event = PaymentLinkWebhook::buildEvent($payload, $signatureHeader, $webhookSecret, $headers);
switch ($event['eventType']) {
case 'payment_link.payment.success':
// Payment completed — fulfill the order
echo "Payment completed for link {$event['id']}\n";
break;
case 'payment_link.payment.failed':
echo "Payment failed for link {$event['id']}\n";
break;
case 'payment_link.payment.expired':
echo "Payment expired for link {$event['id']}\n";
break;
}
http_response_code(200);
echo 'OK';
} catch (\CoinbaseCommerce\Exceptions\SignatureVerificationException $e) {
http_response_code(400);
echo 'Invalid signature';
} catch (\Exception $e) {
http_response_code(500);
echo 'Error: ' . $e->getMessage();
}