Skip to content

Instantly share code, notes, and snippets.

@newhub-spec
Forked from leojbchan/create-payment-intent.ts
Created December 5, 2024 00:35
Show Gist options
  • Save newhub-spec/c3337a35007627edb1bce6afb86a4f38 to your computer and use it in GitHub Desktop.
Save newhub-spec/c3337a35007627edb1bce6afb86a4f38 to your computer and use it in GitHub Desktop.

Revisions

  1. @leojbchan leojbchan created this gist Jul 28, 2021.
    47 changes: 47 additions & 0 deletions create-payment-intent.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import type { NextApiRequest, NextApiResponse } from "next";
    import { LineItem } from "../../utils/types/wooCommerceTypes";
    import { retrieveProductById } from "../../utils/wooCommerceApi";
    import { PaymentIntent } from "../../utils/types/stripeTypes";
    const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

    // retrieve product price from WooCommerce based on ID and multiply by quantity
    const getProductTotal = async (lineItem: LineItem) => {
    let product: LineItem = await retrieveProductById(
    lineItem.product_id.toString()
    );
    let productTotal = lineItem.quantity * (parseFloat(product.price!) * 100);
    return productTotal;
    };

    // map through lineItems and get the total for each lineItem and return sum of totals
    const calculateTotalAmount = async (lineItems: LineItem[]) => {
    const total = await Promise.all(
    lineItems.map((lineItem) => {
    return getProductTotal(lineItem);
    })
    ).then((res) => {
    return res.reduce((curr, next) => curr + next);
    });
    return total;
    };

    export default async function handler(
    req: NextApiRequest,
    // NOTE: not necessary to define at the moment because the response has an <any> type //
    res: NextApiResponse<PaymentIntent>
    ) {
    const data: LineItem[] = req.body;

    try {
    const paymentIntent = await stripe.paymentIntents.create({
    amount: await calculateTotalAmount(data),
    currency: "gbp",
    });
    res.json({
    paymentIntentId: paymentIntent.id,
    clientSecret: paymentIntent.client_secret,
    });
    } catch (error) {
    throw new Error(error);
    }
    }