Stripe is the most popular and secure payment gateway. In this article, we will integrate stripe(server-side) in laravel step by step.
1. Let's create a new laravel project.
2. Install stripe-php package.
3. Create a stripe.php file inside the config folder and add this code.
4. Open .env file and add the stripe secret key environment variable.
Stripe API Keys - https://dashboard.stripe.com/account/apikeys
Note:- For sandbox keys click on test mode option on the stripe dashboard.
Stripe API
Stripe API provides a bunch of methods to simplify the integration process. stripe payment is divided into two steps.
(a). Tokenization:- In this step stripe collects sensitive card or bank account details and returns a token.
Ref:- Stripe API Reference - Create a card token
(b). Charge:- In this step stripe will charge the amount using the token created during tokenization
Note:- amount should be passed in the smallest currency unit. In the above example, 2000 is 20USD. description is optional.
you can also pass extra data in form of key-value pair in metadata field.
Ref:-Stripe API Reference - Create a charge
So let's continue with the integration...
5. Create a payment.blade.php inside the resources/views directory and this code.
Note:- please checkout github repo - https://github.com/ultimateakash/laravel-stripe
6. Create a PaymentController
7. Open PaymentController.php and add this code.
8. Open web.php and add this code.
9. Finally open http://localhost/laravel-stripe/public and test the integration.
For Testing:-
Card Number: 4242424242424242
Month/Year: Any future date
CVV: Any 3 digits
Ref:- Testing Cards | Stripe
If you facing any issues. please post a comment. I will be happy to help you,
Thanks