CRYPTO BOT (Rupizo) - FINAL FIXED BUILD
====================================
New home: https://mybots.indiakart.top/bots/crypto/
Gateway: Rootpeek (Heleket's new API host: https://api.rootpeek.com)

WHAT WAS WRONG (found in your zip + live error logs)
----------------------------------------------------
CRITICAL - money bugs:
1. Double Reject = double refund. Admin withdrawals.php and deposits.php did
   SELECT ... FOR UPDATE OUTSIDE any transaction, so the lock did nothing.
   Two fast Reject clicks refunded the same withdrawal twice. Fixed: transaction
   starts FIRST, row locked inside it, status re-checked before refund/credit.
2. Deposit money lost if user never pressed /start. api.php never created the
   user row, so the webhook credited 0 rows while marking the order Completed.
   Fixed: api.php now creates the user row from initData on every call.
3. Manual "Confirm Sync" could confirm a Failed/cancelled deposit, skipped the
   referral bonus, and never checked the user exists. Fixed: Pending only,
   user must exist, first-deposit bonus now applies same as webhook.
4. Webhook sent "credited" Telegram messages BEFORE the DB commit. If the
   commit failed, the user was told they were paid. Fixed: commit first,
   notify after.
5. Rate mismatch: dashboard fallback was 83.50, deposit fallback was 93.50,
   so the same $1 locked two different INR values. Fixed: one live rate source
   (DB rate if fresh, else live CoinGecko fetch), no hardcoded fallbacks.

CRASH - proven by your live error_log (June 2026):
6. Referral page + admin Referrals page crashed with "Unknown column 'id'".
   Your users table has NO id column, but api.php and referral_admin.php did
   ORDER BY id. Fixed: ORDER BY tg_id.

SECURITY:
7. Live bot token + gateway API key + DB password were hardcoded in config.php
   (and inside the old error_log files, which also leak the token in URLs).
   Fixed: all secrets moved to .env (blocked by .htaccess). ROTATE the bot
   token (@BotFather /revoke) and the Rootpeek API key after upload - the old
   ones have been inside zip files. Also change the DB password in cPanel
   (it is currently the same as the DB username) and update .env.
8. Telegram bot webhook had no secret, anyone could POST fake updates to
   bot.php. Fixed: setWebhook now sends secret_token, bot.php verifies it.
9. No CSRF protection on any admin action. Fixed: session CSRF token on all
   admin POST actions (SameSite=Strict cookies already helped, now enforced).
10. Admin login had no brute-force limit. Fixed: 5 wrong attempts = 15 min lock.
11. settings.php always edited the FIRST admin row (LIMIT 1), not the logged-in
    admin. Fixed: uses the session admin id.
12. cron.php disabled SSL verification (rate feed could be spoofed) and used
    dead WazirX as primary source. Fixed: SSL on, CoinGecko primary, Binance
    fallback, protected by ?key=.
13. No login session regeneration (session fixation). Fixed.
14. Deposit had no min/max; withdrawal had no min and no UPI check.
    Fixed: deposit $1-$5000, withdrawal min Rs.10, UPI must look like name@bank.

DOMAIN / API CHANGES APPLIED:
- MINI_APP_URL + webhook URL: mybots.indiakart.top/bots/crypto/
- Gateway API: https://api.rootpeek.com/v1/payment (was api.heleket.com)
- Settings -> Webhook button now auto-fills the correct /bots/crypto/bot.php
  URL from the install path (no more hardcoded /crypto/bot.php) and shows
  getWebhookInfo after registering. Yes, the dynamic webhook button EXISTS
  in Settings - it was there all along, only the default URL was wrong.
- Webhook now also marks failed/expired gateway payments as Failed instead
  of leaving them Pending forever.

NEW FILES:
- .env / .env.example - all secrets here now. .env is pre-filled with your
  current values so it works immediately; rotate token + API key, then edit .env.
- install.sql - full DB schema (run once in phpMyAdmin; safe on existing DB).
- reset-admin.php - CLI password reset (see step 5). Web access is blocked.

UPLOAD STEPS
------------
1. Upload the contents of crypto/ to:
   /home/ptoaynnt/mybots.indiakart.top/bots/crypto/
   (files at that root, admin/ folder inside it). chmod 644 all files.
   Delete the OLD telebots.store/crypto/ folder and its error_log files.
2. phpMyAdmin -> open ptoaynnt_crypto -> Import -> install.sql -> Go.
   (Creates missing tables only; existing data untouched.)
3. Rotate secrets, then update .env on the server:
   - Telegram @BotFather -> /revoke -> new token -> TG_BOT_TOKEN
   - Rootpeek dashboard -> regenerate API key -> GATEWAY_API_KEY
   - cPanel MySQL -> change DB user password -> DB_PASS
4. Rootpeek merchant dashboard -> set webhook/callback URL to:
   https://mybots.indiakart.top/bots/crypto/webhook.php
   Confirm the Merchant UUID + API key there match .env.
5. Reset your forgotten admin password (cPanel -> Terminal):
   cd ~/mybots.indiakart.top/bots/crypto
   /usr/local/bin/ea-php83 reset-admin.php --user=admin --pass='YourNewPass123' --disable-2fa --create
   (--create makes the admin if none exists; --disable-2fa clears lost 2FA.
   Use your real admin username instead of 'admin' if it was different.
   Password must be 8+ characters. Then log in and set 2FA again in Settings.)
6. Cron (cPanel Cron Jobs or cron-job.org), every 15 minutes:
   https://mybots.indiakart.top/bots/crypto/cron.php?key=<CRON_SECRET from .env>
7. Telegram @BotFather -> /mybots -> your bot -> Bot Settings -> Menu Button
   -> set Web App URL to: https://mybots.indiakart.top/bots/crypto/
8. Admin panel -> Settings -> "Register Engine Webhook" -> click it.
   It should report the active URL as .../bots/crypto/bot.php.
9. Test: /start in the bot -> deposit $1 -> check balance credits ->
   withdraw Rs.10 -> admin Approve -> then a second test Reject to confirm
   the refund happens only once.

NOTE: Heleket's operator (Xeltox Enterprises Ltd, brands Cryptomus/Heleket)
was put under UK sanctions in Oct 2026, and Heleket paused withdrawals for
2 days in Nov 2026. If you have UK customers or UK bank exposure, check with
your CA before routing volume through it. This does not change the code.
