How To Integrate Paypal Payments In Laravel Without Any Sdk Or Package 2025 Guide

image
image
image
image
image
image
image
image
How to Integrate PayPal Payments in Laravel Without Any SDK or Package (2025 Guide)

How to Integrate PayPal Payments in Laravel Without Any SDK or Package (2025 Guide)

Integrating PayPal with Laravel doesn't require third-party SDKs or bloated packages. In this tutorial, we'll use PayPal's REST API and Laravel's built-in Http client to create and capture orders. This guide supports the latest PayPal v2 Checkout API and works seamlessly in Laravel 8 and above.


So, first of all you need to create a account on Paypal to create a account visit this URL - https://developer.paypal.com/home/




Then after login you have to click on the API credential as shown in the below image


Then you have to click on the create app as show in the below image, to create a new app on Paypal




Then you have the popup like something below just fill the details and create your application




Then after you will receive you client Id and secret as given in below image, save it somewhere as we will use it in our application




Now we will follow step by step guide to implement Paypal payment in the laravel application.



Install Laravel using the below command, if you haven't installed it yet

composer create-project laravel/laravel paypal


🚀 Step 1: Create a Controller

Create a controller named PayPalController:

php artisan make:controller PayPalController


✍️ Step 2: Implement PayPal Order Creation and Capture

Paste the following code in PayPalController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PayPalController extends Controller
{
public function paypalCreateOrder()
{
$clientId = env('PAYPAL_CLIENT_ID');
$clientSecret = env('PAYPAL_CLIENT_SECRET');

$response = Http::withBasicAuth($clientId, $clientSecret)
->asForm()
->post('https://api-m.sandbox.paypal.com/v1/oauth2/token', [
'grant_type' => 'client_credentials',
]);

if ($response->successful()) {
$data = $response->json();
$accessToken = $data['access_token'] ?? null;
} else {
return response()->json([
'error' => 'Failed to get access token',
'details' => $response->body(),
], $response->status());
}

$paypalRequestId = '7b92603e-77ed-4896-8e78-5dea2050476a'; // Unique request id, generate dynamically if needed

$url = 'https://api-m.sandbox.paypal.com/v2/checkout/orders';

$returnUrl = url('paypal/capture?paypalRequestId=' . $paypalRequestId);
$cancelUrl = url('paypal/cancel');

$payload = [
"intent" => "CAPTURE",
"payment_source" => [
"paypal" => [
"experience_context" => [
"payment_method_preference" => "IMMEDIATE_PAYMENT_REQUIRED",
"landing_page" => "LOGIN",
"shipping_preference" => "GET_FROM_FILE",
"user_action" => "PAY_NOW",
"return_url" => $returnUrl,
"cancel_url" => $cancelUrl
]
]
],
"purchase_units" => [
[
"invoice_id" => "90210",
"amount" => [
"currency_code" => "USD",
"value" => "230.00",
"breakdown" => [
"item_total" => [
"currency_code" => "USD",
"value" => "220.00"
],
"shipping" => [
"currency_code" => "USD",
"value" => "10.00"
]
]
],
"items" => [
[
"name" => "T-Shirt",
"description" => "Super Fresh Shirt",
"unit_amount" => [
"currency_code" => "USD",
"value" => "20.00"
],
"quantity" => "1",
"category" => "PHYSICAL_GOODS",
"sku" => "sku01",
"image_url" => "https://example.com/static/images/items/1/tshirt_green.jpg",
"url" => "https://example.com/url-to-the-item-being-purchased-1",
"upc" => [
"type" => "UPC-A",
"code" => "123456789012"
]
],
[
"name" => "Shoes",
"description" => "Running, Size 10.5",
"sku" => "sku02",
"unit_amount" => [
"currency_code" => "USD",
"value" => "100.00"
],
"quantity" => "2",
"category" => "PHYSICAL_GOODS",
"image_url" => "https://example.com/static/images/items/1/shoes_running.jpg",
"url" => "https://example.com/url-to-the-item-being-purchased-2",
"upc" => [
"type" => "UPC-A",
"code" => "987654321012"
]
]
]
]
]
];

$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
'PayPal-Request-Id' => $paypalRequestId,
])->post($url, $payload);

if ($response->successful()) {
return redirect()->to($response->json()['links'][1]['href']); // Debugging line to check the response
} else {
return response()->json([
'error' => 'Failed to create PayPal order',
'details' => $response->body(),
], $response->status());
}
}

