Laravel 5.4 Paypal integration – Paypal Payment Gateway

Laravel 5.4 Paypal integration – Paypal Payment Gateway

In this Post We Will Explain About is Laravel 5.4 Paypal integration – Paypal Payment Gateway With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Paypal payment gateway integration in Laravel 5.4 source code example Example

In this post we will show you Best way to implement Learn how to integrate Paypal payment gateway with Laravel 5.4 , hear for Set up Paypal Payment Gateway Integration in Laravel PHP Examplewith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Simple PHP Paypal integration in laravel 5.4 on php based. When we are going to build a simple e-commerce website step by step we must have any one create payment geteway to make a payment. as well as Paypal is a global currency convert and common way to send a simple bank local money transfer money by online. Lets learn how to integrate steps paypal payment gateway with laravel 5.4

Step 1 : Install laravel 5.4

laravel new laravel54

//Devloped by Pakainfo.com 
Crafting application...
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 59 installs, 0 updates, 0 removals
  - Installing doctrine/inflector (v1.1.0): Loading from cache
  - Installing erusev/parsedown (1.6.3): Loading from cache
  - Installing jakub-onderka/php-console-color (0.1): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.4.0): Loading from cache
  - Installing symfony/var-dumper (v3.3.5): Loading from cache
  - Installing psr/log (1.0.2): Loading from cache
   //check error
  - Installing symfony/debug (v3.3.5): Loading from cache
  //check error
  - Installing symfony/console (v3.3.5): Loading from cache
  - Installing nikic/php-parser (v3.0.6): Loading from cache
  - Installing jakub-onderka/php-console-highlighter (v0.3.2): Loading from cache
   //check error
  - Installing dnoegel/php-xdg-base-dir (0.1): Loading from cache
   //check error
  - Installing psy/psysh (v0.8.9): Loading from cache
   //check error
  - Installing vlucas/phpdotenv (v2.4.0): Loading from cache
   //check error
  - Installing symfony/css-selector (v3.3.5): Loading from cache
  - Installing tijsverkoyen/css-to-inline-styles (2.2.0): Loading from cache
  .............

It shall simple install cmd the latest version of more features laravel to your web-application.

Step 2 : Configure your database

In mycase simple settings.env:

//Devloped by Pakainfo.com 
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel54
DB_USERNAME=root
DB_PASSWORD=root

Step 3 : Install guzzlehttp

//Devloped by Pakainfo.com 
composer require guzzlehttp/guzzle

//Devloped by Pakainfo.com 
Using version ^6.3 for guzzlehttp/guzzle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.0): Loading from cache
Writing lock file
Generating optimized autoload files

It will install some latest new version for guzzlehttp

Also Read This 👉   jQuery Ajax GET & POST REQUEST Methods PHP MySQLi

Step 4 : Install Paypal PHP SDK

//Devloped by Pakainfo.com 
composer require paypal/rest-api-sdk-php:*

//Devloped by Pakainfo.com 
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing paypal/rest-api-sdk-php (1.12.0): Loading from cache
Writing lock file
Generating optimized autoload files

It will simple install latest version php-paypal-rest-apk to your sytem vendor folder

Step : 5 Run vendor publish command

php artisan vendor:publish

It will publish simple live_paypal.php file steps under config folder same dir. If it does’t make a file live_paypal.php dont any panic just make it manually.

config/live_paypal.php

<?php
//Devloped by Pakainfo.com 
return array(
/**Devloped by Pakainfo.com  set your paypal credential **/
'client_id' =>'LIVE YOUR CLIENT ID',
'secret' => '=>'LIVE YOUR SECRET KEY',',
'settings' => array(
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 1000,
'log.LogEnabled' => true,
'log.FileName' => storage_path() . '/logs/paypal.log',
'log.LogLevel' => 'FINE'
),
);

Step 6 : make a Route to add payment details and make payment

routes/web.php

//Devloped by Pakainfo.com 
Route::get('checkout', array('as' => 'paypal.paypalwithpayments','uses' => '[email protected]',)); 
Route::post('paypal', array('as' => 'paypal.paypal','uses' => '[email protected]',));
Route::get('paypal', array('as' => 'payment.status','uses' => '[email protected]',));

Step 7 : make Controller

app/Http/Controllers/livePaypalCtrl.php

Run below source code with some command to make a livePaypalCtrl file.

php artisan make:controller livePaypalCtrl

Replace here some code livePaypalCtrl.php with below source code

