Skip to content

Instantly share code, notes, and snippets.

@fayez-nazzal
Created May 3, 2022 12:48
Show Gist options
  • Save fayez-nazzal/ec77f6d9b53fbd101e88d1edc7923016 to your computer and use it in GitHub Desktop.
Save fayez-nazzal/ec77f6d9b53fbd101e88d1edc7923016 to your computer and use it in GitHub Desktop.
Stripe NodeJS Mini Guide / Cheatsheet

Creating Customers

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

One Time Payments

Option 1: Checkout Sessions

+ Quick
+ Built in payment methods, address fields and Promotion Code support
- Hosted on stripe -> not customizable

Steps:

  1. Create a Checkout Session
  2. 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

Option 2: Payment Intents

+ 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:

  1. Create a Payment Intent on the server
  2. 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

Option 3: Invoices & Payment Intents

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

Recurring Payments / Subscription

Option 1: Checkout Sessions

+ Quick
+ Built in payment methods, address fields and Promotion Code support
- Hosted on stripe -> not customizable

Steps:

  1. Create a Checkout Session
  2. 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 })

Getting Products List

  • TODO

Getting Subscriptions List

  • TODO

Getting Invoices

  • TODO

Web hooks

  • TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment