Hello laravel enthusiasts, welcome back. Here I am going to discuss on something very exciting. You will know how to integrate stripe payment gateway in laravel applications.
Why Stripe ?
There are so many payment gateways available, one of the most popular gateways is PayPal, but Stripe has some advantages over PayPal – simple, hassle free, easier integration, setup, maintenance and seamless checkout experience. Moreover integrating stripe with laravel is a breeze.
We just need a payment form which collects card information like card number, expiry date and a CVC code. When these details are sent to stripe it validates and returns a token which is used to process the payment.
Getting started with Stripe
- First, we need to register with stripe, go to stripe and register yourself there.
- Now get the public and private keys from your account which are provided in your account settings.
- Make sure you set your stripe account as a test account so that real transactions does not occur.
While in test mode, we cannot use genuine card information, so it provides us with some test cards,
Stripe payment form
As said earlier, we need a form to collect some data. There are many ready-made awesome stripe payment form, we will grab one and use it here.
The form I used here is from http://bootsnipp.com/snippets/featured/responsive-stripe-payment-form
We modify this form a bit to fit laravel environment. The form is as below,
We place the public key of our stripe account in the data attribute which we use later for processing the payment process.
The payment form which we used, has some validation to it,
Generate a stripe token
Here, when the submit button is clicked, we do not actually submit the form, we first generate a stripe token which processes the payment and then use that token to Charge the user with specified amount.
Now we get the card number, expiry month, expiry year and CVC code and with these details the stripe generates a token.
Now in this
stripeResponseHandler function, append the token which we got to the form input field,
now the form gets submitted and will call to the action specified in the form action.
When the form is submitted, it calls the following action.
We show, if any, success and error messages and redirect back to the main page.
As we show in the form that we charge $300, here we set the currency to 300 * 100. We multiply by 100 because the default currency unit in Stripe is cent.
Here this tutorial is just a sample tutorial to integrate Stripe, so we set a static amount, also did not use any database to save the transaction details.
If we need to save those details we can save then in the above function and also can get the
amount value from the form.
This is how the stripe dashboard looks,