Technical Documentation v1.0.0 WHMCS 9.1.x · PHP 8.2–8.4

Haikhavan Discount Center & Abandoned Cart Recovery

Complete architecture reference, installation guide, database schema, hooks, and troubleshooting.

Quick Installation

  1. Purchase the module and download haikhavan_discount_center-ioncube-8.2-1.0.0-install.zip from your VPSrv Client Area Downloads.
  2. Upload and extract the ZIP into your WHMCS root directory so files land in modules/addons/haikhavan_discount_center/.
  3. Verify PHP 8.2–8.4 with ionCube PHP Loader 14+ / 15.5+ is enabled on your server.
  4. Go to System Settings → Addon Modules in WHMCS Admin, click Activate next to Haikhavan Discount Center & Abandoned Cart Recovery, and assign administrator role access.

Version: 1.0.0
WHMCS Compatibility: WHMCS 8.10+ & WHMCS 9.1.x (PHP 8.1 – 8.3, MySQL 8.x)
Module Type: WHMCS Addon Module (Admin Studio + Client Area Offers Hub + Storefront Countdown Banner + 7 WHMCS Hooks)


Haikhavan Discount Center & Abandoned Cart Recovery (haikhavan_discount_center) — Technical Documentation

1. Architecture & File Layout

haikhavan_discount_center uses PSR-4 autoloading under namespace Haikhavan\DiscountCenter\, WHMCS WHMCS\Database\Capsule for 100% parameterized queries, and zero external CDN libraries.

modules/addons/haikhavan_discount_center/
├── whmcs.json                         # WHMCS schema 1.0 manifest
├── haikhavan_discount_center.php             # Addon lifecycle (_config, _activate, _deactivate, _upgrade, _output, _clientarea)
├── hooks.php                          # 7 hooks: Cart Capture, Checkout Attribution, Abuse Validation, Banner Injector, Navbar, Widget, Cron
├── lib/
│   ├── Database.php                   # 7-table schema installer, settings loader & starter data seeder
│   ├── DiscountEngine.php             # Multi-rule evaluation & stacking precedence engine + featured banner selector
│   ├── CartRecoveryService.php        # Cart snapshots, 1h/24h/72h drip runner, tblpromotions coupon generator & ROI/Funnel analytics
│   ├── AbuseGuard.php                 # IP velocity cap & duplicate new-client promo abuse detector
│   ├── AdminController.php            # Admin SPA dispatcher, Formula-Safe CSV exports & CSRF-protected AJAX API
│   └── ClientController.php           # Client Area Active Offers Hub, Magic Cart Restore & CSRF AJAX estimator
├── templates/
│   ├── admin/app.php                  # 6-tab Admin Conversion & Recovery Studio
│   └── client/offers.tpl              # Smarty Client Area Offers, Savings Estimator & Saved Carts portal
└── assets/
    ├── css/haikhavan_discount_center.css     # Scoped .dc-wrap styling
    └── js/haikhavan_discount_center.js       # Zero-CDN Canvas Funnel & ROI charts, Live Simulator & AJAX controls

2. Database Schema (7 Custom Tables)

Table Purpose Key Columns & Indexes
mod_hk_dc_rules Multi-rule discount definitions id PK, name, code IDX, rule_type (percentage/fixed/free_setup/volume_tier/bundle) IDX, value DECIMAL(16,2), scope (first_invoice/recurring), recurring_cycles, client_eligibility (all/new/returning), min_cart_total, min_qty, tier_config_json, bundle_product_ids_json, target_product_ids_json, billing_cycles_json, priority IDX, stackable, max_uses, uses_count, starts_at, ends_at, is_active IDX, show_banner, banner_text
mod_hk_dc_redemptions Promo usage & ROI attribution ledger id PK, rule_id IDX, userid IDX, order_id IDX, invoice_id, promo_code IDX, cart_subtotal, discount_amount, net_total, ip_address, created_at
mod_hk_dc_abandoned_carts Captured shopping carts id PK, cart_token UNIQUE, session_hash IDX, userid IDX, email IDX, customer_name, items_json, cart_value, currency, status (abandoned/reminded/recovered/expired/unsubscribed) IDX, recovery_step, recovery_promo_code, recovery_promo_id, recovered_order_id, recovered_amount, last_reminded_at, recovered_at, ip_address
mod_hk_dc_recovery_steps Configurable 1h / 24h / 72h drip steps id PK, step_number UNIQUE, name, delay_hours, subject, body_html, attach_coupon, coupon_type, coupon_value, coupon_expiry_days, is_active
mod_hk_dc_recovery_logs Drip email & coupon issuance audit log id PK, cart_id IDX, step_id, email, subject_rendered, promo_code, status IDX, summary, sent_at
mod_hk_dc_abuse_logs Coupon Abuse & Velocity Guard events id PK, userid IDX, email, ip_address IDX, promo_code, rule_id, reason, severity (warn/blocked), created_at
mod_hk_dc_saved_carts Client shareable saved carts id PK, userid IDX, token UNIQUE, title, items_json, estimated_total, created_at

