From 293d5bd65da31d3ebeefeade2c6bf9fd764ce03d Mon Sep 17 00:00:00 2001 From: dwindown Date: Fri, 26 Dec 2025 23:54:13 +0700 Subject: [PATCH] Fix: Call handle-order-paid directly from webhook instead of relying on DB trigger The database trigger approach wasn't working because the trigger either doesn't exist or the required database settings (app.base_url, app.service_role_key) aren't configured. This is a simpler, more reliable solution: - pakasir-webhook updates order to 'paid' status - pakasir-webhook then directly calls handle-order-paid edge function - No dependency on database triggers or settings This matches how other parts of the system work - direct function calls with environment variables, not database triggers. --- supabase/functions/pakasir-webhook/index.ts | 27 +++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/supabase/functions/pakasir-webhook/index.ts b/supabase/functions/pakasir-webhook/index.ts index 98a6602..7277891 100644 --- a/supabase/functions/pakasir-webhook/index.ts +++ b/supabase/functions/pakasir-webhook/index.ts @@ -86,7 +86,7 @@ serve(async (req) => { }); } - // Update order status - this will trigger the database trigger + // Update order status const { error: updateError } = await supabase .from("orders") .update({ @@ -109,7 +109,30 @@ serve(async (req) => { }); } - console.log("[WEBHOOK] Order updated to paid:", order.id, "- Trigger will handle the rest"); + console.log("[WEBHOOK] Order updated to paid:", order.id, "- Calling handle-order-paid"); + + // Call handle-order-paid edge function directly to process the order + try { + const handlePaidUrl = `${SUPABASE_URL}/functions/v1/handle-order-paid`; + await fetch(handlePaidUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${PAKASIR_WEBHOOK_SECRET || 'anonymous'}`, + }, + body: JSON.stringify({ + order_id: order.id, + user_id: order.user_id, + total_amount: order.total_amount, + payment_method: payload.payment_method || "unknown", + payment_provider: "pakasir", + }), + }); + console.log("[WEBHOOK] Called handle-order-paid successfully"); + } catch (error) { + console.error("[WEBHOOK] Failed to call handle-order-paid:", error); + // Don't fail the webhook response if handle-order-paid fails + } return new Response(JSON.stringify({ success: true, order_id: order.id }), { status: 200,