Attaching a customer to every payment might be very useful, In case of recurring payments or invoices, it is required .
Note: Code samples are minimal to only focus on Stripe, you might want to wrap in try-catch blocks for error handling.
const stripeAPI = require("stripe")(process.env.STRIPE_SECRET_KEY);
// all properties of `customers.create` is optional
const customer = await stripeAPI.customers.create({
name: customer_name,
email: customer_email,
// the ID of the payment_method to attach to the user, usually created in the client side & taken from request body.
payment_method: payment_method,
})
In this gist, we will use customers for Stripe Subscriptions & Invoices.
Stripe docs reference: https://stripe.com/docs/api/customers/create?lang=node
+ Quick
+ Built in payment methods, address fields and Promotion Code support
- Hosted on stripe -> not customizable
Steps:
- Create a Checkout Session
- Redirect to Checkout
Note: Code samples are minimal to only focus on Stripe, you might want to wrap in try-catch blocks for error handling.
const stripeAPI = require("stripe")(process.env.STRIPE_SECRET_KEY);
const session = await stripeAPI.checkout.sessions.create({
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
line_items: [
// price_id's are taken from the stripe dashboard
{price: product1_price_id, quantity: 1},
{price: product2_price_id, quantity: 1},
],
mode: 'payment', // one time payment
});
// Send the session ID with the response
res.json({ status: "success", session_id: session.id })
Note that the stripeAPI.checkout.sessions.create
returns a session object, the id
property of that object is required for Step 2: Redirect to Checkout
which is performed in the front-end:
stripe.redirectToCheckout({
sessionId: session.id
})
Stripe docs reference: https://stripe.com/docs/api/checkout/sessions/create?lang=node
+ Can be customized to look like your brand flavour ( in frontend )
- Takes more time.
- Built-in features are fewer than checkout sessions.
- You have to provide the price explicitly in your code.
Steps:
- Create a Payment Intent on the server
- Confirm payment on the client / front-end using the payment intent's client secret
Note: Code samples are minimal to only focus on Stripe, you might want to wrap in try-catch blocks for error handling.
const stripeAPI = require("stripe")(process.env.STRIPE_SECRET_KEY);
const paymentIntent = await stripeAPI.paymentIntents.create({
// amount may be taken from the client side
amount: amount,
// amount may be taken from the client side
currency: currency,
// the ID of the payment_method to attach to the user, usually created in the client side & taken from request body.
payment_method: payment_method,
});
// Send the clientSecret of the paymentIntent with the response
res.json({ status: "success", clientSecret: paymentIntent.client_secret })
await stripe!.confirmCardPayment(clientSecret, {
payment_method: id, // same id that was given to the request
})
In the client, confirming payments looks like the following (ReactJS):
Stripe docs reference: https://stripe.com/docs/api/payment_intents/create?lang=node
The advantage of this approach is when you have a fixed list of products in the stripe dashboard, which have specified pricing there. You might want to charge using the product's price in your Stripe dashboard without explicitly providing it in the code.
The steps are the following
+ Quick
+ Built in payment methods, address fields and Promotion Code support
- Hosted on stripe -> not customizable
Steps:
- Create a Checkout Session
- Redirect to Checkout
Note: Code samples are minimal to only focus on Stripe, you might want to wrap in try-catch blocks for error handling.
const stripeAPI = require("stripe")(process.env.STRIPE_SECRET_KEY);
const session = await stripeAPI.checkout.sessions.create({
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
line_items: [
// price_id's are taken from the stripe dashboard
// The price_id must be recurring, *not one time*
{price: product1_price_id, quantity: 1},
{price: product2_price_id, quantity: 1},
],
mode: 'subscription', // recurring payment
});
// Send the session ID with the response
res.json({ status: "success", session_id: session.id })
- TODO
- TODO
- TODO
- TODO