Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Created September 27, 2021 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoyfellow/9b05513d0254664f03ad29d23069b142 to your computer and use it in GitHub Desktop.
Save acoyfellow/9b05513d0254664f03ad29d23069b142 to your computer and use it in GitHub Desktop.
Stripe Stuff
//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