How to integrate Stripe Payment Gateway in Laravel 7/8
In this article we will learn how to integrate stripe payment gateway in Laravel with example . Stripe is most popular and most commonly used Payment gateway for lots of organizations and e-commerce sites for it's easy and hassle free transaction .
Stripe provides lots of accessibility and most easy to use process for both the user as well as developers . Being one of the most commonly used payment gateway you can use it on your Laravel or PHP projects too . So lets see how can you integrate stripe payment API in your Laravel project to build payment APIs.
Stripe provides lots of accessibility and most easy to use process for both the user as well as developers . Being one of the most commonly used payment gateway you can use it on your Laravel or PHP projects too . So lets see how can you integrate stripe payment API in your Laravel project to build payment APIs.
In this example we will integrate stripe on our project and create customer on stripe using Stripe API .
Content :
- Install Stripe
- Get API keys from Stripe
- Store API keys on your project
- Create route
- Create a customer on stripe
Install Stripe :
Use the following command to install stripe on your project .
composer require stripe/stripe-php
Get API keys from Stripe :
To work with stripe we need to get the APIs keys from Stripe , for that first create your account on stripe. After successful creation on account on stripe , login to your account on stripe and go to Developer option to get API keys and get your API keys from there as shown in the screenshots .
Store API keys on your project :
Now we have to store the API keys on our project . You can store it either in your .env file or any constant file . In this example i will be storing the API keys in a constant file inside config . You can store it wherever you need .
'Stripe_account' => [ 'publishable_key' => 'pk_test_51JFC6****************************', 'secret_key' => 'sk_test_51JFC6r***************************' ]
Create Route :
Create a route to create customer on stripe , so that we can check whether stripe is properly integrated or not .
Route::post('cratecustomer', [paymentController::class,'createCustomerOnStripe']);
Create a customer on Stripe :
Lets create a customer on stripe , use the following codes on your controller to create customer on stripe.
paymentController.php :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class paymentController extends Controller { public function createCustomerOnStripe(Request $request) { try{ $data = $request->all(); $email = $data['email']; // Get stripe secret key from your constants file $stripe_account = Config('constants.Stripe_account'); $stripe_secret = $stripe_account['secret_key']; // create a customer on stripe for the requested email $stripe = new \Stripe\StripeClient( $stripe_secret ); $customer = $stripe->customers->create([ 'email' => $email, 'description' => 'customer '.$email, ]); return $customer; } catch(\Exception $e){ return false; } } }
In this code we are only providing email and description to create a customer , but you can provide more details like address , phone number , postal code and payment method etc. you can also visit the stripes official documentation for more detail .
Output :
Now you can hit your API to check the result , for this I'm using postman to test my API .
for successful response you will get a proper response from stripe with the customer detail created .
Thank you for reading this article 😊
For any query do not hesitate to comment 💬