How to Integrate PayPal Payment Gateway with Laravel

AuthorSumit Dey Sarkar

Pubish Date07 Aug 2023

categoryLaravel

In this tutorial, we will learn how to integrate the PayPal payment gateway into your Laravel application.

 

How to Integrate PayPal Payment Gateway with Laravel

 

How to integrate PayPal payment gateway into a Laravel application:

 

Step 1: Create a Laravel Project

If you haven't done so already, use Composer to start a new Laravel project:

composer create-project --prefer-dist laravel/laravel project-name
cd project-name

 

Step 2: Set Up Your PayPal Account

You need a PayPal Business account to accept payments. Once you have an account, obtain your PayPal API credentials, including the Client ID and Secret Key.

 

Step 3: Install the PayPal SDK

Install the PayPal SDK for PHP using Composer:

composer require paypal/rest-api-sdk-php

 

Step 4: Configure PayPal API Credentials

Add your PayPal API credentials to the .env file of your Laravel project. Open the .env file and add the following lines:

PAYPAL_CLIENT_ID=your_client_id
PAYPAL_SECRET=your_secret_key
PAYPAL_MODE=sandbox # Use 'live' for production

 

Step 5: Create a Controller

Create a controller that handles PayPal payments:

php artisan make:controller PayPalController

 

Step 6: Define Routes

In the routes/web.php file, define the routes for handling PayPal payments:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayPalController;

Route::get('/paypal', [PayPalController::class, 'showPaymentForm']);
Route::post('/paypal', [PayPalController::class, 'makePayment']);
Route::get('/paypal/status', [PayPalController::class, 'paymentStatus']);

 

Step 7: Implement PayPal Payment Logic

In your PayPalController, implement the logic for creating and processing payments. Here's a simplified example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;

class PayPalController extends Controller
{
    private $apiContext;

    public function __construct()
    {
        $this->apiContext = new ApiContext(
            new OAuthTokenCredential(
                config('paypal.client_id'),
                config('paypal.secret')
            )
        );
        $this->apiContext->setConfig(config('paypal.settings'));
    }

    public function showPaymentForm()
    {
        // Display your payment form here
    }

    public function makePayment(Request $request)
    {
        // Create a payment and redirect to PayPal for approval
    }

    public function paymentStatus()
    {
        // Handle payment status after returning from PayPal
    }
}

In the makePayment method, you would create a payment using the PayPal SDK and redirect the user to PayPal for approval. In the paymentStatus method, you would handle the payment status after the user returns from PayPal.

 

Step 8: Create the Payment Form

Create a view file that contains the payment form where users can enter payment details.

 

Step 9: Test the Integration

In your .env file, make sure the PAYPAL_MODE is set to 'sandbox' for testing. You can create a sandbox PayPal account for testing.

 

That's a basic outline of how to integrate PayPal into a Laravel application. Remember that the actual implementation details may vary based on the Laravel version and any updates to the PayPal SDK. Make sure to consult the official Laravel and PayPal documentation for the most up-to-date information.

Comments 0

Leave a comment