Skip to main content

Fraud Detection

Related docs: Security Compliance · Payment Architecture · Microservices · Data Models


1. Overview

Pakashop implements a dedicated fraud detection microservice (pakashop-fraud, port 3006) that evaluates every payment initiation in real-time. The system uses a rules-based engine with composite risk scoring to detect and prevent fraudulent transactions.


2. Architecture

Payment Initiation Request


pakashop-backend (port 3080)

├──► pakashop-fraud (port 3006) ──► Risk Score Calculation
│ │
│ ├── Velocity Check
│ ├── Amount Anomaly
│ ├── Self-Dealing Detection
│ ├── Geographic Analysis
│ └── Device Fingerprinting

│◄─── { score: 45, status: "PASSED", rules: [] }


Payment proceeds OR blocked

3. Fraud Rules Engine

3.1 Active Rules

Rule IDNameThresholdWeightDescription
VEL_001Payment Velocity>5 payments/hour from same IP25Detects rapid-fire payment attempts
VEL_002Account Velocity>10 payments/day from same user20Detects account takeover or testing
AMT_001Amount Anomaly>3x average order value20Detects unusual purchase amounts
AMT_002Micro-Amount TestingLess than K 1.00 payment15Detects card testing behavior
SDL_001Self-DealingBuyer = Seller100Immediate block; same account buying from own shop
GEO_001Geographic AnomalyPayment from new country/region15Detects account takeover from unusual location
DEV_001New DeviceFirst payment from new device10Elevated scrutiny for new devices
DEV_002Device MismatchDevice fingerprint changed20Detects session hijacking
HRS_001Off-Hours ActivityPayment between 2-5 AM local time5Slight elevation for unusual hours

3.2 Risk Score Calculation

// services/fraud/src/engine/score.js
function calculateRiskScore(evaluation) {
let score = 0;
const triggeredRules = [];

for (const rule of RULES) {
if (rule.evaluate(evaluation)) {
score += rule.weight;
triggeredRules.push(rule.id);
}
}

// Cap at 100
score = Math.min(score, 100);

return {
score,
triggeredRules,
status: score < 30 ? 'PASSED' : score < 70 ? 'REVIEW' : 'BLOCKED'
};
}

3.3 Score Interpretation

Score RangeStatusAction
0-29PASSEDPayment proceeds normally
30-69REVIEWPayment blocked; added to fraud review queue
70-100BLOCKEDPayment blocked immediately; order flagged

4. Data Collection

4.1 Evaluation Context

The fraud service collects the following data for each evaluation:

{
"userId": "uuid",
"orderId": "uuid",
"ipAddress": "192.168.1.1",
"userAgent": "Mozilla/5.0...",
"deviceFingerprint": "abc123...",
"amount": 250.00,
"currency": "ZMW",
"paymentMethod": "MTN_MONEY",
"phoneNumber": "+260977123456",
"shopId": "uuid",
"billingAddress": { ... },
"timestamp": "2026-05-12T10:00:00Z",
"isNewDevice": false,
"previousPaymentsCount": 5,
"previousPaymentsToday": 2,
"accountAgeDays": 30
}

4.2 Redis Cache for Velocity Checks

Key: fraud:velocity:{ip}:{hourBucket}
Value: { paymentCount, amountTotal, lastPaymentAt }
TTL: 1 hour

Key: fraud:velocity:user:{userId}:{dayBucket}
Value: { paymentCount, amountTotal, lastPaymentAt }
TTL: 24 hours

5. API Endpoints

5.1 Internal Evaluation Endpoint

Called by pakashop-backend during payment initiation.

POST /internal/evaluate
Headers: X-Internal-Key: {INTERNAL_API_KEY}

Request:

{
"userId": "uuid",
"orderId": "uuid",
"ipAddress": "192.168.1.1",
"deviceFingerprint": "abc123",
"amount": 250.00,
"currency": "ZMW",
"paymentMethod": "MTN_MONEY",
"shopId": "uuid",
"timestamp": "2026-05-12T10:00:00Z"
}

Response:

{
"score": 45,
"status": "REVIEW",
"triggeredRules": ["VEL_001", "AMT_001"],
"recommendation": "Flag for manual review",
"evaluatedAt": "2026-05-12T10:00:01Z"
}

5.2 Admin Review Queue Endpoints

MethodPathAuthDescription
GET/api/v1/fraud/queueFRAUD_ANALYST, PLATFORM_ADMINList pending reviews
GET/api/v1/fraud/queue/:idFRAUD_ANALYST, PLATFORM_ADMINGet review details
POST/api/v1/fraud/queue/:id/approveFRAUD_ANALYST, PLATFORM_ADMINApprove blocked transaction
POST/api/v1/fraud/queue/:id/rejectFRAUD_ANALYST, PLATFORM_ADMINReject blocked transaction
GET/api/v1/fraud/rulesPLATFORM_ADMINList all fraud rules
PUT/api/v1/fraud/rules/:idPLATFORM_ADMINUpdate rule (threshold, weight, active status)
GET/api/v1/fraud/statsPLATFORM_ADMINFraud statistics dashboard

6. Admin Review Workflow

6.1 Review Dashboard

The fraud review dashboard displays:

  • Blocked transaction details (amount, user, shop, payment method)
  • Triggered rules with explanations
  • User's payment history
  • Risk score breakdown
  • Device fingerprint information
  • Geographic location data

6.2 Review Actions

ActionEffect
ApprovePayment proceeds; FraudEvent.status = APPROVED; order continues
RejectOrder cancelled; FraudEvent.status = REJECTED; customer notified
EscalateMarked for senior review; FraudEvent.status = ESCALATED
Add NoteAnalyst adds contextual note to FraudEvent.notes

7. Configuration

7.1 Environment Variables

# Fraud Detection
FRAUD_VELOCITY_HOURLY_LIMIT=5
FRAUD_VELOCITY_DAILY_LIMIT=10
FRAUD_AMOUNT_MULTIPLIER=3.0
FRAUD_MICRO_AMOUNT_THRESHOLD=1.00
FRAUD_REVIEW_SCORE_THRESHOLD=30
FRAUD_BLOCK_SCORE_THRESHOLD=70
FRAUD_CACHE_TTL_HOURS=24

7.2 Rule Configuration

Rules can be dynamically adjusted via the admin dashboard without redeployment:

// PUT /api/v1/fraud/rules/VEL_001
{
"threshold": 5,
"weight": 25,
"isActive": true,
"description": "Payment velocity from same IP"
}

8. Monitoring

8.1 Key Metrics

MetricAlert ThresholdDescription
fraud_evaluation_time_ms> 100ms p95Time to evaluate a transaction
fraud_blocked_rate> 10%Percentage of payments blocked
fraud_false_positive_rate> 5%Approved transactions that were flagged
fraud_queue_depth> 100Number of pending reviews
fraud_review_time_minutes> 30Average time to review a case

8.2 Alerts

  • High block rate: May indicate overly aggressive rules; review thresholds
  • High false positive rate: May indicate rules too sensitive; adjust weights
  • Queue backlog: Scale fraud analyst team or adjust auto-approval thresholds

For internal use only. Do not distribute outside Pakashop engineering.