All files / server/api/checkout/paypal capture.get.ts

76.47% Statements 13/17
66.66% Branches 4/6
100% Functions 0/0
76.47% Lines 13/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x         2x 2x  
export default defineEventHandler(async event => {
    const config = useRuntimeConfig(event);
    const orderId = getQuery(event).token as string;
    if (!orderId) return sendRedirect(event, "/panier?cancelled=1");
    const token = await paypalToken(config.public.paypalClientId, config.paypalClientSecret);
    const response = await fetch(`https://api-m.paypal.com/v2/checkout/orders/${encodeURIComponent(orderId)}/capture`, {
        method: "POST",
        headers: {Authorization: `Bearer ${token}`, "Content-Type": "application/json"}
    });
    const data = await response.json() as any;
    if (response.ok) {
        const db = database(event);
        await ready(db);
        await db.prepare("INSERT OR IGNORE INTO orders(provider,provider_order_id,amount_cents,status,customer_email,created_at) VALUES('paypal',?,?,?,?,?)").bind(orderId, Math.round(Number(data.purchase_units?.[0]?.payments?.captures?.[0]?.amount?.value || 0) * 100), data.status, data.payer?.email_address || null, new Date().toISOString()).run();
    }
    return sendRedirect(event, response.ok ? "/commande?success=paypal" : "/panier?error=paypal");
});