@chipmobilesdk/rn-payment 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Timeout, retry, and cache policy.
3
+ *
4
+ * Defaults and bounds live here rather than at each call site so there is one
5
+ * place to read what the package will wait for, and so consumers can read the
6
+ * values they are bound by instead of discovering them from a timeout in
7
+ * production.
8
+ *
9
+ * Out-of-band values are a configuration error rather than being silently
10
+ * clamped: quietly turning a 1-second timeout into 3 seconds would tell the app
11
+ * it configured something it did not.
12
+ */
13
+
14
+ import { PaymentConfigError } from '../diagnostics/errors';
15
+ import type {
16
+ BackoffConfig,
17
+ RetryConfig,
18
+ TimeoutConfig,
19
+ } from '../config/types';
20
+
21
+ /** Local IPC to Play Services. Slower than this means unavailable, not busy. */
22
+ export const DEFAULT_CONNECT_TIMEOUT_MS = 10_000;
23
+ /** A network round trip; a paywall that hangs longer should show its error state. */
24
+ export const DEFAULT_PRODUCT_QUERY_TIMEOUT_MS = 15_000;
25
+ /** Runs at startup, so it must not hold the first render hostage. */
26
+ export const DEFAULT_SYNC_TIMEOUT_MS = 20_000;
27
+ /**
28
+ * Covers opening the store surface only.
29
+ *
30
+ * Time the user spends *inside* the store's own UI is deliberately excluded
31
+ * (FR-035): a user reading the terms, switching to their banking app for an OTP,
32
+ * or simply hesitating must never have their purchase cancelled by our clock.
33
+ */
34
+ export const DEFAULT_PURCHASE_LAUNCH_TIMEOUT_MS = 30_000;
35
+ /** Per finalize attempt, not per transaction — the queue owns overall persistence. */
36
+ export const DEFAULT_FINALIZE_ATTEMPT_TIMEOUT_MS = 15_000;
37
+
38
+ export const CONNECT_TIMEOUT_BOUNDS = { min: 3_000, max: 60_000 } as const;
39
+ export const PRODUCT_QUERY_TIMEOUT_BOUNDS = { min: 3_000, max: 60_000 } as const;
40
+ export const SYNC_TIMEOUT_BOUNDS = { min: 5_000, max: 120_000 } as const;
41
+ export const PURCHASE_LAUNCH_TIMEOUT_BOUNDS = {
42
+ min: 5_000,
43
+ max: 120_000,
44
+ } as const;
45
+ export const FINALIZE_ATTEMPT_TIMEOUT_BOUNDS = {
46
+ min: 3_000,
47
+ max: 60_000,
48
+ } as const;
49
+
50
+ /**
51
+ * Prices and offer eligibility are shown at the moment of purchase, so a short
52
+ * TTL with on-demand refresh beats a long one (FR-029).
53
+ */
54
+ export const DEFAULT_PRODUCT_CACHE_TTL_MS = 900_000;
55
+ export const PRODUCT_CACHE_TTL_BOUNDS = { min: 60_000, max: 86_400_000 } as const;
56
+
57
+ /**
58
+ * How long a successful sync keeps entitlements trustworthy offline: **7 days**.
59
+ *
60
+ * Fixed by the specification (FR-055) after weighing both failure directions.
61
+ * The realistic offline cases — travel, poor coverage, a device left unused —
62
+ * resolve within days. Seven days covers a holiday without connectivity while
63
+ * bounding how long a refunded entitlement can outlive its refund to one week.
64
+ *
65
+ * Past this the entitlement becomes `unknown`, never `not_entitled` (FR-055a):
66
+ * the package stops asserting rather than telling a paying customer they did not
67
+ * pay.
68
+ */
69
+ export const DEFAULT_STALE_AFTER_MS = 604_800_000;
70
+ export const STALE_AFTER_BOUNDS = {
71
+ min: 3_600_000,
72
+ max: 7_776_000_000,
73
+ } as const;
74
+
75
+ /** Keeps rapid app switching from becoming a request storm (PERF-002). */
76
+ export const DEFAULT_FOREGROUND_SYNC_MIN_INTERVAL_MS = 60_000;
77
+ export const FOREGROUND_SYNC_MIN_INTERVAL_BOUNDS = {
78
+ min: 0,
79
+ max: 3_600_000,
80
+ } as const;
81
+
82
+ /** Bounded so a dropped billing connection cannot become a retry storm (FR-020). */
83
+ export const DEFAULT_RECONNECT_BACKOFF: Required<BackoffConfig> = {
84
+ initialDelayMs: 1_000,
85
+ maxDelayMs: 30_000,
86
+ factor: 2,
87
+ maxAttempts: 5,
88
+ jitter: true,
89
+ };
90
+
91
+ /**
92
+ * Finalize retry.
93
+ *
94
+ * Bounded **per session** but never permanently abandoned: Google auto-refunds
95
+ * an unacknowledged purchase after three days, so exhausting this session's
96
+ * attempts must leave the transaction queued for the next launch, not dropped
97
+ * (FR-045).
98
+ */
99
+ export const DEFAULT_FINALIZE_BACKOFF: Required<BackoffConfig> = {
100
+ initialDelayMs: 2_000,
101
+ maxDelayMs: 60_000,
102
+ factor: 2,
103
+ maxAttempts: 5,
104
+ jitter: true,
105
+ };
106
+
107
+ export const BACKOFF_MAX_DELAY_BOUNDS = { min: 5_000, max: 600_000 } as const;
108
+ export const BACKOFF_ATTEMPT_BOUNDS = { min: 1, max: 10 } as const;
109
+
110
+ interface Bounds {
111
+ readonly min: number;
112
+ readonly max: number;
113
+ }
114
+
115
+ function requireInBounds(
116
+ label: string,
117
+ value: number | undefined,
118
+ fallback: number,
119
+ bounds: Bounds,
120
+ ): number {
121
+ if (value === undefined) {
122
+ return fallback;
123
+ }
124
+ if (!Number.isFinite(value) || value < bounds.min || value > bounds.max) {
125
+ throw new PaymentConfigError({
126
+ code: 'PAYMENT_INVALID_CATALOG',
127
+ message: `${label} must be between ${bounds.min} and ${bounds.max}ms, received ${value}`,
128
+ operation: 'configure',
129
+ offenders: [label],
130
+ });
131
+ }
132
+ return value;
133
+ }
134
+
135
+ export interface ResolvedTimeouts {
136
+ connectMs: number;
137
+ productQueryMs: number;
138
+ syncMs: number;
139
+ purchaseLaunchMs: number;
140
+ finalizeAttemptMs: number;
141
+ }
142
+
143
+ export function resolveTimeouts(config?: TimeoutConfig): ResolvedTimeouts {
144
+ return {
145
+ connectMs: requireInBounds(
146
+ 'timeouts.connectMs',
147
+ config?.connectMs,
148
+ DEFAULT_CONNECT_TIMEOUT_MS,
149
+ CONNECT_TIMEOUT_BOUNDS,
150
+ ),
151
+ productQueryMs: requireInBounds(
152
+ 'timeouts.productQueryMs',
153
+ config?.productQueryMs,
154
+ DEFAULT_PRODUCT_QUERY_TIMEOUT_MS,
155
+ PRODUCT_QUERY_TIMEOUT_BOUNDS,
156
+ ),
157
+ syncMs: requireInBounds(
158
+ 'timeouts.syncMs',
159
+ config?.syncMs,
160
+ DEFAULT_SYNC_TIMEOUT_MS,
161
+ SYNC_TIMEOUT_BOUNDS,
162
+ ),
163
+ purchaseLaunchMs: requireInBounds(
164
+ 'timeouts.purchaseLaunchMs',
165
+ config?.purchaseLaunchMs,
166
+ DEFAULT_PURCHASE_LAUNCH_TIMEOUT_MS,
167
+ PURCHASE_LAUNCH_TIMEOUT_BOUNDS,
168
+ ),
169
+ finalizeAttemptMs: requireInBounds(
170
+ 'timeouts.finalizeAttemptMs',
171
+ config?.finalizeAttemptMs,
172
+ DEFAULT_FINALIZE_ATTEMPT_TIMEOUT_MS,
173
+ FINALIZE_ATTEMPT_TIMEOUT_BOUNDS,
174
+ ),
175
+ };
176
+ }
177
+
178
+ function resolveBackoff(
179
+ label: string,
180
+ config: BackoffConfig | undefined,
181
+ fallback: Required<BackoffConfig>,
182
+ ): Required<BackoffConfig> {
183
+ const maxDelayMs = requireInBounds(
184
+ `${label}.maxDelayMs`,
185
+ config?.maxDelayMs,
186
+ fallback.maxDelayMs,
187
+ BACKOFF_MAX_DELAY_BOUNDS,
188
+ );
189
+ const maxAttempts = requireInBounds(
190
+ `${label}.maxAttempts`,
191
+ config?.maxAttempts,
192
+ fallback.maxAttempts,
193
+ BACKOFF_ATTEMPT_BOUNDS,
194
+ );
195
+ return {
196
+ initialDelayMs: config?.initialDelayMs ?? fallback.initialDelayMs,
197
+ maxDelayMs,
198
+ factor: config?.factor ?? fallback.factor,
199
+ maxAttempts,
200
+ jitter: config?.jitter ?? fallback.jitter,
201
+ };
202
+ }
203
+
204
+ export interface ResolvedRetry {
205
+ reconnect: Required<BackoffConfig>;
206
+ finalize: Required<BackoffConfig>;
207
+ }
208
+
209
+ export function resolveRetry(config?: RetryConfig): ResolvedRetry {
210
+ return {
211
+ reconnect: resolveBackoff(
212
+ 'retry.reconnect',
213
+ config?.reconnect,
214
+ DEFAULT_RECONNECT_BACKOFF,
215
+ ),
216
+ finalize: resolveBackoff(
217
+ 'retry.finalize',
218
+ config?.finalize,
219
+ DEFAULT_FINALIZE_BACKOFF,
220
+ ),
221
+ };
222
+ }
223
+
224
+ export function resolveStaleAfterMs(value?: number): number {
225
+ return requireInBounds(
226
+ 'staleAfterMs',
227
+ value,
228
+ DEFAULT_STALE_AFTER_MS,
229
+ STALE_AFTER_BOUNDS,
230
+ );
231
+ }
232
+
233
+ export function resolveProductCacheTtlMs(value?: number): number {
234
+ return requireInBounds(
235
+ 'productCacheTtlMs',
236
+ value,
237
+ DEFAULT_PRODUCT_CACHE_TTL_MS,
238
+ PRODUCT_CACHE_TTL_BOUNDS,
239
+ );
240
+ }
241
+
242
+ export function resolveForegroundSyncMinIntervalMs(value?: number): number {
243
+ return requireInBounds(
244
+ 'foregroundSyncMinIntervalMs',
245
+ value,
246
+ DEFAULT_FOREGROUND_SYNC_MIN_INTERVAL_MS,
247
+ FOREGROUND_SYNC_MIN_INTERVAL_BOUNDS,
248
+ );
249
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Normalized public errors.
3
+ *
4
+ * Error codes are **public API**: renaming one is a breaking change (FR-078,
5
+ * LIB-008). They are the vocabulary an app branches on, so they must outlive any
6
+ * internal refactor and must never leak a store's own code.
7
+ */
8
+
9
+ export const PAYMENT_ERROR_CODES = [
10
+ 'PAYMENT_NOT_CONFIGURED',
11
+ 'PAYMENT_INVALID_CATALOG',
12
+ 'PAYMENT_NOT_READY',
13
+ 'PAYMENT_DISABLED',
14
+ 'PAYMENT_UNAVAILABLE',
15
+ 'PAYMENT_UNSUPPORTED_PLATFORM',
16
+ 'PAYMENT_ENVIRONMENT_BLOCKED',
17
+ 'PAYMENT_PRODUCTION_CONFIG_IN_NON_PRODUCTION_BUILD',
18
+ 'PAYMENT_PURCHASE_IN_PROGRESS',
19
+ 'PAYMENT_USER_CANCELLED',
20
+ 'PAYMENT_PENDING',
21
+ 'PAYMENT_ALREADY_OWNED',
22
+ 'PAYMENT_INELIGIBLE',
23
+ 'PAYMENT_UNKNOWN_PRODUCT',
24
+ 'PAYMENT_PRODUCT_UNAVAILABLE_IN_REGION',
25
+ 'PAYMENT_TIMEOUT',
26
+ 'PAYMENT_NETWORK_ERROR',
27
+ 'PAYMENT_STORE_ERROR',
28
+ 'PAYMENT_FINALIZE_FAILED',
29
+ 'PAYMENT_VALIDATION_REJECTED',
30
+ 'PAYMENT_VALIDATION_UNAVAILABLE',
31
+ 'PAYMENT_STORAGE_UNAVAILABLE',
32
+ ] as const;
33
+
34
+ export type PaymentErrorCode = (typeof PAYMENT_ERROR_CODES)[number];
35
+
36
+ /** Which operation produced an error. Part of the public error shape (FR-078). */
37
+ export type PaymentOperation =
38
+ | 'configure'
39
+ | 'initialize'
40
+ | 'getProducts'
41
+ | 'purchase'
42
+ | 'finalize'
43
+ | 'consume'
44
+ | 'sync'
45
+ | 'restore'
46
+ | 'changePlan'
47
+ | 'validate'
48
+ | 'persist';
49
+
50
+ export class PaymentError extends Error {
51
+ readonly code: PaymentErrorCode;
52
+ readonly operation: PaymentOperation;
53
+ readonly retryable: boolean;
54
+ /** Redacted where appropriate — never a raw receipt, token, or store object (FR-074). */
55
+ readonly cause?: string;
56
+
57
+ constructor(init: {
58
+ code: PaymentErrorCode;
59
+ message: string;
60
+ operation: PaymentOperation;
61
+ retryable?: boolean;
62
+ cause?: string;
63
+ }) {
64
+ super(init.message);
65
+ this.name = 'PaymentError';
66
+ this.code = init.code;
67
+ this.operation = init.operation;
68
+ this.retryable = init.retryable ?? false;
69
+ this.cause = init.cause;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * A configuration problem the app can act on.
75
+ *
76
+ * Separate from `PaymentError` because configuration faults are developer-facing
77
+ * and must never be shown to an end user — the message-key map keeps that
78
+ * separation explicit (FR-085).
79
+ */
80
+ export class PaymentConfigError extends PaymentError {
81
+ /** The offending catalog entries or entitlement keys, named (FR-011). */
82
+ readonly offenders: string[];
83
+
84
+ constructor(init: {
85
+ code: PaymentErrorCode;
86
+ message: string;
87
+ operation: PaymentOperation;
88
+ offenders?: string[];
89
+ }) {
90
+ super({ ...init, retryable: false });
91
+ this.name = 'PaymentConfigError';
92
+ this.offenders = init.offenders ?? [];
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Strips anything that must never reach a log or event.
98
+ *
99
+ * Applied at the boundary rather than trusted to call sites: a single forgotten
100
+ * interpolation is how receipts end up in logs (NFR-006).
101
+ */
102
+ export function redact(value: unknown): string {
103
+ if (value instanceof Error) {
104
+ return value.message;
105
+ }
106
+ if (typeof value === 'string') {
107
+ return value;
108
+ }
109
+ return '[redacted]';
110
+ }
@@ -0,0 +1,266 @@
1
+ /**
2
+ * Typed events and the emitter.
3
+ *
4
+ * The bar is FR-077: the event stream **alone** must be enough to reconstruct a
5
+ * transaction's full lifecycle, so that "the user says they paid but has no
6
+ * entitlement" can be investigated from logs without a reproduction.
7
+ *
8
+ * Forbidden in every payload, without exception (FR-076, FR-074): store objects,
9
+ * raw receipts, verification payloads, backend tokens, payment details, the
10
+ * obfuscated account identifier, and the cache-scope token.
11
+ */
12
+
13
+ import { redact, type PaymentErrorCode, type PaymentOperation } from './errors';
14
+ import type {
15
+ EntitlementKey,
16
+ Environment,
17
+ PendingResolution,
18
+ ProductKey,
19
+ Unsubscribe,
20
+ UnavailableReason,
21
+ PaymentStatusKind,
22
+ } from '../config/types';
23
+
24
+ export type PaymentEventType =
25
+ | 'initialization'
26
+ | 'connection_changed'
27
+ | 'products_queried'
28
+ | 'purchase_started'
29
+ | 'purchase_succeeded'
30
+ | 'purchase_cancelled'
31
+ | 'purchase_pending'
32
+ // How a pending purchase ended, once per transaction. `settled` precedes the
33
+ // `entitlement_granted` it leads to (FR-102), added in 0.2.0.
34
+ | 'purchase_pending_resolved'
35
+ | 'purchase_failed'
36
+ | 'transaction_finalized'
37
+ | 'transaction_consumed'
38
+ | 'finalize_retry_scheduled'
39
+ | 'sync_completed'
40
+ | 'entitlement_granted'
41
+ | 'entitlement_revoked'
42
+ | 'validation_decided'
43
+ | 'validation_unavailable'
44
+ | 'environment_blocked'
45
+ | 'config_warning'
46
+ | 'internal_error';
47
+
48
+ interface BaseEvent {
49
+ timestamp: number;
50
+ /** Ties one purchase cycle together end to end. Absent for lifecycle events. */
51
+ correlationId?: string;
52
+ }
53
+
54
+ export type SyncTrigger = 'initialize' | 'foreground' | 'manual' | 'already_owned';
55
+
56
+ export type PaymentEvent = BaseEvent &
57
+ (
58
+ | {
59
+ type: 'initialization';
60
+ status: PaymentStatusKind;
61
+ unavailableReason?: UnavailableReason;
62
+ retryable?: boolean;
63
+ }
64
+ | {
65
+ type: 'connection_changed';
66
+ connected: boolean;
67
+ reason?: string;
68
+ attempt?: number;
69
+ }
70
+ | {
71
+ type: 'products_queried';
72
+ productKeys: ProductKey[];
73
+ found: number;
74
+ missing: ProductKey[];
75
+ fromCache: boolean;
76
+ }
77
+ | { type: 'purchase_started'; productKey: ProductKey }
78
+ | { type: 'purchase_succeeded'; productKey: ProductKey }
79
+ | { type: 'purchase_cancelled'; productKey: ProductKey }
80
+ | { type: 'purchase_pending'; productKey: ProductKey }
81
+ | {
82
+ type: 'purchase_pending_resolved';
83
+ productKey: ProductKey;
84
+ outcome: PendingResolution;
85
+ }
86
+ | {
87
+ type: 'purchase_failed';
88
+ productKey: ProductKey;
89
+ code: PaymentErrorCode;
90
+ retryable: boolean;
91
+ }
92
+ | {
93
+ type: 'transaction_finalized';
94
+ productKey: ProductKey;
95
+ attempts: number;
96
+ }
97
+ | { type: 'transaction_consumed'; productKey: ProductKey }
98
+ | {
99
+ type: 'finalize_retry_scheduled';
100
+ productKey: ProductKey;
101
+ attempt: number;
102
+ delayMs: number;
103
+ }
104
+ | {
105
+ type: 'sync_completed';
106
+ restoredCount: number;
107
+ revokedCount: number;
108
+ trigger: SyncTrigger;
109
+ }
110
+ | {
111
+ type: 'entitlement_granted';
112
+ entitlementKey: EntitlementKey;
113
+ sources: ProductKey[];
114
+ }
115
+ | {
116
+ type: 'entitlement_revoked';
117
+ entitlementKey: EntitlementKey;
118
+ reason: 'expired' | 'revoked_by_store' | 'validation_rejected' | 'consumed';
119
+ }
120
+ | {
121
+ type: 'validation_decided';
122
+ productKey: ProductKey;
123
+ decision: 'approved' | 'rejected';
124
+ }
125
+ | {
126
+ type: 'validation_unavailable';
127
+ productKey: ProductKey;
128
+ fallback: 'grant_and_queue' | 'withhold';
129
+ }
130
+ | {
131
+ type: 'environment_blocked';
132
+ productKey: ProductKey;
133
+ environment: Environment;
134
+ }
135
+ | { type: 'config_warning'; detail: string }
136
+ | {
137
+ type: 'internal_error';
138
+ code: PaymentErrorCode;
139
+ operation: PaymentOperation;
140
+ }
141
+ );
142
+
143
+ export type PaymentEventListener = (event: PaymentEvent) => void;
144
+
145
+ /**
146
+ * A `PaymentEvent` with the timestamp left to the emitter.
147
+ *
148
+ * Distributive on purpose: a plain `Omit<PaymentEvent, 'timestamp'>` collapses the
149
+ * discriminated union into one object type with only the shared keys, which would
150
+ * reject every variant-specific field. `T extends unknown` forces the mapping to
151
+ * apply per member and keeps the union discriminated.
152
+ */
153
+ type WithOptionalTimestamp<T> = T extends unknown
154
+ ? Omit<T, 'timestamp'> & { timestamp?: number }
155
+ : never;
156
+
157
+ export type PaymentEventInput = WithOptionalTimestamp<PaymentEvent>;
158
+
159
+ export interface PaymentEmitter {
160
+ emit(event: PaymentEvent): void;
161
+ subscribe(listener: PaymentEventListener): Unsubscribe;
162
+ /** Disarms permanently. After this nothing is emitted (FR-082). */
163
+ dispose(): void;
164
+ listenerCount(): number;
165
+ disposed(): boolean;
166
+ }
167
+
168
+ /**
169
+ * Creates the emitter.
170
+ *
171
+ * Two properties are deliberate:
172
+ *
173
+ * 1. **A throwing listener cannot break anything** (FR-081). Each listener is
174
+ * invoked defensively, the throw is handed to `onListenerError`, and delivery
175
+ * continues to the remaining listeners. One app's broken analytics callback
176
+ * must not stop the package granting an entitlement or stop another listener
177
+ * hearing about it.
178
+ *
179
+ * The failure is **not** re-emitted as an event, deliberately. A consumer's own
180
+ * bug is not a payment-lifecycle event, and reporting it through the channel
181
+ * that just failed would both invite recursion and double event traffic for
182
+ * every event while that listener stays broken. It goes to the logger instead.
183
+ *
184
+ * 2. **After `dispose`, nothing is emitted** (FR-082). A store callback can
185
+ * arrive after cleanup; it must be dropped silently rather than reach a
186
+ * listener whose component is gone.
187
+ */
188
+ export function createEmitter(
189
+ onListenerError?: (error: unknown, event: PaymentEvent) => void,
190
+ ): PaymentEmitter {
191
+ const listeners = new Set<PaymentEventListener>();
192
+ let isDisposed = false;
193
+
194
+ const emit = (event: PaymentEvent): void => {
195
+ if (isDisposed) {
196
+ return;
197
+ }
198
+ // Snapshot first: a listener that subscribes or unsubscribes during
199
+ // delivery must not mutate the set being iterated.
200
+ for (const listener of [...listeners]) {
201
+ try {
202
+ listener(event);
203
+ } catch (error) {
204
+ try {
205
+ onListenerError?.(error, event);
206
+ } catch {
207
+ // Even the error reporter is not trusted to behave.
208
+ }
209
+ }
210
+ }
211
+ };
212
+
213
+ return {
214
+ emit,
215
+ subscribe(listener: PaymentEventListener): Unsubscribe {
216
+ if (isDisposed) {
217
+ return () => undefined;
218
+ }
219
+ listeners.add(listener);
220
+ let removed = false;
221
+ return () => {
222
+ if (removed) {
223
+ return;
224
+ }
225
+ removed = true;
226
+ listeners.delete(listener);
227
+ };
228
+ },
229
+ dispose(): void {
230
+ isDisposed = true;
231
+ listeners.clear();
232
+ },
233
+ listenerCount: () => listeners.size,
234
+ disposed: () => isDisposed,
235
+ };
236
+ }
237
+
238
+ /**
239
+ * Fields that must never appear in an event payload.
240
+ *
241
+ * Used by `__tests__/redaction.test.ts` to walk every emitted event across the
242
+ * whole suite. Enumerating the ban here rather than in the test keeps the rule
243
+ * next to the type it constrains.
244
+ */
245
+ export const FORBIDDEN_EVENT_FIELDS: readonly string[] = [
246
+ 'verificationPayload',
247
+ 'receipt',
248
+ 'purchaseToken',
249
+ 'transactionReceipt',
250
+ 'obfuscatedAccountId',
251
+ 'scopeToken',
252
+ 'token',
253
+ 'signature',
254
+ ];
255
+
256
+ /** Correlation ids tie one purchase cycle together; no user data goes into them. */
257
+ export function createCorrelationId(
258
+ counter: { next: number },
259
+ prefix = 'pc',
260
+ ): string {
261
+ const id = `${prefix}_${counter.next.toString(36)}`;
262
+ counter.next += 1;
263
+ return id;
264
+ }
265
+
266
+ export { redact };