# Best VLE Tools ko Payment Gateway se Connect Karna

Ye guide batati hai ki apni website (Best VLE Tools ya koi bhi doosri site)
ko is payment gateway se kaise integrate karein — step by step.

---

## Step 1: Website register karna

1. Gateway ke admin dashboard mein login karein
2. **Websites → Add website** pe click karein
3. Naam aur URL bharein (jaise "Best VLE Tools", `https://bestvletools.in`)
4. Save karne ke baad, **API keys** button pe click karein
5. **Generate live key** (ya pehle test karne ke liye **Generate test key**)
   dabaayein
6. Screen pe do cheezein dikhengi — **API Key** aur **API Secret**. Ye
   secret **sirf ek baar dikhta hai**, isliye turant kahin surakshit jagah
   copy kar lein (jaise apni website ke `.env` ya config file mein)

---

## WordPress / WooCommerce store hai? Plugin use karein (code likhne ki zaroorat nahi)

Agar aapki website **WordPress + WooCommerce** pe bani hai, to neeche wale
custom-code Steps follow karne ki zaroorat nahi — hamara ready-made
WooCommerce plugin use karein:

**📦 Download: [Best VLE UPI Gateway plugin (best-vle-upi-gateway.zip)](best-vle-upi-gateway.zip)**

Plugin setup:

1. WordPress admin mein **Plugins → Add New → Upload Plugin** se
   `best-vle-upi-gateway.zip` upload karke activate karein
2. **WooCommerce → Settings → Payments → Best VLE UPI Gateway → Manage**
   pe jaayein
3. Gateway ko enable karein, aur **Gateway base URL**, **API Key**, **API
   Secret** bharein (Step 1 se mile huye — upar dekhein)
4. Plugin settings mein dikh raha **"Your webhook URL"** copy karein
5. Gateway admin panel mein **Websites → Add website** (ya existing
   website ke **API keys → Webhook**) mein ye webhook URL paste karke save
   karein — jo **webhook signing secret** (`whsec_...`) mile, use plugin
   ke **"Webhook signing secret"** field mein paste kar dein
6. **Save changes**, phir **Test connection** button se confirm kar lein
   ki API key/secret sahi hain

Bas — ab checkout page pe "UPI (GPay / PhonePe / Paytm)" payment option
apne aap dikhega, aur payment success hote hi order automatically
Processing/Completed ho jaayega (webhook ke through).

---

## Step 2: Payment banana (jab customer "Pay Now" dabaye) — custom/non-WordPress websites ke liye

Best VLE Tools ke jis bhi page pe payment lena hai, wahan ye code likhein:

```php
<?php
$apiKey = "pk_live_xxxxxxxxxxxx";      // Step 1 mein mila hua
$apiSecret = "sk_xxxxxxxxxxxx";         // Step 1 mein mila hua
$gatewayUrl = "https://pay.yourdomain.in"; // aapka gateway jahan host hai

$orderId = "BVT-" . time(); // apna unique order ID banayein
$amount = 99.00;             // jo amount customer ko charge karna hai

$ch = curl_init($gatewayUrl . "/api/v1/payment/create.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-API-Key: $apiKey",
    "X-API-Secret: $apiSecret"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "order_id"      => $orderId,
    "amount"        => $amount,
    "customer_name" => $_SESSION['user_name'] ?? 'Customer',
    "callback_url"  => "https://bestvletools.in/payment-success.php?order=" . $orderId,
]));

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

if ($response['success']) {
    // Customer ko gateway ke payment page pe bhej dein
    header("Location: " . $response['payment_url']);
    exit;
} else {
    echo "Payment shuru nahi ho paya: " . $response['error']['message'];
}
```

Customer ab gateway ke QR-wale payment page pe pahunch jaayega, wahan scan
karke pay karega.

---

## Step 3: Pata kaise chalega ki payment ho gaya (Webhook)

**✅ Ye ab ban chuka hai aur kaam karta hai.**

1. Website add karte waqt (ya baad mein) ek **Webhook URL** set karein
   (Admin → Websites → API keys → webhook set karein), jaise
   `https://bestvletools.in/api/payment/webhook.php`
2. Jab payment "success" ho, gateway khud us URL pe ek POST request
   bhejega, is format mein:
   ```json
   {
     "event": "payment.success",
     "event_id": "a1b2c3d4e5f6...",
     "payment_id": "PAY-20260830-00038",
     "order_id": "BVT-1234567890",
     "amount": 99.00,
     "currency": "INR",
     "status": "success",
     "transaction_id": "873364754890",
     "payment_method": "UPI",
     "paid_at": "2026-08-30T12:03:19+05:30"
   }
   ```
