How To Integrate PayPal In Laravel 8 Tutorial

Hi Artisan,

In this tutorial, i would like to guide you step by step how to integrate PayPal payment gateway in laravel 8 application. We can easily integrate paypal payment gateway in laravel 8. i written laravel 8 paypal integration so your user can easily payment vie paypal account and credit card information.

In this laravel payment gateway integration tutorial i am using integrate paypal api using srmklive laravel paypal package. srmklive/laravel-paypal package provide methods of paypal code api. In this example tutorial you will learn how to use express checkout method in laravel 8 application.

So you need to follow some steps to complete it. In this tutorial, we will use srmklive package for laravel paypal integrate in laravel 8. you just need to follow few steps to getting done payment integration in php laravel 8.

integrate-paypal-payment-gateway-in-laravel-app

 

Visit Developer.paypal.com to create your business account, so that you can buy/sell products easily over the internet and transfer funds to your account.

Next create a PayPal developer mode and create a sandbox account to get important credentials like client_key and secret_key, to test PayPal integration with your PayPal app.

laravel 8 setup paypal

Look here we use packages to integrate PayPal payment gateway in our Laravel application .

 

Read Also Authentication using Facebook, Google, Twitter and Github with Laravel Socialite

 

Getting Started

As we know Paypal payment gateway is a more popular gateway in web development. almost client or people prefer to use paypal payment gateway for money transfer in his website. Paypal is a user friendly gateway to transfer word wide.

Step 1 : Install Laravel Application

 

For getting fresh laravel app, open your terminal OR command prompt and run bellow command:

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

 

Step 2 : Install Required Packages

Now we require to install srmklive/paypal package for paypal integration, that way we can use it's method. So Open your terminal and run bellow command.

composer require srmklive/paypal

Now open config/app.php file and add service provider and aliase.

config/app.php

'providers' => [

	Srmklive\PayPal\Providers\PayPalServiceProvider::class

]

 

We can also custom changes on srmklive/paypal package, so if you also want to changes then you can fire bellow command and get config file in config/paypal.php.

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

 

You can view paypal.php file like as bellow:

config/paypal.php

return [

    'mode'    => env('PAYPAL_MODE', 'sandbox')

    'sandbox' => [

        'username'    => env('PAYPAL_SANDBOX_API_USERNAME', ''),

        'password'    => env('PAYPAL_SANDBOX_API_PASSWORD', ''),

        'secret'      => env('PAYPAL_SANDBOX_API_SECRET', ''),

        'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),

        'app_id'      => 'APP-80W284485P519543T',

    ],

    'live' => [

        'username'    => env('PAYPAL_LIVE_API_USERNAME', ''),

        'password'    => env('PAYPAL_LIVE_API_PASSWORD', ''),

        'secret'      => env('PAYPAL_LIVE_API_SECRET', ''),

        'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),

        'app_id'      => '',

    ],

    'payment_action' => 'Sale',

    'currency'       => env('PAYPAL_CURRENCY', 'USD'),

    'billing_type'   => 'MerchantInitiatedBilling',

    'notify_url'     => '',

    'locale'         => '',

    'validate_ssl'   => false,

];

 

Step 3: Add Routes

Here, we need to add resource route for paypal payment gateway. so open your "routes/web.php" file and add following route.

routes/web.php

Route::get('payment', 'PayPalController@payment')->name('payment');
Route::get('cancel', 'PayPalController@cancel')->name('payment.cancel');
Route::get('payment/success', 'PayPalController@success')->name('payment.success');

 

Step 4: Create Controller

In this step, now we should create new controller as PayPalController. So run bellow command and create new controller. bellow controller for create with some methods.

php artisan make:controller PayPalController

After bellow command you will find new file in this path "app/Http/Controllers/PayPalController.php".

app/Http/Controllers/PayPalController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\ExpressCheckout;
   
class PayPalController extends Controller
{
    
    public function payment()
    {
        $data = [];
        $data['items'] = [
            [
                'name' => 'codechief.org',
                'price' => 100,
                'desc'  => 'Description goes herem',
                'qty' => 1
            ]
        ];
  
        $data['invoice_id'] = 1;
        $data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";
        $data['return_url'] = route('payment.success');
        $data['cancel_url'] = route('payment.cancel');
        $data['total'] = 100;
  
        $provider = new ExpressCheckout;
  
        $response = $provider->setExpressCheckout($data);
  
        $response = $provider->setExpressCheckout($data, true);
  
        return redirect($response['paypal_link']);
    }
   
    public function cancel()
    {
        dd('Sorry you payment is canceled');
    }
  
    public function success(Request $request)
    {
        $response = $provider->getExpressCheckoutDetails($request->token);
  
        if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
            dd('Your payment was successful. You can create success page here.');
        }
  
        dd('Something is wrong.');
    }
}

 

Step 5: Create View File

In this step, we need to update welcome.blade.php file. in this file we will put one button for paypal payment gateway. so let's put bellow code:

resources/views/products/welcome.blade.php

 

Step 6: Add Configuration to .env file

In this step, we will set configuration value like paypal username, secret and certificate key in .env file.

.env

PAYPAL_MODE=sandbox
PAYPAL_SANDBOX_API_USERNAME=sb-e2n47..
PAYPAL_SANDBOX_API_PASSWORD=XKCGW...
PAYPAL_SANDBOX_API_SECRET=A0EXIz....
PAYPAL_CURRENCY=INR
PAYPAL_SANDBOX_API_CERTIFICATE=

 

Now we are ready to run our this application example with laravel 6 so run bellow command for quick run:

php artisan serve

 

then visit this following url to check it. 

http://localhost:8000/

 

Read also : Laravel 6 REST API with Passport Tutorial with Ecommerce Project

 

Hope it can help you.

 

#laravel #paypal-integration #paypal #payment-gateway-integration #guzzlehttp #paypal-payment-gateway