Integrate Paypal Payment Gateway In Laravel Full Step By Step Guide

image
image
image
image
image
image
image
image
Integrate PayPal Payment Gateway in Laravel – Full Step-by-Step Guide

Integrate PayPal Payment Gateway in Laravel – Full Step-by-Step Guide

Integrating a payment gateway is a critical step for any e-commerce or service-based website. PayPal is one of the most popular and widely trusted gateways globally. In this tutorial, we’ll walk you through the process of integrating the PayPal payment gateway in a Laravel application using the latest methods.

By the end of this guide, you'll have a working PayPal checkout integrated into your Laravel app.


Prerequisites

Before we start, make sure you have the following:

  1. Laravel (v8 or higher recommended)
  2. Composer
  3. A PayPal Developer Account
  4. Basic knowledge of Laravel (routes, controllers, views)
  5. PHP 7.4 or higher


Step 1: Set Up PayPal Developer Account

  1. Go to PayPal Developer and log in.
  2. Navigate to Dashboard > My Apps & Credentials.
  3. Under Sandbox, click “Create App”.
  4. Name your app and link it to your sandbox business account.
  5. After creating, you’ll get Client ID and Secret.

You’ll use these credentials for testing.


Step 2: Install PayPal SDK Package

We’ll use paypal/rest-api-sdk-php or an abstraction like srmklive/paypal for Laravel.

Install the package via composer:

composer require srmklive/paypal

Then publish the config:

php artisan vendor:publish --provider="Srmklive\PayPal\PayPalServiceProvider"


Step 3: Configure PayPal Credentials

Edit your .env file and add:

PAYPAL_MODE=sandbox
PAYPAL_SANDBOX_CLIENT_ID=YourSandboxClientID
PAYPAL_SANDBOX_CLIENT_SECRET=YourSandboxSecret
PAYPAL_CURRENCY=USD

Then update the config/paypal.php file if needed, or ensure it reads from the .env file.


Step 4: Create Routes

Add the following routes to routes/web.php:

use App\Http\Controllers\PayPalController;

Route::get('/paypal/checkout', [PayPalController::class, 'checkout'])->name('paypal.checkout');
Route::get('/paypal/checkout-success', [PayPalController::class, 'success'])->name('paypal.success');
Route::get('/paypal/checkout-cancel', [PayPalController::class, 'cancel'])->name('paypal.cancel');


Step 5: Create the Controller

Generate a controller:

php artisan make:controller PayPalController

Add the following code to PayPalController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;

class PayPalController extends Controller
{
public function checkout()
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();

$provider->setAccessToken($token);
$order = $provider->createOrder([
"intent" => "CAPTURE",
"application_context" => [
"return_url" => route('paypal.success'),
"cancel_url" => route('paypal.cancel'),
],
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => "10.00"
]
]
]
]);

foreach ($order['links'] as $link) {
if ($link['rel'] === 'approve') {
return redirect()->away($link['href']);
}
}

return redirect()->route('home')->with('error', 'Something went wrong.');
}

public function success(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();
$provider->setAccessToken($token);
$result = $provider->capturePaymentOrder($request->token);

if ($result['status'] === 'COMPLETED') {
return redirect()->route('home')->with('success', 'Payment successful!');
}

return redirect()->route('home')->with('error', 'Payment failed.');
}

public function cancel()
{
return redirect()->route('home')->with('error', 'You have canceled the transaction.');
}
}


Step 6: Test the Integration

  1. Visit /paypal/checkout.
  2. You'll be redirected to the PayPal sandbox.
  3. Log in with a sandbox buyer account.
  4. Approve the payment.
  5. You’ll be redirected to the success or cancel URL.


Optional: Add Frontend Button

In your blade file (e.g., welcome.blade.php):

<a href="{{ route('paypal.checkout') }}" class="btn btn-primary">Pay $10 with PayPal</a>


Going Live

Once you're ready to go live:

  1. Switch PAYPAL_MODE=live
  2. Replace PAYPAL_SANDBOX_CLIENT_ID and SECRET with your Live credentials
  3. Update URLs accordingly if your live site has different routes/domains


Troubleshooting

  1. 401 Unauthorized: Check credentials.
  2. Empty redirect URL: Make sure your return/cancel URLs are publicly accessible.
  3. Currency not supported: Use USD or check supported currencies for your account.


Conclusion

Integrating PayPal with Laravel is quite seamless with the help of packages like srmklive/paypal. Whether you're building a product marketplace, SaaS app, or donation platform, this setup will get your payment process running smoothly.

If you want to support subscriptions, invoicing, or advanced analytics, consider using PayPal’s other API endpoints.