Yash Gupta

Taking money through an app you do not control

Customers on iPhone can now buy subscription plans and wallet credits inside the app. The purchase happens on Apple's side, and all my server receives is a message claiming it happened. Everything below follows from not trusting that message.

Role
Designed and built the server side
Stack
NestJS, PostgreSQL, Drizzle ORM, App Store Server API
Status
Live in production since 7 Oct 2026
Unit tests
33

The problem in one sentence

A web payment tells you it succeeded through a gateway you called yourself. An in-app purchase tells you it succeeded through the customer's phone, and anyone can send that request with any transaction id and any product id attached. The app's word is not evidence.

What the server does instead

iOS app buys through StoreKit
        |
        v  { transactionId, productId }
POST /payment/apple/activate
        |
        +--> App Store Server API: what is this transaction?
        |      returns a signed transaction, checked against
        |      Apple's root certificates, then the product compared
        |
        +--> catalogue: which plan or how many credits is this product?
        |
        +--> the same fulfilment path the web checkout already used

The last line matters more than it looks. An Apple purchase lands in the same payment records and the same credit ledger as a card payment, so reporting, wallets and expiry did not need a parallel implementation with its own bugs.

Four decisions

1. A replay is stopped by the database, not by code

Mobile networks drop. The app retries. Without protection, one purchase grants twice, which is real money.

A common instinct is to check for an existing record first and skip if it is there. That loses a race: two requests can both look, both find nothing, and both write. Instead the constraint lives in Postgres as a partial unique index over the gateway and the transaction id, applied only to Apple rows. The second insert cannot happen, regardless of timing.

A replayed request returns success, not an error. The caller wanted the purchase to be active, and it is.

2. The same transaction from a different account is a conflict

A retry comes from the same user. The same transaction id arriving under a different account is either a bug or someone reusing a receipt, so it returns 409 and is logged with both accounts. Treating that as a retry would be the actual vulnerability.

3. If Apple cannot confirm it, nobody gets anything

When the App Store API is unreachable, activation fails with a 503 and no entitlement is written. A purchase that cannot be confirmed is not yet a purchase. Because the whole path is idempotent, the customer simply retries and it activates.

4. Tax runs in the opposite direction

The web checkout adds 18% GST on top of a base price. Apple shows the customer a single tax-inclusive price and remits from it, so the catalogue price is already the gross amount charged. The server therefore extracts the tax rather than adding it. Adding it would have recorded a total the customer was never charged, which is the kind of error that is only found later, in accounting.

Two things I would not have predicted

  • App Review buys in the sandbox. A production server that only ever asks the production environment fails every reviewer purchase, and the app gets rejected for a bug that does not exist. The server asks the configured environment first, and when Apple says it does not know the transaction, it asks the other one. Production is asked first so genuine purchases do not pay for an extra round trip, and the default is sandbox, so a half-configured deployment cannot accidentally accept real receipts.
  • Certificate armour. Apple ships its root certificates as binary DER files, but files get re-encoded as text PEM as they move through emails and wikis. The loader accepts both, because the alternative failure is an opaque verification error that points at nothing. A neighbouring trap in the same family: the In-App Purchase key is not the Sign in with Apple key, and using the wrong one fails as a bare 401.

What I would tell someone building this next

  • Decide what your idempotency key is before you write anything else, and put it in the schema.
  • Make a duplicate a success and a mismatch a conflict. They are different events.
  • Fulfil through the path you already have, so a second payment source does not become a second ledger.
  • Find out which environment your reviewers use before you submit, not after the rejection.