You check: your own backend
Documents @snap4id-sdk/app-embed 1.0.0 · @snap4id-sdk/partner-api 1.0.0 · @snap4id-sdk/purchase-verifier 1.0.0
You check purchases in your own backend, next to your user accounts and purchase records. Your app sends the receipt
to your server; your server validates it against your store details and then asks Snap4ID to unlock the job with a call to our
unlock API (Partner API) signed with your Ed25519 key. Snap4ID never sees the receipt before your backend accepts it. Our verifier library,
@snap4id-sdk/purchase-verifier, runs the same checks as Snap4ID’s verifier, so you don’t write receipt cryptography
yourself. The receipt is still never checked in the app.
No backend code wanted, only checks on your infrastructure? You check: our verifier server.
1. Keys and onboarding
Section titled “1. Keys and onboarding”-
Generate an Ed25519 key pair; keep the private key in your secret manager.
Terminal window openssl genpkey -algorithm ed25519 -out partner.pemopenssl pkey -in partner.pem -pubout -out partner.pub.pem -
Send
partner.pub.pem(the public half only) and your product ids to Snap4ID; you get a key id back. -
Optionally give Snap4ID a webhook URL for
job.paid(webhooks). -
Snap4ID sets your partner configuration to “you check: your own backend”.
Rotate by registering a new key, switching to it, then asking Snap4ID to revoke the old one.
2. The app side
Section titled “2. The app side”React Native: add onVerifyPurchase; its presence tells the component that your backend checks purchases.
createSnap4IdClient({ partnerId: 'your-partner-id', purchases: 'built-in', // or your own store code onVerifyPurchase: async (receipt, { jobId, paymentRef, price }) => { const res = await fetch('https://api.example.pl/snap4id/purchases', { method: 'POST', headers: { 'content-type': 'application/json', authorization: `Bearer ${await userToken()}` }, body: JSON.stringify({ jobId, paymentRef, receipt, price }), }); if (res.status === 409 && (await res.json()).error === 'already_paid') throw new AlreadyPaidError(); if (!res.ok) throw new Error(`verify failed: ${res.status}`); }, onCompleted,});Resolve once the job is unlocked; reject otherwise (the purchase stays unfinished and is verified again later). A
rejection with code: 'already_paid' (AlreadyPaidError from @snap4id-sdk/app-embed, or the Partner API client’s
error) makes the component report duplicate_purchase and finish the purchase. The call is bounded at 15 s.
Native host: the same request from your store code, then purchase.result { status: 'succeeded' } without a receipt
(host contract).
3. The backend side
Section titled “3. The backend side”import { createPartnerClient, isPartnerApiError } from '@snap4id-sdk/partner-api';import { choosePrice, verifyReceipt } from '@snap4id-sdk/purchase-verifier';
const snap4id = createPartnerClient({ baseUrl: 'https://snap4.id', partnerId: 'your-partner-id', keyId: process.env.SNAP4ID_KEY_ID!, privateKey: process.env.SNAP4ID_PARTNER_KEY_PEM!, // PKCS#8 PEM, checked at start-up});
export async function handlePurchase({ jobId, paymentRef, receipt, price }) { // 1. Your user authentication: is this your user, and may they buy for this job? // 2. Store the purchase as pending (recovery), then verify offline: store signature, your app and product, // the binding to paymentRef, the environment. const verified = await verifyReceipt( { receipt, payment_ref: paymentRef }, { config: partnerConfig, policy: { deployment: 'production', acceptTestReceipts: false } }, ); if (!verified.ok) return { status: 400, error: verified.code }; const { purchase } = verified; // 3. Unlock (idempotent per job + transaction; a repeat of a success answers 200 again). try { const { order_code } = await snap4id.unlock(jobId, { payment_ref: paymentRef, platform: purchase.platform, product_id: purchase.productId, transaction_id: purchase.transactionId, environment: purchase.environment, ...choosePrice(purchase.storePrice, price), // the signed store price wins over the app's }); return { status: 200, orderCode: order_code }; } catch (e) { if (isPartnerApiError(e, 'already_paid')) return { status: 409, error: 'already_paid' }; // refund this purchase throw e; // retryable codes: keep the purchase and retry later }}partnerConfig holds your store details (bundle id / package name, product ids, Play licensing public key, accepted
environments) in the partner configuration format; validate it
at start-up with PartnerConfigSchema. A complete backend with a state file, recovery and the webhook is in the SDK
repository: examples/partner-backend.
Errors to handle
Section titled “Errors to handle”Partner API code |
What to do |
|---|---|
already_paid (409) |
the job was paid by another transaction: refund this purchase |
transaction_already_used (409) |
the transaction unlocked another job: do not deliver for it |
job_not_payable (409) |
the job does not wait for a store payment: do not charge |
payment_ref_mismatch, unknown_product, environment_not_allowed, invalid_request (400) |
fix the input or ask Snap4ID to update your configuration |
not_found (404) |
wrong job id for your partner |
unauthorized (401) |
key id, key, partner id or clock; never retried |
server_error, timeout, network |
retries exhausted: keep the purchase and retry later |
Quote error.requestId to Snap4ID support. All codes: error codes; wire contract:
Partner API.
Recovery
Section titled “Recovery”Store the purchase before you unlock. Run a periodic job that unlocks pending purchases again (already_paid becomes
a refund) and uses getJob(jobId) to copy the files of paid jobs until expires_at.
