---
name: snap4id-verify-receipts-backend
description: Implement Snap4ID backend trust - a Node endpoint that verifies App Store / Play receipts with @snap4id-sdk/purchase-verifier and unlocks the job with @snap4id-sdk/partner-api, plus the job.paid webhook. Use when the partner verifies purchases on its own backend.
---

# Verify receipts in your backend

1. Keys: an Ed25519 key pair (`openssl genpkey -algorithm ed25519`); the public half goes to Snap4ID, which returns a
   key id. Read the private key and key id from the secret manager / environment, never from the repository.
2. Client: `createPartnerClient({ baseUrl: 'https://snap4.id', partnerId, keyId, privateKey })` at start-up.
3. Partner configuration JSON (bundle id / package name, product ids, Play public key, `allowed_environments:
   ["production"]`) validated with `PartnerConfigSchema` at start-up.
4. `POST /purchases` (behind the app's user authentication), body `{ jobId, paymentRef, receipt, price? }`:
   - store the purchase as pending first (for recovery);
   - `verifyReceipt({ receipt, payment_ref: paymentRef }, { config, policy: { deployment: 'production',
     acceptTestReceipts: false } })` → `400 { error: code }` when not ok, nothing sent to Snap4ID;
   - `client.unlock(jobId, { payment_ref, platform, product_id, transaction_id, environment, ...choosePrice(purchase.storePrice, price) })`;
   - `200 { unlocked: true, orderCode }`; `isPartnerApiError(e, 'already_paid')` → record a refund, answer
     `409 { error: 'already_paid' }`; retryable codes (`server_error`, `timeout`, `network`) → `502`, keep pending.
5. App side: `onVerifyPurchase` posts that body and throws `AlreadyPaidError` (from `@snap4id-sdk/app-embed`) on
   `409 already_paid`; resolves only on `200`.
6. Webhook `POST /webhooks/snap4id` on the raw body: `client.verifyWebhook({ headers, rawBody })`; `401` on failure,
   `503` on `keys_unavailable`, never `410`; dedupe on `event.event_id`; answer `2xx` fast, copy files after with
   `getJob`.
7. A periodic recovery job: unlock pending purchases again; copy files of paid jobs until `expires_at`.
8. Tests: unlock success, repeat (idempotent), `already_paid` → refund recorded, webhook with valid / tampered
   signature and a duplicate `event_id`.
