Dynamic UPI QR Code Payment Integration in PHP (With Locked Amount & Order ID)


If you want to accept online payments from customers in India for your website, e-commerce store, or digital products, most people use payment gateways like Razorpay, Cashfree, or PayUMoney. These gateways usually charge 2% to 3% on every transaction, which makes a big difference for small business owners. On top of that, KYC approval and bank settlements can also take several days.

If you want zero transaction fees and instant bank settlement without using any third-party payment gateway, then in this tutorial we will learn Dynamic UPI QR Code Payment Integration using PHP and MySQL. This is one of the best self-hosted solutions. You don't have to pay 2%–3% per transaction to any payment gateway, and you don't have to wait 2–3 days or more for settlement—all those problems are solved directly.

In this system, we set the amount beforehand so the customer can easily pay the exact amount on your website. When they scan the QR code using Google Pay, PhonePe, or any other UPI app, the amount you set and your UPI ID appear automatically on their screen, and they can pay you directly.

In a normal static QR code, the customer has to type the amount manually, which creates a risk of wrong payment. In this tutorial, we will build a Dynamic UPI QR Code system where:

  • The payment Amount stays completely locked (the customer cannot edit it).
  • A Unique Order ID is automatically attached to every payment.
  • A 12-Digit UTR Number Verification and duplicate-blocking system is included to stop fake payments.

How Does Dynamic UPI QR Code and Locked Amount Work?

In India, popular UPI apps like Google Pay, PhonePe, Paytm, and BHIM support NPCI's official UPI Deep Linking specification. When a user scans a QR code generated in the upi://pay format, the UPI app reads the parameters below and automatically locks the payment amount and order details:

Parameter Full Name Example Purpose
pa Payee Address yourbusiness@okhdfcbank Your Merchant UPI ID
pn Payee Name Techno Smarter Your business or website name
am Amount 499.00 Locks the exact payment amount
cu Currency INR Always INR for Indian Rupees
tr Transaction Ref ID ORD202611198421 Your Unique Order ID for tracking
tn Transaction Note Order ORD202611198421 Payment note shown in UPI history and bank statement

Let's Build the Dynamic UPI QR Code Payment System

Step 1: Create the MySQL Database and Table (database.sql)

First, open phpMyAdmin, create a new database, and run the SQL query below.

Keep in mind that we have added a UNIQUE constraint on the utr_number column. This makes sure no user can submit the same 12-digit UTR number twice for two different orders.

