Tokenization replaces the PAN used by a system with another value. For application developers, the important question is what that value authorizes in context: which provider, environment, customer and operation it belongs to. A stored reference without those boundaries can be reused for the wrong customer even when the application never sees a PAN.
Two token families, two integration contracts
| Type | Where the application uses it | What to verify |
|---|---|---|
| Gateway reference | Through that provider’s API or vault | Account, environment, customer association and permitted operations |
| EMV payment token | In supported network payment flows | Token domain controls, lifecycle and required transaction data |
EMVCo’s payment-tokenization overview describes replacing the PAN for payment processing. That is different from an opaque saved-method ID. A network token can be PAN-shaped; a gateway ID need not be. The format alone is not a way to establish its permissions.
Avoid universal claims that every token is mathematically irreversible, worthless if stolen or unusable on a network. The implementation and its controls determine those properties. Likewise, a card replacement may be handled by a network-token lifecycle or a provider updater; neither guarantees that every saved payment method will continue working.
Save through the provider’s supported setup flow
For Stripe, a SetupIntent flow can establish a method for future use without an immediate charge. Collect consent for the intended use and associate the result with the correct customer. Creating an arbitrary payment-method ID and writing it to a table is not the whole integration.
On your server, resolve the signed-in user to the provider customer, retrieve or verify the provider-side setup result, and record the required association. A browser-supplied customer ID is not authority to attach another customer’s method. Keep sandbox and live references separate. The Stripe testing scenarios and provider fixture reference supply the appropriate sandbox inputs; locally generated PANs are for isolated format tests.
Test ownership before requesting a charge
This offline model assumes the saved record came from your database and context came from authenticated server state. It makes no API request and performs no authentication itself:
export function tokenDecision(saved, context) {
// context comes from authenticated server state, never from a request body.
if (!saved || !context || !['sandbox', 'live'].includes(context.mode) ||
['provider', 'customer', 'tokenId'].some(key =>
typeof saved[key] !== 'string' || !saved[key] ||
typeof context[key] !== 'string' || !context[key])) return 'invalid';
if (saved.provider !== context.provider) return 'wrong-provider';
if (saved.mode !== context.mode) return 'wrong-mode';
if (saved.customer !== context.customer) return 'wrong-customer';
if (saved.tokenId !== context.tokenId) return 'wrong-token';
if (saved.state === 'removed') return 'removed';
if (saved.state !== 'active') return 'invalid';
if (saved.consent !== true) return 'consent-required';
return 'eligible-for-provider-request'; // Not a successful charge.
}
export function removeLocalToken(saved, context) {
const decision = tokenDecision(saved, context);
if (!['eligible-for-provider-request', 'consent-required', 'removed'].includes(decision)) {
throw new TypeError('Token does not belong to this server context');
}
return { ...saved, state: 'removed' };
}The result eligible-for-provider-request means only that this example’s local checks passed. The provider may still reject the reference, require authentication, return a pending state or decline the payment. Production consent records should describe the agreed purpose and terms; the boolean here is a deliberately simplified test input.
| Case | Input / condition | Expected result |
|---|---|---|
| same-owner | Same provider, sandbox, customer-a and token-example | "eligible-for-provider-request" |
| other-customer | Saved for customer-a; server context is customer-b | "wrong-customer" |
| live-context | Sandbox reference presented to live context | "wrong-mode" |
| other-provider | Reference belongs to demo-gateway, not another-gateway | "wrong-provider" |
| other-token | Server context requests token-other | "wrong-token" |
| removed | Saved reference is marked removed | "removed" |
| no-consent | Local consent flag is false | "consent-required" |
Treat provider + mode + customer + tokenId as separate values in the association. Matching a token’s text alone is not enough. Persist the state returned by a removal operation; keeping the original object leaves the local method active.
Removal is a lifecycle event
test('Removing a local token prevents reuse, including a repeated removal', () => {
const { saved, context } = cases.token[0];
assert.equal(tokenDecision(saved, context), 'eligible-for-provider-request');
const removed = removeLocalToken(saved, context);
assert.equal(tokenDecision(removed, context), 'removed');
assert.deepEqual(removeLocalToken(removed, context), removed);
assert.equal(saved.state, 'active'); // immutable input, persist the returned state
assert.throws(() => removeLocalToken(saved, {...context, customer: 'customer-b'}));
});The test covers save-state use, local removal, repeated removal and a wrong-customer request. It does not call a provider’s detach endpoint. In a deployed integration, define what removal means for future charges, subscriptions and work already queued. Use provider-specific idempotency and durable application state for retries. Race a queued charge against a removal in an integration test rather than assuming a pure function resolves concurrency.
To run the model, extract the token lifecycle project and use Node.js 22 or later:
node --test guide-examples.test.mjs
node guide-demo.mjs token
No keys or live payment data are required. The expected results are published from the same JSON cases exercised by the test runner.
Keep the surrounding data flow small
Retain the reference, association and product-required display metadata. Do not retain the submitted security code; the CVV guide explains the restriction and its logging test. Avoid copying token IDs into unrelated telemetry simply because they are not PANs.
Tokenization can reduce exposure without automatically removing a database or application from PCI scope. Hosted fields also do not automatically imply SAQ A-EP; eligibility depends on the implementation and the full conditions. The PCI developer guide links the current guidance and maps the data flow. Add provider failures, owner mismatches and removal races to the payment-form test checklist.