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.
This commit is contained in:
dwindown
2025-12-26 23:54:13 +07:00
parent 390fde9bf2
commit 293d5bd65d

View File

@@ -86,7 +86,7 @@ serve(async (req) => {
}); });
} }
// Update order status - this will trigger the database trigger // Update order status
const { error: updateError } = await supabase const { error: updateError } = await supabase
.from("orders") .from("orders")
.update({ .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 }), { return new Response(JSON.stringify({ success: true, order_id: order.id }), {
status: 200, status: 200,