简体   繁体   中英

Stripe redirectToCheckout - “TypeError: stripe.redirectToCheckout is not a function” Rails

I have a syntax problem I guess but I cannot find why:

in my show.html.erb file, where I want to redirect to checkout:

 <script>
    const paymentButton = document.getElementById('pay');
    paymentButton.addEventListener('click', () => {
      const stripe = '<%= Rails.configuration.stripe[:publishable_key]%>';
      stripe.redirectToCheckout({
        sessionId: '<%=@order.checkout_session_id %>'
      });
    });
  </script>

In my stripe.rb:

if Rails.env.production?
Rails.configuration.stripe = {
  publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
  secret_key:      ENV['STRIPE_SECRET_KEY'],
  signing_secret:  ENV['STRIPE_WEBHOOK_SECRET_KEY']
  }
else
  Rails.configuration.stripe = {
    publishable_key: ENV['STRIPE_PUBLISHABLE_TEST_KEY'],
    secret_key:      ENV['STRIPE_SECRET_TEST_KEY'],
    signing_secret:  ENV['STRIPE_WEBHOOK_SECRET_TEST_KEY']
  }
end

The redirection to stripe checkout doesn't work with:

const stripe = '<%= Rails.configuration.stripe[:publishable_key]%>';

Although everything works perfectly with this:

const stripe = Stripe('<%= ENV['STRIPE_PUBLISHABLE_KEY'] %>');

Thanks for your help!

const stripe = '<%= Rails.configuration.stripe[:publishable_key]%>';

This line is assigning the publishable key to a string constant.

const stripe = Stripe('<%= ENV['STRIPE_PUBLISHABLE_KEY'] %>');

This line is initializing an instance of Stripe.js passing the publishable key.

Assuming the configuration is correct and Rails.configuration.stripe[:publishable_key] contains the publishable key, (you can check this in the console or logging it out with the Rails.logger ) you likely want:

var stripe = Stripe('<%= Rails.configuration.stripe[:publishable_key] %>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM