All files / server/utils checkout.ts

100% Statements 31/31
100% Branches 8/8
100% Functions 2/2
100% Lines 31/31

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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35    3x 3x 3x 3x 3x 3x 3x 3x 3x 24x 24x 22x 22x 22x 22x 22x 24x 3x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 1x 1x  
import type {H3Event} from "h3";
 
export async function checkoutLines(
    event: H3Event,
    lines: Array<{ id: number; quantity: number }>,
    promoCode?: string
) {
    const db = database(event);
    await ready(db);
    const raw = [] as Array<{ id: number; name: string; price: number; quantity: number }>;
    for (const line of lines.slice(0, 20)) {
        const row = await db.prepare("SELECT id,name,price_cents FROM products WHERE id=? AND active=1").bind(Number(line.id)).first<any>();
        if (row) raw.push({
            id: row.id,
            name: row.name,
            price: row.price_cents,
            quantity: Math.max(1, Math.min(10, Number(line.quantity) || 1))
        });
    }
    if (!raw.length) throw createError({statusCode: 400, statusMessage: "Panier vide"});
    const {lines: discounted, promoApplied, promoError} = await applyDiscountsToCheckout(event, raw, promoCode);
    return {lines: discounted, promoApplied, promoError};
}
 
export async function paypalToken(clientId: string, secret: string) {
    const auth = btoa(`${clientId}:${secret}`);
    const response = await fetch("https://api-m.paypal.com/v1/oauth2/token", {
        method: "POST",
        headers: {Authorization: `Basic ${auth}`, "Content-Type": "application/x-www-form-urlencoded"},
        body: "grant_type=client_credentials"
    });
    if (!response.ok) throw createError({statusCode: 502, statusMessage: "PayPal indisponible"});
    return (await response.json() as any).access_token as string;
}