3. Multi-Rule Discount Evaluation Algorithm (DiscountEngine::evaluateCart)

Active rules are loaded ordered by priority ASC, id ASC. For each rule, the engine checks in order:

  1. Schedule window (starts_at <= now <= ends_at)
  2. Global usage cap (max_uses == 0 || uses_count < max_uses)
  3. Client eligibility (all | new | returning)
  4. Minimum cart subtotal & minimum item quantity
  5. Promo code match (for exclusive rules requiring a specific code)
  6. Billing cycle eligibility (billing_cycles_json)
  7. Bundle completeness (all bundle_product_ids_json present in cart) and target product intersection
  8. Stacking lock: if a higher-priority exclusive rule (stackable = 0) has already been applied, lower-priority non-stackable rules are skipped while stackable add-ons (stackable = 1, e.g., Volume Tiers, Bundles, Free Setup) continue evaluating cleanly against the running subtotal.

4. Abandoned Cart Recovery & Auto-Coupon Engine (CartRecoveryService)

  • Capture: ClientAreaPageCart snapshots $_SESSION['cart']['products'] with product names, quantities, cycles, estimated value, and logged-in client or guest email into mod_hk_dc_abandoned_carts keyed by cart_token (cart_<20 hex>).
  • Drip Sequence: processDueDrips() (or manual triggerNextStep($cartId)) advances carts through Step 1 (1h), Step 2 (24h + 10% coupon), and Step 3 (72h + 15% expiring coupon).
  • Native WHMCS Coupon Creation: When a step has attach_coupon = 1, generateSingleUseCoupon() inserts a unique RECOVER-XXXXXX record directly into tblpromotions with maxuses = 1, applyonce = 1, onceperclient = 1, and expirationdate = today + coupon_expiry_days.
  • Magic Restore URL: index.php?m=haikhavan_discount_center&action=restore&token=<cart_token> displays the saved cart banner and routes straight to cart.php?a=view&promocode=<RECOVER-XXXXXX>.
  • Checkout Attribution: AfterShoppingCartCheckout matches the session/client's open abandoned cart, updates status = 'recovered', records recovered_order_id & recovered_amount, and logs promo redemptions.

5. WHMCS Hooks Inventory (hooks.php)

Hook Purpose
ClientAreaPageCart Captures/updates abandoned cart snapshots automatically.
AfterShoppingCartCheckout Marks abandoned carts as recovered with revenue attribution and logs promo redemptions.
ShoppingCartValidateCheckout Invokes AbuseGuard::evaluateAttempt() to block velocity bursts & duplicate new-client IP claims.
ClientAreaFooterOutput Injects the dismissible Storefront Promo & Live Countdown Banner (#hkdc-promo-bar) when a banner rule is active.
ClientAreaPrimaryNavbar Adds Active Offers (index.php?m=haikhavan_discount_center) to the Client Area navigation bar.
AdminHomeWidgets Renders the Haikhavan Discount Center & Cart Recovery dashboard widget.
DailyCronJob Runs CartRecoveryService::processDueDrips() automatically.

6. Security Model & API Reference

  • CSRF Protection: All Admin AJAX POST operations (save_rule, toggle_rule, delete_rule, simulate, trigger_recovery, mark_recovered, process_due_drips, save_step, test_abuse_guard) and Client AJAX POST operations (validate_promo, save_cart_snapshot) enforce 48-char random session tokens via hash_equals() (HTTP 403 on failure).
  • Coupon Abuse & Velocity Guard: Enforces configurable hourly per-IP attempt caps and blocks duplicate redemptions of client_eligibility='new' promos from IPs with existing redemptions, returning HTTP 429 on client validation or checkout validation errors.
  • CSV Formula Injection Hardening: Both CSV export endpoints (export=redemptions, export=carts) neutralize cells beginning with =, +, -, @, \t, or \r.
  • XSS & SQLi Protection: 100% Eloquent/Capsule parameterized queries; all PHP/Smarty/JS outputs properly escaped.