Integrating Stripe to your app can be separated into two parts, frontend validation and submitting orders through backend server
I”ll be going over frontend validation in this post. This process is to validate to see if your credit card is valid. This is a complex alogoritm luckily stripe has provided this for us.
First We need to create Stripe Client and create instance of elements
12var stripe = Stripe('pk_test_KOdpJMtqiqwsnwz6D0XXXXX'); // You test api keysvar elements = stripe.elements();Your payment form must have an id of ‘payment-form’
123456789101112131415<form action="/charge" method="post" id="payment-form"><div class="form-row"><label for="card-element">Credit or debit card</label><div id="card-element"><!-- a Stripe Element will be inserted here. --></div><!-- Used to display Element errors --><div id="card-errors" role="alert"></div></div><button>Submit Payment</button></form>Custom styling can be passed to options when creating an Element.
Create an instance of the card Element
Add an instance of the card Element into the
card-element
div1234567891011121314// Custom styling can be passed to options when creating an Element.var style = {base: {// Add your base input styles here. For example:fontSize: '16px',lineHeight: '24px'}};// Create an instance of the card Elementvar card = elements.create('card', {style: style});// 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
12345678card.addEventListener('change', function(event) {var displayError = document.getElementById('card-errors');if (event.error) {displayError.textContent = event.error.message;} else {displayError.textContent = '';}});Handle form submission - When form is submitted, stripe will create token.
12345678910111213141516// Handle form submissionvar form = document.getElementById('payment-form');form.addEventListener('submit', function(event) {event.preventDefault();stripe.createToken(card).then(function(result) {if (result.error) {// Inform the user if there was an errorvar errorElement = document.getElementById('card-errors');errorElement.textContent = result.error.message;} else {// Send the token to your serverstripeTokenHandler(result.token);}});});Create token - this will create an hidden input field with the stripe token, which will be used in the server backend
12345678910111213function stripeTokenHandler(token) {// Insert the token ID into the form so it gets submitted to the servervar 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 formform.submit();}
Links
[StripeJS Doc] (https://stripe.com/docs/stripe.js)
[StripeJS Element] (https://stripe.com/docs/elements)
[My Express Github Shopping Cart] (https://github.com/kuanhsuh/express-shoppingCart)