Adding Stripe Javascript (sitewide, but would only need on single page: /podcast)

Trying to embed a stripe checkout form. It’d need to load the Stripe JS and it’d be great if it was only on a specific page, but I’m not sure how to make it load on the website at all!

It’s…

<!-- Load Stripe.js on your website. -->
<script src="https://js.stripe.com/v3"></script>

<!-- Create a button that your customers click to complete their purchase. Customize the styling to suit your branding. -->
<button
  style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em"...
etc. etc.

I found an undocumented way to add a script loading with
// src: https://js.stripe.com/v3/

But that doesn’t work because Stripe Checkout still requires a script to go in the actual page with :confused:

<button
  style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em"
  id="checkout-button-price_STRINGSTRING"
  role="link"
  type="button"
>
  Checkout
</button>

<div id="error-message"></div>

<script>
(function() {
  var stripe = Stripe('pk_live_STRINGSTRING');

  var checkoutButton = document.getElementById('checkout-button-price_STRINGSTRING');
  checkoutButton.addEventListener('click', function () {
    // When the customer clicks on the button, redirect
    // them to Checkout.
    stripe.redirectToCheckout({
      lineItems: [{price: 'price_STRINGSTRING', quantity: 1}],
      mode: 'payment',
      // Do not rely on the redirect to the successUrl for fulfilling
      // purchases, customers may not always reach the success_url after
      // a successful payment.
      // Instead use one of the strategies described in
      // https://stripe.com/docs/payments/checkout/fulfillment
      successUrl: 'https://sim.show/success',
      cancelUrl: 'https://sim.show/canceled',
    })
    .then(function (result) {
      if (result.error) {
        // If `redirectToCheckout` fails due to a browser or network
        // error, display the localized error message to your customer.
        var displayError = document.getElementById('error-message');
        displayError.textContent = result.error.message;
      }
    });
  });
})();
</script>

Hey @salis, that script-loading trick is indeed what you’ll need here. Then, if you only need it on the /podcast page, I’d suggest adding the <button> and <div id="error-message"></div> code into that page, and then putting everything from the <script> tags into your blog’s Custom Javascript field.

Let us know if that works out!

Thanks @matt! That’s what I tried to do yesterday and again today, but while it looks like the .js is correctly loaded, the rendered button is just plain text.