API Online
support@rechapi.com
Developers

Recharge API Integration in PHP: Step-by-Step with Code

Learn how to integrate a mobile recharge API in PHP using cURL: send a recharge, handle SUCCESS, PENDING and FAILED, avoid double recharges and use callbacks.

This tutorial shows how to connect your PHP website or software to a recharge API. We use plain PHP and cURL — no framework needed — so it works on any shared hosting or VPS.

By the end you will be able to send a recharge, read the result, and handle slow (pending) transactions safely.

Before you start

  • A ClubApi account with completed KYC (create one here)
  • Your API token from Account → API
  • Your server's public IP added in IP whitelist
  • PHP 7.4 or newer with the cURL extension enabled
  • The operator ID list from the API docs

Step 1: Create a table for your orders

Always save the order in your own database before calling the API. If something goes wrong, you will still know what you sent.

CREATE TABLE recharges (
  id INT AUTO_INCREMENT PRIMARY KEY,
  urid VARCHAR(40) NOT NULL UNIQUE,
  mobile VARCHAR(15) NOT NULL,
  operator_id INT NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  status VARCHAR(10) DEFAULT 'PENDING',
  order_id VARCHAR(40) NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

The urid column is UNIQUE. This stops the same order from being saved twice.

Step 2: Send the recharge request

Create a file recharge.php:

<?php
// ----- settings -----
$token = "YOUR_TOKEN";                 // from Account > API
$apiUrl = "https://api.clubapi.in/transaction.php";

// ----- order details (from your form) -----
$mobile = "9876543210";
$operatorId = "1";
$amount = "199";
$urid = "TXN" . time() . rand(100, 999);   // your unique id

// TODO: save $urid, $mobile, $operatorId, $amount in your database here

// ----- call the API -----
$data = array(
    "token" => $token,
    "urid" => $urid,
    "operatorId" => $operatorId,
    "mobile" => $mobile,
    "amount" => $amount
);

$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

$result = json_decode($response, true);

Step 3: Handle the result

The response tells you the status. Handle all three cases:

if ($error != "" || $result == null) {
    // network problem or timeout: we do NOT know what happened
    $status = "PENDING";
} else {
    $status = $result["status"];   // SUCCESS / PENDING / FAILED
}

if ($status == "SUCCESS") {
    echo "Recharge successful";
} elseif ($status == "FAILED") {
    echo "Recharge failed, amount not deducted";
} else {
    echo "Recharge is being processed. You will get the final status soon.";
}

// TODO: update status (and orderId) in your database using $urid
Very important: timeoutsIf the request times out, the recharge may still have been done. Mark it PENDING and wait for the callback. Never send the same recharge again with a new urid, or the customer may be recharged twice.

Step 4: Receive the final status by callback

When a recharge is PENDING, ClubApi sends the final status to your callback URL as soon as the operator replies. Set the URL in Account → API → Callback, then update the order in your database when it arrives. Our recharge API callback guide has a complete, safe example.

Step 5: Test before going live

  1. Do a few small recharges on your own numbers.
  2. Check the result in your database and in Transaction History in your account.
  3. Turn off your internet in the middle of a request (or set a 1 second timeout) to test the PENDING path.
  4. Check that the callback updates the order.

Security checklist

  • Keep the token on the server only — never put it in JavaScript or a mobile app.
  • Whitelist only your own server IPs.
  • Validate the mobile number (10 digits) and amount before calling the API.
  • Use HTTPS for your website and callback URL.
  • Regenerate the token immediately if you think it has leaked.

That is all you need for a working recharge integration. For all operators, error codes and other services, see the developer documentation.

Frequently asked questions

Which PHP version do I need?

Any modern PHP version (7.4 or newer) with the cURL extension works.

What should I do if the API request times out?

Mark the transaction PENDING and wait for the callback. Do not resend the same recharge with a new ID.

Can I call the recharge API from a mobile app directly?

No. Call it from your server so your API token stays secret. Your app should talk to your server.

Why is my request rejected?

The most common reasons are a wrong token, a server IP that is not whitelisted, or low wallet balance.

Share this article
ClubApi Team

We build recharge, bill payment, AEPS and KYC APIs at Rechapi Private Limited and help businesses across India launch their own services.

Related articles