<?php
/**Devloped by Pakainfo.com 
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use URL;
use Session;
use Redirect;

/**Devloped by Pakainfo.com All Paypal Details class **/
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use Illuminate\Support\Facades\Input;
//Devloped by Pakainfo.com 
class livePaypalCtrl extends HomeController
{
    private $_api_context;
    /**
     */**Devloped by Pakainfo.com  Create a new controller instance.
     *
     */**Devloped by Pakainfo.com  @return void
     */
    public function __construct()
    {
        parent::__construct();
        
        $paypal_conf = \Config::get('paypal');
        $this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
        $this->_api_context->setConfig($paypal_conf['settings']);
    }
    /**
     *Devloped by Pakainfo.com  Show the application paywith paypalpage.
     *
     *Devloped by Pakainfo.com  @return \Illuminate\Http\Response
     */
    public function payWithPaypal()
    {
        return view('paypalwithpayments');
    }
    /**
     * Devloped by Pakainfo.com Store a details of payment with paypal.
     *
     *Devloped by Pakainfo.com  @param  \Illuminate\Http\Request  $request
     *Devloped by Pakainfo.com  @return \Illuminate\Http\Response
     */
    public function postPaymentWithpaypal(Request $request)
    {
    	$this->validate($request, [
	    'name' => 'required',
	    'item_qty' => 'required|numeric',
	    'amount' => 'required|numeric'
	]);
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $live_item_1 = new Item();
        $live_item_1->setName($request->get('name')) 
            ->setCurrency('USD')
            ->setQuantity($request->get('item_qty'))
            ->setPrice($request->get('amount')); 
        $item_list = new ItemList();
        $item_list->setItems(array($live_item_1));
        $total_amount = new Amount();
        $total_amount->setCurrency('USD')
            ->setTotal(( $request->get('amount') * $request->get('item_qty') ));
        $pay_transaction = new Transaction();
        $pay_transaction->setAmount($total_amount)
            ->setItemList($item_list)
            ->setDescription('Live Your simple transaction description');
        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(URL::route('payment.status'))
            ->setCancelUrl(URL::route('payment.status'));
        $pay_paypal = new Payment();
        $pay_paypal->setIntent('Sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirect_urls)
            ->setTransactions(array($pay_transaction));

        try {
            $pay_paypal->create($this->_api_context);
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                \Session::put('error','Connection timeout');
                return Redirect::route('paypal.paypalwithpayments');
            } else {
                \Session::put('error','Some error occur, sorry for inconvenient');
                return Redirect::route('paypal.paypalwithpayments');
              
            }
        }
        foreach($pay_paypal->getLinks() as $link) {
            if($link->getRel() == 'approval_url') {
                $redirect_url = $link->getHref();
                break;
            }
        }

        Session::put('paypal_payment_id', $pay_paypal->getId());
        if(isset($redirect_url)) {
            return Redirect::away($redirect_url);
        }
        \Session::put('error','Unknown error occurred');
        return Redirect::route('paypal.paypalwithpayments');
    }
    public function getPaymentStatus()
    {

        $payment_id = Session::get('paypal_payment_id');

        Session::forget('paypal_payment_id');
        if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
            \Session::put('error','Payment failed');
            return Redirect::route('paypal.paypalwithpayments');
        }
        $pay_paypal = Payment::get($payment_id, $this->_api_context);

        $execution = new PaymentExecution();
        $execution->setPayerId(Input::get('PayerID'));

        $result = $pay_paypal->execute($execution, $this->_api_context);
 
        if ($result->getState() == 'approved') { 
            
            \Session::put('success','Payment success');
            return Redirect::route('paypal.paypalwithpayments');
        }
        \Session::put('error','Payment failed');
        return Redirect::route('paypal.paypalwithpayments');
    }
  }

Step 8 : make blade file using CMD to enter item some details

resources/paypalwithpayments.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
 <!-- Devloped by Pakainfo.com -->
    <div class="row">
        <div class="col-sm-8 col-sm-offset-2">
            <div class="panel panel-default">
                @if ($message = Session::get('success'))
                <div class="custom-alerts alert alert-success fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                    {!! $message !!}
                </div>
                <?php Session::forget('success');?>
                @endif
                @if ($message = Session::get('error'))
                <div class="custom-alerts alert alert-danger fade in">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                    {!! $message !!}
                </div>
                <?php Session::forget('error');?>
                @endif
                <div class="panel-heading">Paypal integration in Laravel</div>
				 <!-- Devloped by Pakainfo.com -->
                <div class="panel-body">
                    <form class="form-horizontal" method="POST" id="payment-form" role="form" action="{!! URL::route('paypal.paypal') !!}" >
                        {{ csrf_field() }}
                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-sm-4 control-label">Item Name</label>
                            <div class="col-sm-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" autofocus>
                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
						 <!-- Devloped by Pakainfo.com -->
                        <div class="form-group{{ $errors->has('item_qty') ? ' has-error' : '' }}">
                            <label for="item_qty" class="col-sm-4 control-label">Total Item Quantity</label>
                            <div class="col-sm-6">
                                <input id="item_qty" type="number" class="form-control" name="item_qty" value="{{ old('item_qty') }}" autofocus>
                                @if ($errors->has('item_qty'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('item_qty') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
						 <!-- Devloped by Pakainfo.com -->
                        <div class="form-group{{ $errors->has('amount') ? ' has-error' : '' }}">
                            <label for="amount" class="col-sm-4 control-label">Item Amount</label>
                            <div class="col-sm-6">
                                <input id="amount" type="text" class="form-control" name="amount" value="{{ old('amount') }}" autofocus>
                                @if ($errors->has('amount'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('amount') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                     <!-- Devloped by Pakainfo.com -->
                        <div class="form-group">
                            <div class="col-sm-6 col-sm-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Payment with Paypal
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Last step to Run your webapplication in browser Like as a Mozila

Also Read This 👉   like and dislike button code in php

Eg : http://localhost:8888/Paypal

Simple folder : integration/laravel54/public/checkout

Note : We should login before try any checkout. If We face paypal unsupported some ssl protocol check version error, Please here check verbose ssl is manually enabled in your php curl version check first at phpinfo()

Paypal integration Demo Outputs

Laravel 5.4 Paypal integration - Paypal Payment Gateway
Laravel 5.4 Paypal integration – Paypal Payment Gateway
Laravel 5.4 Paypal integration - Paypal Payment Gateway
Laravel 5.4 Paypal integration – Paypal Payment Gateway
Laravel 5.4 Paypal integration - Paypal Payment Gateway
Laravel 5.4 Paypal integration – Paypal Payment Gateway
Laravel 5.4 Paypal integration - Paypal Payment Gateway
Laravel 5.4 Paypal integration – Paypal Payment Gateway

Example

I hope you have Got What is Paypal integration with Laravel 5.4 And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.