How To Integrate PayPal Payment Gateway In Laravel

How To Integrate PayPal Payment Gateway In Laravel

paypal payment gateway in larvel

Easy Way to Integrate PayPal Payment Gateway in Laravel

Nowadays, the importance of online transaction is well known to everyone in this evolving world of internet technology. In fact, it is the easiest and fastest way to settle the payments anywhere in the world. Through an online payment gateway, anyone can easily buy or sell products online and can carry out their online transactions all around the world. But there is no doubt that while doing online transactions, there are high risks of security threats also. Thus, it is significant that you should implement robust security mechanisms in order to avoid malicious attempts.

paypal payment gateway in larvel

In this e-commerce world, if you want to settle your online transactions between the customers and merchants, then Laravel payment gateways play a vital role. Recently, there are many popular gateway methods which implement security mechanisms and reliable services, one being such PayPal. In this blog, we will demonstrate how to configure and deploy Laravel in PayPal to make your transaction of funds safer and more secure.

Steps to Configure PayPal in Laravel Applications

If you want to configure PayPal in Laravel applications, then, at first, you need to install the PayPal package in your app by using the composer command. Then, you need to open your SSH terminal and paste the ” composer require paypal/rest-api-sdk-php” command under public_html/paypal_project.

Creation of PayPal Account:

First of all, you need to create your business account by visiting developer.paypal.com. By doing so, it will be easier for you to buy or sell products online over the internet and also you can seamlessly transfer funds to your account. After that, you need to create a PayPal developer mode and also a sandbox account in order to get all other important credentials like a secret key and client key. Then, test all its integration with your Laravel app.

Once you have successfully created your app on the hosting platform for Laravel project, you need to click on this app. As soon as you do this, it will show you your client _id and secret keys. Now, you have to copy all these credentials and paste them in your .env file.

PAYPAL_CLIENT_ID=

PAYPAL_SECRET=

PAYPAL_MODE=sandbox

After doing this, you have to create a new file with the name paypal.php.in/config directory. Then, place the listed-below codes 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’

  ),

];

Set Up Database Migration Product:

Next, you need to create a setup database migration. This you can do by pasting the “php artisan make: migration create_products_table” command in the SSH terminal.

After you have created the migration successfully, you can now change the following code in the migration file.

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 making the necessary changes, you have to paste the following command on dsh terminal php artisan migrate.

Create a Model File for Payment:

As soon as you have configured all the credentials for PayPal API, you have to create a model file with the product name and paste the following code in it.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model

{

  public $fillable = [‘name’,’details’,’price’];

}

Set Up Database Migration Payment:

Then, you have to create and set up database migration payment. For doing this, you need to copy the command “php artisan make: migration create_payment_table” and paste it into the SSH terminal. For creating a table for payment, you need to paste the given codes.

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”);

  }

}

After you have created and a setup migration for payment successfully, you can create a model for payment by pasting the following code in your namespace App.

use Illuminate\Database\Eloquent\Model;

class Payment extends Model

{

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

}

Create PayPal Payment form View

Here, you can add any form and that too in the way you want. For view purpose, the form has 1 input field for entering the amount and also a button 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>

Update Routes

After you have successfully created the view, you need to open the routes/web.php file. Then, 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’;

});

Create PayPal Payment Controller

For writing and managing all the PayPal related information, you need to create a new controller. If you want to create a payment controller, you need to paste the command “php artisan make: controller PaymentController.”  By pasting this command at /app/http/Controllers, you will be able to create a new controller 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 ‘Payment has been done and your payment id is: ‘.$payment_id;

      }else{

          return ‘Payment has failed’;

      }

  }

}

After successful redirection from the PayPal payment gateway, you can easily fetch response data in payment Info method. Also, you can update the payment table with the transaction ID.

By following the above procedures, you can easily implement a Laravel PayPal payment gateway in web applications. Through proper implementation of Laravel Paypal, you will not only make your app secure for the transaction of funds but it also allows the site owners to manage capitals in a more secure way. But while performing these procedures, if you face any problem or any queries regarding any of the process, you can contact any reliable team of professionals who have acquired in-depth knowledge of this integration process and solve all such issues with appropriate solutions.Get the best payment gateway UAE solution by UAE website development professionals

Leave a Reply

Your email address will not be published.

Open chat
1
Hello there!
Can we help you?
Call Now Button