Step-by-Step Guide: Laravel PayPal Payment Integration with netshell/paypal Package

Spread the love

PayPal is a highly popular payment gateway chosen by many due to its exceptional security and user-friendly interface. If you’re looking to integrate PayPal Payment Integration API into your Laravel 10 application, this comprehensive guide will assist you in achieving just that. In this article, you’ll learn how to seamlessly incorporate PayPal payment integration into your Laravel 5 application with just a few simple steps. We provide a complete example of setting up PayPal Express Checkout from the ground up. By the end of this tutorial, you’ll have a clear preview of how the PayPal API functions within your application.

Also Read: How to rename Git Branch Name with Git Command?

Preview of PayPal Payment Integration:

image 1 Step-by-Step Guide: Laravel PayPal Payment Integration with netshell/paypal Package

Step 1: Installation

In the initial step, we will install ‘netshell/paypal‘ for seamless PayPal payment integration. This package enables us to effortlessly process payments using the PayPal API for our project. To get started, simply execute the following command in your command prompt or terminal:

composer require netshell/paypal dev-master

To proceed, we must include the provider and alias paths in the ‘config/app.php‘ file. Open the mentioned file and insert the following code:

config/app.php
return [
	......
	$provides => [
		......
		......,
		'Netshell\Paypal\PaypalServiceProvider'
	],
	$aliases => [
		.....
		.....,
		'Paypal' => 'Netshell\Paypal\Facades\Paypal'
	]
]

Step 2: Configuration

In this step, it’s essential to configure the PayPal client_id and secret for your payment gateway. Start by opening the ‘services.php‘ file located in the ‘config‘ folder and include the following configuration settings:

Also Read: How to integrate LINKEDIN LOGIN in Laravel 10

config/services.php
return [
	.....
	.....
	'paypal' => [
        'client_id' => 'paypal_client_id',
        'secret' => 'paypal_secret'
    ],
]

Step 3: Route and Controller

In this step, we will create a route for PayPal integration. To achieve this, add the following command to your ‘routes.php‘ file:

routes/web.php
Route::group(['middleware' => ['web']], function () {
    Route::get('payPremium', ['as'=>'payPremium','uses'=>'PaypalController@payPremium']);
    Route::post('getCheckout', ['as'=>'getCheckout','uses'=>'PaypalController@getCheckout']);
    Route::get('getDone', ['as'=>'getDone','uses'=>'PaypalController@getDone']);
    Route::get('getCancel', ['as'=>'getCancel','uses'=>'PaypalController@getCancel']);
});

Next, generate a new ‘PaypalController‘ controller using the following command:

php artisan make:controller PaypalController

After running this command, please copy the code provided below into the newly created file.

app/Http/Controllers/PaypalController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Paypal;

class PaypalController extends Controller
{
    private $_apiContext;

    public function __construct()
    {
        $this->_apiContext = PayPal::ApiContext(
            config('services.paypal.client_id'),
            config('services.paypal.secret'));
        $this->_apiContext->setConfig(array(
            'mode' => 'sandbox',
            'service.EndPoint' => 'https://api.sandbox.paypal.com',
            'http.ConnectionTimeOut' => 30,
            'log.LogEnabled' => true,
            'log.FileName' => storage_path('logs/paypal.log'),
            'log.LogLevel' => 'FINE'
        ));
    }

    public function payPremium()
    {
    	return view('payPremium');
    }

    public function getCheckout(Request $request)
	{
	    $payer = PayPal::Payer();
	    $payer->setPaymentMethod('paypal');
	    $amount = PayPal:: Amount();
	    $amount->setCurrency('USD');
	    $amount->setTotal($request->input('pay'));
	    $transaction = PayPal::Transaction();
	    $transaction->setAmount($amount);
	    $transaction->setDescription('Buy Premium '.$request->input('type').' Plan on '.$request->input('pay'));
	    $redirectUrls = PayPal:: RedirectUrls();
	    $redirectUrls->setReturnUrl(route('getDone'));
	    $redirectUrls->setCancelUrl(route('getCancel'));
	    $payment = PayPal::Payment();
	    $payment->setIntent('sale');
	    $payment->setPayer($payer);
	    $payment->setRedirectUrls($redirectUrls);
	    $payment->setTransactions(array($transaction));
	    $response = $payment->create($this->_apiContext);
	    $redirectUrl = $response->links[1]->href;
	    return redirect()->to( $redirectUrl );
	}

	public function getDone(Request $request)
	{
	    $id = $request->get('paymentId');
	    $token = $request->get('token');
	    $payer_id = $request->get('PayerID');
	    $payment = PayPal::getById($id, $this->_apiContext);
	    $paymentExecution = PayPal::PaymentExecution();
	    $paymentExecution->setPayerId($payer_id);
	    $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
	    print_r($executePayment);
	}

	public function getCancel()
	{
	    return redirect()->route('payPremium');
	}
}

Step 4: Blade View

For the final step, we need to create a ‘payPremium.blade.php‘ file and insert the code below. Within this file, we will showcase premium plans such as Small, Medium, and Advanced. Please add the following code:

Also Read: How to Integrate Stripe Payment Gateway In Laravel 10

