# π‘οΈ PromptPay Guard
**Zero-Trust Low-Latency Security Firewall for Autonomous AI Agent Payments on PayPal Orders API v2**
[](https://nodejs.org/)
[](https://developer.paypal.com/docs/api/orders/v2/)
[](https://ai.google.dev/)
[-10B981?style=for-the-badge&logo=checkmarx&logoColor=white)](#-automated-testing--verification-suites)
[](#-cost-profiling--financial-unit-economics)
[](LICENSE)
[](https://youtu.be/2_1kc6dqQbE)
[](https://www.loom.com/share/7d3de0a2bc9f4cd8be16d7e94b46700a)
*Built for the **PayPal AI Hackathon 2026** β’ Autonomous Agent Commerce & FinTech Track*
[πΊ Watch on YouTube](https://youtu.be/2_1kc6dqQbE) β’ [π₯ Watch on Loom](https://www.loom.com/share/7d3de0a2bc9f4cd8be16d7e94b46700a) β’ [Why Gemini 1.5?](#-why-google-gemini-15) β’ [Edge Cases Defense](#οΈ-overcoming-architectural-blind-spots--edge-cases) β’ [Screenshots](#-live-dashboard--defense-screenshots) β’ [Live Dashboard](#-interactive-telemetry-dashboard--simulator) β’ [Architecture](#-system-architecture--request-lifecycle) β’ [3-Zone Risk Engine](#-3-zone-operational-risk-engine--radial-gauge) β’ [Quickstart](#-quickstart-guide) β’ [API Reference](#-complete-api-reference)
π¬ Video Walkthrough & Interactive Demo
πΉ YouTube Walkthrough: https://youtu.be/2_1kc6dqQbE
πΉ Loom Interactive Walkthrough: https://www.loom.com/share/7d3de0a2bc9f4cd8be16d7e94b46700aDemonstrates the Two-Tier Zero-Trust pipeline, 5 live attack vector simulations, SVG radial gauge animations, Human-in-the-Loop triaging overrides, and settlement verification with PayPal Orders API v2.
π Problem Statement: The Autonomous Agent Dilemma
As autonomous AI agents (AutoGPT, LangChain, CrewAI, Claude Computer Use) transition from informational assistants to economic actors, they are granted delegated authority to execute financial settlements. However, standard OAuth access tokens and API keys authenticate the client software, not the legitimacy of the intent.
This introduces severe financial vulnerabilities:
- Adversarial Prompt Injection & Context Smuggling: Malicious instructions injected into invoice text, web scrapes, or customer prompts can deceive an agent into overriding its budget and routing corporate capital to unauthorized escrow accounts.
- Salami Micro-Drain Attacks: High-frequency autonomous loops executing rapid sub-dollar micro-transactions ($0.99) designed to fly beneath traditional batch auditing thresholds.
- Vendor Spoofing & Typo-Squatting Homoglyphs: Subtle visual homoglyphs (e.g.
[email protected]vs[email protected]) deceive vision/scraping agents into settling invoices to fraudulent payees. - Astronomical LLM Inference OpEx: Running monolithic frontier models (GPT-4 or Claude 3.5 Sonnet at ~$0.00235/tx) to inspect 100% of incoming volumetric traffic imposes unsustainable operational costs on FinTech platforms.
π‘ The Solution: Two-Tier Zero-Trust Payment Firewall
PromptPay Guard operates as a high-throughput, low-latency Reverse Proxy positioned directly upstream of the PayPal Orders API v2. It enforces deterministic programmatic constraints before invoking cost-efficient micro-LLM semantic verification:
- β‘ Tier-1 In-Memory Fast Heuristics (<1ms | $0.00 Cost): Evaluates strict schema types, atomic sliding-window rate limiting (Salami defense), single & cumulative daily budget ceilings, and exact regex vendor whitelists with homoglyph detection. Absorbs ~38% of malicious traffic at zero LLM cost.
- π§ Tier-2 Semantic Intent Engine (~180ms | Gemini 1.5): Compares the user's declared business intent against the agent's live reasoning scratchpad and context window to detect prompt injections, role reversals, and semantic divergence.
- π― 3-Zone Operational Risk Model: Categorizes transactions into Low Risk (0β30: Autonomous Clearance), Medium Risk (31β70: HITL Triaging Suspended), and Critical Risk (71β100: Instant Adversarial Drop).
- π€ Human-in-the-Loop (HITL) Triaging State Machine: Suspends ambiguous payments and allows authorized administrators to approve (
POST /api/v1/pay/override) or reject transactions before PayPal execution. - π Immutable Compliance Ledger: Preserves complete audit trails with 1-click RFC 4180 CSV and JSON export endpoints.
- π‘ Real-Time PayPal Webhooks: Asynchronously tracks
PAYMENT.CAPTURE.COMPLETED,PAYMENT.CAPTURE.DECLINED, andPAYMENT.CAPTURE.DENIEDcallbacks with cryptographic transmission header authentication and replay protection. - π Enterprise Idempotency Protection: Caches evaluated responses for 120 seconds via
x-idempotency-key, short-circuiting replays withx-cache: HITand zero duplicate PayPal orders. - π‘οΈ Context Truncation & Token Exhaustion Defense: Binds incoming
raw_contextto max 4,000 characters, returning HTTP 413BLOCKED_T1at $0.00 compute before touching LLMs. - πΎ State Persistence Fallback: Automatically serializes budget spend and pending HITL queues to
.cache/runtime-state.jsononSIGINT/SIGTERMand rehydrates on startup.
π Benefits of this Middleware Architecture
Positioned as an intelligent reverse proxy directly between autonomous agents and the PayPal Orders API v2, PromptPay Guard offers distinct advantages:
- Deterministic Speed (<0.5ms): Over 38% of malicious payloads, rate-limit violations, and typo-squatted merchants are dropped in Tier-1 in under 0.5ms before ever hitting an LLM or network gateway.
- Massive Cost Optimization (98.8% Savings): Prevents wasteful token burn by executing semantic AI analysis only on pre-sanitized payloads.
- Human-in-the-Loop (HITL) Governance: Empowers compliance teams with a 3-zone risk modelβsuspending medium-risk actions (31β70 risk score) for 1-click manual admin approval or instant cancellation.
- Regulatory Auditability: Automatically maintains an immutable, tamper-evident compliance ledger with 1-click exports to standard RFC 4180 CSV and JSON formats.
- Fail-Secure Architecture: If external LLM or network providers experience outages or timeouts, the gateway defaults to blocking execution rather than passing unchecked payments.
π§ Why Google Gemini 1.5?
Rather than using generic, expensive frontier models, PromptPay Guard was specifically architected around the Gemini 1.5 model family (Flash & Pro):
- Sub-Cent Unit Economics: Gemini 1.5 Flash costs only $0.075 / 1M input and $0.30 / 1M output tokens, allowing high-frequency payment gateways to perform deep semantic checks for fractions of a cent. This is the cornerstone of our 98.8% OpEx reduction.
- Dynamic Model Tiering: Routine micro-transactions are evaluated by Gemini 1.5 Flash for sub-200ms latency, while high-value or ambiguous transactions dynamically route to Gemini 1.5 Pro for multi-step reasoning.
- Native JSON Schema Enforcement: Geminiβs structured output guarantees deterministic JSON responses (
risk_score,threat_type,rationale) without hallucinated markdown formatting, preventing downstream pipeline parsing crashes. - Context Scratchpad Ingestion: Effortlessly digests lengthy agent reasoning scratchpads, invoices, and system prompt contexts in a single pass to spot covert goal deviation.
ποΈ System Architecture & Request Lifecycle
flowchart TD
classDef agent fill:#1e293b,stroke:#06b6d4,stroke-width:2px,color:#fff;
classDef t1 fill:#0f172a,stroke:#f59e0b,stroke-width:2px,color:#fff;
classDef t2 fill:#0f172a,stroke:#8b5cf6,stroke-width:2px,color:#fff;
classDef hitl fill:#451a03,stroke:#f59e0b,stroke-width:2px,color:#fff;
classDef pp fill:#082f49,stroke:#0284c7,stroke-width:2px,color:#fff;
classDef drop fill:#4c0519,stroke:#e11d48,stroke-width:2px,color:#fff;
classDef audit fill:#064e3b,stroke:#10b981,stroke-width:2px,color:#fff;
Agent["π€ Autonomous AI Agent<br/>(AutoGPT / LangChain / CrewAI)"]:::agent
Agent -->|"POST /api/v1/pay"| Ingress["π‘οΈ PromptPay Guard Ingress<br/>Helmet CSP β’ 512KB Bound β’ Express"]
subgraph Tier1["β‘ Tier-1: Heuristics & Boundary Engine (<1ms | $0.00 Cost)"]
T1_Schema["1.1 Strict Type & Finite Sanity<br/>(Rejects NaN, Exp 1e5, Sub-cents)"]
T1_Budget["1.2 Budget Guard<br/>(Single Cap $100 β’ Daily Cap $500)"]
T1_Velocity["1.3 Sliding-Window Rate Limiter<br/>(5 tx / 60s Atomic Salami Defense)"]
T1_Vendor["1.4 Anti-Spoof Vendor Whitelist<br/>(Exact Domain & Homoglyph Checker)"]
end
Ingress --> T1_Schema --> T1_Budget --> T1_Velocity --> T1_Vendor
T1_Vendor -->|"Policy Violation Detected"| DropT1["π HTTP 403 / 429 Drop<br/>Zero Token Burn β’ $0.00 Marginal Cost"]:::drop
subgraph Tier2["π§ Tier-2: Semantic Intent Engine (Gemini 1.5 Flash / Pro)"]
T2_Prompt["2.1 Calibrated Intent Evaluation<br/>(Declared Intent vs. Agent Context)"]
T2_Zone["2.2 Threat Score Resolution (0-100)"]
end
T1_Vendor -->|"Tier-1 Cleared"| T2_Prompt --> T2_Zone
subgraph Zones["π― 3-Zone Operational Decision Engine"]
Zone_Low["π’ Zone 1: LOW RISK (0-30)<br/>Autonomous Clearance"]
Zone_Med["π‘ Zone 2: MEDIUM RISK (31-70)<br/>Suspends Execution for HITL"]:::hitl
Zone_Crit["π΄ Zone 3: CRITICAL RISK (71-100)<br/>Adversarial Threat Intercepted"]:::drop
end
T2_Zone --> Zone_Low
T2_Zone --> Zone_Med
T2_Zone --> Zone_Crit
Zone_Crit -->|"Prompt Injection Detected"| DropT2["π HTTP 403 Forbidden<br/>Zero PayPal API Overhead"]:::drop
Zone_Med -->|"Awaiting Admin 2FA"| HITL_Store["π¦ Suspended Pending Store<br/>(pendingHitlStore Map)"]:::hitl
HITL_Store -->|"POST /api/v1/pay/override"| HITL_Action{"Admin Action"}:::hitl
HITL_Action -->|"APPROVE"| PayPal_API
HITL_Action -->|"REJECT"| DropHITL["π Dropped by Admin"]:::drop
subgraph Settlement["π³ PayPal Settlement Network"]
PayPal_API["PayPal Orders API v2<br/>OAuth 2.0 β’ /v2/checkout/orders"]:::pp
PayPal_Capture["Capture Order Settlement<br/>/v2/checkout/orders/{id}/capture"]:::pp
PayPal_Hook["π‘ PayPal Webhook Listener<br/>ID: 7F072555EA7227937<br/>COMPLETED β’ DECLINED β’ DENIED"]:::pp
end
Zone_Low -->|"Direct Permit"| PayPal_API
PayPal_API --> PayPal_Capture
PayPal_Capture -->|"Webhook Callback"| PayPal_Hook
subgraph Compliance["π Compliance & Observability Layer"]
Ledger["Immutable Compliance Ledger Store<br/>RFC 4180 CSV & JSON Export"]:::audit
Telemetry["SSE Real-Time Telemetry Stream<br/>/api/v1/telemetry/stream"]:::audit
Metrics["Unit Cost & Financial Telemetry<br/>/api/v1/metrics"]:::audit
end
DropT1 --> Ledger
DropT2 --> Ledger
PayPal_Capture --> Ledger
PayPal_Hook --> Ledger
Ledger --> Telemetry
Ledger --> Metrics
π― 3-Zone Operational Risk Engine & Radial Gauge
PromptPay Guard replaces legacy binary pass/fail filters with an enterprise 3-Zone Risk Operational Engine:
[ 0 ----------------- 30 ] [ 31 ----------------- 70 ] [ 71 ---------------- 100 ]
π’ LOW RISK π‘ MEDIUM RISK π΄ CRITICAL RISK
Autonomous Clearance HITL Triaging Required Instant Adversarial Drop
Direct PayPal Capture Execution Suspended Zero PayPal API Overhead
| Operational Zone | Threat Score | Security Verdict | UI Radial Gauge | PayPal Settlement Behavior |
|---|---|---|---|---|
| Zone 1: LOW RISK | 0 β 30 |
Autonomous Clearance | π’ Emerald (0-30 LOW) |
Direct PayPal Capture: Settle immediately via Orders API v2. |
| Zone 2: MEDIUM RISK | 31 β 70 |
HITL Triaging Required | π‘ Amber (31-70 MEDIUM) |
Execution Suspended: Stored in pending state. PayPal is not called until confirmed via POST /api/v1/pay/override. |
| Zone 3: CRITICAL RISK | 71 β 100 |
Adversarial Drop | π΄ Crimson (71-100 CRITICAL) |
Instant HTTP 403: Drops payload at firewall edge. Zero token burn on PayPal. |
π€ Human-in-the-Loop (HITL) Triaging Pipeline
When a transaction falls in the Medium Risk Zone (31β70) or contains dual-authorization trigger phrases (e.g., "human-in-the-loop", "pending review"):
- State Suspension: PromptPay Guard generates a unique
hitl_id, logs the transaction to the immutable audit ledger, and stores its verified payload inpendingHitlStore. - Interactive UI Action Dialog: The Dashboard displays an amber triaging modal with threat analysis details, user intent context, and dual-action controls:
[Approve & Execute PayPal Capture]: CallsPOST /api/v1/pay/overridewithaction: "APPROVE". Invokes PayPal Orders API v2, debits budget, updates ledger toAPPROVED, and emits real-time SSE updates.[Reject & Drop]: CallsPOST /api/v1/pay/overridewithaction: "REJECT". Cancels the transaction, updates ledger toBLOCKED, and closes the alert.
- Idempotence & Anti-Replay: Once an override action is processed, subsequent attempts with the same
hitl_idreturn404 NOT_FOUNDto prevent replay attacks.
π‘ Real-Time PayPal Webhook Listener
PromptPay Guard incorporates a dedicated webhook endpoint to track downstream settlement callbacks:
- Webhook ID:
7F072555EA7227937 - Webhook URL:
https://washing-division-shakiness.ngrok-free.dev/api/v1/paypal-webhook(also aliased at/api/v1/webhooks/paypal) - Tracked Lifecycle Events:
PAYMENT.CAPTURE.COMPLETED: Marks the transaction in the compliance ledger asCAPTURED_SETTLED, updates aggregate volume, and broadcasts real-time feed cards.PAYMENT.CAPTURE.DECLINED: Flags settlement as declined by processor, updating ledger and dashboard alerts.PAYMENT.CAPTURE.DENIED: Marks settlement as denied with full metadata.
- Webhook SLA Guarantee: The handler processes incoming callbacks and returns an immediate HTTP
200 OKto ensure adherence to PayPal's webhook SLA and prevent retry storms.
π Immutable Compliance Ledger & Audit Export
To satisfy FinTech regulatory and compliance requirements (SOX, SOC 2, PCI-DSS):
- Tamper-Evident Storage:
src/telemetry/auditLedger.jsmaintains an append-only in-memory ledger storing:timestamp(ISO 8601)agent_iddeclared_intentamount(USD)recipient& whitelist domaintier1_decision(PASS/BLOCKED)tier2_threat_score(0β100)final_status(APPROVED,BLOCKED,PENDING_HITL,CAPTURED_SETTLED)reason(threat type or approval justification)paypal_capture_id&paypal_order_idinspection_cost_usd&model_used
- Export APIs:
GET /api/v1/audit/export?format=csv: Streams RFC 4180-compliant CSV download (promptpay-guard-compliance-ledger.csv).GET /api/v1/audit/export?format=json: Streams structured JSON ledger (promptpay-guard-compliance-ledger.json).
- Header Buttons: One-click
[Download CSV]and[Download JSON]buttons in the dashboard header trigger direct browser downloads without page refresh.
π‘οΈ Overcoming Architectural Blind Spots & Edge Cases
High-concurrency autonomous payments introduce subtle operational blind spots. PromptPay Guard explicitly resolves these four enterprise failure modes:
| Identified Blind Spot | Threat Scenario | Implemented Remediation | Verified Test |
|---|---|---|---|
| Duplicate Settlement (Network Retries) | Autonomous agent retries after an ambiguous network delay, causing accidental double payments on PayPal. | Idempotency Engine (x-idempotency-key): In-memory 120-second cache replays prior settlements instantly, preventing duplicate PayPal capture invocations. |
TC-EDGE-01 |
| Denial-of-Wallet (Token Exhaustion) | Malicious agent feeds a 100k+ character context window, running up excessive token billing or triggering API timeouts. | Strict Payload Ceiling: Tier-1 instantaneously enforces a strict 4,000-character bound on raw_context, dropping oversized payloads with HTTP 413 at $0.00 compute cost. |
TC-EDGE-02 |
| Webhook Replay & Spoofing | Adversary sends mock PAYMENT.CAPTURE.COMPLETED events to mark unpaid orders as settled. |
Cryptographic Signature Validation: Rejects requests missing PayPal transmission headers (HTTP 401), checks for timestamp skew (>10m), and drops duplicate transmission IDs (HTTP 409). |
TC-EDGE-03TC-EDGE-04 |
| Process Recycle State Loss | Node.js process crashes or restarts, wiping out in-memory sliding-window budgets and pending HITL queues. | Graceful Lifecycle Persistence: Traps SIGINT/SIGTERM to synchronously persist state to .cache/runtime-state.json, auto-rehydrating all stores on startup. |
TC-EDGE-05 |
π° Cost Profiling & Financial Unit Economics
PromptPay Guard was engineered to eliminate the unsustainable OpEx of evaluating 100% of raw agent traffic through frontier LLMs:
Financial Architecture Comparison
| Metric / Scale | Model A: Monolithic Frontier (GPT-4 / Claude 3.5 Sonnet) | Model B: PromptPay Guard Two-Tier Zero-Trust | Absolute Savings ($) | Efficiency Gain (%) |
|---|---|---|---|---|
| Tier-1 Heuristic Cost | N/A ($0.00 filtering) | $0.00 (~38% of traffic dropped free) | β | β |
| Tier-2 Semantic Pricing | $3.00/1M in β’ $15.00/1M out | $0.075/1M in β’ $0.30/1M out (Gemini Flash) | β | β |
| Cost Per Transaction (CPT) | $0.002350 | $0.000026 | +$0.002324 / tx | 98.89% |
| 100,000 Transactions / mo | $235.00 | $2.62 | $232.38 | 98.89% |
| 1,000,000 Transactions / mo | $2,350.00 | $26.18 | $2,323.82 | 98.89% |
| 10,000,000 Transactions / mo | $23,500.00 | $261.83 | $23,238.17 | 98.89% |
Generate executive-ready Markdown cost reports anytime by running npm run cost:report (outputs to docs/COST_ANALYSIS_REPORT.md).
π₯οΈ Interactive Telemetry Dashboard & Simulator
Access the real-time dark-mode command center at http://localhost:3000:
- Live Telemetry Stream: Connects via Server-Sent Events (SSE) to display real-time transaction cards, latency breakdowns, and defense badges.
- Dynamic Model Switcher: Dropdown supporting on-the-fly toggling between
gemini-1.5-flash($0.075 / 1M in) andgemini-1.5-pro($1.25 / 1M in). - Interactive Radial Risk Gauge: Real-time circular meter animating between Green (0β30), Amber (31β70), and Crimson (71β100).
- 5 One-Click Attack Simulation Presets:
[Safe Transaction]: Valid Figma license renewal ($144.00) β Passes & Settles to PayPal.[Prompt Injection Attack]: Instruction override payload β Blocked at Tier-2 (403 Forbidden).[Salami Burst]: 12 rapid micro-payments ($0.99) β Throttled at Tier-1 (429 Rate Limit).[Typo-squatted Vendor]: Homoglyph domain spoof (amaz0n-billing.net) β Blocked at Tier-1 (403 Whitelist).[HITL Triaging State]: Lands in Medium Risk zone (Score: 52) β Suspends & reveals inline Approve/Reject action dialog.
- Manual Custom Payload Tester: Form allowing arbitrary agent IDs, declared intents, amounts, recipient emails, and scratchpad context windows.
- Animated Defense Toasters: Real-time notification banners detailing intercepted threat categories.
πΈ Live Dashboard & Defense Screenshots
| π’ Autonomous Clearance & Settlement | π‘ Human-in-the-Loop (HITL) Suspension |
|---|---|
![]() Legitimate agent payment ($144 Figma) cleared & captured via PayPal API v2 |
![]() Medium risk payment (Score 52) suspended for 1-click admin approval |
| π΄ Salami Micro-Drain Throttled (Tier-1) | π§ͺ Automated Test Verification Suite |
|---|---|
![]() 12 rapid micro-payments instantly throttled in 0.05ms at $0.00 cost |
![]() 100% pass rate across PRD functional tests (9/9) & Enterprise Edge Cases (8/8) |
β‘ Quickstart Guide
1. Prerequisites
- Node.js:
v20.0.0or higher (Tested onv24.15.0) - npm:
v10.0.0or higher - PayPal Sandbox Developer Account: developer.paypal.com
- Google AI Studio Key: aistudio.google.com (Optional: local high-accuracy heuristic fallback kernel included)
2. Installation
# Clone the repository
git clone https://github.com/kanangandhi93/promptpay-guard.git
cd promptpay-guard
# Install production dependencies
npm install
# Configure environment variables
cp .env.example .env
3. Environment Configuration (.env)
PORT=3000
HOST=localhost
# Gemini API Configuration
GEMINI_API_KEY=your_gemini_api_key_here
GEMINI_MODEL=gemini-1.5-flash
# PayPal Sandbox REST Credentials
PAYPAL_CLIENT_ID=your_paypal_sandbox_client_id
PAYPAL_CLIENT_SECRET=your_paypal_sandbox_client_secret
PAYPAL_BASE_URL=https://api-m.sandbox.paypal.com
PAYPAL_WEBHOOK_ID=7F072555EA7227937
PAYPAL_WEBHOOK_URL=https://washing-division-shakiness.ngrok-free.dev/api/v1/paypal-webhook
# Tier-1 Heuristics Constraints
TIER1_MAX_VELOCITY_PER_WINDOW=5
TIER1_VELOCITY_WINDOW_SECONDS=60
TIER1_SINGLE_TRANSACTION_LIMIT=100.00
TIER1_DAILY_CUMULATIVE_LIMIT=500.00
# Fallback & Mock Sandbox
ENABLE_MOCK_FALLBACK=true
4. Running the Application
# Start server in production mode
npm start
# Start server in development mode (with hot reloading)
npm run dev
Open http://localhost:3000 in your browser to explore the live dashboard.
π‘ Complete API Reference
1. Execute Agent Payment (Core Proxy)
Evaluates incoming transactions through Tier-1 and Tier-2 before delegating to PayPal Orders API v2.
- URL:
/api/v1/pay(or alias/api/v1/orders/execute) - Method:
POST - Headers:
Content-Type: application/json
Request Payload:
{
"agent_id": "agent_procure_figma_01",
"user_intent": "Renew annual Figma license for design team at $144.00",
"recipient_email": "[email protected]",
"amount": 144.00,
"currency": "USD",
"max_budget_cap": 500.00,
"model": "gemini-1.5-flash",
"context_window": "Quarterly review approved. Verified vendor license renewal for Figma design suite."
}
Success Response (200 OK - Autonomous Clearance):
{
"success": true,
"status": "APPROVED",
"order_id": "ORD-SANDBOX-8CA38961",
"capture_id": "CAP-SANDBOX-8CA38961",
"approval_url": "https://www.sandbox.paypal.com/checkoutnow?token=8CA38961",
"amount": 144.00,
"currency": "USD",
"recipient_email": "[email protected]",
"vendor_name": "Figma Inc.",
"security_verification": {
"decision": "PERMIT_AND_SETTLE",
"threat_score": 2,
"risk_zone": "LOW_RISK",
"risk_label": "Autonomous Clearance",
"eval_engine": "Gemini 1.5 Flash",
"inference_cost_usd": 0.0000381,
"latency_breakdown": {
"tier1_heuristics_ms": 0.062,
"tier2_semantic_ms": 178.4,
"paypal_gateway_ms": 210.2,
"total_e2e_ms": 388.7
}
}
}
Suspended Response (200 OK - Medium Risk HITL Required):
{
"success": false,
"status": "PENDING_HITL",
"decision": "SUSPEND_FOR_HITL",
"hitl_id": "EVT-516BABB0",
"threat_score": 52,
"risk_zone": "MEDIUM_RISK",
"risk_label": "Requires Human Confirmation (HITL)",
"reason": "Moderate intent ambiguity or audit keyword detected. Suspended pending administrator override.",
"amount": 85.00,
"currency": "USD",
"recipient_email": "[email protected]"
}
Rejection Response (403 Forbidden - Prompt Injection Attack):
{
"success": false,
"status": "BLOCKED",
"decision": "DROP_PAYLOAD",
"blocked_stage": "TIER2_INJECTION",
"threat_score": 98,
"threat_type": "PROMPT_INJECTION",
"reason": "Direct / Indirect Prompt Injection detected: Adversarial instruction override sequence identified in agent context window.",
"security_context": {
"event_id": "EVT-951A8C42",
"latency_breakdown": {
"tier1_heuristics_ms": 0.058,
"tier2_semantic_ms": 1.25,
"paypal_gateway_ms": 0,
"total_ms": 1.308
}
}
}
2. Human-in-the-Loop Admin Override
Confirms or rejects a suspended Medium Risk transaction.
- URL:
/api/v1/pay/override - Method:
POST - Headers:
Content-Type: application/json
{
"hitl_id": "EVT-516BABB0",
"action": "APPROVE", // or "REJECT"
"admin_user": "admin_security_lead",
"notes": "Verified vendor quote against purchase requisition"
}
3. PayPal Webhook Listener
Receives settlement callbacks directly from PayPal, verifies event lifecycle status, and emits SSE notifications.
- URL:
/api/v1/paypal-webhook(or alias/api/v1/webhooks/paypal) - Method:
POST - Headers:
Content-Type: application/json - Webhook ID:
7F072555EA7227937 - Tracked Events:
PAYMENT.CAPTURE.COMPLETED,PAYMENT.CAPTURE.DECLINED,PAYMENT.CAPTURE.DENIED
4. Exportable Compliance Ledger
- CSV Export:
GET /api/v1/audit/export?format=csvβ Returns RFC 4180-compliant CSV download. - JSON Export:
GET /api/v1/audit/export?format=jsonβ Returns structured compliance JSON.
5. Financial Intelligence & Unit Economics
Returns real-time aggregate unit costs, token usage, Tier-1 zero-cost block rates, and dollars saved vs frontier baseline.
- URL:
/api/v1/metrics - Method:
GET
{
"totalRequestsCount": 42,
"tier1ZeroCostDrops": 16,
"tier2EvaluationsCount": 26,
"totalInferenceCostUsd": 0.001092,
"avg_cost_per_request": 0.000026,
"cost_saved_vs_frontier": 0.0976,
"zero_cost_block_rate_pct": 38.1,
"tokens": {
"inputTokens": 8320,
"outputTokens": 2210,
"totalTokens": 10530
}
}
π§ͺ Automated Testing & Verification Suites
PromptPay Guard maintains an exhaustive automated test suite with 60 passing tests across 7 specialized harnesses:
# Run all PRD functional test cases (TC-POS-01..03, TC-NEG-01..06)
npm run test:all
# Run Enterprise Edge Cases & Architectural Remediation Suite (8 tests)
npm run test:edge
# Run Defensive Security & Penetration Audit (18 tests)
npm run test:security
# Run Fail-Secure Resilience & Network Timeout Suite (5 tests)
npm run test:resilience
# Run Dynamic Model Switching & Telemetry Metrics Suite (7 tests)
npm run test:models
# Run Audit Ledger, PayPal Webhook & HITL Triaging Suite (13 tests)
npm run test:audit
# Run High-Concurrency Load & Stress Testing Benchmark
npm run test:load
Test Suite Execution Summary
| Test Suite Harness | File Location | Test Count | Key Invariants Verified | Status |
|---|---|---|---|---|
| Edge Cases & Remediation Suite | tests/edge-cases-remediation.test.js |
8 | Idempotency key 120s caching (x-cache: HIT), raw_context 4000 char bounding (HTTP 413, $0.00 cost), Webhook transmission header auth (401), Webhook duplicate replay rejection (409), State persistence & rehydration |
β 8/8 PASS |
| Audit, Webhook & HITL Suite | tests/audit-webhook-hitl.test.js |
13 | RFC 4180 CSV headers, JSON ledger, PayPal Webhook ID 7F072555EA7227937, 3-zone thresholds, HITL Approve/Reject state machine | β 13/13 PASS |
| Defensive Security Suite | tests/security.test.js |
18 | Type confusion (Exp 1e5, NaN), fractional sub-cents, ReDoS regex, homoglyph domains, Helmet CSP headers, 512KB payload limits | β 18/18 PASS |
| Resilience & Fail-Secure Suite | tests/resilience-fail-secure.test.js |
5 | LLM Gateway 500/timeout (Fail-Secure drop 503/504), malformed AI output recovery, PayPal 504 rollback idempotency | β 5/5 PASS |
| Dynamic Models & Metrics Suite | tests/dynamic-model-metrics.test.js |
7 | Gemini 1.5 Flash vs Pro provider adapter, exact token usage pricing, /api/v1/metrics frontier savings telemetry |
β 7/7 PASS |
| PRD Core Functional Suite | scripts/run-all-tests.js |
9 | PRD TC-POS-01..03, TC-NEG-01..06 core business validation matrix | β 9/9 PASS |
| Total Automated Tests | β | 60 | End-to-End Complete Production Coverage | β 100% PASS |
ποΈ Project Directory Structure
paypal/
βββ docs/
β βββ COST_ANALYSIS_REPORT.md # Automated Unit Economics & Cost Profiling Report
βββ scripts/
β βββ generate-cost-report.js # OpEx analysis generator script
β βββ run-all-tests.js # PRD core test suite runner
β βββ simulate-prompt-injection.js # Adversarial prompt injection simulation (TC-NEG-01)
β βββ simulate-safe-transaction.js # Legitimate payment simulation (TC-POS-01)
β βββ simulate-salami-attack.js # Salami micro-drain burst simulation (TC-NEG-03)
βββ services/
β βββ semanticEngine.js # Root Provider Adapter for dynamic models & pricing
βββ src/
β βββ config.js # Enterprise thresholds, approved vendors & credentials
β βββ index.js # Express server, pipeline orchestration & SSE router
β βββ paypal/
β β βββ client.js # PayPal Orders API v2 (OAuth 2.0, order create & capture)
β βββ public/
β β βββ app.js # Client SSE listener, radial gauge updater & HITL dialogs
β β βββ index.html # Real-time dark-mode telemetry dashboard & simulator UI
β βββ services/
β β βββ semanticEngine.js # Core semantic engine adapter (Flash & Pro token profiling)
β βββ telemetry/
β β βββ auditLedger.js # Immutable compliance ledger (RFC 4180 CSV / JSON)
β β βββ eventBus.js # SSE event broadcaster & financial metrics accumulator
β βββ tier1/
β β βββ budgetGuard.js # Single cap & cumulative daily budget enforcement
β β βββ schemaValidator.js # Strict input sanitization & boundary validation
β β βββ tier1Pipeline.js # Tier-1 orchestration (<1ms execution target)
β β βββ velocityLimiter.js # Atomic sliding-window rate limiter (Salami defense)
β β βββ vendorWhitelist.js # Exact regex & anti-homoglyph vendor verification
β βββ tier2/
β β βββ intentClassifier.js # Gemini 1.5 semantic intent classification kernel
β β βββ promptTemplates.js # Calibrated system prompts for prompt injection defense
β βββ utils/
β βββ idempotencyStore.js # In-memory idempotency engine with 120s TTL
β βββ statePersistence.js # State serializer & rehydrator (.cache/runtime-state.json)
βββ tests/
β βββ audit-webhook-hitl.test.js # Webhooks, audit ledger & HITL triaging suite (13 tests)
β βββ dynamic-model-metrics.test.js # Dynamic models & metrics calculation suite (7 tests)
β βββ edge-cases-remediation.test.js# Idempotency, 4000 char bounding, webhook security (8 tests)
β βββ load-test.js # Concurrency, throughput & high-burst stress benchmark
β βββ resilience-fail-secure.test.js# Gateway outages, malformed JSON & PayPal rollback (5 tests)
β βββ security.test.js # Comprehensive penetration audit suite (18 tests)
βββ .env.example # Environment variables template with Webhook ID & URL
βββ CONTRIBUTING.md # Open-source developer contribution guidelines
βββ LICENSE # MIT License
βββ package.json # Project dependencies, scripts & metadata
βββ README.md # Publication-grade documentation
βββ SECURITY.md # Enterprise FinTech vulnerability disclosure policy
π PayPal AI Hackathon Highlights
- Addresses the Missing Link in Agentic Commerce: API keys authenticate the caller; PromptPay Guard authenticates the semantic intent behind the financial transaction.
- Sub-Millisecond Edge Defense: Tier-1 heuristics resolve in <0.1ms at $0.00 marginal cost, filtering out 38% of malicious traffic before invoking LLMs.
- Sub-Second E2E Settlement: Full pipeline evaluation (Tier-1 + Tier-2 Gemini + PayPal Orders API v2) settles in ~380ms.
- 98.8% Operational Cost Reduction: Replacing brute-force frontier LLM architectures with PromptPay Guard saves $23,238 per 10M transactions.
- Turnkey Judge Evaluation: Start the application via
npm startand test all 5 attack presets in real-time via the interactive dashboard at http://localhost:3000.
βοΈ License & Attribution
This project is licensed under the terms of the MIT License (or Apache License 2.0).
See the LICENSE file for complete details.
π€ Author & Architecture
- Original Creator & Lead Architect: Kanan Gandhi
- Submission Platform: PayPal AI Agent Hackathon 2026
- Project Showcase: Devpost Project | YouTube Demonstration
Notice: "PromptPay Guard", its Two-Tier Zero-Trust heuristics pipeline, radial risk assessment workflows, and architectural designs are original intellectual work developed by Kanan Gandhi. Any public re-distribution, derivative research, or implementation must retain proper attribution to the original author.
Built With
- ai
- aimodel
- gemini
- html
- javascript
- paypal


