Co-authored-by: wolfoftyreso-debug <250630591+wolfoftyreso-debug@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-07-12 14:23:39 +00:00
parent ef1d3e5728
commit 4555906c2d
+15 -9
View File
@@ -103,19 +103,25 @@ async function handleCheckoutCompleted(session: any, env: StripeEnv) {
// Markera ordern som betald genom att matcha stripe_session_id i items[0]. // Markera ordern som betald genom att matcha stripe_session_id i items[0].
const paid = session.payment_status === "paid" || session.status === "complete"; const paid = session.payment_status === "paid" || session.status === "complete";
if (paid && session.id) { if (paid && session.id) {
// Match by scanning recent pending orders for this session id inside items[0]
const { data: rows, error: selErr } = await supabase const { data: rows, error: selErr } = await supabase
.from("orders") .from("orders")
.select("id, items, status") .select("id, items, status")
.contains("items", [{ stripe_session_id: session.id }]); .eq("status", "pending")
.order("created_at", { ascending: false })
.limit(50);
if (selErr) console.error("orders select failed:", selErr); if (selErr) console.error("orders select failed:", selErr);
if (rows?.length) { const match = rows?.find((r: any) =>
for (const row of rows) { Array.isArray(r.items) && r.items.some((it: any) => it?.stripe_session_id === session.id)
if (row.status === "paid") continue; );
await supabase if (match) {
.from("orders") const { error: updErr } = await supabase
.update({ status: "paid", updated_at: new Date().toISOString() }) .from("orders")
.eq("id", row.id); .update({ status: "paid", updated_at: new Date().toISOString() })
} .eq("id", match.id);
if (updErr) console.error("orders update failed:", updErr);
} else {
console.log("No pending order matched session", session.id);
} }
} }
} }