resources/views/payPremium.blade.php
@extends('layouts.app')
@section('content')
<style type="text/css">
.db-bk-color-one {
    background-color: #f55039;
}
.db-bk-color-two {
    background-color: #46A6F7;
}
.db-bk-color-three {
    background-color: #47887E;
}
.db-bk-color-six {
    background-color: #F59B24;
}
.db-padding-btm {
    padding-bottom: 50px;
}
.db-button-color-square {
    color: #fff;
    background-color: rgba(0, 0, 0, 0.50);
    border: none;
    border-radius: 0px;
}
.db-button-color-square:hover {
    color: #fff;
    border: none;
}
.db-pricing-eleven {
    margin-bottom: 30px;
    margin-top: 50px;
    text-align: center;
    box-shadow: 0 0 5px rgba(0, 0, 0, .5);
    color: #fff;
    line-height: 30px;
}
.db-pricing-eleven ul {
    list-style: none;
    margin: 0;
    text-align: center;
    padding-left: 0px;
}
.db-pricing-eleven ul li {
    padding-top: 10px;
    padding-bottom: 10px;
    cursor: pointer;
}
.db-pricing-eleven ul li i {
    margin-right: 5px;
}
.db-pricing-eleven .price {
    background-color: rgba(0, 0, 0, 0.5);
    padding: 40px 20px 20px 20px;
    font-size: 60px;
    font-weight: 900;
    color: #FFFFFF;
}
.db-pricing-eleven .price small {
    color: #B8B8B8;
    display: block;
    font-size: 12px;
    margin-top: 22px;
}
.db-pricing-eleven .type {
    background-color: #52E89E;
    padding: 40px 10px;
    font-weight: 900;
    text-transform: uppercase;
    font-size: 30px;
}
.db-pricing-eleven .pricing-footer {
    padding: 10px;
}
.db-pricing-eleven.popular {
    margin-top: 10px;
}
.db-pricing-eleven.popular .price {
	padding-top: 50px;
}
</style>
<div class="container">
   <div class="row text-center">
        <div class="col-md-12">
            <h3>Step-by-Step Guide: Laravel PayPal Payment Integration with netshell/paypal Package</h3>
        </div>
    </div>
 
        <div class="row db-padding-btm db-attached">
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <div class="db-wrapper">
                	{!! Form::open(array('route' => 'getCheckout')) !!}
                		{!! Form::hidden('type','small') !!}
                		{!! Form::hidden('pay',30) !!}
	                    <div class="db-pricing-eleven db-bk-color-one">
	                        <div class="price">
	                            <sup>$</sup>30
	                                <small>per quarter</small>
	                        </div>
	                        <div class="type">
	                            SMALL PLAN
	                        </div>
	                        <ul>
	                            <li><i class="glyphicon glyphicon-print"></i>30+ Accounts </li>
	                            <li><i class="glyphicon glyphicon-time"></i>150+ Projects </li>
	                            <li><i class="glyphicon glyphicon-trash"></i>Lead Required</li>
	                        </ul>
	                        <div class="pricing-footer">
	                            <button class="btn db-button-color-square btn-lg">BOOK ORDER</button>
	                        </div>
	                    </div>
                    {!! Form::close() !!}
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <div class="db-wrapper">
                	{!! Form::open(array('route' => 'getCheckout')) !!}
                		{!! Form::hidden('type','medium') !!}
                		{!! Form::hidden('pay',45) !!}
	                <div class="db-pricing-eleven db-bk-color-two popular">
	                    <div class="price">
	                        <sup>$</sup>45
	                                <small>per quarter</small>
	                    </div>
	                    <div class="type">
	                        MEDIUM PLAN
	                    </div>
	                    <ul>
	                        <li><i class="glyphicon glyphicon-print"></i>30+ Accounts </li>
	                        <li><i class="glyphicon glyphicon-time"></i>150+ Projects </li>
	                        <li><i class="glyphicon glyphicon-trash"></i>Lead Required</li>
	                    </ul>
	                    <div class="pricing-footer">
	                        <button class="btn db-button-color-square btn-lg">BOOK ORDER</button>
	                    </div>
	                </div>
	                {!! Form::close() !!}
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
                <div class="db-wrapper">
                	{!! Form::open(array('route' => 'getCheckout')) !!}
                		{!! Form::hidden('type','advance') !!}
                		{!! Form::hidden('pay',68) !!}
	                <div class="db-pricing-eleven db-bk-color-three">
	                    <div class="price">
	                        <sup>$</sup>68
	                                <small>per quarter</small>
	                    </div>
	                    <div class="type">
	                        ADVANCE PLAN
	                    </div>
	                    <ul>
	                        <li><i class="glyphicon glyphicon-print"></i>30+ Accounts </li>
	                        <li><i class="glyphicon glyphicon-time"></i>150+ Projects </li>
	                        <li><i class="glyphicon glyphicon-trash"></i>Lead Required</li>
	                    </ul>
	                    <div class="pricing-footer">
	                        <button class="btn db-button-color-square btn-lg">BOOK ORDER</button>
	                    </div>
	                </div>
	                {!! Form::close() !!}
                </div>
            </div>
        </div>
</div>
@endsection

We trust you’ll discover our Laravel Blogs to be a valuable resource.

Leave a Comment