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');
|
$rawBody = file_get_contents('php://input');
|
||||||
|
|
||||||
|
if (!$rawBody) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(['error' => 'Empty body']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
$payload = json_decode($rawBody, true);
|
$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
|
// Verify HMAC signature if secret is configured
|
||||||
if (BTCPAY_WEBHOOK_SECRET) {
|
if (BTCPAY_WEBHOOK_SECRET) {
|
||||||
$sigHeader = $_SERVER['HTTP_BTCPAY_SIG'] ?? '';
|
$sigHeader = $_SERVER['HTTP_BTCPAY_SIG'] ?? '';
|
||||||
@@ -25,16 +38,9 @@ if (BTCPAY_WEBHOOK_SECRET) {
|
|||||||
$type = $payload['type'] ?? null;
|
$type = $payload['type'] ?? null;
|
||||||
$invoiceId = $payload['invoiceId'] ?? null;
|
$invoiceId = $payload['invoiceId'] ?? null;
|
||||||
|
|
||||||
if (!$type || !$invoiceId) {
|
if (!$type) {
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
echo json_encode([
|
echo json_encode(['error' => 'Missing type', 'keys' => array_keys($payload ?: [])]);
|
||||||
'error' => 'Missing type or invoiceId',
|
|
||||||
'received' => [
|
|
||||||
'type' => $type,
|
|
||||||
'invoiceId' => $invoiceId,
|
|
||||||
'keys' => array_keys($payload ?: []),
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +50,12 @@ if ($type !== 'InvoiceSettled' && $type !== 'InvoicePaymentSettled') {
|
|||||||
exit;
|
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
|
// Fetch invoice from BTCPay to get listing ID from metadata
|
||||||
$btcpayUrl = BTCPAY_BASE_URL . '/api/v1/stores/' . BTCPAY_STORE_ID . '/invoices/' . urlencode($invoiceId);
|
$btcpayUrl = BTCPAY_BASE_URL . '/api/v1/stores/' . BTCPAY_STORE_ID . '/invoices/' . urlencode($invoiceId);
|
||||||
$btcpayContext = stream_context_create([
|
$btcpayContext = stream_context_create([
|
||||||
@@ -66,8 +78,7 @@ $invoice = json_decode($btcpayResponse, true);
|
|||||||
$listingId = $invoice['metadata']['listingId'] ?? null;
|
$listingId = $invoice['metadata']['listingId'] ?? null;
|
||||||
|
|
||||||
if (!$listingId) {
|
if (!$listingId) {
|
||||||
http_response_code(400);
|
echo json_encode(['ok' => true, 'action' => 'skipped', 'reason' => 'No listingId in invoice metadata']);
|
||||||
echo json_encode(['error' => 'No listingId in invoice metadata']);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ class PageMyListings extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await directus.getListings({
|
const response = await directus.getListings({
|
||||||
|
fields: [
|
||||||
|
'id', 'status', 'title', 'slug', 'price', 'currency',
|
||||||
|
'condition', 'payment_status', 'date_created', 'user_created',
|
||||||
|
'images.directus_files_id.id',
|
||||||
|
'location.id', 'location.name'
|
||||||
|
],
|
||||||
filter: {
|
filter: {
|
||||||
user_created: { _eq: user.id }
|
user_created: { _eq: user.id }
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -327,7 +327,6 @@ class DirectusService {
|
|||||||
'price',
|
'price',
|
||||||
'currency',
|
'currency',
|
||||||
'condition',
|
'condition',
|
||||||
'payment_status',
|
|
||||||
'date_created',
|
'date_created',
|
||||||
'user_created',
|
'user_created',
|
||||||
'images.directus_files_id.id',
|
'images.directus_files_id.id',
|
||||||
|
|||||||
Reference in New Issue
Block a user