ai-flash-card / src / helpers / make-payment.js
make-payment.js
Raw
import getStripe from './get-stripe'

const makePayment = async (title, price) => {
    const checkoutSession = await fetch('/api/checkout_sessions', {
        method: 'POST',
        headers: { 
            contentType: 'application/json',
            origin: window.location.origin 
        },
        body: JSON.stringify({ 
            plan: title, 
            amount: price 
        })
    })
    const checkoutSessionJson = await checkoutSession.json()

    if (checkoutSessionJson.error) {
        console.warn(`Error: ${checkoutSessionJson.message}`)
        return
    }

    const stripe = await getStripe()
    const { error } = await stripe.redirectToCheckout({
        sessionId: checkoutSessionJson.id,
    })

    if (error) {
        console.warn(error.message)
    }
}

export default makePayment;