Paypal Payment Integration using netshell/paypal in Laravel

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

We know that paypal is a very popular payment gateway. We are most of the person are choose paypal for the payment gateway because it is very secure and also pretty simple way to use. Today now in this post i will show you Paypal Payment Integration by using netshell/paypal in Laravel application.

If we want to use Paypal API in our laravel application then i hope this post will help us and we can easily use this. Now in this post we can lean how to add the Paypal payment integration in laravel application. So we need to follow just few step and implement paypal api in our application. Here I will give you full example of paypal express checkout demo from the scratch, after the finish you can found.

Step 1: Installation

At first need to step we need to install netshell/paypal for the Paypal payment integration. By using this package through we can make payment by using Paypal api for our project. So at first need to fire the bellow command in your cmd or terminal:

composer require netshell/paypal dev-master

Okay now we need to add the provider path and alias path in our config/app.php file. So need open that file and then add the bellow code.

config/app.php

return [
	......
	$provides => [
		......
		......,
		'Netshell\Paypal\PaypalServiceProvider'
	],
	$aliases => [
		.....
		.....,
		'Paypal' => 'Netshell\Paypal\Facades\Paypal'
	]
]
Step 2: Configration

So now in this step we need to set paypal client_id and also secret for Payment gateway. So at first need to open services.php file in config folder and need to add the bellow code on config file:

config/services.php

return [
	.....
	.....
	'paypal' => [
        'client_id' => 'paypal_client_id',
        'secret' => 'paypal_secret'
    ],
]
Step 3: Route and Controller

So in this step we will add need route for paypal integration. So let’s add the bellow command on routes.php file.

app/Http/routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('payPremium', ['as'=>'payPremium','uses'=>'[email protected]']);
    Route::post('getCheckout', ['as'=>'getCheckout','uses'=>'[email protected]']);
    Route::get('getDone', ['as'=>'getDone','uses'=>'[email protected]']);
    Route::get('getCancel', ['as'=>'getCancel','uses'=>'[email protected]']);
});

Okay, now need to create new PaypalController controller by using bellow command:

php artisan make:controller PaypalController

After this command need to copy the bellow code on that 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

Now we are in the last step we need to create a new file as payPremium.blade.php file and need to put the bellow code on that file. So in this file we will display the premium plan as like small, medium and advance. So let’s put the bellow code.

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>Laravel 5 - Payment Using Paypal</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

Read Also : How to convert Object into Array in PHP?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →