• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
How to Integrate PayPal Payment Gateway in Laravel 10: A Complete Guide
Home
Laravel

How to Integrate PayPal Payment Gateway in Laravel 10: A Complete Guide

June 15th, 2025 Huzoor Bux Laravel, PHP 2 comments

Facebook Twitter Google+ LinkedIn Pinterest

Laravel, with its elegant and expressive syntax, offers a robust platform for web application development. When it comes to online payment integration, PayPal stands out as a trusted and secure option. In this guide, we will delve into the process of integrating the PayPal payment gateway into a Laravel 10 application, step by step.

Table of Contents

  1. Prerequisites
  2. Setting up Laravel 10
  3. Creating a PayPal Developer Account
  4. Integration Steps
  5. Testing the Integration
  6. Conclusion

1. Prerequisites:

  • A fresh Laravel 10 application.
  • Composer installed on your machine.
  • A PayPal Developer account.

2. Setting up Laravel 10:

If you haven’t set up Laravel 10, you can do so with the following command:

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

3. Creating a PayPal Developer Account:

Before integrating, ensure you have a developer account on PayPal. This will allow you to access the sandbox environment for testing.

  • Visit the PayPal Developer Site
  • Sign up or log in to your account.
  • Navigate to the ‘My Apps & Credentials’ section and create a new app to get your CLIENT_ID and SECRET.

4. Integration Steps:

4.1 Install the PayPal SDK:

Use composer to pull in the PayPal SDK.

composer require paypal/rest-api-sdk-php

4.2 Configuration:

In your .env file, add the following:

PAYPAL_CLIENT_ID=YOUR_CLIENT_ID

PAYPAL_SECRET=YOUR_SECRET

PAYPAL_MODE=sandbox # Use 'live' for production

4.3 Create the PayPal Controller:

Generate a controller to handle PayPal payments.

php artisan make:controller PayPalController

In PayPalController.php:

Let’s update the controller to handle viewing an item, adding it to an order, and initiating the PayPal payment:
namespace App\Http\Controllers;



use Illuminate\Http\Request;

use PayPal\Auth\OAuthTokenCredential;

use PayPal\Rest\ApiContext;

use PayPal\Api\Amount;

use PayPal\Api\Payer;

use PayPal\Api\Payment;

use PayPal\Api\RedirectUrls;

use PayPal\Api\Transaction;



class PayPalController extends Controller

{

    private $_api_context;



    public function __construct() {

        // Configuration setup as previously shown

        $paypal_conf = config('paypal');

        $this->_api_context = new ApiContext(new OAuthTokenCredential(

            $paypal_conf['client_id'],

            $paypal_conf['secret']

        ));

        $this->_api_context->setConfig($paypal_conf['settings']);

    }



    public function viewItem() {

        return view('paypal');

    }



    public function makePayment() {

        $payer = new Payer();

        $payer->setPaymentMethod('paypal');



        $amount = new Amount();

        $amount->setTotal('10.00')  // You can change this value for the item price

              ->setCurrency('USD');



        $transaction = new Transaction();

        $transaction->setAmount($amount)

            ->setDescription('Your Item Description');



        $redirect_urls = new RedirectUrls();

        $redirect_urls->setReturnUrl(route('status'))

            ->setCancelUrl(route('view.item'));



        $payment = new Payment();

        $payment->setIntent('sale')

            ->setPayer($payer)

            ->setRedirectUrls($redirect_urls)

            ->setTransactions([$transaction]);



        try {

            $payment->create($this->_api_context);

        } catch (\Exception $ex) {

            return redirect()->route('view.item')->with('error', 'There was an error processing your payment with PayPal.');

        }



        foreach($payment->getLinks() as $link) {

            if($link->getRel() == 'approval_url') {

                return redirect($link->getHref());

            }

        }

    }



    public function getPaymentStatus(Request $request) {

        // Payment ID validation and retrieval code should go here



        return view('payment-status', ['status' => 'Payment was successful!']);  // Simplified for demonstration

    }

}