public function paypalCaptureOrder(Request $request)
{
$paypalRequestId = $request->paypalRequestId;
$clientId = env('PAYPAL_CLIENT_ID');
$clientSecret = env('PAYPAL_CLIENT_SECRET');

$response = Http::withBasicAuth($clientId, $clientSecret)
->asForm()
->post('https://api-m.sandbox.paypal.com/v1/oauth2/token', [
'grant_type' => 'client_credentials',
]);

if ($response->successful()) {
$data = $response->json();
$accessToken = $data['access_token'] ?? null;
} else {
return response()->json([
'error' => 'Failed to get access token',
'details' => $response->body(),
], $response->status());
}

$response = Http::withToken($accessToken)
->withHeaders([
'Content-Type' => 'application/json',
'PayPal-Request-Id' => $paypalRequestId,
])
->post("https://api-m.sandbox.paypal.com/v2/checkout/orders/{$paypalRequestId}/capture", []);

// Check response
if ($response->successful()) {
return response()->json([
'status' => 'success',
'data' => $response->json()
]);
} else {
return response()->json([
'status' => 'error',
'error' => $response->json()
], $response->status());
}
}
}


🔗 Step 3: Define Routes in web.php


use App\Http\Controllers\PayPalController;

Route::get('/paypal/create', [PayPalController::class, 'paypalCreateOrder']);
Route::get('/paypal/capture/{orderId}', [PayPalController::class, 'paypalCaptureOrder']);


🧪 Step 4: Testing with PayPal Sandbox


  1. Visit /paypal/create to create an order.
  2. You'll receive a PayPal approval link in the response.
  3. Redirect the user to the link.
  4. After payment, PayPal will redirect to /paypal/capture/{orderId}.
  5. In that route, your app will capture the payment.


🧩 Step 5: Add PayPal API Credentials to .env

To keep your credentials secure and easy to manage across environments (local, staging, production), add the following lines to your .env file:

PAYPAL_CLIENT_ID=YourPayPalClientID
PAYPAL_CLIENT_SECRET=YourPayPalClientSecret


🔁 Step 6: Clear Route Cache (if needed)

After making changes to your routes or .env file, it's good practice to clear cached routes and configuration to avoid unexpected issues.

Run the following Artisan commands:

php artisan route:clear
php artisan config:clear
php artisan cache:clear


🌐 Step 7: Test the Integration in Browser

Finally, start your Laravel development server by running:

php artisan serve

Now, open your browser and visit:

http://localhost:8000/paypal/create


when you hit the above URL you can see something like below



This will initiate the PayPal order creation process. If everything is set up correctly, you should see a JSON response with the PayPal order details including the approval link where the user can proceed with the payment.


✅ Step 8: Access the Full Source Code


You can download or clone the full working code from our official GitHub repository:

🔗 GitHub Repo: https://github.com/codehunger-team/paypal-payment-integration-in-laravel-12/tree/master

It includes:

  1. PayPalController with order creation and capture logic
  2. Routes setup
  3. Example .env configuration
  4. Sample PayPal order payload
  5. Instructions to run and test it locally


📌 Make sure to place this after Step 7 in your blog article or wherever it makes most sense in your content structure. You could also include it in a dedicated “Source Code” or “GitHub Repository” section near the end of the article. Let me know if you'd like the blog content edited directly for placement.


🧾 Conclusion: Seamless PayPal Integration in Laravel Without SDKs


Integrating PayPal payment gateway in Laravel without relying on third-party packages or SDKs gives developers complete control over the payment flow, API handling, and response customization. By directly communicating with PayPal’s RESTful APIs using Laravel’s robust HTTP client, you ensure a lightweight, efficient, and secure implementation tailored to your specific business logic.


This approach not only eliminates the overhead of external dependencies but also empowers you to fully understand the payment lifecycle — from generating an access token using OAuth 2.0, to creating orders, and finally capturing payments. Whether you're building an eCommerce platform, SaaS product, subscription service, or a donation system, having direct PayPal API integration in your Laravel application enhances flexibility, maintainability, and future scalability.


Here’s a quick recap of what we covered in this tutorial:

  1. 🛠️ Set up your PayPal sandbox credentials for safe testing
  2. 🔐 Use Laravel's native HTTP client to handle authentication and authorization
  3. 🧾 Craft dynamic and detailed purchase units with shipping, item breakdown, and metadata
  4. 💳 Capture payments using PayPal Order ID securely and efficiently
  5. 🌐 Test the full flow using Laravel’s local development server and sandbox environment
  6. ⚙️ Configure your .env file and clear your Laravel route & config cache for a fresh start


🚀 Benefits of Raw PayPal API Integration in Laravel

  1. ✅ No package bloat or hidden dependencies
  2. ✅ Improved application performance
  3. ✅ Full API-level control and customization
  4. ✅ Easier debugging and logging of PayPal interactions
  5. ✅ Perfect for both monolithic and microservice-based Laravel architectures


By following this comprehensive, step-by-step guide, you are now capable of adding PayPal payment support in any Laravel project — without a single external package.


If you found this tutorial helpful, don’t forget to share it with your fellow developers and bookmark it for your next Laravel + PayPal project. Need a production-ready PayPal module or custom Laravel development? Let’s talk!


Youtube Url: