Laravel – Paypal Payment Integration example using netshell/paypal package from scratch

Laravel – Paypal Payment Integration example using netshell/paypal package from scratch

In this post we will give you information about Laravel – Paypal Payment Integration example using netshell/paypal package from scratch. Hear we will give you detail about Laravel – Paypal Payment Integration example using netshell/paypal package from scratchAnd how to use it also give you demo for it if it is necessary.

Paypal is very popular payment gateway. Most of the persone are choose paypal as payment gateway because it is very secure and pretty simple way to use. If you want to use Paypal API in your laravel 5 application then this post will help you and you can easily use this. In this post you can lean how to add Paypal payment integration in laravel 5 application. So you have to also need to follow just few step and implement paypal api in your application. I give you full example of paypal express checkout demo from scratch, after finish you can found bellow preview for paypal api.

Preview:

Step 1: Installation

In first step we will install netshell/paypal for Paypal payment integration. this package through we can make payment using Paypal api for our project. so first fire bellow command in your cmd or terminal:

composer require netshell/paypal dev-master

Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.

config/app.php

return [

......

$provides => [

......

......,

'NetshellPaypalPaypalServiceProvider'

],

$aliases => [

.....

.....,

'Paypal' => 'NetshellPaypalFacadesPaypal'

]

]

Step 2: Configration

In this step we have to set paypal client_id and secret for Payment gateway. So first open services.php file in config folder and add bellow config file:

config/services.php

return [

.....

.....

'paypal' => [

'client_id' => 'paypal_client_id',

'secret' => 'paypal_secret'

],

]

Step 3: Route and Controller

in this step will add route for paypal integration. so let’s add bellow command on routes.php file.

app/Http/routes.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']);

});

Ok, now create new PaypalController controller using bellow command:

php artisan make:controller PaypalController

After this commmand copy bellow code on that file.

app/Http/Controllers/PaypalController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpRequests;

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


In last step we require to create payPremium.blade.php file and put bellow code on that file. In this file we will display premium plan like small, medium and advance. So let’s put 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 >

<div >

<div >

<h3>Laravel 5 - Payment Using Paypal</h3>

</div>

</div>

<div >

<div >

<div >

{!! Form::open(array('route' => 'getCheckout')) !!}

{!! Form::hidden('type','small') !!}

{!! Form::hidden('pay',30) !!}

<div >

<div >

<sup>$</sup>30

<small>per quarter</small>

</div>

<div >

SMALL PLAN

</div>

<ul>

<li><i ></i>30+ Accounts </li>

<li><i ></i>150+ Projects </li>

<li><i ></i>Lead Required</li>

</ul>

<div >

<button >BOOK ORDER</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

<div >

<div >

{!! Form::open(array('route' => 'getCheckout')) !!}

{!! Form::hidden('type','medium') !!}

{!! Form::hidden('pay',45) !!}

<div >

<div >

<sup>$</sup>45

<small>per quarter</small>

</div>

<div >

MEDIUM PLAN

</div>

<ul>

<li><i ></i>30+ Accounts </li>

<li><i ></i>150+ Projects </li>

<li><i ></i>Lead Required</li>

</ul>

<div >

<button >BOOK ORDER</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

<div >

<div >

{!! Form::open(array('route' => 'getCheckout')) !!}

{!! Form::hidden('type','advance') !!}

{!! Form::hidden('pay',68) !!}

<div >

<div >

<sup>$</sup>68

<small>per quarter</small>

</div>

<div >

ADVANCE PLAN

</div>

<ul>

<li><i ></i>30+ Accounts </li>

<li><i ></i>150+ Projects </li>

<li><i ></i>Lead Required</li>

</ul>

<div >

<button >BOOK ORDER</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

</div>

</div>

@endsection

Open your browser and try this…..

Hope this code and post will helped you for implement Laravel – Paypal Payment Integration example using netshell/paypal package from scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

43  +    =  49

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US