Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Setup Laravel 5.7 Application in Just 1-click on Different Cloud Providers Like GCE , AWS , DO , Linode , Vultr Deploy Now!

Integrate Laravel Paypal Payment Gateway For Fast Online Payments

Updated on September 29, 2018

5 Min Read
laravel paypal integration
Reading Time: 5 minutes

If you are operative in the e-commerce circuit, then you must have known the importance of online transaction. As it is the most viable and fastest way to settle payments anywhere in the world. And because of evolving internet technology, today anyone can easily buy or sell products and can settle payments with online transaction all around the world.

But online transaction brings high risks of serious security threats also. Therefore robust security mechanism for online payments is required to avoid potential malicious attempts. That is where the importance of Laravel Paypal payment gateways comes into play. As these gateways helps businesses to focus and capitalize on the benefits of online transactions, while keeping their customers and systems secured from the external threats.

Integrating PayPal with Laravel
1. Payment Gateway
2. How it works?
3. Config PayPal in Laravel application
4. Create PayPal Account
5. Setup Migration Product
6. Product Model for Payment
7. Setup Migration Payment
8. Payment Model
9. PayPal payment Form View
10. Routes
11. PayPal Payment Controller
12. PayPal Payment Status
13. Wrapping up!

Read More About: Create Laravel Ecommerce Store With Aimeos

Payment Gateway

Payment gateway is a typical e-commerce service that helps process credit card payments for online businesses. The main functionality of Payment gateways is that they facilitate online transactions by transferring confidential funds information between payment portals and front end funds processors/banks.

How it works?

The basic responsibility of payment gateways is to authorize funds transaction between clients and merchants. Below are the intrinsic steps on how payment gateways works:

Step 1: The process starts when a customer first visits the website and places an order for the respective products through checkout button.

Step 2: Next the merchant will send the details of that order to the payment gateway and then after the transaction is sent to the issuing bank, so that it can authorize the transaction.

Step 3: Once completing the authentication process of transaction, the customer’s issuing bank approves or declines the transaction based on the available funds in the customer’s account.

Step 4: Then after the payment gateway sends that message to the merchant.

Step 5: The bank settles the transaction amount with the payment gateway and the payment gateway settles the money with the merchant.

In the world of e-commerce, Laravel payment gateways plays a vital role in settling up online transactions between customers and merchants. Because Payment gateways are solely responsible for the authorization of that transaction. Hence are developed in a very protective manner to cover all the possible security checks. Currently PayPal, Skrill, Stripe and Square are the popular payment gateway methods. Because their payment security mechanisms and reliable services are rated top among the list.

So in this article, i will demonstrate how to deploy PayPal payment gateway in a Laravel app to make its funds transaction safe and secured.

You Might Also Like: Top US Payment Gateways for Ecommerce Stores

Config PayPal in Laravel application

For Laravel payment gateway integration, first install the PayPal package in your app using the composer command. Open your SSH terminal and paste the following command under public_html/paypal_project.

composer require paypal/rest-api-sdk-php

Create PayPal Account

Visit Developer.paypal.com to create your business account, so that you can buy/sell products easily over the internet and transfer funds to your account.

Next create a Paypal developer mode and create a sandbox account to get important credentials like client_key and secret key, to test its integration with your Laravel app.

After creating your app successfully on the hosting for Laravel project, click on it and it will show you client_id and secret keys. Copy these credentials and paste them in your .env file.

PAYPAL_CLIENT_ID=

PAYPAL_SECRET=

PAYPAL_MODE=sandbox

Next, create a new file with the name paypal.php in /config directory and place the following code in the file.

<?php

return [

   'client_id' => env('PAYPAL_CLIENT_ID',''),

   'secret' => env('PAYPAL_SECRET',''),

   'settings' => array(

       'mode' => env('PAYPAL_MODE','sandbox'),

       'http.ConnectionTimeOut' => 30,

       'log.LogEnabled' => true,

       'log.FileName' => storage_path() . '/logs/paypal.log',

       'log.LogLevel' => 'ERROR'

   ),

];

Setup Migration Product

Now create and setup database migration. Paste the following command in the SSH terminal:

php artisan make:migration create_products_table

After successfully creating migration, change the following code in migration file.

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration

{

   public function up()

   {

       Schema::create('products', function (Blueprint $table) {

           $table->increments('id');

           $table->string('name');

           $table->text('details');

           $table->float('price');

           $table->timestamps();

       });

   }

   public function down()

   {

       Schema::drop("products");

   }

}

