Uncaught ReferenceError: Stripe is not defined - STRIPE ERROR

不问归期 提交于 2020-03-16 03:00:16

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43172843/uncaught-referenceerror-stripe-is-not-defined-stripe-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!