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

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

'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' => 'livePaypalCtrl@payWithPaypal',)); 
Route::post('paypal', array('as' => 'paypal.paypal','uses' => 'livePaypalCtrl@postPaymentWithpaypal',));
Route::get('paypal', array('as' => 'payment.status','uses' => 'livePaypalCtrl@getPaymentStatus',));

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

_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')
@if ($message = Session::get('success'))
{!! $message !!}
@endif @if ($message = Session::get('error'))
{!! $message !!}
@endif
Paypal integration in Laravel
{{ csrf_field() }}
@if ($errors->has('name')) {{ $errors->first('name') }} @endif
@if ($errors->has('item_qty')) {{ $errors->first('item_qty') }} @endif
@if ($errors->has('amount')) {{ $errors->first('amount') }} @endif
@endsection

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

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.

Leave a Comment