After that change, paste the following command on ssh terminal php artisan migrate.

You Might Also Like: Why Use Laravel Ecommerce Packages Instead of Read-Made CMSs?

Product Model for Payment

After successfully configuring all credentials for PayPal API, now let’s create a model file with the name of Product and paste the below code in it

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

   public $fillable = ['name','details','price'];

}

Setup Migration Payment

Now to create and setup database migration, copy the below command and paste it into the SSH terminal:

php artisan make:migration create_payment_table

Paste the below code for creating a table for payment.

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreatePaymentsTable extends Migration

{

   public function up()

   {

       Schema::create('payments', function (Blueprint $table) {

           $table->increments('id');

           $table->string('item_number');

           $table->string('transaction_id');

           $table->string('currency_code');

           $table->string('payment_status');

           $table->timestamps();

       });

   }

   public function down()

   {

       Schema::drop("payments");

   }

}

Payment Model

As you have successfully created migration for payment, now let’s create model for payment by pasting the below code in your payment model namespace App;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model

{

   public $fillable = ['item_number','transaction_id','currency_code','payment_status'];

}

PayPal payment Form View

You can add any form here with the way you want. While for the purpose of View, the form has 1 input field to enter the amount and a button also to complete the submission.

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="frmTransaction" id="frmTransaction">

   <input type="hidden" name="business" value="{{$paypal_id}}">

   <input type="hidden" name="cmd" value="_xclick">

    <input type="hidden" name="item_name" value="{{$product->name}}">

   <input type="hidden" name="item_number" value="{{$product->id}}">

   <input type="hidden" name="amount" value="{{$product->price}}">   

   <input type="hidden" name="currency_code" value="USD">   

   <input type="hidden" name="cancel_return" value="http://demo.expertphp.in/payment-cancel">

   <input type="hidden" name="return" value="http://demo.expertphp.in/payment-status">

</form>

<script>document.frmTransaction.submit();</script>

Routes

After creating the view successfully, now open the routes/web.php file and update routes for payment application in it.

Route::get('payment-status',array('as'=>'payment.status','uses'=>'PaymentController@paymentInfo'));

Route::get('payment',array('as'=>'payment','uses'=>'PaymentController@payment'));

Route::get('payment-cancel', function () {

   return 'Payment has been canceled';

});

PayPal Payment Controller

Now create a new controller to write and manage all the PayPal related PHP stuff. Just paste the below shown command to create a payment Controller.

php artisan make:controller PaymentController

The above command will create a new controller at /app/http/Controllers with the name PaymentController.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Product;

use App\Payment;

class PaymentController extends Controller {

   public function payment(Request $request){

          $product=Product::find($request->id);    

     return view('payment',compact('product'));

   }

   public function paymentInfo(Request $request){        

       if($request->tx){

           if($payment=Payment::where('transaction_id',$request->tx)->first()){

               $payment_id=$payment->id;

           }else{

               $payment=new Payment;

               $payment->item_number=$request->item_number;

               $payment->transaction_id=$request->tx;

               $payment->currency_code=$request->cc;

               $payment->payment_status=$request->st;

               $payment->save();

               $payment_id=$payment->id;

           }

       return 'Pyament has been done and your payment id is : '.$payment_id;

       }else{

           return 'Payment has failed';

       }

   }

}

Payment method are used to render form of payment gateway where selected product data is passed to buy.

After successful redirection from paypal payment gateway we fetch response data in payment Info method and according that we update payment table with transaction id.

You Might Also Like: Working With Controllers in Laravel

PayPal Payment Status

In order to notify the users about the status of the payment whether it is successfully completed or failed, following code is executed to output the status of the payment.

Wrapping up!

So in this article, i have demonstrated how to implement Laravel PayPal payment gateway in web applications. As implementing the Laravel PayPal integration, will make the app secured for funds transaction and will allow site owners to securely manage their capitals. The article covers the complete installation and step by step integration of PayPal payment gateway with the Laravel app. So that you can acquire thorough knowledge of its integration and use in the platform.

Still if you have any further queries regarding this article, you can feel free to ask your questions in the comments section below.

Share your opinion in the comment section. COMMENT NOW

Pardeep Kumar

Pardeep is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He love to work on Open source platform , Frameworks and working on new ideas. You can email him at pardeep.kumar@cloudways.com

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

THERE’S MORE TO READ.