Blog

Home / Blog

Stripe payment gateway integration in laravel 9 tutorial

Learn how to easily integrate the Stripe payment gateway into your Laravel 9 application with this step-by-step tutorial. Streamline your payment processing and enhance user experience with secure and efficient transactions.

Paypal integration in laravel 9

PayPal integration in Laravel 9 allows developers to easily accept payments on their website through the popular online payment platform. With seamless integration, users can securely process transactions and manage orders hassle-free.

Stripe is a popular payment gateway that allows businesses to accept online payments from customers all around the world. Integrating Stripe with a Laravel 9 application is a simple and straightforward process that can significantly improve the user experience of your website or e-commerce platform.

In this tutorial, we will go through the steps required to integrate Stripe payment gateway with a Laravel 9 application. By the end of this tutorial, you will be able to accept credit card payments from your customers securely and efficiently.

Step 1: Create a Stripe Account

The first step in integrating Stripe with Laravel 9 is to create a Stripe account. Go to the Stripe website and sign up for an account. Once you have created your account, you will have access to your API keys, which you will need to integrate Stripe into your Laravel application.

Step 2: Install the Stripe PHP Library

Next, you will need to install the Stripe PHP library into your Laravel 9 project. You can do this using Composer by running the following command in your terminal:

```
composer require stripe/stripe-php
```

Step 3: Set up Your Stripe API Keys

After installing the Stripe PHP library, you will need to set up your API keys in your Laravel 9 application. You can do this by adding your Stripe API keys to your .env file:

```
STRIPE_KEY=your_stripe_public_key
STRIPE_SECRET=your_stripe_secret_key
```

You can find your API keys on the Stripe dashboard after logging in to your account.

Step 4: Create a Payment Form

Now that you have set up your API keys, you can create a payment form where your customers can enter their credit card details to make a payment. In your Laravel 9 application, create a new view file called payment.blade.php and add the following code:

```html


@csrf







```

Step 5: Process the Payment

Next, you will need to create a route in your routes file that will handle the payment processing. In your web.php file, add the following code:

```php
Route::post('/payment/process', 'PaymentController@process')->name('payment.process');
```

Now, create a new controller called PaymentController by running the following command in your terminal:

```
php artisan make:controller PaymentController
```

Then, add the following code to your PaymentController:

```php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use StripeCharge;
use StripeStripe;

class PaymentController extends Controller
{
public function process(Request $request)
{
Stripe::setApiKey(env('STRIPE_SECRET'));

$amount = $request->amount;
$token = $request->stripeToken;

try {
Charge::create(array(
amount => $amount,
currency => usd,
source => $token,
description => Test payment
));

return redirect()->back()->with('success', 'Payment successful');
} catch (Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
}
```

Step 6: Display Payment Status

Finally, you can display the payment status to your customers by updating your payment.blade.php file with the following code:

```html
@if(session('success'))

{{ session('success') }}

@endif

@if(session('error'))

{{ session('error') }}

@endif
```

That's it! You have now successfully integrated Stripe payment gateway with your Laravel 9 application. Your customers can now make secure and efficient payments using their credit cards.

In conclusion, integrating Stripe with Laravel 9 is a simple and straightforward process that can significantly improve the user experience of your website or e-commerce platform. By following the steps outlined in this tutorial, you can start accepting online payments from customers all around the world in no time. Stripe provides a reliable and secure payment gateway that is easy to integrate with Laravel, making it a popular choice for businesses of all sizes.

Payment gateway integration services

Streamline your online payment process with our expert payment gateway integration services. Securely accept payments and increase conversion rates with ease.