export async function POST(request: Request) {
const order = await resolveOrder(await request.json());
const idempotencyKey = `checkout-${order.id}`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10_000);
try {
const response = await fetch(
"https://merchant-demo.zahlo.eu/api/v1/checkout-sessions",
{
signal: controller.signal,
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.ZAHLO_API_KEY!,
"Idempotency-Key": idempotencyKey,
},
body: JSON.stringify({
mode: "embedded",
amountMinor: order.totalMinor,
currency: order.currency,
method: "card",
orderId: order.id,
}),
}
);
const body = await response.json();
return Response.json(body, {
headers: { "Cache-Control": "no-store" },
status: response.status,
});
} catch (error) {
if (error instanceof DOMException && error.name === "AbortError") {
return Response.json(
{
error: {
code: "timeout",
message: "Retry with the same Idempotency-Key.",
retryable: true,
},
},
{ status: 504 }
);
}
throw error;
} finally {
clearTimeout(timeout);
}
}