I was messing with this for days. But I final got there!
Here is my solution:
<script src="https://js.stripe.com/v3/"></script>
<script src="{{ asset('js/my_stripe.js') }}"></script>
<link rel="stylesheet" type="text/css" href="{{ asset('css/stripe.css') }}">
<form action="/user/payment/stripe" method="post" id="payment-form">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12 mb-3">
<label for="firstName">Enter Card Details</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
</div>
</div>
<div id="card-errors" role="alert"></div>
</div>
<button id="complete-orders" class="btn btn-success btn-block">Submit Payment</button>
</form>
<script>
// Create a Stripe client.
var stripe = Stripe('auth stuff here');
// Create an instance of Elements.
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// 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');
// Handle 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;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
var options = {
name: document.getElementById('card-name').value,
address_line1: document.getElementById('card-address').value,
address_city: document.getElementById('card-city').value,
address_state: document.getElementById('card-province').value,
address_zip: document.getElementById('card-postcode').value,
};
document.getElementById('card-city').disable = true;
stripe.createToken(card, options).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;
document.getElementById('card-city').disable = false;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripeTokenHandler(token) {
// Insert 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);
// Submit the form
form.submit();
}
</script>
Controller
\Stripe\Stripe::setApiKey('Auth key');
// Token is created using Stripe Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];
try {
$charge = \Stripe\Charge::create([
'amount' => $this->total,
'currency' => 'gbp',
'description' => Carbon::now().' '.$request->input('lastName'),
'source' => $token,
]);
} catch(\Stripe\Exception\CardException $e) {
$request->session()->flash('fail-message', 'Your payment was declined.');
return redirect()->route('checkout');
} catch (\Stripe\Exception\RateLimitException $e) {
$request->session()->flash('fail-message', 'To many requests to the API.');
return redirect()->route('checkout');
} catch (\Stripe\Exception\InvalidRequestException $e) {
$request->session()->flash('fail-message', 'Invalid parameters.');
return redirect()->route('checkout');
} catch (\Stripe\Exception\AuthenticationException $e) {
$request->session()->flash('fail-message', 'There are problems with authentication.');
return redirect()->route('checkout');
} catch (\Stripe\Exception\ApiConnectionException $e) {
$request->session()->flash('fail-message', 'There is a problem with the network.');
return redirect()->route('checkout');
} catch (\Stripe\Exception\ApiErrorException $e) {
$request->session()->flash('fail-message', 'There is a problem with the API.');
return redirect()->route('checkout');
} catch (Exception $e) {
$request->session()->flash('fail-message', 'We don\'t know what happened.');
return redirect()->route('checkout');
}
Edit: if you want to see there details on your dashboard you can use this, but just follow what I have done and everything will work. Its important to understand that I didn't include all the html so the inputs for the options are missing.
var options = {
name: document.getElementById('card-name').value,
address_line1: document.getElementById('card-address').value,
address_city: document.getElementById('card-city').value,
address_state: document.getElementById('card-province').value,
address_zip: document.getElementById('card-postcode').value,
};
document.getElementById('card-city').disable = true;
stripe.createToken(card, options).then(function(result) {
if (result.error) {