## Inspiration

AI assistants are starting to buy things for people. A payment made by an agent can be perfectly valid for PayPal and still be wrong for the buyer: a different model, a price that goes over budget once tax and shipping are added, a seller the buyer never approved, or a product whose description quietly tells the agent to ignore the rules. When that happens, the merchant has no record of what the buyer actually authorised.

## What it does
Countersign sits between the AI assistant and the payment.

1. The buyer describes a purchase in plain words. Gemini turns it into a draft **mandate**: product type, condition, quantity, specifications, budget, seller rule and expiry.
2. The buyer reviews the exact terms and confirms. The confirmed version is hashed and can never be edited; any change creates a new version that must be confirmed again.
3. The assistant, or the buyer, proposes a product. The server prices it and a **deterministic rule engine** checks it rule by rule and returns ALLOW, BLOCK, REAUTHORISE or REVIEW.
4. Only on ALLOW does the server create a **PayPal order**, from its own stored quote.
5. After the buyer approves in PayPal, the server **fetches the order from PayPal again** and re-checks the order ID, approval status, payee, currency, amount, line item, mandate status and the whole policy.
6. The server captures only if every check passes. Otherwise nothing is captured and the buyer sees which rule failed and why.
7. A signature-verified **PayPal webhook** is reconciled against the capture, and a **receipt** links the instruction, the mandate, every rule check, the PayPal order and capture, and a hash-chained audit trail.

When the rules refuse the assistant's pick, the refusal is sent back and the assistant tries again, up to three times. A labelled simulation shows a manipulated agent choosing the product whose description says to ignore the budget; the rules refuse it and the assistant recovers. An Activity page lists every proposal and what the rules decided, and any receipt can be shared as a read-only link.

The AI drafts, proposes and explains. It never confirms a mandate, sets an amount or payee, overrides a rule, or calls PayPal.

## How we built it

- **Backend:** Python, FastAPI, Pydantic, SQLAlchemy and Alembic on PostgreSQL. One self-contained package with a pure rule engine (no I/O), a payment-provider interface with a PayPal adapter, and a persisted checkout state machine.
- **PayPal (Sandbox):** OAuth 2.0 client credentials, Orders v2 (create, show, capture) with `PayPal-Request-Id` idempotency, the JavaScript SDK for buyer approval, and webhooks verified through PayPal's verify-webhook-signature API.
- **AI:** Google Gemini over its REST API with a response schema. Output is validated in code before use. Catalogue text is passed as quoted, untrusted data.
- **Frontend:** React, four screens: instructions, product choice, checkout, receipt.
- **Deployment:** one Docker container on Railway with managed PostgreSQL.

## Challenges we ran into

- **Knowing when not to capture.** A lost capture response must not lead to a second capture. The checkout records that a capture was requested before calling PayPal, and on an unknown outcome it asks PayPal what happened before doing anything else.
- **Webhooks arrive late and out of order.** We keep payment state and webhook reconciliation as two separate things, so the receipt does not wait for the webhook and an early event cannot mark an order as paid.
- **Keeping the AI out of the decision.** The model that reads product descriptions can be manipulated, so the payment decision never depends on it. A test has the model choose the product whose description tells it to ignore the budget; the rule engine still refuses it.

## Accomplishments that we're proud of

- 85 automated tests covering every rule, order tampering, lost capture responses, duplicate requests, webhook verification and ordering, and the limits on the AI.
- Verified against the real PayPal Sandbox: order creation from the stored quote, an idempotent retry returning the same order, order retrieval, PayPal refusing a capture before buyer approval, and a changed order being detected. On the deployed site we also completed a buyer-approved capture and received a signature-verified webhook.
- Every refusal names the rule, what the mandate says, what was found, and where that fact came from.

## What we learned

A valid payment and an authorised purchase are different things. The useful control is not a smarter model; it is a fixed check at the moment of capture, plus a record the buyer and the merchant can both read.

## What's next

API keys and per-merchant settings so other companies can put IntentLock in front of their own checkout, more payment providers behind the same interface, receipts signed with a public key, and import and export of AP2-style mandates.

Built With

Share this project:

Updates

Submission history