24

I'm trying to implement Stripe elements into my rails app, but I can't seem to include the stripe.js correctly. Here is my application.html

<%= tag :meta, name: "stripe-key", content: Figaro.env.stripe_publishable_key %>
<script type="text/javascript" src="https://js.stripe.com/v3/"</script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

JS

var stripe = Stripe($("meta[name='stripe-key']").attr("content"))
var elements = stripe.elements();

var card = elements.create('card', {
  style: {
    base: {
      iconColor: '#999',
      color: '#505652',
      lineHeight: '40px',
      fontWeight: 300,
      fontFamily: 'Helvetica Neue',

      '::placeholder': {
        color: '#CFD7E0',
      },
    },
  }
});
// Add an instance of the card UI component into the `card-element` <div>
card.mount('#card-element');

FORM

<form action="/charge" method="post" id="payment-form">
  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element">
    </div>
    <div id="card-errors"></div>
  </div>

  <button>Submit Payment</button>
</form>

Every time I load the page I get this error in the console Uncaught ReferenceError: Stripe is not defined - STRIPE ERROR. I think it has something the to do with the way I'm loading stripe.js but I'm not sure?

2
  • 2
    First script tag not properly formatted. Commented Apr 2, 2017 at 19:54
  • I had a similar error. Mine was because I was calling stripe inside another function. If you remove "var" from in front of "var stripe", it means that stripe is now GLOBAL and not LOCAL, which solved the issue for me. I also waited until page load until performing any actions. Commented Dec 20, 2019 at 23:55

6 Answers 6

35

I suspect what's happening is that Stripe.js is loading AFTER your own javascript. Try moving Stripe.js above your own javascript in the header.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you explain more about why that matters?
8

Stripe.js is now loaded after all other scripts, which means it isn't available immediately anymore. I have moved script from body tag to head tag.

 <head>
   <script src="https://js.stripe.com/v3/"></script>
 </head>

1 Comment

While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
7

What tripped me off is that their documentation have tutorials where they skip the initialisation of the Stripe library. So if you start from one of these tutorials and try their sample code it just won't work.

Instead you need to add this line somewhere in your script:

var stripe = Stripe("your_stripe_publishable_key");

Comments

5

Might be late, but in case someone else have the same issue, just add the following in your

<HEAD></HEAD>
<script src="https://js.stripe.com/v2/"></script>

Knowing that they recommend migrating to v3 asap.

Comments

4

You might use something like this

<%= javascript_include_tag 'https://js.stripe.com/v3/', 'data-turbolinks-track': 'reload' %>

Comments

0

Just init stripe just if function Stripe() is a function, and go retry with an interval every 500ms till the function is defined.

After that, clear the interval.

So you can defer the stripe script and increase page load speed

/// init stripe var
var stripe; var stripeElements;
var setStripe = setInterval(function(){
    if (typeof Stripe === "function"){
        stripe = Stripe('YOUR PUBLIC KEY');
        stripeElements = stripe.elements();
        clearInterval(setStripe);
    } 
},500);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.