Skip to main content

Member Billing and Checkout with the Memberstack DOM Package

Josh Lopez
Josh Lopez
  • Updated

This article explains how to send members to Stripe Checkout and to the Stripe customer portal with the Memberstack DOM package. The Memberstack DOM package provides a JavaScript API for integrating Memberstack into browser-based apps. It enables features like member authentication, member management, and billing without needing to call Memberstack's API yourself. This article assumes you already have the Memberstack DOM package installed and configured in your JavaScript application. It also assumes a basic understanding of JavaScript promises and async/await.

Note: The Memberstack DOM Package can be accessed through the Webflow Package as well by using window.$memberstackDom.purchasePlansWithCheckout(...) instead of memberstack.purchasePlansWithCheckout(...).

1) Initiating Checkout

To send a logged-in member to Stripe Checkout for a paid plan, call the purchasePlansWithCheckout method with a price ID:

try {
  // Sends the member to Stripe Checkout
  await memberstack.purchasePlansWithCheckout({
    priceId: "prc_abc123",
  });
} catch (error) {
  console.error(error.message);
}

This starts a Stripe Checkout session for the plan the price belongs to and sends the member there. Some things to note:

  • The priceId parameter is required. It is the Memberstack price ID, which starts with prc_. You can copy it from the plan's prices under Plans in your dashboard. A Stripe price ID (price_...) or a plan ID does not work: the call fails with "Price doesn't exist".
  • The member must be logged in. Otherwise the call fails with "You must be logged in to perform this action."
  • This returns a promise, so you need to await it or use .then(). If something goes wrong, the promise rejects and the member stays on the page, so handle errors in a try/catch block.
  • Free plans have no prices, so they don't go through checkout. Add them with addPlan({ planId }) instead, as shown in Member Management with the Memberstack DOM Package.

There are additional optional parameters you can pass:

  • cancelUrl - Where to send the member if they leave checkout without paying. If no cancelUrl is provided, they come back to the current page (without its query string).
  • successUrl - Where to send the member after they pay. If no successUrl is provided, they go to the On Signup / On Purchase redirect of their top plan in Plan priority that has one, which is not necessarily the plan they just bought (or your app's default one). If there is none, they go back to the page where checkout started, without its query string. If your app requires email verification and the member isn't verified yet, they go to the On Verification Required redirect instead.
  • autoRedirect - Whether to send the member to Stripe automatically. It defaults to true. Set it to false to get the checkout URL back in data.url instead of redirecting.
  • couponId - A Stripe coupon ID, or a Stripe promotion code's ID (promo_..., not the code customers type), to apply a discount. If you don't pass one, members can enter a promotion code on the Checkout page.
  • metadataForCheckout - String key-value pairs to add to the metadata of the Stripe Checkout session and of the subscription (or, for one-time prices, the payment) it creates.

successUrl and cancelUrl take a full URL or a path that starts with / (such as /welcome), which is added to your site's domain. Query strings on them are dropped. For example, to get the checkout URL without redirecting:

const { data } = await memberstack.purchasePlansWithCheckout({
  priceId: "prc_abc123",
  successUrl: "/welcome",
  cancelUrl: "/pricing",
  autoRedirect: false,
});

// The Stripe Checkout URL, to use when you're ready
console.log(data.url);

2) Managing Billing

To send a logged-in member to the Stripe customer portal, use the launchStripeCustomerPortal method:

try {
  // Sends the member to the Stripe customer portal
  await memberstack.launchStripeCustomerPortal({
    returnUrl: "/account",
  });
} catch (error) {
  console.error(error.message);
}

By default, the portal lets the member update their payment method, see their invoice history and cancel their subscription. Cancellation takes effect at the end of the billing period, unless you turn on Cancel subscriptions immediately under Settings → Stripe in your dashboard. To take away the cancel option, turn on Remove the “Cancel” button in the same settings. Some things to note:

  • The member must be logged in and already have a Stripe customer. A member who doesn't have one yet (for example, one who has never started a checkout) gets the error "No billing information found for this member". To create one for every member at signup, turn on Always create a Stripe customer under Settings → Stripe.
  • returnUrl - Where to send the member when they leave the portal: a full URL or a path that starts with /. If no returnUrl is provided, they come back to the page they opened the portal from (or, if one of their plans changed in the portal and that plan has an On Signup / On Purchase redirect, to that redirect).
  • autoRedirect - It defaults to true. Set it to false to get the portal URL back in data.url instead of redirecting.

Like checkout, this returns a promise, so you'll need to await it or use .then().

That covers the basics of integrating billing and checkout with Memberstack DOM!

Check out the other articles in this series:

  1. Getting Started with the Memberstack DOM Package (Front-end API)
  2. Working with Memberstack DOM Package Modals
  3. Managing App Data with the Memberstack DOM Package
  4. Member Authentication with the Memberstack DOM Package
  5. Member Billing and Checkout with the Memberstack DOM Package
  6. Member Management with the Memberstack DOM Package
  7. Events and Webhooks with Memberstack DOM

Was this article helpful?

Didn’t find your answer?

Get an instant answer from Rey, or reach a human. Either way, we’re happy to help.

Comments

No comments yet. Start the conversation below.

Please sign in to leave a comment.