Inspiration
Small online sellers lose money on refunds and disputes. The cause is rarely bad intent. Usually a deadline slipped by or the evidence was incomplete. A PayPal dispute gives the seller a fixed window to respond. If you miss it or submit a weak evidence packet, the funds go to the buyer. Refund requests arrive as free-text messages that someone has to check against the store's return policy, one at a time.
The obvious answer is an AI agent that handles all of it. But a fully autonomous agent with access to your PayPal account is frightening. It can hallucinate a tracking status, refund the wrong order, or concede a dispute you would have won. We wanted to find the middle ground: an agent that does the routine work on its own and asks before anything material.
What it does
Spike is a daily, deadline-driven operations desk for PayPal refunds and disputes. The seller opens one screen, the Resolution Center, which lists every open case ordered by response deadline and amount at risk.
For each case:
- Intake: disputes come in from the PayPal Disputes API, and refund requests are registered from buyer messages.
- AI triage: an LLM reads the buyer's message, the store's plain-English refund policy (clauses
P-1toP-5) and the order and tracking facts from our database. It proposes exactly one action and cites the policy clause it relies on. - Deterministic gate: plain code, never the LLM, decides whether that action can run on its own or needs the seller's approval.
- Supervision: the seller reviews the case in a drawer. A Provenance Panel keeps verified facts separate from AI-written text, and an audit timeline shows every state change. The seller can approve, edit the rationale, or snooze for 24 hours.
- Execution: approved actions go to PayPal with idempotency keys (
PayPal-Request-Id), so a retry never refunds twice. - Day Close: the business day stays locked until every pending approval is cleared and every autonomous action has been acknowledged.
How we built it
Our core principle is "LLM proposes, code disposes."
The autonomy rule is small and fully deterministic. A refund runs automatically only when all three conditions hold:
$$ \text{auto}(r) = (\text{amount}_r \le \$25) \;\land\; (\text{reason}_r \in \text{EligibleReasons}) \;\land\; (\text{tracking}_r \ne \text{DELIVERED}) $$
Everything else needs approval: refunds over the cap, contesting a dispute, accepting a claim, and replying to the buyer. Accepting a claim legally concedes the dispute, and replying speaks on the seller's behalf, so the model never does either one alone.
Stack:
- Next.js 15 (App Router), React 19 and strict TypeScript, styled with Tailwind CSS v4
- AG Grid for the dispute queue and the reconciliation ledger
- Supabase Postgres + Drizzle ORM: 9 relational tables with migrations and foreign keys
- Groq running
qwen/qwen3.8-27b, which returns structured JSON validated with Zod. Triage takes under 1.5 seconds. @paypal/agent-toolkitfor refunds and claim acceptance, plus the Disputes REST API for reading disputes and submitting evidencepdf-libto generate the delivery-proof PDF for contested disputes- Vitest + MSW + PGlite: 51 tests across 19 files run fully offline against in-memory Postgres and mocked PayPal endpoints, including an end-to-end test of the whole demo flow
All money is stored as integer cents. Every state transition is written to an append-only audit log, and the app is pinned to the PayPal sandbox with no switch to live money.
Challenges we ran into
- The sandbox dispute lifecycle. Before building anything, we tested the dispute path against the real sandbox. OAuth, authenticated dispute reads and a live order capture all worked. Creating a dispute on demand, submitting evidence and settling it were harder to drive end to end. Rather than fake those steps quietly, we saved real API responses as fixtures and labeled every synthetic step in the app as
[SIMULATED]. - Keeping the LLM in its lane. A model that can trigger refunds must never be the final authority. We limited it to one action per case, required a policy citation for every proposal, and validated its JSON output against a strict Zod schema. If the output fails validation or the API call fails, a deterministic, rule-based fallback writes the proposal instead, so the queue keeps working even when the model doesn't.
- Live and simulated data side by side. Demo purchases and fixture-backed disputes had to live next to real sandbox data without ever reaching a real PayPal endpoint by mistake. We flag every simulated case in the database, route it to a fake PayPal adapter, and show a
[SIMULATED]label in the UI. - Idempotency. Network retries against a payments API can refund the same order twice. Every executor sends a deterministic
PayPal-Request-Id, and we test that path explicitly. - A real-time problem in a 3-minute demo. Deadlines and snoozes are hard to demo in real time, so we added a simulated clock (
+1 Day) that wakes snoozed cases and moves deadlines forward.
What we learned
- Trust in an AI agent comes from where the decision boundary sits, not from how smart the model is. When the rule is a few lines of code, the seller can read it and rely on it.
- Separating facts from prose in the UI matters as much as the model's accuracy.
- Spiking the riskiest API first, with a go/no-go decision, saved us from building on top of assumptions that turned out to be wrong.
- Payment software has to be boring on purpose: integer cents, idempotency keys, append-only audit logs and a hard end-of-day reconciliation step.
What's next
- Use PayPal webhooks (
CUSTOMER.DISPUTE.CREATED,UPDATED,RESOLVED) instead of polling - Run the full dispute lifecycle live once sandbox dispute creation is reliable
- Let each seller set their own auto-refund cap and eligibility rules
- Add multi-seller support and notifications when a deadline is close
Built With
- ag-grid
- drizzle-orm
- groq
- msw
- next.js
- node.js
- paypal
- paypal-rest-api
- pdf-lib
- pglite
- pnpm
- postgresql
- qwen
- react
- supabase
- tailwindcss
- typescript
- vitest
- zod

