Created
April 5, 2018 07:08
-
-
Save pedrogrande/e0f23a57b1bf1e203a0a025ebf8bbad8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var stripe = Stripe('<%= Rails.application.secrets.stripe_pk %>'); | |
var elements = stripe.elements(); | |
var style = { | |
base: { | |
color: '#303238', | |
fontSize: '16px', | |
lineHeight: '48px', | |
fontSmoothing: 'antialiased', | |
'::placeholder': { | |
color: '#ccc', | |
}, | |
}, | |
invalid: { | |
color: '#e5424d', | |
':focus': { | |
color: '#303238', | |
}, | |
}, | |
}; | |
// Create an instance of the card Element | |
var card = elements.create('card', {style: style, hidePostalCode: true}); | |
// Add an instance of the card Element into the `card-element` <div> | |
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 = ''; | |
} | |
}); | |
// Create a token or display an error when the form is submitted. | |
var form = document.getElementById('payment-form'); | |
form.addEventListener('submit', function(event) { | |
event.preventDefault(); | |
function stripeTokenHandler(token) { | |
var form = document.getElementById('payment-form'); | |
var submitButton = document.getElementById('submit-payment'); | |
var hiddenInput = document.createElement('input'); | |
hiddenInput.setAttribute('type', 'hidden'); | |
hiddenInput.setAttribute('name', 'stripeToken'); | |
hiddenInput.setAttribute('value', token.id); | |
form.appendChild(hiddenInput); | |
form.submit(); | |
submitButton.innerHTML = '<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>'; | |
submitButton.disabled = true; | |
} | |
stripe.createToken(card).then(function(result) { | |
if (result.error) { | |
var errorElement = document.getElementById('card-errors'); | |
errorElement.textContent = result.error.message; | |
} else { | |
stripeTokenHandler(result.token); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment