How to implement Stripe with the stripe.js ES Module? (Stripe Checkout, Stripe Connect, Server + Client Side)

I understand that Wix does not easily support stripe in the front-end; adding the prerequisite script tags to HTML header doesn’t seem to work for me, and most of the custom implementations in this thread (https://www.wix. com/corvid/forum/community-discussion/wix-code-stripe?origin=auto_suggest) make use of iFrames. However, with the relatively recent introduction of the ability to install node modules, I am hoping it’s a different story.

My Objective: to integrate Stripe Checkout for Stripe Connect (according to https://stripe. com/docs/payments/checkout/connect). To trigger the Checkout Session when the user clicks the RSVP button.

What I’ve done:

  • I’ve installed the ES module “@stripe/stripe-js” under node_modules.

  • I’ve successfully created a Checkout Session ID in the backend and call it from my page code (see below), ready to redirect the user to the Checkout.

My problem:

  • My loadStripe command is returning null.

  • The README for the ES Module (https://github. com/stripe/stripe-js) states that " If you call loadStripe in a server environment it will resolve to null ." - I am hoping this is the problem.

  • I can’t, for the life of me, get any result other than null.

My code:

Page

import wixData from 'wix-data';
import {createSession} from 'backend/stripeProxy.jsw';
import {loadStripe} from '@stripe/stripe-js';

$w.onReady(function () {
    $w('#RSVPButton').enable();
});

export async function RSVP_click(event) {
    console.log("RSVP button clicked...");

 // Tell user that something is happening
    $w('#RSVPButton').label = "Please wait...";

 // Disable input and button fields
    $w('#inputName').disable();
    $w('#inputTitle').disable();
    $w('#RSVPButton').disable();

 // Configure Stripe with public key and connect account.
    console.log("Configuring Stripe...");
 
    const stripe = await loadStripe('pk_test_XXX', {
        stripeAccount: 'acct_XXX'
    });
    console.log("await loadStripe returns: " + stripe);

    createSession().then(session_id => {
        console.log("Redirecting to checkout...");
        stripe.redirectToCheckout({
            // Instructions from Stripe: Make the id field from the Checkout Session creation API response
            // available to this file, so you can provide it as argument here
            // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
            sessionId: session_id
        }).then(function (result) {
             // Instructions from Stripe: If `redirectToCheckout` fails due to a browser or network
             // error, display the localized error message to your customer
             // using `result.error.message`.
            console.error(result.error.message);
            $w('#redirectReport').text = result.error.message;
        });
    });
}

When the RSVP button is clicked in Preview Mode, it prints…

RSVP button clicked...
Configuring Stripe...
await loadStripe returns:  null
Redirecting to checkout...

… and then nothing happens. :sweat:

I will bow in awe to anyone who can help me make this work.

Unfortunately the redirect will not work on the frontend because you need to import the '<script src=“https://js.stripe.com/v3/”>’ script on the page’s header and then use the .redirectToCheckout() function, something that’s not possible on Wix.

There is a solution, see this comment: https://www.wix.com/corvid/forum/main/comment/5ed563ae8f56de0044c78144

Hi @Shan!

I appreciate that this is an old post but maybe you remember info about mentioned solution as the link you posted to the comment no longer works?

Thanks!

1 Like

Hey everyone,

so has anyone found a (preferably somewhat clean) solution to this?

How can I make use of the Stripe.js library in the Wix frontend?

The only workaround I’ve found so far is to actually redirect to a completely new third-party site just for handling the Stripe.js library call: Redirecting to Stripe Checkout Page - #6 by developersneakybot