How To Integrate PayPal Payment Gateway in Laravel?

For those with an e-commerce site, integration of PayPal Payment Gateway in Laravel plays an important role. Not only does this integration provide a swift and efficient method for the customers to purchase products, but also provides security during money transfers using online platforms.

Laravel is a PHP framework that you can use to make web applications. The best feature is that it comes with built-in features that make it easier for you to design applications.

PayPal is one of the most widely used platforms for online transactions when buying or selling goods. With its high-grade customer support and easy to use systems, PayPal is used in most countries all over the world.

What does Integrating PayPal Payment Gateway in Laravel mean?

While online shopping, customers make payments by generally two approaches. First; they can pay on the same website or app via credit card or other bank accounts. Second; they can make payments off-site and send in receipt screenshots or some other evidence of payment. Integrating PayPal Payment Gateway in Laravel means that your online platform or e-commerce website is linked to PayPal. And now you can buy or sell goods and process payments via PayPal.

Payment gateway means that the transaction is processed completely online. The gateway handles all the logistics and information transfer between the app and the bank.  This process makes the online shopping experience smooth, convenient, and safe for all parties involved.

The process of integrating the two systems means creating an infrastructure that will automate the process of checkout and payment. And there will no longer be a need for manual checking and approving of payment requests.

Steps for Integrating PayPal Payment Gateway in Laravel:

In this article, we will cover an overview of the steps you can take to integrate PayPal Payment Gateway in Laravel. There can be different methods to integrate the two systems, but we will look at the one which is most common and easiest to install.

Here are the steps involved in the integration process. Each step is discussed in detail as well:

  1. Create a PayPal business account
  2. Install a Laravel app
  3. Install a composer package
  4. Enter your PayPal details from your Sandbox account
  5. Create routes for payments
  6. Create a controller
  7. Add a blade view

1.     Create a PayPal business account

For online transactions, you need to create a PayPal account at developer.paypal.com. Then activate your Sandbox account to get access to some important details and passwords that you will need in the process of integrating PayPal Payment Gateway in Laravel.

These details include your secret_key and client_id. You will find them by opening the PayPal account you created.

2.     Create a new Laravel app:

Create and install a new Laravel app according to your specifications. This is a necessary step. (If you already have created a new Laravel app then skip this step and move on to Step 3).

Use the following code to install the app and then get into the file to begin Integrating PayPal Payment Gateway in Laravel:

composer create-project laravel/laravel laravel-paypal-app –prefer-dist

 

cd laravel-paypal-app

3.     Now install Composer:

As I mentioned above, Laravel comes with built-in plugins that are easy to use. One of these plugins is the PayPal composer package. Get the most up-to-date PayPal package by using the command: composer require srmklive\paypal. Use the composer to install your PayPal’s SDK.

composer require srmklive/PayPal

 

After you have installed the composer package plug-in, the command below will register your PayPal account to the Laravel app. Go to config/app.php and enter:

….

 

….

providers’ => [

 

….

 

Srmklive\PayPal\Providers\PayPalServiceProvider::class

 

]

….

….

