fix(webhook): handle Saleor subscription payload format (array)
Some checks failed
Build and Deploy / build (push) Has been cancelled
Some checks failed
Build and Deploy / build (push) Has been cancelled
Saleor sends webhook payloads as arrays in subscription format. This fix extracts the order from the array or object format.
This commit is contained in:
@@ -400,12 +400,24 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
console.log(`Received webhook: ${event} from ${domain}`);
|
console.log(`Received webhook: ${event} from ${domain}`);
|
||||||
console.log("Headers:", { event, domain, apiUrl, hasSignature: !!signature });
|
console.log("Headers:", { event, domain, apiUrl, hasSignature: !!signature });
|
||||||
console.log("Payload keys:", Object.keys(body));
|
console.log("Payload:", JSON.stringify(body).substring(0, 500));
|
||||||
|
|
||||||
if (body.order) {
|
// Handle Saleor subscription payload format (array of events)
|
||||||
console.log("Order ID:", body.order.id);
|
let order = null;
|
||||||
console.log("Order number:", body.order.number);
|
if (Array.isArray(body)) {
|
||||||
console.log("User email:", body.order.userEmail);
|
// Find the order in the array
|
||||||
|
const orderEvent = body.find((item: any) => item.order);
|
||||||
|
if (orderEvent) {
|
||||||
|
order = orderEvent.order;
|
||||||
|
}
|
||||||
|
} else if (body.order) {
|
||||||
|
order = body.order;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order) {
|
||||||
|
console.log("Order ID:", order.id);
|
||||||
|
console.log("Order number:", order.number);
|
||||||
|
console.log("User email:", order.userEmail);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!event) {
|
if (!event) {
|
||||||
@@ -420,9 +432,12 @@ export async function POST(request: NextRequest) {
|
|||||||
return NextResponse.json({ success: true, message: "Event not supported" });
|
return NextResponse.json({ success: true, message: "Event not supported" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = body;
|
if (!order) {
|
||||||
|
console.error("No order found in webhook payload");
|
||||||
|
return NextResponse.json({ error: "No order in payload" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
await handleSaleorWebhook(normalizedEvent, payload);
|
await handleSaleorWebhook(normalizedEvent, { order });
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user