Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript
10.8 KB
Raw Blame
import path from 'node:path';
import assert from 'node:assert/strict';
import { Database, runMigrations } from '../packages/db/src/index.js';
import { ReceivingAccountRepository } from '../packages/db/src/repositories/receiving-account.repository.js';
import { PaymentSessionRepository } from '../packages/db/src/repositories/payment-session.repository.js';
import { MerchantRepository } from '../packages/db/src/repositories/merchant.repository.js';
import { ObservationRepository } from '../packages/db/src/repositories/observation.repository.js';
import { AttributionEngine } from '../packages/core/src/attribution-engine.js';
import { generateQrSvg } from '@youpi/core';
import {
  buildUpiUri,
  buildUpiIntentUri,
  getUpiProviders,
  findUpiProviderByPackage,
  findUpiProviderById,
  PaymentStatus,
  PaymentMethod
} from '../packages/common/src/index.js';

async function runFullRegression() {
  console.log('============================================================');
  console.log('🧪 RUNNING FULL REGRESSION & DYNAMIC VERIFICATION SUITE');
  console.log('============================================================');

  const dbPath = path.resolve(process.cwd(), 'data/youpi_pg.sqlite');
  const prodDb = new Database({ dbPath });
  runMigrations(prodDb);
  const accountRepo = new ReceivingAccountRepository(prodDb);
  const sessionRepo = new PaymentSessionRepository(prodDb);
  const merchantRepo = new MerchantRepository(prodDb);
  const obsRepo = new ObservationRepository(prodDb);
  const attributionEngine = new AttributionEngine({ sessionRepo, obsRepo });

  const merchant = prodDb.queryOne('SELECT * FROM merchants');
  assert.ok(merchant, 'Production merchant must exist');

  // Verify preserved production account
  const originalAccount = accountRepo.findActiveForMerchant(merchant.id);
  assert.ok(originalAccount);
  assert.ok(originalAccount.upiId);
  assert.ok(originalAccount.payeeName);

  let testVpaB = null;
  let testPayment1 = null;
  let testPayment2 = null;

  try {
    // --- 1. MULTIPLE VPA & ONLY ONE ACTIVE ENFORCEMENT ---
    console.log('\n[1] Testing Multiple VPA & Single Active Backend Enforcement...');
    testVpaB = accountRepo.create({
      merchantId: merchant.id,
      upiId: 'test.personal@okhdfcbank',
      payeeName: 'Personal Store Account',
      isMerchantEnabled: false,
      isActive: false
    });
    assert.ok(testVpaB.id);
    assert.equal(testVpaB.upiId, 'test.personal@okhdfcbank');
    assert.equal(testVpaB.isMerchantEnabled, false);
    assert.equal(testVpaB.isActive, false);

    // Verify original account remains active
    let currentActive = accountRepo.findActiveForMerchant(merchant.id);
    assert.equal(currentActive.id, originalAccount.id);
    assert.equal(currentActive.isActive, true);

    // List all accounts
    const allAccounts = accountRepo.findByMerchantId(merchant.id);
    assert.equal(allAccounts.length, 2, 'Merchant must have 2 saved VPAs');
    console.log('  [✓] Successfully created second VPA (Non-Merchant). Multiple VPAs allowed.');

    // --- 2. DYNAMIC PAYMENT SESSION CREATION & SNAPSHOT STABILITY ---
    console.log('\n[2] Testing Payment Creation & Session Stability across VPA Switches...');

    // Payment 1 created while VPA A (Merchant-enabled) is active
    testPayment1 = sessionRepo.create({
      merchantId: merchant.id,
      receivingAccountId: originalAccount.id,
      orderId: 'REG-ORD-001',
      amountPaise: 10000n, // ₹100.00
      method: PaymentMethod.UPI,
      status: PaymentStatus.INITIATED,
      metadata: {
        receivingAccountSnapshot: {
          id: originalAccount.id,
          upiId: originalAccount.upiId,
          payeeName: originalAccount.payeeName,
          isMerchantEnabled: originalAccount.isMerchantEnabled
        }
      }
    });
    assert.equal(testPayment1.receivingAccountId, originalAccount.id);
    console.log('  [✓] Payment 1 created with VPA A (9876543210@axl, Merchant-enabled)');

    // Now merchant switches active VPA to VPA B (Non-Merchant)
    console.log('  [→] Activating VPA B (Non-Merchant)...');
    accountRepo.activateAccount(merchant.id, testVpaB.id);

    // Verify VPA B is active and VPA A is deactivated
    const refreshedA = accountRepo.findById(originalAccount.id);
    const refreshedB = accountRepo.findById(testVpaB.id);
    assert.equal(refreshedB.isActive, true);
    assert.equal(refreshedA.isActive, false);
    console.log('  [✓] Activating VPA B automatically deactivated VPA A.');

    // Payment 2 created while VPA B (Non-Merchant) is active
    testPayment2 = sessionRepo.create({
      merchantId: merchant.id,
      receivingAccountId: testVpaB.id,
      orderId: 'REG-ORD-002',
      amountPaise: 25000n, // ₹250.00
      method: PaymentMethod.UPI,
      status: PaymentStatus.INITIATED,
      metadata: {
        receivingAccountSnapshot: {
          id: testVpaB.id,
          upiId: testVpaB.upiId,
          payeeName: testVpaB.payeeName,
          isMerchantEnabled: testVpaB.isMerchantEnabled
        }
      }
    });
    assert.equal(testPayment2.receivingAccountId, testVpaB.id);
    console.log('  [✓] Payment 2 created with VPA B (test.personal@okhdfcbank, Non-Merchant)');

    // VERIFY SESSION 1 RETAINS VPA A (HISTORICAL IMMUTABILITY)
    const session1Reloaded = sessionRepo.findById(testPayment1.id);
    assert.equal(session1Reloaded.receivingAccountId, originalAccount.id);
    const accountForSession1 = accountRepo.findById(session1Reloaded.receivingAccountId);
    assert.equal(accountForSession1.upiId, '9876543210@axl');
    assert.equal(accountForSession1.isMerchantEnabled, true);
    console.log('  [✓] Session 1 immutability verified: still bound to VPA A even after VPA B was activated');

    // --- 3. DYNAMIC INTENT & DEEP LINK PARAMETERS ---
    console.log('\n[3] Testing Dynamic UPI URIs & Android Intents...');
    const providers = getUpiProviders();
    assert.equal(providers.length, 30);

    // Merchant Mode: App Button Android Intent
    const gpay = findUpiProviderById('google-pay');
    const intentP1 = buildUpiIntentUri({
      payeeVpa: accountForSession1.upiId,
      payeeName: accountForSession1.payeeName,
      amountPaise: session1Reloaded.amountPaise,
      transactionRef: session1Reloaded.orderId,
      currency: 'INR',
      packageName: gpay.packageName
    });
    assert.equal(
      intentP1,
      'intent://pay?pa=9876543210%40axl&pn=Demo Store+Merchant&am=100.00&tn=REG-ORD-001#Intent;scheme=upi;package=com.google.android.apps.nbu.paisa.user;end'
    );
    console.log('  [✓] Payment 1 Intent URI:', intentP1);

    // Merchant Mode: Show More Apps Deep Link
    const deepLinkP1 = buildUpiUri({
      payeeVpa: accountForSession1.upiId,
      payeeName: accountForSession1.payeeName,
      amountPaise: session1Reloaded.amountPaise,
      transactionRef: session1Reloaded.orderId,
      currency: 'INR'
    });
    assert.equal(deepLinkP1, 'upi://pay?pa=9876543210%40axl&pn=Demo Store+Merchant&am=100.00&tn=REG-ORD-001');
    console.log('  [✓] Payment 1 Show More Apps Deep Link:', deepLinkP1);

    // Non-Merchant Mode: QR Code generation
    const qrUriP2 = buildUpiUri({
      payeeVpa: testVpaB.upiId,
      payeeName: testVpaB.payeeName,
      amountPaise: testPayment2.amountPaise,
      transactionRef: testPayment2.orderId,
      currency: 'INR'
    });
    assert.equal(qrUriP2, 'upi://pay?pa=test.personal%40okhdfcbank&pn=Personal+Store+Account&am=250.00&tn=REG-ORD-002');
    const svgP2 = generateQrSvg(qrUriP2, 220);
    assert.ok(svgP2.includes('<svg') && svgP2.includes('</svg>'));
    console.log('  [✓] Payment 2 Non-Merchant QR SVG successfully generated (length: ' + svgP2.length + ' bytes)');

    // Filename Sanitizer Test
    function buildFilename(vpa, amount, date) {
      const pad = n => n.toString().padStart(2, '0');
      const dStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
      const tStr = `${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}`;
      const rawVpa = vpa.replace(/[/\\?%*:|"<>]/g, '_');
      const rawAmt = amount.replace(/[/\\?%*:|"<>]/g, '_');
      return `${rawVpa}_${rawAmt}_${dStr}_${tStr}.png`;
    }
    const fixedDate = new Date('2026-09-10T14:25:30Z');
    const filename = buildFilename(testVpaB.upiId, '250.00', fixedDate);
    assert.ok(filename.startsWith('test.personal@okhdfcbank_250.00_'));
    assert.ok(filename.endsWith('.png'));
    console.log('  [✓] Dynamic QR Download Filename Sanitizer Verified:', filename);

    // --- 4. ATTRIBUTION & OBSERVATION COMPATIBILITY ---
    console.log('\n[4] Testing Observation Ingestion & Attribution Compatibility...');
    sessionRepo.acquireSlotAtomic(testPayment1.id, originalAccount.id);

    const obs = attributionEngine.processObservation({
      receivingAccountId: originalAccount.id,
      source: 'ANDROID_OBSERVER',
      amountPaise: 10000n,
      observedAt: new Date(),
      senderVpa: 'payer@upi',
      bankReferenceNumber: 'UTR99887766',
      fingerprintHash: 'reg_test_fp_' + Date.now(),
      rawPayload: {}
    });

    assert.equal(obs.attributed, true);
    assert.equal(obs.paymentSessionId, testPayment1.id);
    assert.equal(obs.outcomeStatus, PaymentStatus.SUCCESS);
    console.log('  [✓] Attribution Engine attributed payment successfully with active receiving account');

  } finally {
    // --- TEARDOWN TEST DATA & RESTORE PRISTINE PRODUCTION STATE ---
    console.log('\n[→] Cleaning up test records and restoring pristine production merchant state...');
    if (testPayment1) {
      prodDb.execute('DELETE FROM payment_observations WHERE attributed_payment_id = ?', [testPayment1.id]);
      prodDb.execute('DELETE FROM payment_sessions WHERE id = ?', [testPayment1.id]);
    }
    if (testPayment2) {
      prodDb.execute('DELETE FROM payment_sessions WHERE id = ?', [testPayment2.id]);
    }
    if (testVpaB) {
      prodDb.execute('DELETE FROM payment_receiving_accounts WHERE id = ?', [testVpaB.id]);
    }

    // Re-activate original VPA
    accountRepo.activateAccount(merchant.id, originalAccount.id);
    prodDb.execute('UPDATE payment_receiving_accounts SET is_slot_busy = 0, current_active_session_id = NULL');
    prodDb.execute("DELETE FROM audit_logs WHERE entity_type IN ('PAYMENT_SESSION', 'PAYMENT_OBSERVATION')");

    const finalActive = accountRepo.findActiveForMerchant(merchant.id);
    assert.equal(finalActive.id, originalAccount.id);
    assert.equal(finalActive.isActive, true);
    assert.equal(finalActive.isSlotBusy, false);

    prodDb.close();
    console.log('[✓] Production database restored to pristine state: VPA 9876543210@axl active, slot free.');
  }

  console.log('============================================================');
  console.log('🎉 FULL REGRESSION & E2E VERIFICATION COMPLETED WITH 100% SUCCESS');
  console.log('============================================================');
}

runFullRegression().catch(err => {
  console.error('REGRESSION FAILED:', err);
  process.exit(1);
});