Sha256: fbbb6b69b289a79c3382d8298d037ad0aa9295005be342360ae9ba0d41cb34ae

Contents?: true

Size: 1.34 KB

Versions: 27

Compression:

Stored size: 1.34 KB

Contents

// Create a Stripe client.
var stripe = Stripe(window.stripe_publishable_key);

// Create an instance of Elements.
var elements = stripe.elements();

var card = elements.create('card');
card.mount('#card-element');

card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

var form = document.getElementById('stripe-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error.
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server.
      stripeTokenHandler(result.token);
    }
  });
})

function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('stripe-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
stripe-rails-2.6.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.5.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.4.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.5 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.4 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.3 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.2 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.1 app/assets/stripe/stripe_elements.js
stripe-rails-2.3.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.2.1 app/assets/stripe/stripe_elements.js
stripe-rails-2.2.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.1.0 app/assets/stripe/stripe_elements.js
stripe-rails-2.0.0 app/assets/stripe/stripe_elements.js
stripe-rails-1.10.2 app/assets/stripe/stripe_elements.js
stripe-rails-2.0.0.pre app/assets/stripe/stripe_elements.js
stripe-rails-1.10.1 app/assets/stripe/stripe_elements.js
stripe-rails-1.10.0 app/assets/stripe/stripe_elements.js
stripe-rails-1.9.1 app/assets/stripe/stripe_elements.js
stripe-rails-1.9.0 app/assets/stripe/stripe_elements.js
stripe-rails-1.8.2 app/assets/stripe/stripe_elements.js