Inspiration

Business partners often split sales money by a deal written in plain English. For example: "Alice gets her investment back first, then we split 60/40, and refunds come out of a reserve." Someone then does the maths by hand on every sale, and refunds are worse. Nobody knows who owes what until an argument starts.

I wanted the contract itself to run the money. You write the deal in words and the app pays the partners and handles refunds the way the contract says.

What it does

  1. You describe the deal in a chat. Claude asks questions until nothing is ambiguous, such as who pays PayPal's payout fee or what a partial refund means.
  2. Claude turns the contract into two small Python functions, one for payouts and one for refunds. It also writes its own tests.
  3. ClausePay checks the code, then you write three scenarios of your own, run them and approve each.
  4. You freeze the contract. Every sale PayPal reports then runs through it and the partners are paid automatically, and each sale has a Refund button.

It runs on the PayPal sandbox only, so no real money moves.

How I built it

  • Backend: Flask, with a background thread that settles payouts, and PayPal's Orders, Payouts, Vault and webhook APIs.
  • AI: Claude (claude-sonnet-5-5) returns JSON that follows a fixed schema.
  • A sandbox for the generated code:
    • It is parsed first and rejected if it has imports, floats or unbounded loops.
    • It then runs in a separate Python process with limits on time, CPU and memory.
  • Memory: the functions remember nothing. A state dict goes in and comes out, and ClausePay keeps it in a per-contract journal file.
    • An entry is pending until PayPal confirms the payout, then it is committed or failed.
    • Sale and refund IDs are unique, so a repeated notification is never counted twice.
    • The code's SHA-256 hash is checked before every run.
  • Exact money: everything uses Decimal, never floats. For every sale, with $S$ the amount paid by the buyer and $f$ the PayPal fee, ClausePay requires

$$\sum_i \text{payout}_i \;+\; \text{payout fees covered} \;+\; \text{held back} \;=\; S - f$$

to hold to the cent. Refunds have the same kind of rule, and a contract that breaks it is rejected.

  • Reports: ClausePay builds every figure. Claude only writes the explanation, using placeholders, and any text containing a number of its own is rejected.

What I learned

  • Don't trust generated maths. Claude is good at turning a contract into code but sometimes gets cents wrong. Checking its work with plain code and its own tests mattered more than prompting.
  • Fees are inputs. The sandbox charged $f = \operatorname{round}(0.024\,S, 2) + 0.30$ on a sale, not PayPal's published rates, so the rates became settings.
  • Charging partners' wallets: when a partner pays PayPal's fee on their own charge, the gross amount $g$ needed so that they net $n$ satisfies

$$g - \big(0.024\,g + 0.30\big) = n \quad\Rightarrow\quad g = \left\lceil \frac{n + 0.30}{0.976} \right\rceil_{\text{cents}}$$

I checked this against 400 amounts.

  • Idempotency: payment notifications repeat and requests get retried, so every sale, refund and charge needs a stable ID.

Challenges

  • Ambiguity. "Split the rest 60/40" leaves open the rounding, the payout fee, and what a refund does to an investment already paid back. The chat exists to force those answers.
  • Refunds. A refund may need money from partners who already received theirs. Charging their saved wallets automatically, without ever double-charging on a retry, took the most care.
  • A half-finished payout must not move the contract's memory. The pending/commit journal solves it.
  • Sandbox quirks. PayPal's sandbox login sometimes dead-ends on a "Page unavailable" screen, and the Payouts API uses currency where other APIs use currency_code.
  • Free hosting. Render's free plan wipes the disk on redeploy. I accepted that for a demo and documented it.

Built With

Share this project:

Updates

Submission history