3. Request ke saath ye headers aayenge:
   - `X-Webhook-Signature` — HMAC-SHA256 signature, verify karne ke liye
   - `X-Webhook-Timestamp` — Unix timestamp
   - `X-Webhook-Event-ID` — unique ID (retry hone par bhi same rehta hai)

4. Best VLE Tools ka `webhook.php` ye receive karke order ko turant
   "activated" mark kar dega. Signature verify karne ke liye:

```php
<?php
$rawBody = file_get_contents('php://input');
$receivedSignature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
$timestamp = $_SERVER['HTTP_X_WEBHOOK_TIMESTAMP'] ?? '';
$webhookSecret = "whsec_xxxxxxxxxxxx"; // jo aapko webhook set karte waqt mila tha

$expectedSignature = hash_hmac('sha256', $timestamp . '.' . $rawBody, $webhookSecret);

if (!hash_equals($expectedSignature, $receivedSignature)) {
    http_response_code(401);
    exit('Invalid signature');
}

$data = json_decode($rawBody, true);
if ($data['event'] === 'payment.success') {
    // Order activate karo: $data['order_id']
}

http_response_code(200);
echo json_encode(['received' => true]);
```

**Important:** Agar aapka server 2xx status return nahi karta, gateway
automatically retry karega (1, 5, 15, 60, 180 minute ke gap pe, total 5
attempts). Isliye webhook receiver mein hamesha `http_response_code(200)`
(ya koi 2xx) bhejna zaroori hai jab successfully process ho jaye.

**Cron job zaroori hai:** Webhook delivery cPanel ke cron job pe depend
karta hai (`cron/process-webhooks.php`, har 1-2 minute mein). Agar ye cron
set nahi hai, webhook queue toh honge lekin kabhi bhejenge nahi. Setup ke
liye `PHASE10_README.md` dekhein.

---

## Step 4: Abhi ke liye — Status Check (polling)

Jab tak webhook nahi bana, aap khud check kar sakte hain ki payment hua ya
nahi:

```php
<?php
$paymentId = "PAY-20260830-00038"; // wahi jo Step 2 mein mila tha

$ch = curl_init($gatewayUrl . "/api/v1/payment/status.php?payment_id=" . $paymentId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-API-Key: $apiKey",
    "X-API-Secret: $apiSecret"
]);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);

if ($response['status'] === 'success') {
    // Order activate karo
} elseif ($response['status'] === 'pending') {
    // Abhi wait karo
}
```

Ye `payment-success.php` (jo callback_url mein diya tha) ke andar chalayein
— customer jab gateway se wapas redirect hoke aayega, tab status check
karke order activate kar dein.

---

## Kya-kya chahiye hoga is sabko chalane ke liye

1. ✅ Gateway server live hona chahiye (PHP hosting pe deployed)
2. ✅ Aapka Android phone (business UPI ke saath) monitor app chalate hue
3. ⚠️ `default_upi_vpa` setting mein aapka real UPI ID daala hona chahiye
   (Admin → Settings → UPI) — installer wizard mein bhi ye set kar sakte hain
4. ⚠️ Website ka API key/secret generate kiya hona chahiye (Step 1)
5. ⚠️ **cPanel mein 3 cron jobs add karne honge** (details:
   `PHASE10_README.md`) — inke bina matching/webhook ka safety-net kaam
   nahi karega, aur webhook delivery bilkul nahi hogi

---

## Deployment kaise karein (naya server pe)

1. Poore project ko apne hosting pe upload karein
2. Browser mein `https://yourdomain.in/install/` kholein
3. 6-step wizard follow karein — database, admin account, settings sab
   yahin set ho jaayega
4. cPanel mein 3 cron jobs add karein (`PHASE10_README.md` mein exact
   commands hain)
5. Admin dashboard mein login karke Android device register karein, phone
   pe app install karein
6. Website add karke API key generate karein — `MERCHANT_INTEGRATION_GUIDE.md`
   ka Step 1-2 follow karein

---

## Abhi kya-kya baaki hai (transparency ke saath)

`PROGRESS_TRACKER.md` file mein poora scorecard hai. Webhook Engine, Cron
scripts, aur Installer Wizard — teeno ab ban chuke hain aur real testing
se verify kiye ja chuke hain. Baaki bacha hua sab optional polish hai
(Testing Mode, Notification Center UI, rate-limit config UI) — system
functionally complete hai.
