Laravel - Paypal Payment Integration example using netshell/paypal package from scratch
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 => [
......
......,
'Netshell\Paypal\PaypalServiceProvider'
],
$aliases => [
.....
.....,
'Paypal' => 'Netshell\Paypal\Facades\Paypal'
]
]
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 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
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 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
Open your browser and try this.....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.