Laravel 9 Paypal Payment Gateway Integration

Websolutionstuff | Jan-17-2023 | Categories : Laravel PHP

In this article, we will see laravel 9 paypal payment gateway integration. Here, we will learn how to integrate the paypal payment gateway integration in laravel 9. Most of the merchants accept online payments option for payment gateway integration. A payment gateway is a tool that allows merchants to accept online payments like Stripe, PayPal, Payoneer, and Razorpay.

PayPal is the faster, safer way to send and receive money or make an online payment. Also, it can manage digital wallets, money management, and more. We will use paypal/rest-api-sdk-php package. This SDK is deprecated. You can continue to use it, but no new features or support requests will be accepted.

For alternatives, please visit the current SDK homepage on the PayPal Developer Portal.

So, let's see paypal payment gateway integration in laravel 9, paypal integration in laravel 9, paypal/rest-api-sdk-php laravel 9, and laravel 9 paypal integration.

How to integrate PayPal payment gateway integration in laravel 9



Step 1: Install Laravel 9 Application

Step 2: Setup Database Configuration

Step 3: Install paypal/rest-api-sdk-php Package

Step 4: Configuration paypal.php File

Step 5: Add Route in web.php File

Step 6: Create Controller

Step 7: Create Blade File


Step 1: Install Laravel 9 Application

In this step, we will install the laravel 9 application using the composer command.

composer create-project --prefer-dist laravel/laravel laravel_9_paypal_integration


Step 2: Setup Database Configuration

Now, we will set up database configuration like database name, username, password, etc. So, let's open the .env file and fill in the details like as below.




 Step 3: Install paypal/rest-api-sdk-php Package

In this step, we will install paypal/rest-api-sdk-php package using the composer command. composer is the recommended way to install the SDK. Alternatively, if you prefer not to use Composer, but want to install Paypal SDK, you can do it by direct download.

composer require paypal/rest-api-sdk-php

After installation of the PayPal package, we required a client id and secret key for Paypal integration. So, we need to login into PayPal developer mode and create a new sandbox account for the same.

After login into Paypal, you need to get the client id and secret key like the below screenshot.



Step 4: Configuration paypal.php file

Now, we need to configure the paypal.php file. So, we have manually created one file in config/paypal.php and added the below code.

return [ 
    'client_id' => 'Enter Your Client ID',
	'secret' => 'Enter Your Secret Key',
    'settings' => array(
        'mode' => 'sandbox',
        'http.ConnectionTimeOut' => 1000,
        'log.LogEnabled' => true,
        'log.FileName' => storage_path() . '/logs/paypal.log',
        'log.LogLevel' => 'FINE'


Step 5: Add Route in web.php File

 In this step, we will add routes on the web.php file



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

Route::get('paywithpaypal', array('as' => 'paywithpaypal','uses' => 'PaypalController@payWithPaypal'));
Route::post('paypal', array('as' => 'paypal','uses' => 'PaypalController@postPaymentWithpaypal'));
Route::get('paypal', array('as' => 'status','uses' => 'PaypalController@getPaymentStatus'));



Step 6: Create Controller

Now, we will create the PaypalController.php file and add the following code to that file.


namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use URL;
use Session;
use Redirect;
use Input;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;

class PaypalController extends Controller
    private $_api_context;
    public function __construct()
        $paypal_configuration = \Config::get('paypal');
        $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_configuration['client_id'], $paypal_configuration['secret']));

    public function payWithPaypal()
        return view('paywithpaypal');

    public function postPaymentWithpaypal(Request $request)
        $payer = new Payer();

    	$item_1 = new Item();

        $item_1->setName('Product 1')

        $item_list = new ItemList();

        $amount = new Amount();

        $transaction = new Transaction();
            ->setDescription('Enter Your transaction description');

        $redirect_urls = new RedirectUrls();

        $payment = new Payment();
        try {
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                \Session::put('error','Connection timeout');
                return Redirect::route('paywithpaypal');                
            } else {
                \Session::put('error','Some error occur, sorry for inconvenient');
                return Redirect::route('paywithpaypal');                

        foreach($payment->getLinks() as $link) {
            if($link->getRel() == 'approval_url') {
                $redirect_url = $link->getHref();
        Session::put('paypal_payment_id', $payment->getId());

        if(isset($redirect_url)) {            
            return Redirect::away($redirect_url);

        \Session::put('error','Unknown error occurred');
    	return Redirect::route('paywithpaypal');

    public function getPaymentStatus(Request $request)
        $payment_id = Session::get('paypal_payment_id');

        if (empty($request->input('PayerID')) || empty($request->input('token'))) {
            \Session::put('error','Payment failed');
            return Redirect::route('paywithpaypal');
        $payment = Payment::get($payment_id, $this->_api_context);        
        $execution = new PaymentExecution();
        $result = $payment->execute($execution, $this->_api_context);
        if ($result->getState() == 'approved') {         
            \Session::put('success','Payment success !!');
            return Redirect::route('paywithpaypal');

        \Session::put('error','Payment failed !!');
		return Redirect::route('paywithpaypal');


Step 7: Create Blade File

Now, we need to create one blade file for view. So, add the below code in your paywithpaypal.php file.

	<meta charset="utf-8">
	<title>Laravel 9 Paypal Payment Gateway Integration - Websolutionstuff</title>
	<link href="" rel="stylesheet">
  <script src=""></script>
  <script src=""></script>
	<div class="container">
    <div class="row">    	
        <div class="col-md-8 col-md-offset-2">        	
        	<h3 class="text-center" style="margin-top: 30px;">Laravel 9 Paypal Payment Gateway Integration - Websolutionstuff</h3>
            <div class="panel panel-default" style="margin-top: 60px;">

                @if ($message = Session::get('success'))
                <div class="custom-alerts alert alert-success fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                    {!! $message !!}
                <?php Session::forget('success');?>

                @if ($message = Session::get('error'))
                <div class="custom-alerts alert alert-danger fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                    {!! $message !!}
                <?php Session::forget('error');?>
                <div class="panel-heading"><b>Paywith Paypal</b></div>
                <div class="panel-body">
                    <form class="form-horizontal" method="POST" id="payment-form" role="form" action="{!! URL::route('paypal') !!}" >
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('amount') ? ' has-error' : '' }}">
                            <label for="amount" class="col-md-4 control-label">Enter Amount</label>

                            <div class="col-md-6">
                                <input id="amount" type="text" class="form-control" name="amount" value="{{ old('amount') }}" autofocus>

                                @if ($errors->has('amount'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('amount') }}</strong>
                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Paywith Paypal



Once you will enter the amount you will get a form like this.


After clicking on continue, you will get a success form like the below screenshot.



You might also like:

Recommended Post
Featured Post
How to Generate App Key in Laravel 11
How to Generate App Key in Lar...

Hello, laravel web developers! In this article, we'll see how to generate an app key in laravel 11. In laravel 11 we...

Read More


How To Send Email Using SendGrid In Laravel 9
How To Send Email Using SendGr...

In this article, we will see how to send email using SendGrid in laravel 9. Laravel provides a clean API over...

Read More


How to Generate QR Code in Node.js
How to Generate QR Code in Nod...

In this example we will see how to generate QR Code in Node.js application. In this example we will use qrcode npm...

Read More


How To Check RAM And CPU Usage In Laravel
How To Check RAM And CPU Usage...

In this tutorial, I will show you how to check RAM and CPU usage in laravel in ubuntu OS. Many times we requir...

Read More