fix: webhook handles test events and missing metadata gracefully
This commit is contained in:
@@ -8,8 +8,21 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
}
|
||||
|
||||
$rawBody = file_get_contents('php://input');
|
||||
|
||||
if (!$rawBody) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Empty body']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payload = json_decode($rawBody, true);
|
||||
|
||||
if ($payload === null) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid JSON', 'raw' => substr($rawBody, 0, 500)]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verify HMAC signature if secret is configured
|
||||
if (BTCPAY_WEBHOOK_SECRET) {
|
||||
$sigHeader = $_SERVER['HTTP_BTCPAY_SIG'] ?? '';
|
||||
@@ -25,16 +38,9 @@ if (BTCPAY_WEBHOOK_SECRET) {
|
||||
$type = $payload['type'] ?? null;
|
||||
$invoiceId = $payload['invoiceId'] ?? null;
|
||||
|
||||
if (!$type || !$invoiceId) {
|
||||
if (!$type) {
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'error' => 'Missing type or invoiceId',
|
||||
'received' => [
|
||||
'type' => $type,
|
||||
'invoiceId' => $invoiceId,
|
||||
'keys' => array_keys($payload ?: []),
|
||||
],
|
||||
]);
|
||||
echo json_encode(['error' => 'Missing type', 'keys' => array_keys($payload ?: [])]);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -44,6 +50,12 @@ if ($type !== 'InvoiceSettled' && $type !== 'InvoicePaymentSettled') {
|
||||
exit;
|
||||
}
|
||||
|
||||
// For settled events, invoiceId is required
|
||||
if (!$invoiceId) {
|
||||
echo json_encode(['ok' => true, 'action' => 'test_acknowledged', 'type' => $type]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Fetch invoice from BTCPay to get listing ID from metadata
|
||||
$btcpayUrl = BTCPAY_BASE_URL . '/api/v1/stores/' . BTCPAY_STORE_ID . '/invoices/' . urlencode($invoiceId);
|
||||
$btcpayContext = stream_context_create([
|
||||
@@ -66,8 +78,7 @@ $invoice = json_decode($btcpayResponse, true);
|
||||
$listingId = $invoice['metadata']['listingId'] ?? null;
|
||||
|
||||
if (!$listingId) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No listingId in invoice metadata']);
|
||||
echo json_encode(['ok' => true, 'action' => 'skipped', 'reason' => 'No listingId in invoice metadata']);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user