Implementing Payment Gateway in Laravel (Stripe, Razorpay, PayPal, etc.)

Implementing Payment Gateway in Laravel (Stripe, Razorpay, PayPal, etc.)

Introduction

Integrating a payment gateway in Laravel is essential for handling online transactions. In this guide, we will cover the integration of Stripe, Razorpay, and PayPal in a Laravel application.

1. Installing Required Packages

For each payment gateway, install the required package using Composer:

# Stripe
composer require stripe/stripe-php

# Razorpay
composer require razorpay/razorpay

# PayPal
composer require paypal/rest-api-sdk-php

2. Configuring Environment Variables

Add your payment gateway credentials to the .env file:

# Stripe
STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

# Razorpay
RAZORPAY_KEY=your-razorpay-key
RAZORPAY_SECRET=your-razorpay-secret

# PayPal
PAYPAL_CLIENT_ID=your-paypal-client-id
PAYPAL_SECRET=your-paypal-secret
PAYPAL_MODE=sandbox

3. Implementing Stripe Payment

In routes/web.php, define a route:

use App\Http\Controllers\PaymentController;
Route::post('/stripe/payment', [PaymentController::class, 'stripePayment']);

Create the PaymentController and add the Stripe payment logic:

use Stripe\Stripe;
use Stripe\Charge;
use Illuminate\Http\Request;

public function stripePayment(Request $request)
{
    Stripe::setApiKey(env('STRIPE_SECRET'));
    
    $charge = Charge::create([
        'amount' => $request->amount * 100,
        'currency' => 'usd',
        'source' => $request->stripeToken,
        'description' => 'Payment using Stripe',
    ]);
    
    return response()->json(['success' => 'Payment successful!']);
}

4. Implementing Razorpay Payment

Add the Razorpay route:

Route::post('/razorpay/payment', [PaymentController::class, 'razorpayPayment']);

Add the Razorpay payment logic in the controller:

use Razorpay\Api\Api;

public function razorpayPayment(Request $request)
{
    $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
    
    $payment = $api->payment->fetch($request->payment_id);
    
    if ($payment->status == 'captured') {
        return response()->json(['success' => 'Payment successful!']);
    }
    return response()->json(['error' => 'Payment failed!']);
}

5. Implementing PayPal Payment

Add the PayPal route:

Route::get('/paypal/payment', [PaymentController::class, 'paypalPayment']);

Implement the PayPal payment logic:

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;

public function paypalPayment()
{
    $apiContext = new ApiContext(new OAuthTokenCredential(
        env('PAYPAL_CLIENT_ID'),
        env('PAYPAL_SECRET')
    ));
    
    $payment = new Payment();
    // Configure PayPal payment settings here
    
    return response()->json(['success' => 'Payment processed!']);
}

Conclusion

By following this guide, you can successfully integrate multiple payment gateways (Stripe, Razorpay, and PayPal) into your Laravel application. Choose the gateway that best suits your business needs.

© 2025 Developer Sahayak. All Rights Reserved.

0 Comments