providers’ => [

 

….

 

‘PayPal’ => Srmklive\PayPal\Facades\PayPal::class

4.     Define your PayPal details:

Now to integrate PayPal Payment Gateway in Laravel you need to make a database connection. For that, copy-paste the secret_key and client_id into the .env file. You will find this information in your sandbox account you created in Step 01 of this guide. (This is an important step to strengthen your payment security).

At this point, your screen will show you the account details and credentials such as username and passwords.

PAYPAL_MODE=sandbox

 

PAYPAL_SANDBOX_API_USERNAME=xxxxx-xxx-xx

PAYPAL_SANDBOX_API_PASSWORD=abc123

PAYPAL_SANDBOX_API_SECRET=xxxxxxx-xxxxx-xxx

PAYPAL_CURRENCY=USD

PAYPAL_SANDBOX_API_CERTIFICATE=

5.     Add routes:

In the case of payments, there are three routes you will need to clarify and input in your routes/web.php file. These are Payments, Payment success, and cancellation.

<?php

 

 

use Illuminate\Support\Facades\Route;

 

/*

|————————————————————————–

| Web Routes

|————————————————————————–

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the “web” middleware group. Now create something great!

|

*/

 

Route::get(‘/’, function () {

return view(‘home’);

});

 

 

Route::get(‘handle-payment’, ‘PayPalPaymentController@handlePayment’)->name(‘make.payment’);

Route::get(‘cancel-payment’, ‘PayPalPaymentController@paymentCancel’)->name(‘cancel.payment’);

Route::get(‘payment-success’, ‘PayPalPaymentController@paymentSuccess’)->name(‘success.payment’);

6.     Create Controller

To do this, you will have to create a controller (run PHP artisan make: controller PayPalPaymentController to create a controller). This is where all the payment logistics will be processed.  Enter the following code in your app/Http/Controllers/PayPalPaymentController.php file:

<?php

 

 

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Srmklive\PayPal\Services\ExpressCheckout;

 

class PayPalPaymentController extends Controller

{

public function handlePayment()

{

$product = [];

$product[‘items’] = [

[

‘name’ => ‘Nike Joyride 2’,

‘price’ => 112,

‘desc’  => ‘Running shoes for Men’,

‘qty’ => 2

]

];

 

$product[‘invoice_id’] = 1;

$product[‘invoice_description’] = “Order #{$product[‘invoice_id’]} Bill”;

$product[‘return_url’] = route(‘success.payment’);

$product[‘cancel_url’] = route(‘cancel.payment’);

$product[‘total’] = 224;

 

$paypalModule = new ExpressCheckout;

 

$res = $paypalModule->setExpressCheckout($product);

$res = $paypalModule->setExpressCheckout($product, true);

 

return redirect($res[‘paypal_link’]);

}

 

public function paymentCancel()

{

dd(‘Your payment has been declend. The payment cancelation page goes here!’);

}

 

public function paymentSuccess(Request $request)

{

$paypalModule = new ExpressCheckout;

$response = $paypalModule->getExpressCheckoutDetails($request->token);

 

if (in_array(strtoupper($response[‘ACK’]), [‘SUCCESS’, ‘SUCCESSWITHWARNING’])) {

dd(‘Payment was successfull. The payment success page goes here!’);

}

 

dd(‘Error occured!’);

}

}



7.     Blade view:

Now that you have completed most of the steps involved in Integrating PayPal Payment Gateway in Laravel, it is time to compose a blade file so you can see the PayPal payment workings by clicking on an icon! To do this you have to copy-paste the code in your product.blade.php file:

<!DOCTYPE html>

 

<html lang=”{{ str_replace(‘_’, ‘-‘, app()->getLocale()) }}”>

 

<head>

<meta charset=”utf-8″>

<meta name=”viewport” content=”width=device-width, initial-scale=1″>

 

<title>Laravel 7 PayPal Payment Gateway Integration Tutorial</title>

 

<link rel=”stylesheet” href=”https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css” />

</head>

 

<body>

<div class=”container mt-5 text-center”>

<h2>Laravel 7 PayPal Integration Example</h2>

 

<a href=”{{ route(‘make.payment’) }}” class=”btn btn-primary mt-3″>Pay $224 via Paypal</a>

</div>

</body>

 

</html>

 

Once this blade view file is set up, the process of Integrating PayPal Payment Gateway in Laravel is complete. Now your screen will show you the output of the above code.

Side note: you can follow these steps to gain a deeper understanding of the process of Integrating PayPal Payment Gateway in Laravel. And use GitHub to get the full code!



Final words:

Integrating PayPal Payment Gateway in Laravel can take your online business or e-commerce store to the next level. Your customers will be able to pay directly on the app or website without having to worry about any security breaches.

Follow these simplified steps and copy the code when required! You will be able to integrate the PayPal Payment Gateway in Laravel within no time using this process.

Customer experience is just as important as the product you want to sell. For that, integrate PayPal Payment Gateway in Laravel so you can receive payments on time and enjoy a seamless process of online transactions.

Shopping Basket