Başar BallıözProduct Engineer
← All posts

Verifying App Store and Google Play subscriptions in Node.js

RPMBase sells one subscription on two stores. The rule I built everything around: the app never decides who is Premium. The server asks Apple or Google, binds the purchase to one account, and keeps it in sync when the store says something changed.

By Başar Ballıöz · · 5 min read

The short answer

To verify in-app subscriptions on your own server, never trust a receipt or flag sent by the app. Send the store's transaction id or purchase token to your backend, look it up with Apple's App Store Server API or Google Play Developer API, bind it to exactly one user, and keep it current with App Store Server Notifications V2. Verify every notification's signature against a pinned Apple root certificate, and store each notification id so a retry is never applied twice.

The rest of this post is how that looks in the Node.js API behind RPMBase, including the parts the documentation does not make obvious.

What Premium adds, and what stays free, is on the RPMBase pricing page.

The flow at a glance

  1. The app completes a purchase with the store and sends the server an identifier: Apple's transaction id, or Google's product id and purchase token.
  2. The server looks that identifier up directly with the store, over HTTPS, with its own credentials.
  3. If the store says the subscription is active, the server binds the purchase's stable id to the signed-in user and grants Premium until the store's expiry date.
  4. From then on, store notifications (renewals, expiries, refunds) update the same record.

The app shows what the server says. Nothing on the phone can turn Premium on by itself.

Apple: look the transaction up, and know the sandbox trap

On iOS the server calls the App Store Server API with a signed JWT and gets back the transaction as a JWS. Because the server fetched it over TLS from Apple itself, decoding that payload is safe; the trust comes from the connection, not from something the client sent.

The trap is the environment. TestFlight and review purchases are sandbox transactions, and they do not exist in production. The usual advice is to retry in sandbox when production answers 404 with error code 4040010 ("transaction id not found"). That is only half of it:

  • A live app gets 404 with 4040010 for a sandbox transaction.
  • An app that is not yet published gets 401 from production instead.

If you only retry on 4040010, App Review's own test purchase fails before your first release, which is exactly when you can least afford it. RPMBase falls through to sandbox on 401 or 404, and a failed sandbox lookup still grants nothing.

Two more checks before granting anything: the transaction's bundleId must match the app, and the productId must be one of the app's Premium products.

App Store Server Notifications V2: verify the signature yourself

Notifications are different. Apple calls your endpoint, so anyone can send a request that looks like one. The body is a JWS, and it is only trustworthy after three checks:

  1. The algorithm is ES256 and the header carries an x5c certificate chain.
  2. The chain is intact and anchored: each certificate is signed by the next, and one of them matches the SHA-256 fingerprint of Apple Root CA - G3, pinned in the code. The fingerprint is public, so pinning it costs nothing.
  3. The signature matches the leaf certificate's key. In Node's crypto, ES256 signatures in a JWS are raw r||s bytes, so verify with dsaEncoding: "ieee-p1363". The default DER encoding will reject every valid notification.

The transaction inside the notification, signedTransactionInfo, is itself a JWS and goes through the same verification.

Store every notification id before acting on it

Apple retries notifications when your endpoint is slow or fails, so the same event can arrive more than once. Each notification has a notificationUUID. RPMBase upserts it into its own collection first; if the row already existed, the notification is acknowledged and nothing else happens. Only a first sighting changes the subscription.

Then the notification type decides the change:

Notification typeWhat RPMBase does
SUBSCRIBED, DID_RENEW, OFFER_REDEEMEDPremium active until the new expiry date
DID_FAIL_TO_RENEWMarked as billing retry; Premium runs to the current expiry, not cut off at once
EXPIRED, GRACE_PERIOD_EXPIREDBack to Free at the expiry date
REFUND, REVOKE, or any revocation dateRevoked and back to Free immediately
Anything elseStored for the record, no change

A notification for a purchase that was never bound to an account is stored and ignored. It cannot create Premium for anyone.

Google Play: check expiry, payment state and acknowledgement

On Android the server uses a service account to call the Google Play Developer API's subscription endpoint with the product id and purchase token. Three fields matter:

  • expiryTimeMillis must be in the future.
  • paymentState of 0 means the payment is still pending. Do not grant yet.
  • acknowledgementState of 0 means nobody has acknowledged the purchase. Google refunds purchases that are not acknowledged within three days, so the server acknowledges it right after verifying.

One detail caught me out: renewal order ids. The first order looks like GPA.1234-5678-9012-34567, and each renewal appends a counter: GPA.1234-5678-9012-34567..0, ..1, and so on. If you bind the full order id, every renewal looks like a new purchase. RPMBase strips the ..N suffix and binds the stable prefix.

One purchase, one account

A verified purchase is bound to exactly one user by its stable id: Apple's original transaction id, or Google's order prefix. Binding the same purchase to the same user again is a no-op, so a restore or a retry is safe. Binding it to a different user returns 409: "this purchase is already linked to another account". Without that rule, one subscription could unlock Premium on any number of accounts by restoring purchases on each.

Common questions

Do I need server-side verification if I use a purchases SDK?

Not always, but if your server grants anything (more cars, a larger AI allowance), the server should be the one that checks. Otherwise the server has to trust the client.

Is decoding the JWS from the App Store Server API without verifying it safe?

Only because your server fetched it from Apple over TLS. A JWS that arrives from the client, or from a webhook, must have its signature and chain verified.

Why does my sandbox purchase fail before my app is live?

Production returns 401, not 404, for an app that has never been released. Retry in sandbox on both.

What I learned

The store is the source of truth; your database is a cache of it. Every change to Premium comes from a lookup or a signed notification, never from the app.

Make every write idempotent. Notifications repeat, users restore, networks retry. Unique ids for notifications and purchases turn all of that into no-ops.

Test the unpublished state. The first purchase your backend ever sees is App Review's, in sandbox, before release. Make sure that path works first.

More posts

LLM API cost control per user: how RPMBase keeps the AI bill boundedHow RPMBase keeps OpenAI costs bounded per user: server-side limits, a worst-case cost reservation, atomic budgets in MongoDB, shared pools for free accounts and a hard cap. Keyword cannibalization in practice: one query, three moves, zero impressionsA real case from RPMVault: moving "motorcycle maintenance app" between pages three times cost its impressions. The query ownership rules, title limits and lock test I use now.