CREATE TABLE `upi_payments` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `order_id` varchar(50) NOT NULL,
  `utr_number` varchar(12) NOT NULL,
  `amount` decimal(10,2) NOT NULL DEFAULT 0.00,
  `payment_status` varchar(20) NOT NULL DEFAULT 'Pending',
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  UNIQUE KEY `unique_utr_number` (`utr_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

Step 2: Create connection file: config.php 

Create a config file to connect PHP to the database. We will use PHP PDO prepared statements to make payment secured. 

config.php

<?php
define("DB_HOST", "localhost");
define("DB_NAME", "tute");
define("DB_USER", "root");
define("DB_PASS", "");
try {
    $pdo = new PDO(
        "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
        DB_USER,
        DB_PASS
    );
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database Connection Failed: " . $e->getMessage());
}

?>

Step 3: Create a Dynamic QR Code with Amount

Here, we will create a secure UPI payment form with a dynamic QR code where you can set the payment amount according to your product or order. The QR code will be generated automatically based on the amount you set.

When a customer scans this QR code using PhonePe, Google Pay, or any UPI app, the exact amount you configured will appear automatically. Along with the amount, your UPI ID is also embedded in the QR code, so both your UPI ID and the payable amount show up directly inside the customer's UPI app. The customer can pay right away without typing the amount manually.

We will also include an input box for the UPI Transaction ID (UTR Number). After completing the payment, the customer can enter the UTR number to confirm the order, along with the Order ID they are paying for.

Once submitted, the payment details are saved in the database for admin verification. We will also create a separate page where the UTR number and payment status can be checked.

index.php

<?php require_once("config.php");
$upi_id = "technosmarter@upi"; 
$payee_name = "Techno Smarter"; 
$order_id = "ORDER_" . time(); 
$amount = "72000"; 
$product_name = "Premium Laptop";
$currency = "INR";

$upi_intent_string = "upi://pay?pa=" . $upi_id . "&pn=" . urlencode($payee_name) . "&tr=" . $order_id . "&am=" . $amount . "&cu=" . $currency;

$qr_code_image = "https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=" . urlencode($upi_intent_string);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic UPI Payment - Techno Smarter</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f8f9fa; }
        .payment-card { max-width: 450px; margin: 50px auto; border-radius: 12px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        .qr-box { padding: 15px; border: 2px dashed #0d6efd; border-radius: 10px; display: inline-block; background: #fff; }
        .amount-text { font-size: 28px; font-weight: bold; color: #198754; }
    </style>
</head>
<body>

<div class="container">
    <div class="card payment-card">
        <div class="card-header bg-primary text-white text-center py-3">
            <h4 class="mb-0">Secure UPI Payment</h4>
        </div>
        <div class="card-body text-center p-4">
            
            <h5 class="text-dark mb-1"><?php echo $product_name; ?></h5>
            <p class="text-muted small mb-3">Order ID: <strong><?php echo $order_id; ?></strong></p>
            
            <div class="amount-text mb-4">₹<?php echo $amount; ?></div>

            <!-- Dynamic QR Code -->
            <div class="qr-box mb-4">
                <img src="<?php echo $qr_code_image; ?>" alt="UPI QR Code" class="img-fluid" width="220">
            </div>

            <p class="text-muted small">Scan with any UPI App (Google Pay, PhonePe, Paytm)</p>
            
            <div class="d-grid gap-2 d-md-block d-sm-none mb-3">
                <a href="<?php echo $upi_intent_string; ?>" class="btn btn-success btn-lg w-100">
                    Pay via UPI App Directly
                </a>
            </div>

            <hr>

            <form action="verify-payment.php" method="POST" class="mt-3 text-start">
                <div class="mb-3">
                    <label class="form-label fw-bold">Enter 12-Digit UTR / Reference No.</label>
                    <input type="hidden" name="order_id" value="<?php echo $order_id; ?>">
                    <input type="text" name="utr_number" class="form-control" placeholder="e.g. 312345678901" required minlength="12" maxlength="12">
                </div>
                <button type="submit" class="btn btn-primary w-100">Verify Payment</button>
            </form>

        </div>
    </div>
</div>

</body>
</html>

Step 3: Payment Verification Page

After the customer scans the QR code, completes the payment, enters their UTR number, and clicks the submit button, they will be redirected to this payment verification page.

This page displays all payment-related details such as the Order ID, Payment Status, and UTR Number. By default, the status will show as Pending because the admin verifies the payment manually.

All details are saved in the MySQL database. After checking the payment in your bank or UPI app, you can go to the database (or admin panel) and update the status to 1 if the payment is received. If the payment has not been received, leave it as 0.

verify_payment.php


<?php require_once("config.php");
$message = "";
$status = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $order_id   = htmlspecialchars(trim($_POST['order_id']));
    $utr_number = htmlspecialchars(trim($_POST['utr_number']));
    $amount     = "72000"; 

    // Basic Validation
    if (empty($order_id) || empty($utr_number)) {
        $status = "error";
        $message = "Order ID and UTR Number are required!";
    } elseif (strlen($utr_number) !== 12 || !is_numeric($utr_number)) {
        $status = "error";
        $message = "Invalid UTR Number. It must be exactly 12 digits.";
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO upi_payments (order_id, utr_number, amount, payment_status) VALUES (:order_id, :utr_number, :amount, 'Pending')");
            
            $stmt->bindParam(':order_id', $order_id);
            $stmt->bindParam(':utr_number', $utr_number);
            $stmt->bindParam(':amount', $amount);
            
            $stmt->execute();

            $status = "success";
            $message = "Payment details submitted successfully! We will verify your UTR and process your order shortly.";

        } catch (PDOException $e) {
            if ($e->getCode() == 23000) {
                $status = "error";
                $message = "This UTR Number has already been used. Please check your UTR or contact support.";
            } else {
                $status = "error";
                $message = "Something went wrong! " . $e->getMessage();
            }
        }
    }
} else {
    // Direct access not allowed
    header("Location: upi-payment.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Verification - Techno Smarter</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f8f9fa; }
        .result-card { max-width: 500px; margin: 80px auto; border-radius: 12px; border: none; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        .icon-circle { width: 80px; height: 80px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; }
        .bg-success-light { background-color: #d1e7dd; color: #0f5132; font-size: 40px; }
        .bg-danger-light { background-color: #f8d7da; color: #842029; font-size: 40px; }
    </style>
</head>
<body>

<div class="container">
    <div class="card result-card p-4 text-center">
        
        <?php if ($status == "success"): ?>
            <div class="icon-circle bg-success-light">✓</div>
            <h3 class="text-success mb-3">Verification Pending</h3>
            <p class="text-muted mb-4"><?php echo $message; ?></p>
            <div class="bg-light p-3 rounded mb-4 text-start">
                <div class="mb-1"><strong>Order ID:</strong> <?php echo $order_id; ?></div>
                <div class="mb-1"><strong>UTR Number:</strong> <?php echo $utr_number; ?></div>
                <div class="mb-1"><strong>Status:</strong> <span class="badge bg-warning text-dark">Under Review</span></div>
            </div>
            <a href="./" class="btn btn-outline-success w-100">Make Another Payment</a>
            
        <?php else: ?>
            <div class="icon-circle bg-danger-light">✗</div>
            <h3 class="text-danger mb-3">Submission Failed</h3>
            <p class="text-muted mb-4"><?php echo $message; ?></p>
            <a href="./" class="btn btn-danger w-100">Try Again</a>
        <?php endif; ?>

    </div>
</div>

</body>
</html>

This is how you can easily integrate a Dynamic UPI QR Code Payment System in PHP and MySQL without using any third-party payment gateway. Since it is completely self-hosted and payments happen directly through UPI apps like Google Pay or PhonePe, you don't need Razorpay, PayUMoney, Cashfree, or any other gateway.

Frequently Asked Questions (FAQs)

Q1: Are there any charges or monthly fees on Dynamic UPI QR Code payments?

No. Since the money goes directly from the customer's bank account to your merchant bank account, there is 0% gateway fee and no monthly charge.

Q2: Can a customer change the payment amount after scanning the QR code?

No. Because the am parameter is set in the upi://pay link, the amount box gets locked inside GPay, PhonePe, Paytm, and BHIM apps. The customer has to pay the exact order amount.

Q3: Why does the payment fail when scanning a QR code screenshot from the phone gallery?

According to NPCI security rules, uploading a QR code image from the phone gallery has a maximum limit of ₹2,000. For payments above ₹2,000, the customer should scan the QR code live using the camera or click the "Pay Directly via UPI App" button on mobile.

Q4: Can this payment system be made 100% automatic without manual UTR checking?

Manual UTR verification is 100% free and works best for freelancers, coaching institutes, and digital product sellers. If you want fully automatic (webhook-based) verification, you will need to integrate an official merchant API from providers like Paytm Business API, PhonePe Payment Gateway, or Cashfree.


Please Share

Recommended Posts:-