Inspiration
Autonomous AI agents are increasingly tasked with executing real-world commercial actions—ordering supplies, booking cloud resources, and purchasing SaaS licenses. However, probabilistic model outputs should never have direct, unconstrained access to payment rails. Prompt injection, hallucinated parameters, or runaway agent loops can drain accounts in seconds.
PayPal ScopeGuard solves this by enforcing a fail-closed perimeter: AI can propose; deterministic policy and the human buyer decide, before executing on PayPal's trusted rails.
What it does
PayPal ScopeGuard is an agentic transaction policy guard and sandbox execution proxy built on the PayPal Developer Platform (Orders v2 API).
- AI Intent Extraction & Guardrails: Intercepts unconstrained LLM checkout decisions and extracts strictly typed purchase intents using JSON Schema structured outputs.
- Deterministic Spend Policies: Evaluates transaction amounts against strict integer minor-unit per-transaction caps and rolling-window spend limits before any network call.
- Merchant Category Allowlisting: Blocks unauthorized merchant categories and flags ambiguous transactions.
- Mandatory Human-in-the-Loop Approval: Pauses checkout until the human buyer explicitly approves the transaction via the web dashboard.
- PayPal Sandbox Orders v2 Execution: Dispatches idempotent orders to PayPal's Sandbox (
api-m.sandbox.paypal.com) usingPayPal-Request-Idheaders. - Distributed Failure Recovery & Webhook Verification: Recovers from dropped capture responses through deterministic reconciliation without double-charging, and verifies incoming PayPal webhook notifications using raw-body RSA signature verification.
How we built it
- PayPal Developer Platform (Sandbox Orders v2 REST API): Built a typed
PayPalApiClientin Node.js ESM targetinghttps://api-m.sandbox.paypal.com. Handles OAuth2 client credentials authentication,POST /v2/checkout/orderswith idempotency keys, order state queries (GET /v2/checkout/orders/{id}), and final order capture (POST /v2/checkout/orders/{id}/capture). - AI Tool & Model Integration: Integrated structured intent adapters compatible with OpenAI and Google Gemini APIs using strict JSON schemas to safely convert freeform agent actions into verified checkout requests while discarding injected overrides.
- Webhook RSA Verifier: Validates asymmetric signatures (
SHA256withRSA) on raw PayPal webhook payloads (CHECKOUT.ORDER.APPROVED) to guarantee authenticity and deduplicate replayed events. - Deterministic State Engine: Atomic state store and order queue ensuring that budget reservations survive process restarts and network partitions. GitHub Actions tests the suite across Node 22 and 24 with 25/25 passing unit tests.
Challenges we ran into
- Distributed Payment Invariants: Ensuring that network drops during a
capturecall never result in double-spending or released budget reservations. We solved this through PayPal idempotency keys and state reconciliation. - Tamper-Proof Webhook Validation: Parsing webhook JSON can subtly alter character representations, invalidating RSA signatures. We implemented byte-exact raw-body hashing against PayPal public certificates.
- Constraining LLM Probabilism: Designing fail-closed JSON schema validators so that malformed or malicious LLM outputs are safely rejected prior to policy evaluation.
Accomplishments that we're proud of
- 100% Test Coverage on Core Invariants: 25/25 unit tests passing in under 400ms, covering sandbox payload formation, spend caps, concurrency limits, webhook verification, and crash recovery.
- Idempotent Fault-Tolerant Architecture: End-to-end recovery mechanism that reconciles ambiguous capture outcomes against PayPal's authoritative state without duplicating transactions.
- Fail-Closed Security: Absolute guarantee that no agent prompt can bypass spend limits or trigger an automatic capture without explicit buyer authorization.
What's next
- Expanding the proxy to support PayPal Subscriptions v1 and Vault APIs for recurring agentic workflows.
- Multi-signature buyer approvals for high-value enterprise agent operations.
- Packaged npm middleware for instant drop-in integration with popular agent frameworks (LangChain, LlamaIndex, Eliza).
Built With
- ai-agents
- gemini-api
- idempotency
- javascript
- node.js
- openai
- paypal
- paypal-sandbox
- rest-api
