Payu Payment Gateway Integration In Laravel 12

image
image
image
image
image
image
image
image
Payu Payment Gateway Integration In Laravel 12

Payu Payment Gateway Integration In Laravel 12

Are you a freelancer, startup, or agency in India looking to integrate PayU payment gateway in your Laravel application without relying on third-party packages? This guide will walk you through step-by-step integration of PayU in Laravel using test mode, without using any external libraries.

Whether you're building an e-commerce platform, donation system, or subscription-based SaaS, this manual PayU integration offers you complete control over the payment flow.


Before starting the blog please make sure that you have created your account at payu if not please visit this URL - https://onboarding.payu.in/app/account/signin?redirect_to=https%3A%2F%2Fpayu.in%2Fbusiness


Here you have to create your account, this page will looks like the below one




Now after registration go to the developer setting. to collect your merchant key and salt




Now let's begin the integration


πŸ› οΈ What Is PayU and Why Use It?

PayU is a leading Indian payment gateway trusted by 450,000+ businesses. It supports:

  1. Credit/Debit Cards
  2. UPI
  3. Wallets
  4. Net Banking
  5. EMI
  6. Buy Now Pay Later (BNPL)

By using PayU Test Mode, you can simulate transactions without real money. This is ideal for developers and QA teams before going live.



🧩 Requirements

Before starting, ensure:

  1. Laravel 8/9/10 installed
  2. Routes, Controllers, Views ready
  3. You have PayU Test Credentials:
  4. Merchant Key
  5. Merchant Salt
  6. Test URL: https://test.payu.in/_payment


πŸ“ Folder Structure Overview

Here's what we’ll build:

routes/web.php
app/Http/Controllers/PayuController.php
resources/views/payu/redirect.blade.php


πŸ” Step 1: PayU Route Setup (web.php)

Add these routes to your routes/web.php file:


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayuController;

Route::get('/payu/payment', [PayuController::class, 'paymentForm'])->name('payu.paymentForm');
Route::post('/payu/success', [PayuController::class, 'paymentSuccess'])->name('payu.success');
Route::get('/payu/failure', [PayuController::class, 'paymentFailure'])->name('payu.failure');


/payu/payment: Initiates the payment form

/payu/success: Callback for successful payments

/payu/failure: Callback for failed transactions


🧠 Step 2: Generate PayU Hash (PayuController.php)


Now create the PayuController in app/Http/Controllers:


php artisan make:controller PayuController


Paste the following code:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PayuController extends Controller
{
public function paymentForm()
{
$url = "https://test.payu.in/_payment";

$posted = [
'key' => env('MERCHANT_KEY'),
'txnid' => 'Txn' . uniqid(),
'amount' => '1000',
'firstname' => 'PayU User',
'email' => 'test@gmail.com',
'phone' => '9876543210',
'productinfo' => 'iPhone',
'surl' => route('payu.success'),
'furl' => route('payu.failure'),
'service_provider' => 'payu_paisa',
'mid' => env('MERCHANT_ID'),
];

$salt = env('MERCHANT_SALT');
$posted['hash'] = $this->generateHash($posted, $salt);
$posted['action'] = $url;

return view('payu.redirect', compact('posted'));
}

public function paymentSuccess(Request $request)
{
return "βœ… Payment Successful! <br> Transaction ID: " . $request->txnid;
}

public function paymentFailure(Request $request)
{
return "❌ Payment Failed!";
}

public function generateHash($params, $salt)
{
$key = $params['key'];
$txnid = $params['txnid'];
$amount = $params['amount'];
$productinfo = $params['productinfo'];
$firstname = $params['firstname'];
$email = $params['email'];
$udf1 = $params['udf1'] ?? '';
$udf2 = $params['udf2'] ?? '';
$udf3 = $params['udf3'] ?? '';
$udf4 = $params['udf4'] ?? '';
$udf5 = $params['udf5'] ?? '';

$hashString = $key . '|' . $txnid . '|' . $amount . '|' . $productinfo . '|' .
$firstname . '|' . $email . '|' . $udf1 . '|' . $udf2 . '|' .
$udf3 . '|' . $udf4 . '|' . $udf5 . '||||||' . $salt;

return strtolower(hash('sha512', $hashString));
}
}


βœ… Tip: Use uniqid() for generating unique transaction IDs during testing.


πŸ“„ Step 3: Create Redirect Form (Blade View)

In resources/views/payu/, create redirect.blade.php:

<h2>Redirecting to PayU...</h2>

<form action="{{ $posted['action'] }}" method="post" name="payuForm">
@foreach($posted as $key => $value)
@if($key !== 'action')
<input type="hidden" name="{{ $key }}" value="{{ $value }}">
@endif
@endforeach
</form>

<script>
document.payuForm.submit();
</script>


This form auto-submits to PayU with hidden fields.

πŸ”’ Step 4: Add Environment Variables (.env)

In your .env file, add:

MERCHANT_KEY=yourTestMerchantKey
MERCHANT_SALT=yourTestMerchantSalt
MERCHANT_ID=yourTestMerchantID

Replace values with the credentials from your PayU test dashboard.


πŸ§ͺ Step 5: Test Your Integration

Now visit:

http://yourdomain.test/payu/payment

You should:

  1. Be redirected to the PayU payment page (test mode)
  2. Choose "Test Card" to simulate the payment
  3. Be redirected back to success/failure URL


When you hit the above URL you have the screen like the below one



πŸ” Optional: Debugging and Logs

To debug issues:

  1. Check Laravel logs: storage/logs/laravel.log
  2. Ensure .env variables are correct
  3. Confirm the hash format follows the sequence:
key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5||||||salt


βœ… Conclusion: What You’ve Built

You’ve successfully integrated PayU payment gateway into your Laravel application without any third-party package, entirely in test mode.

This manual integration provides:

  1. Full control over payment data
  2. Better debugging
  3. No package dependency


πŸ“‚ Download the Complete Source Code

To make your integration even easier, we have prepared a ready-to-use Laravel 12 project with PayU payment gateway integration in test mode. You can download the entire source code from our GitHub repository:

πŸ‘‰ https://github.com/codehunger-team/integrate-payu-payment-gateway-in-laravel-12.git

Clone the repo, set your .env variables for merchant key, salt, and merchant ID, and you’re good to go!



If you're looking to go live with production PayU payments, stay tuned for our next blog on Production PayU integration with Laravel.

πŸ“’ Need help with custom Laravel payment integration? Contact CodeHunger β€” your trusted development partner for India and beyond.