Loading image

Blogs / Programming

How To Integrate Paypal Payment Gateway In Laravel 10

How To Integrate Paypal Payment Gateway In Laravel 10

  • showkat ali
  • 0 Comments
  • 3710 View

 

PayPal Payment Gateway integration in Laravel 10?

Hi there!
This post will provide you with a broad overview of how to integrate the PayPal Payment Gateway with Laravel 10.PayPal can be integrated into your Laravel application to give users a smooth payment experience. PayPal allows you to take payments from clients all over the world and even set up automatic payments for your goods or services.

In this article, we will walk you through the steps necessary to integrate PayPal into your Laravel application. We will be using the PayPal PHP SDK, which provides a simple and user-friendly interface for interacting with the PayPal API.

Here are the specifics for integrating PayPal in Laravel 10.

  1. Activate your PayPal developer account. Login to PayPal

  2. Open a business PayPal account.

  3. Create a MyApps credentials account.

  4. Install the Laravel application.

  5. Install composer 

  6. Create a controller

  7. Create route to Make payment channels

 

Activate your PayPal Developer account. Login to PayPal.


"Follow the instructions in the image to log in to your PayPal account. Once logged in, take the next critical step: create your own business PayPal account. Remember, this is a critical point that should not be missed!"

 Create a MyApps credentials account

     

     

After creating a MyApps credentials account,.


"After successfully creating your app, head over to the app's page and locate the client ID and secret key. These essential credentials are highlighted in the image below for your reference."

Install the Laravel application.

Create and set up a fresh Laravel application in accordance with your requirements. This action is required. (If you have previously made a new Laravel app, skip this step and go to Step 3).

To install the application and access the file to start integrating the PayPal Payment Gateway in Laravel, use the following code:

composer global require laravel/installer laravel new example-app

Install Composer Packages of PayPal.

 

composer require paypal/rest-api-sdk-php

Create route to Make payment channels

      

php artisan make:Controller PayPalController

 

PayPalController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PayPal\Api\Payer;
use PayPal\Api\Amount;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Rest\ApiContext;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;


class PayPalController extends Controller
{
     public function payment()
    {
    $sum = 500;
        $apiContext = new ApiContext(
          new OAuthTokenCredential( 'ClientID',  'ClientSecret'  ) );
// dd($apiContext);
      $payer = new Payer();
      $payer->setPaymentMethod("paypal");
      // dd($payer);
      // Set redirect URLs
      $redirectUrls = new RedirectUrls();
      $redirectUrls->setReturnUrl(route('paypal.success'))
          ->setCancelUrl(route('paypal.cancel'));
      // dd($redirectUrls);
      // Set payment amount
      $amount = new Amount();
      $amount->setCurrency("USD")
          ->setTotal($sum);


      // Set transaction object
      $transaction = new Transaction();
      $transaction->setAmount($amount)
          ->setDescription(" Hello ");
      //   dd($transaction);
      // Create the full payment object
      $payment = new Payment();
      $payment->setIntent('sale')
          ->setPayer($payer)
          ->setRedirectUrls($redirectUrls)
          ->setTransactions(array($transaction));
      // dd($payment);
      // Create payment with valid API context
      try {

          $payment->create($apiContext);
          // dd($payment);
          // Get PayPal redirect URL and redirect the customer
          // $approvalUrl =
          return redirect($payment->getApprovalLink());
          // dd($approvalUrl);
          // Redirect the customer to $approvalUrl
      } catch (PayPalConnectionException $ex) {
          echo $ex->getCode();
          echo $ex->getData();
          die($ex);
      } catch (Exception $ex) {
          die($ex);
      }
  }


  
// this is success route
public function success(Request $request)
{
    $apiContext = new ApiContext(
        new OAuthTokenCredential('ClientID', 'ClientSecret') );

    // Get payment object by passing paymentId
    $paymentId = $_GET['paymentId'];
    $payment = Payment::get($paymentId, $apiContext);
    $payerId = $_GET['PayerID'];

    // Execute payment with payer ID
    $execution = new PaymentExecution();
    $execution->setPayerId($payerId);

    try {
        dd('success');
        

    } catch (PayPalConnectionException $ex) {
        echo $ex->getCode();
        echo $ex->getData();
        die($ex);
    } catch (Exception $ex) {
        die($ex);
    }
}

// this is cancel route
  public function cancel()
{
        dd('payment cancel');
}
}

Create the route

Route::get('paypal-payment',[PayPalController::class,"payment"])->name('paypal.payment');
Route::get('paypal-success',[PayPalController::class,"success"])->name('paypal.success');
Route::get('paypal-cancel',[PayPalController::class,'cancel'])->name('paypal.cancel');

 

Now we are ready to run this application example with Laravel 8, so run the below command for a quick run:

php artisan serve
http://localhost:8000/paypal-payment

Conclusion

Finally, integrating the PayPal Payment Gateway into your Laravel application may assist you in expanding your online store or business. This connection allows your clients to make secure payments on your app or website.

Simply follow these simple instructions, and keep the code handy in case you need it. Following this process, you, you will quickly integrate the PayPal Payment Gateway into your Laravel application.

How to Integrate CoinGate with Laravel 10: Click here 

 

  • Programming
showkat ali Author

showkat ali

Greetings, I'm a passionate full-stack developer and entrepreneur based in Pakistan. I specialize in PHP, Laravel, React.js, Node.js, JavaScript, and Python. I own interviewsolutionshub.com, where I share tech tutorials, tips, and interview questions. I'm a firm believer in hard work and consistency. Welcome to interviewsolutionshub.com, your source for tech insights and career guidance

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

Dynamic Drag  and Drop Form Builder with React: A Step-by-Step Guide

Dynamic Drag and Drop Form Builder with React: A Step-by-Step Guide

showkat ali
/
Programming

Read More
How NVIDIA’s AI Technologies Can Accelerate Your Deep Learning, Data Science, and Edge AI Projects

How NVIDIA’s AI Technologies Can Accelerate Your Deep Learning, Data Science, and Edge AI Projects

showkat ali
/
Technology

Read More
The List of Top  Leading Web Frameworks and Technologies in 2024

The List of Top Leading Web Frameworks and Technologies in 2024

showkat ali
/
Programming

Read More
List of CDN Links for jQuery and Bootstrap: All Versions Included

List of CDN Links for jQuery and Bootstrap: All Versions Included

showkat ali
/
Programming

Read More
The Rise of the First AI Software Engineer | devin cognition

The Rise of the First AI Software Engineer | devin cognition

showkat ali
/
Programming

Read More
In-Depth Guide to Laravel Validation Rules for Special Fields

In-Depth Guide to Laravel Validation Rules for Special Fields

showkat ali
/
Programming

Read More