4.4 Implementing Payment Methods:

Inside the same controller, implement methods like getPaymentStatus, makePayment, etc., to handle various stages of the payment process.

4.5 Setting Routes:

In web.php:

Route::get('view-item', 'PayPalController@viewItem')->name('view.item');

Route::get('paypal', 'PayPalController@makePayment')->name('paypal');

Route::get('payment-status', 'PayPalController@getPaymentStatus')->name('status');

4.6 Views:

Create a view named paypal.blade.php within the resources/views directory. This view will contain a button to initiate the payment:

<!-- resources/views/paypal.blade.php -->



<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>PayPal Integration with Laravel 10</title>

<link rel="stylesheet" href="{{ asset('css/app.css') }}">

</head>

<body>



<div class="container mt-5">

<h2 class="text-center">PayPal Integration with Laravel 10</h2>

<div class="d-flex justify-content-center mt-4">

<a href="{{ route('paypal') }}" class="btn btn-primary">Pay with PayPal</a>

</div>

</div>



<script src="{{ asset('js/app.js') }}"></script>

</body>

</html>

This view displays a button that, when clicked, initiates the payment process by redirecting the user to the PayPal site. Upon successful payment, they’ll be redirected back to your site where you can display the payment status.

Running the Application:

  1. Migration and Database Setup:Before running the application, ensure that you’ve set up your database credentials in the .env file. Then run the migrations:
    php artisan migrate
  2. Development Server:Laravel provides a development server out of the box. To start the server:
    php artisan serve

    Once the server is up and running, you can access the application via http://127.0.0.1:8000 or http://localhost:8000.

  3. Viewing the PayPal Page:Navigate to http://127.0.0.1:8000/paypal to see the PayPal payment button and initiate the payment process.

Remember, this is a basic implementation for demonstration purposes. In a real-world scenario, you’d likely want to expand on this, handling various product or service pricing, integrating a shopping cart, handling various currencies, and enhancing the UI/UX for a smoother user experience. Always ensure to rigorously test the application, especially when dealing with financial transactions.

Conclusion:

Integrating the PayPal payment gateway in Laravel 10 is straightforward with the SDK provided. This integration allows Laravel developers to offer a secure and trusted payment method for their users. Remember to switch to ‘live’ mode in the .env file once you’re ready to move to production and always ensure your application’s security when dealing with financial transactions.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • Laravel 10
  • Laravel tutorial.
  • online payment
  • payment gateway
  • PayPal integration
  • PayPal SDK
  • secure transaction
  • web application development
Facebook Twitter Google+ LinkedIn Pinterest
Next article 3 reasons why you SHOULD write tests
Previous article Javascript Image Compress using HTML5 Canvas & File API before Upload

Huzoor Bux

I am a PHP Developer

Related Posts

PHP Beyond 2023: Unfurling the Road Less Traveled PHP
June 15th, 2025

PHP Beyond 2023: Unfurling the Road Less Traveled

Using PHP and OOP Concepts to Build Custom WordPress Themes: A Modern Approach PHP
June 15th, 2025

Using PHP and OOP Concepts to Build Custom WordPress Themes: A Modern Approach

5 Mistakes that make you look like a noob in PHP PHP
June 15th, 2025

5 Mistakes that make you look like a noob in PHP

2 Comments

  1. Yasir
    December 20, 2023 at 7:04 am Reply ↓

    paypalController Error
    “Trying to access array offset on value of type null”

    please resolve this issue in your code .

  2. Yasir
    December 20, 2023 at 7:24 am Reply ↓

    problem with paypal Controller

    not working fix please

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Cool HTM5 Features, Part 2
  • Programming Languages for Better Job Opportunities
  • PHP Beyond 2023: Unfurling the Road Less Traveled
  • Create pure CSS based toggle visibility button
  • Cool HTML5 features
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.