How To Integrate Paypal Payment Gateway In Laravel

Websolutionstuff | Jul-22-2020 | Categories : Laravel PHP

In this tutorial I will teach you the most important topic of how to integrate the PayPal payment gateway in laravel, payment integration is a very useful and important feature in an e-commerce website. Here, I am talking about PayPal integration in laravel, in this tutorial, I will explain to you how to integrate the PayPal payment gateway in the latest version of laravel.

PayPal is an American company operating a worldwide online payments system that supports online money transfers and serves as an electronic alternative to traditional paper methods like checks and money orders. So, follow the below steps for laravel paypal integration.

I will help you with laravel PayPal payment gateway integration in very easy steps with screen print.

Step 1: Install Laravel Application for Paypal Integration
Step 2: Create Database Configuration
Step 3: Configuration PHP File Of Paypal
Step 4: Add Route
Step 5: Create PaypalController
Step 6: Create Blade file For View


 Step 1: Install Laravel Application for Paypal Integration

We are creating a new project setup for this laravel payment gateway paypal example. So, create a new project using the below command.

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



 Step 2: Database Configuration

In the second step, we will set up database configuration for the example database name, username, password, etc for our laravel paypal integration. So, let's open the .env file and fill in all details like as below: 

DB_DATABASE=your database name(paypal_integration)
DB_USERNAME=your database username(root)
DB_PASSWORD=your database password(root)


 Step 3: Configuration PHP File Of Paypal

Run the following command in your project to get the latest version of the paypal API package using composer.

composer require paypal/rest-api-sdk-php

After installation of the paypal package, we required client_id and secret key for paypal integration, so we need to login in paypal developer mode and create a new sandbox account for the same.

After login in 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, I 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: Create Route 

 I have created a route as below.


use Illuminate\Support\Facades\Route;

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

I have created a new controller to manage all the PayPal related logic. So, copy the below code in the paypalcontroller.


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 for View

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>How to integrate paypal payment in Laravel -</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;">How to integrate paypal payment in Laravel -</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



And you will get the output of the above code like the below screenshot.

home page

once you will enter the amount you will get a form like this. if you don't have an account then you need to create an account first.




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


Once you will get this view then after it will show another successful message form of your transaction.

success message 

Finally, our tutorial is completed, hope you will get your output like this.



You might also like :

Recommended Post
Featured Post
How To Bind Data In React JS
How To Bind Data In React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the...

Read More


Laravel 8 Mobile Number OTP Authentication using Firebase
Laravel 8 Mobile Number OTP Au...

Hello All, In this tutorial i will show you laravel 8 mobile number OTP authentication using firebase, There are many...

Read More


How to Install Elasticsearch in Laravel 10
How to Install Elasticsearch i...

Hey developers! If you're diving into the world of Laravel 10 and looking to supercharge your application's...

Read More


How to Send Bulk Mail Using Queue in Laravel 9
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using queue in laravel 9. Laravel queue is used for sending bulk...

Read More