[Code] Auto Open Checkout Based on URL Params

Article author
Duncan from Memberstack
  • Updated

You can automatically open a Stripe Checkout session using the following code snippet & URL parameters. This is especially handy when trying to email someone a checkout link.

Update Your Sitewide Footer/Body Code

Place the following code in the BODY section of your site.

<script type="module">
import * as queryString from "https://cdn.jsdelivr.net/npm/native-querystring@1.1.1/+esm";
let ms = window.$memberstackDom

ms.getCurrentMember().then(({ data: member }) => { const params = queryString.parse(window.location.search); let priceId = params['priceId'] if (params['openCheckout'] === "true" && priceId) { member && ms.purchasePlansWithCheckout({ priceId }); } }); </script>

Update Login Page Footer/Body Code

<script type="module">
import * as queryString from "https://cdn.jsdelivr.net/npm/native-querystring@1.1.1/+esm";
let params = queryString.parse(window.location.search);

if (params && params["openCheckout"] === "true") { const REDIRECT_TO = window.location.pathname let priceId = params["priceId"] let qs = queryString.stringify({ priceId, openCheckout: 'true' }); let redirectUrl = `${REDIRECT_TO}?${qs}` const element = document.querySelector('[data-ms-form="login"]'); if (element && redirectUrl) { element.setAttribute('redirect', redirectUrl); element.setAttribute('data-redirect', redirectUrl); } } </script>

Building your Link:

Anytime you want to share a link that auto-opens checkout, add the following URL param to your link:

/YOUR_LOGIN_PAGE?openCheckout=true&priceId=YOUR_PRICE_ID, where YOUR_PRICE_IDis a valid price ID in your Memberstack account.

Limitations

Please be aware that this code will only function for logged-in Members. I recommend linking to your site's login page in case they do need to log in before continuing. 

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.