PHP Laravel 6 PayPal Payment Gateway API integration step by step

Today, We want to share with you PHP Laravel 6 PayPal Payment Gateway API integration step by step.In this post we will show you Set up Paypal Payment Gateway Integration in Laravel 6 PHP Example, hear for PayPal Payment Gateway API integration in Laravel6 we will give you demo and example for implement.In this post, we will learn about How to integrate PayPal payment gateway in Laravel 6 with an example.

PHP Laravel 6 PayPal Payment Gateway API integration step by step

There are the Following The simple About Paypal payment gateway integration in Laravel 6.0 step by step Full Information With Example and source code.

As I will cover this Post with live Working example to develop Integrate Laravel Paypal Payment Gateway For Fast Online Payments, so the composer require paypal rest api sdk php is used for this example is following below.

Phase – 1 : Create new laravel application

create a new Fresh laravel 6 Web application

composer create-project --prefer-dist laravel/laravel blog

Phase – 2 : Database Configuration

configure chnage database setting in .env file

DB_HOST=localhost
DB_DATABASE=best_marketuts
DB_USERNAME=virat_im31
DB_PASSWORD=reset@sdjksjdksd

Phase – 3 : Install Required Packages

install paypal/rest-api-sdk-php

composer require paypal/rest-api-sdk-php

config/paypal.php

You can view paypal.php file like as bellow:


'client_id' =>'paypal client_id',
'secret' => 'paypal secret ID',

'settings' => array(
	'mode' => 'sandbox',
	'http.ConnectionTimeOut' => 1000,
	'log.LogEnabled' => true,
	'log.FileName' => storage_path() . '/logs/paypal.log',
	'log.LogLevel' => 'FINE'
	),
);

Phase – 4 : Define a Laravel 6 Route

routes/web.php

Route::get('planwithcharge', array('as' => 'planwithcharge','uses' => 'TransactionController@planWithCharge',));
Route::post('paypal', array('as' => 'paypal','uses' => 'TransactionController@postPaymentWithpaypal',));
Route::get('paypal', array('as' => 'status','uses' => 'TransactionController@getPaymentStatus',));

Phase – 5 : Make a Laravel 6 Controller

app/Http/Controllers/TransactionController.php

data_v1_api = new ApiContext(new OAuthTokenCredential($all_cridentials['client_id'], $all_cridentials['secret']));
        $this->data_v1_api->setConfig($all_cridentials['settings']);
    }


    public function planWithCharge()
    {
        return view('planwithcharge');
    }

    public function postPaymentWithpaypal(Request $request)
    {
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

    	$item_1 = new Item();

        $item_1->setName('Item 1')
            ->setCurrency('USD')
            ->setQuantity(1)
            ->setPrice($request->get('price'));

        $item_list = new ItemList();
        $item_list->setItems(array($item_1));

        $price = new Amount();
        $price->setCurrency('USD')
            ->setTotal($request->get('price'));

        $transaction = new Transaction();
        $transaction->setAmount($price)
            ->setItemList($item_list)
            ->setDescription('Your transaction description');

        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(URL::route('status'))
            ->setCancelUrl(URL::route('status'));

        $payment = new Payment();
        $payment->setIntent('Sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirect_urls)
            ->setTransactions(array($transaction));

        try {
            $payment->create($this->data_v1_api);
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                \Session::put('error','Sorry Your Connection timeout');
                return Redirect::route('planwithcharge');
            } else {
                \Session::put('error','Sorry, Some error occur, sorry for inconvenient');
                return Redirect::route('planwithcharge');
            }
        }

        foreach($payment->getLinks() as $link) {
            if($link->getRel() == 'approval_url') {
                $redirect_url = $link->getHref();
                break;
            }
        }

        Session::put('live_trans_ref_code', $payment->getId());

        if(isset($redirect_url)) {
            return Redirect::away($redirect_url);
        }

        \Session::put('error','Unknown error occurred');
    	return Redirect::route('planwithcharge');
    }

    public function getPaymentStatus()
    {

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

        Session::forget('live_trans_ref_code');
        if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
            \Session::put('error','Sorry dear Your Payment failed');
            return Redirect::route('planwithcharge');
        }
        $payment = Payment::get($payment_id, $this->data_v1_api);

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

        $result = $payment->execute($execution, $this->data_v1_api);

        if ($result->getState() == 'approved') { 

            \Session::put('success','Payment success');
            return Redirect::route('planwithcharge');
        }
        \Session::put('error','Payment failed');

		return Redirect::route('planwithcharge');
    }
}

Phase – 6 : Create Blade File

resources/total_likes/planwithcharge.blade.php

@extends('layouts.app')

@section('content')

Paypal payment gateway integration in Laravel 6 step by step

@if ($message = Session::get('success'))
{!! $message !!}
@endif @if ($message = Session::get('error'))
{!! $message !!}
@endif
Paywith Paypal
{{ csrf_field() }}
@if ($errors->has('price')) {{ $errors->first('price') }} @endif
@endsection
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel payment gateway package.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment