# Walkthrough of Changes

The following updates have been successfully completed in the NextPay codebase:

## 1. Database Diagnostic Tool & Mollpay Live Tester

- **Database Test Tool:** Created [db_test.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/db_test.php) in the website root directory. This script performs step-by-step diagnostic checks on different database host/credential configurations and outputs the exact MySQL response directly in the browser to simplify troubleshooting on live cPanel/Hostinger deployments.
- **Mollpay Payin Live Tester:** Created [test_mollpay.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/test_mollpay.php) in the website root directory. This script executes a live POST payment request directly to Mollpay's API and dumps the raw response headers and body directly in the browser to verify signature validation, endpoint statuses, and merchant settings.

---

## 2. Gateway Router ON/OFF Switch

- **UPI Manager Toggle:** Integrated a Gateway Router Switch panel in [addupi.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upimenger/addupi.php) to toggle `gateway_status` ON (Active) or OFF (Disabled).
- **Conditional Package Display:** Configured [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php) to check `gateway_status`. When turned OFF, it automatically hides all gateway packages from the frontend purchase dashboards, showing only P2P user consignments and manual packages.

---

## 3. Database Auto-Seeder & Helper (Up to 25,000 INR)

- **Standard Dynamic Seeder:** Updated the auto-migration seeder block in [db.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/db.php) and the seeder tool [seed_packages.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/seed_packages.php) to clean up old verbose entries and seed exactly 19 standard package sizes (200, 300, 500, 600, 700, 800, 1000, 1200, 1500, 2000, 2500, 3000, 4000, 5000, 8000, 10000, 15000, 20000, 25000 INR). This significantly reduces clutter on the buy screens.

---

## 4. SQL Placeholders & Checkout verification Fixes

- **SQL Parameter Count Fix:** Fixed the buy order insertion query in both [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php). An extra `?,` placeholder parameter in the SQL insert query had been causing SQL exceptions during order creation (making them fail to redirect and causing them to get stuck or fail). The parameter list is now perfectly aligned (exactly 11 parameter markers for 11 query arguments).
- **Gateway Checkout Verification Bypass:** Modified [checkout.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/checkout.php) check conditions. Gateway orders (`is_gateway = 1`) no longer require users to link their own active UPI ID. Users can now view the dynamic gateway payment details (Seller Name, Seller UPI ID, Amount) and proceed to confirm payments immediately without being redirected to the KYC/UPI link page (`kycpa.php`).
- **Clean Package Cards (Tag Removal):** Completely deleted the `offer-reward-tag` span elements from both [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php) HTML card templates, ensuring display strings like `'Gateway Package'`, `'Manual Package'`, or `'P2P Consignment'` are never generated or shown.

---

## 5. Agent Panel Login Role Check

- **Case-Insensitive Role Check:** Fixed the role authorization constraints in [agent/login.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/agent/login.php) and [agent/header.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/agent/header.php). The SQL database stores user roles in lowercase format (e.g. `'agent'`, `'admin'`, `'sub_admin'`), but the agent panel was checking for titlecase string arrays (`'Agent'`, `'Admin'`, `'SubAdmin'`).
- The validator functions now normalize role input to lowercase before making comparisons, allowing seamless login for promoter, agent, admin, and sub-admin accounts.

---

## 6. Callback Webhook Robustness for Mollpay

- **Callback Parameter Fallbacks:** Updated [callback.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/callback.php) parameter retrieval logic. Added fallback parameter keys to support `order_id` / `out_trade_no` for the order number, and `pay_status` / `result` for the success state.
- **Success Status List:** Expanded the payment status verification conditions. It now accepts boolean `true`, string `"true"`, integer `1`, `"success"`, and `"recharge success"` as successful payment indicators to capture all possible formatting updates from the Mollpay API.

---

## 7. Cleaned Payment Details Display

- **Redundant Status Row Removed:** Removed the hardcoded `"Status: Active"` row from the payment checkout details screen in [checkout.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/checkout.php) for standard UPI and UPI Plus payment configurations. The checkout details list now displays exactly and only the **Seller Name** (Bank Name), **Seller UPI ID**, and **Remittance Amount** to keep user details clean and structured.

---

## 8. Consignment Auto-Selling Splits & Balance Deduction (Up to 25,000 INR)

- **Randomized Auto-Selling Splits:** Updated `syncUserAutoSellingOrders($pdo, $user_id)` helper function in [db.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/db.php). Instead of splitting unallocated seller balances sequentially into identical amounts (which generated back-to-back 2000, 2000, 2000 orders), the algorithm now dynamically shuffles and randomly selects from a diverse list of denominations (25000, 20000, 15000, 10000, 8000, 5000, 4000, 3000, 2500, 2000, 1500, 1200, 1000, 800, 700, 600, 500, 300, 200 INR) until the balance is fully allocated.
- **Toggle Off Clean-Up:** When a user turns OFF their "In Sell" status, all their pending active consignment sells (`Paying` status) are automatically removed from the database to stop them from appearing on the purchase screens.
- **Payout Balance Deduction:** Configured automatic balance deduction upon payout completion. When a buy order matched to a P2P consignment completes (in `triggerAutoSell()`), or when the administrator manually marks a sell order as successful in [admin/sell_orders.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/admin/sell_orders.php), the corresponding payout amount is instantly subtracted from the seller's platform `itoken_balance` to prevent balance duplication.

---

## 9. Dynamic Manual UPI Rotation, Manual on Top & Amount Dedup

- **Manual Packages on TOP:** Updated [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php). Manual packages added by the manager are grouped and displayed strictly at the very top of the list.
- **Gateway & User Consignments Shuffled Below:** Re-enabled P2P user consignment sell orders (`sell_orders`) to show on the purchase dashboard page. They are merged together with the gateway packages and shuffled/mixed dynamically below the manual packages.
- **Unique Package Amounts (Gateway Suppression):** Added a dynamic duplicate amount filter in [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php). When a manual package or P2P consignment of amount X is active, the system automatically suppresses/hides the duplicate gateway card of that same amount X, ensuring each amount shows up as a single, unique card.
- **Dynamic P2P Manual Rotation:** Updated [upibuy.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuy.php) and [upibuyplus.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/upibuyplus.php) manual package selection checks. If a manager configures multiple manual packages for the same price/amount, the backend queries the database for the active manual accounts sorting them dynamically by `order_count ASC`, incrementing the count upon assignment. This rotates manual UPIs seamlessly instead of repeating the same single handle.
- **Robust Local Fallback:** Updated `get_next_rotating_upi` in [db.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/db.php) to filter fallback options by active manual packages first (sorted by order count and price range). This ensures that if the Mollpay API fails, the system automatically selects a real rotating manual account instead of showing a dummy placeholder handle.

---

## 10. Startup Popup Announcement Toggle Control

- **Admin ON/OFF Toggle Switch:** Integrated a new setting field `site_popup_status` inside [settings.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/admin/settings.php). Administrators can now toggle this setting value to `'1'` (Active / ON) or `'0'` (Disabled / OFF) in the admin global settings console.
- **Verification Sync:** Modified [home.php](file:///c:/Users/Ashut/Downloads/Telegram%20Desktop/NEXTPAY_PATHAN/home.php). The global announcement startup popup modal logic reads `site_popup_status` and only triggers modal output if active, preventing startup clutter when disabled.
