Stripe Input Field Won't Display
I have a Laravel app that has a checkout form with Stripe integrated. For some reason, the credit card input will no longer render on the page. See here: https://videogamestore.best/guest-checkout.
It was there before and now it's not. I'm not sure what I've changed. The public and secret key are still valid and I've edited the CSS for the input to display in bolder colours (just in case the styling was affecting the visibility of the input).
I'm sure the problem occurs because I get this error in the console:
Uncaught SyntaxError: Unexpected end of input
...... guest-checkout:4
So, I've probably misplaced an input tag but I can't find it in the Stripe code:
(function() {
// creates a Stripe client
var stripe = Stripe("{{ config('services.stripe.public') }}");
// creates an instance of Elements
var elements = stripe.elements();
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: 'red',
iconColor: 'red'
}
};
// creates 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');
// handles real-time validation errors from the card Element
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
}
});
// handles form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
// disables the submit button to prevent repeated clicks
document.getElementById('submit-payment').disabled = true;
stripe.createToken(card).then(function(result) {
if (result.error) {
// informs the user if there was an error
// enables the submit button if validation fails
document.getElementById('submit-payment').disabled = false;
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// sends the token to the server
stripeTokenHandler(result.token);
}
});
});
// submits the form with the token ID
function stripeTokenHandler(token) {
// inserts the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// submits the form
form.submit();
}
})();
What's going on here?
Thanks
Answer
I believe the problem is that your HTML with embedded script is being improperly minified, specifically the issue is that the comments are not being removed.
I end up receiving a single line with this in the middle:
<script> (function() { // creates ...
and that //
comment markup is breaking the rest of the JS. You need to ensure your code is properly minified, or move the script to an external resource which skips minification.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM