Created
September 27, 2021 14:16
-
-
Save acoyfellow/9b05513d0254664f03ad29d23069b142 to your computer and use it in GitHub Desktop.
Stripe Stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//pseudocode, a mix of frontend + backend javascript: | |
//step1 (frontend): | |
const checkout = async () => { | |
try { | |
let resp = await fetch(`/api/checkout`, { | |
method: "POST", | |
headers: { | |
"content-type": "application/json", | |
}, | |
body: JSON.stringify({ | |
customer: $userData?.stripe?.customer?.id || false, // not all users are customers in stripe yet | |
email: $store.user.email, | |
product: selectedPlan.id, | |
}), | |
}); | |
let session = await resp.json(); | |
stripe.redirectToCheckout({ sessionId: session.id }); | |
} catch (e) { | |
console.error(e); | |
loading = false; | |
} | |
}; | |
// backend | |
let checkoutSession = { | |
success_url, | |
cancel_url, | |
payment_method_types: ['card'], | |
line_items: [ | |
{ | |
price: body.product, | |
quantity: 1, | |
}, | |
], | |
mode: 'subscription', | |
}; | |
if (body.customer) { | |
checkoutSession.customer = body.customer; | |
} else { | |
checkoutSession.customer_email = body.email; | |
}; | |
const session = await stripe.checkout.sessions.create(checkoutSession); | |
// return the session to the client, and redirect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment