@chipmobilesdk/rn-payment 0.4.0 → 0.5.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.
- package/CHANGELOG.md +482 -334
- package/README.md +925 -897
- package/package.json +52 -54
- package/skills/sdk-rn-payment/SKILL.md +175 -174
- package/src/adapter/createStoreProvider.ts +116 -118
- package/src/adapter/iap-adapter.ts +627 -660
- package/src/adapter/port.ts +209 -215
- package/src/config/types.ts +298 -310
- package/src/config/validate.ts +224 -232
- package/src/core/mutex.ts +25 -128
- package/src/core/retry.ts +95 -133
- package/src/core/timers.ts +272 -267
- package/src/diagnostics/errors.ts +124 -117
- package/src/diagnostics/events.ts +282 -266
- package/src/diagnostics/messageKeys.ts +93 -95
- package/src/engine/engine.ts +921 -999
- package/src/engine/finalize.ts +343 -346
- package/src/engine/foreground.ts +29 -0
- package/src/engine/products.ts +259 -259
- package/src/engine/purchase.ts +589 -614
- package/src/engine/subscriptions.ts +146 -157
- package/src/entitlements/aggregate.ts +261 -242
- package/src/entitlements/mapping.ts +67 -67
- package/src/entitlements/policy.ts +121 -129
- package/src/index.ts +5 -9
- package/src/persistence/mmkv-store.ts +158 -158
- package/src/persistence/snapshot.ts +220 -247
- package/src/react/PaymentProvider.tsx +90 -79
- package/src/react/hooks.ts +236 -274
- package/src/testing/conformance.ts +169 -0
- package/src/testing/fakeStore.ts +360 -393
- package/src/testing/index.ts +19 -10
package/src/engine/finalize.ts
CHANGED
|
@@ -1,346 +1,343 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The finalization queue — the package's hardest invariant (FR-041, FR-042).
|
|
3
|
-
*
|
|
4
|
-
* Google auto-refunds and revokes a purchase that is not acknowledged within
|
|
5
|
-
* **three days**. So finalization cannot be a step the app might forget, and it
|
|
6
|
-
* cannot live only in memory: the failure case FR-042 names is the app being
|
|
7
|
-
* force-killed in the instant between payment and acknowledgement.
|
|
8
|
-
*
|
|
9
|
-
* The ordering that makes this work is: **write the intent, then call the store.**
|
|
10
|
-
* A crash before the write means the purchase is still owned and the next sync
|
|
11
|
-
* re-queues it from `queryOwned`. A crash after the write means the queue has a
|
|
12
|
-
* record and the next startup drains it. There is no window where a paid
|
|
13
|
-
* transaction is forgotten.
|
|
14
|
-
*
|
|
15
|
-
* Consumption stays app-triggered (FR-043), because only the app knows whether
|
|
16
|
-
* the item was actually banked in its own data.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import type {
|
|
20
|
-
FinalizeResult,
|
|
21
|
-
ProductKey,
|
|
22
|
-
TransactionRef,
|
|
23
|
-
UnfinalizedSummary,
|
|
24
|
-
} from '../config/types';
|
|
25
|
-
import type { ProviderPort } from '../adapter/port';
|
|
26
|
-
import type { ResolvedRetry } from '../core/timers';
|
|
27
|
-
import { backoffDelayMs } from '../core/retry';
|
|
28
|
-
import { withTimeout } from '../core/mutex';
|
|
29
|
-
import { PaymentError } from '../diagnostics/errors';
|
|
30
|
-
import {
|
|
31
|
-
pendingFinalizations,
|
|
32
|
-
type PersistedPurchase,
|
|
33
|
-
type PersistedSnapshot,
|
|
34
|
-
} from '../persistence/snapshot';
|
|
35
|
-
import type { PaymentEventInput } from '../diagnostics/events';
|
|
36
|
-
|
|
37
|
-
export interface FinalizerDeps {
|
|
38
|
-
provider: ProviderPort;
|
|
39
|
-
retry: ResolvedRetry;
|
|
40
|
-
/** `timeouts.finalizeAttemptMs`: one acknowledge or consume call may take no longer. */
|
|
41
|
-
attemptTimeoutMs: number;
|
|
42
|
-
snapshot: () => PersistedSnapshot;
|
|
43
|
-
commit: (next: PersistedSnapshot) => void;
|
|
44
|
-
emit: (event: PaymentEventInput) => void;
|
|
45
|
-
now: () => number;
|
|
46
|
-
disposed: () => boolean;
|
|
47
|
-
/** Injected so a test can run the retry schedule without waiting it out. */
|
|
48
|
-
setTimeoutFn?: (fn: () => void, ms: number) => FinalizeTimerHandle;
|
|
49
|
-
clearTimeoutFn?: (handle: FinalizeTimerHandle) => void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** A Node `Timeout` and a browser numeric id are both valid here. */
|
|
53
|
-
type FinalizeTimerHandle = ReturnType<typeof setTimeout> | number;
|
|
54
|
-
|
|
55
|
-
export interface Finalizer {
|
|
56
|
-
/** Records the intent to finalize, then attempts it. Safe to call repeatedly. */
|
|
57
|
-
enqueue(transactionId: string): Promise<FinalizeResult>;
|
|
58
|
-
/** Drains every outstanding finalization. Runs at startup and on foreground. */
|
|
59
|
-
drain(): Promise<void>;
|
|
60
|
-
/** App-triggered consumption of a consumable (FR-043). */
|
|
61
|
-
consume(ref: TransactionRef): Promise<FinalizeResult>;
|
|
62
|
-
/** Paid-but-unsettled transactions, so the condition is observable (FR-045). */
|
|
63
|
-
unfinalized(): UnfinalizedSummary[];
|
|
64
|
-
/** Cancels pending retries. Idempotent, called from cleanup. */
|
|
65
|
-
dispose(): void;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function createFinalizer(deps: FinalizerDeps): Finalizer {
|
|
69
|
-
const setTimeoutFn = deps.setTimeoutFn ?? setTimeout;
|
|
70
|
-
const clearTimeoutFn = deps.clearTimeoutFn ?? clearTimeout;
|
|
71
|
-
|
|
72
|
-
/** One timer per transaction, so two transactions retry independently. */
|
|
73
|
-
const timers = new Map<string, FinalizeTimerHandle>();
|
|
74
|
-
/** Guards against two drains racing the same transaction. */
|
|
75
|
-
const inFlight = new Set<string>();
|
|
76
|
-
|
|
77
|
-
const find = (transactionId: string): PersistedPurchase | undefined =>
|
|
78
|
-
deps.snapshot().purchases.find(p => p.transactionId === transactionId);
|
|
79
|
-
|
|
80
|
-
const patch = (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return { kind: '
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (
|
|
199
|
-
return { kind: '
|
|
200
|
-
}
|
|
201
|
-
if (record.
|
|
202
|
-
return { kind: 'failed', code: '
|
|
203
|
-
}
|
|
204
|
-
if (record.state === '
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (deps.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (
|
|
300
|
-
return { kind: '
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
},
|
|
345
|
-
};
|
|
346
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The finalization queue — the package's hardest invariant (FR-041, FR-042).
|
|
3
|
+
*
|
|
4
|
+
* Google auto-refunds and revokes a purchase that is not acknowledged within
|
|
5
|
+
* **three days**. So finalization cannot be a step the app might forget, and it
|
|
6
|
+
* cannot live only in memory: the failure case FR-042 names is the app being
|
|
7
|
+
* force-killed in the instant between payment and acknowledgement.
|
|
8
|
+
*
|
|
9
|
+
* The ordering that makes this work is: **write the intent, then call the store.**
|
|
10
|
+
* A crash before the write means the purchase is still owned and the next sync
|
|
11
|
+
* re-queues it from `queryOwned`. A crash after the write means the queue has a
|
|
12
|
+
* record and the next startup drains it. There is no window where a paid
|
|
13
|
+
* transaction is forgotten.
|
|
14
|
+
*
|
|
15
|
+
* Consumption stays app-triggered (FR-043), because only the app knows whether
|
|
16
|
+
* the item was actually banked in its own data.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type {
|
|
20
|
+
FinalizeResult,
|
|
21
|
+
ProductKey,
|
|
22
|
+
TransactionRef,
|
|
23
|
+
UnfinalizedSummary,
|
|
24
|
+
} from '../config/types';
|
|
25
|
+
import type { ProviderPort } from '../adapter/port';
|
|
26
|
+
import type { ResolvedRetry } from '../core/timers';
|
|
27
|
+
import { backoffDelayMs } from '../core/retry';
|
|
28
|
+
import { withTimeout } from '../core/mutex';
|
|
29
|
+
import { PaymentError } from '../diagnostics/errors';
|
|
30
|
+
import {
|
|
31
|
+
pendingFinalizations,
|
|
32
|
+
type PersistedPurchase,
|
|
33
|
+
type PersistedSnapshot,
|
|
34
|
+
} from '../persistence/snapshot';
|
|
35
|
+
import type { PaymentEventInput } from '../diagnostics/events';
|
|
36
|
+
|
|
37
|
+
export interface FinalizerDeps {
|
|
38
|
+
provider: ProviderPort;
|
|
39
|
+
retry: ResolvedRetry;
|
|
40
|
+
/** `timeouts.finalizeAttemptMs`: one acknowledge or consume call may take no longer. */
|
|
41
|
+
attemptTimeoutMs: number;
|
|
42
|
+
snapshot: () => PersistedSnapshot;
|
|
43
|
+
commit: (next: PersistedSnapshot) => void;
|
|
44
|
+
emit: (event: PaymentEventInput) => void;
|
|
45
|
+
now: () => number;
|
|
46
|
+
disposed: () => boolean;
|
|
47
|
+
/** Injected so a test can run the retry schedule without waiting it out. */
|
|
48
|
+
setTimeoutFn?: (fn: () => void, ms: number) => FinalizeTimerHandle;
|
|
49
|
+
clearTimeoutFn?: (handle: FinalizeTimerHandle) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A Node `Timeout` and a browser numeric id are both valid here. */
|
|
53
|
+
type FinalizeTimerHandle = ReturnType<typeof setTimeout> | number;
|
|
54
|
+
|
|
55
|
+
export interface Finalizer {
|
|
56
|
+
/** Records the intent to finalize, then attempts it. Safe to call repeatedly. */
|
|
57
|
+
enqueue(transactionId: string): Promise<FinalizeResult>;
|
|
58
|
+
/** Drains every outstanding finalization. Runs at startup and on foreground. */
|
|
59
|
+
drain(): Promise<void>;
|
|
60
|
+
/** App-triggered consumption of a consumable (FR-043). */
|
|
61
|
+
consume(ref: TransactionRef): Promise<FinalizeResult>;
|
|
62
|
+
/** Paid-but-unsettled transactions, so the condition is observable (FR-045). */
|
|
63
|
+
unfinalized(): UnfinalizedSummary[];
|
|
64
|
+
/** Cancels pending retries. Idempotent, called from cleanup. */
|
|
65
|
+
dispose(): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function createFinalizer(deps: FinalizerDeps): Finalizer {
|
|
69
|
+
const setTimeoutFn = deps.setTimeoutFn ?? setTimeout;
|
|
70
|
+
const clearTimeoutFn = deps.clearTimeoutFn ?? clearTimeout;
|
|
71
|
+
|
|
72
|
+
/** One timer per transaction, so two transactions retry independently. */
|
|
73
|
+
const timers = new Map<string, FinalizeTimerHandle>();
|
|
74
|
+
/** Guards against two drains racing the same transaction. */
|
|
75
|
+
const inFlight = new Set<string>();
|
|
76
|
+
|
|
77
|
+
const find = (transactionId: string): PersistedPurchase | undefined =>
|
|
78
|
+
deps.snapshot().purchases.find(p => p.transactionId === transactionId);
|
|
79
|
+
|
|
80
|
+
const patch = (transactionId: string, changes: Partial<PersistedPurchase>): void => {
|
|
81
|
+
const snapshot = deps.snapshot();
|
|
82
|
+
deps.commit({
|
|
83
|
+
...snapshot,
|
|
84
|
+
purchases: snapshot.purchases.map(p =>
|
|
85
|
+
p.transactionId === transactionId ? { ...p, ...changes } : p,
|
|
86
|
+
),
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* One store call under `attemptTimeoutMs`. A call that never settles used to
|
|
92
|
+
* hold this transaction's in-flight guard and, through `drain`, the sync that
|
|
93
|
+
* awaited it. Timing out counts as a failed attempt, so the retry schedule and
|
|
94
|
+
* the next-session drain both still apply.
|
|
95
|
+
*/
|
|
96
|
+
const bounded = (call: Promise<void>, operation: 'finalize' | 'consume'): Promise<void> =>
|
|
97
|
+
withTimeout(
|
|
98
|
+
call,
|
|
99
|
+
deps.attemptTimeoutMs,
|
|
100
|
+
() =>
|
|
101
|
+
new PaymentError({
|
|
102
|
+
code: 'PAYMENT_TIMEOUT',
|
|
103
|
+
message: `The store did not answer the ${operation} call in time`,
|
|
104
|
+
operation,
|
|
105
|
+
retryable: true,
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const clearTimer = (transactionId: string): void => {
|
|
110
|
+
const timer = timers.get(transactionId);
|
|
111
|
+
if (timer !== undefined) {
|
|
112
|
+
clearTimeoutFn(timer);
|
|
113
|
+
timers.delete(transactionId);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Attempts one finalize call.
|
|
119
|
+
*
|
|
120
|
+
* A consumable is **not** acknowledged here: it waits for the app's explicit
|
|
121
|
+
* `consume`. Acknowledging it now would tell the store the entitlement was
|
|
122
|
+
* delivered before the app has recorded the item, and a crash in between would
|
|
123
|
+
* lose a paid item with no way to recover it.
|
|
124
|
+
*/
|
|
125
|
+
const attempt = async (transactionId: string): Promise<FinalizeResult> => {
|
|
126
|
+
if (deps.disposed()) {
|
|
127
|
+
return { kind: 'failed', code: 'PAYMENT_NOT_CONFIGURED' };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const record = find(transactionId);
|
|
131
|
+
if (!record) {
|
|
132
|
+
return { kind: 'unknown_transaction' };
|
|
133
|
+
}
|
|
134
|
+
if (record.finalizeState === 'done') {
|
|
135
|
+
// Idempotent by design: calling twice is a success, not an error (FR-044).
|
|
136
|
+
return { kind: 'already_finalized' };
|
|
137
|
+
}
|
|
138
|
+
if (record.productType === 'one_time_consumable') {
|
|
139
|
+
// The app owns this moment. Nothing to do until it calls consume().
|
|
140
|
+
return { kind: 'already_finalized' };
|
|
141
|
+
}
|
|
142
|
+
if (inFlight.has(transactionId)) {
|
|
143
|
+
return { kind: 'retry_scheduled', attempts: record.attempts };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
inFlight.add(transactionId);
|
|
147
|
+
patch(transactionId, { finalizeState: 'in_flight' });
|
|
148
|
+
|
|
149
|
+
let failed = false;
|
|
150
|
+
try {
|
|
151
|
+
await bounded(deps.provider.acknowledge(record.token), 'finalize');
|
|
152
|
+
} catch {
|
|
153
|
+
failed = true;
|
|
154
|
+
} finally {
|
|
155
|
+
// Released before scheduling the retry, deliberately. The guard exists to
|
|
156
|
+
// stop two concurrent store calls for one transaction, nothing more — if
|
|
157
|
+
// it were still held here, the next scheduled attempt would be refused by
|
|
158
|
+
// our own lock and the retry schedule would silently stall after one try.
|
|
159
|
+
inFlight.delete(transactionId);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const attempts = record.attempts + 1;
|
|
163
|
+
|
|
164
|
+
if (!failed) {
|
|
165
|
+
clearTimer(transactionId);
|
|
166
|
+
patch(transactionId, {
|
|
167
|
+
finalizeState: 'done',
|
|
168
|
+
state: 'purchased',
|
|
169
|
+
attempts,
|
|
170
|
+
});
|
|
171
|
+
deps.emit({
|
|
172
|
+
type: 'transaction_finalized',
|
|
173
|
+
productKey: record.productKey,
|
|
174
|
+
attempts,
|
|
175
|
+
});
|
|
176
|
+
return { kind: 'finalized' };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
patch(transactionId, { finalizeState: 'failed', attempts });
|
|
180
|
+
return scheduleRetry(transactionId, record.productKey, attempts, 'acknowledge');
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* One consume call for a consumable the app has banked (FR-043).
|
|
185
|
+
*
|
|
186
|
+
* Refuses anything that is not a settled consumable. Consuming a subscription
|
|
187
|
+
* or a permanent unlock would tell the store the user used it up, and a
|
|
188
|
+
* pending purchase has nothing to consume yet.
|
|
189
|
+
*/
|
|
190
|
+
const consumeAttempt = async (transactionId: string): Promise<FinalizeResult> => {
|
|
191
|
+
if (deps.disposed()) {
|
|
192
|
+
return { kind: 'failed', code: 'PAYMENT_NOT_CONFIGURED' };
|
|
193
|
+
}
|
|
194
|
+
const record = find(transactionId);
|
|
195
|
+
if (!record) {
|
|
196
|
+
return { kind: 'unknown_transaction' };
|
|
197
|
+
}
|
|
198
|
+
if (record.productType !== 'one_time_consumable') {
|
|
199
|
+
return { kind: 'failed', code: 'PAYMENT_NOT_CONSUMABLE' };
|
|
200
|
+
}
|
|
201
|
+
if (record.state === 'pending') {
|
|
202
|
+
return { kind: 'failed', code: 'PAYMENT_PENDING' };
|
|
203
|
+
}
|
|
204
|
+
if (record.state === 'consumed') {
|
|
205
|
+
// Consuming twice must grant the item once (FR-044).
|
|
206
|
+
return { kind: 'already_finalized' };
|
|
207
|
+
}
|
|
208
|
+
if (inFlight.has(transactionId)) {
|
|
209
|
+
// Concurrent consume calls collapse to one, for the same reason.
|
|
210
|
+
return { kind: 'already_finalized' };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
inFlight.add(transactionId);
|
|
214
|
+
patch(transactionId, { finalizeState: 'in_flight' });
|
|
215
|
+
|
|
216
|
+
let failed = false;
|
|
217
|
+
try {
|
|
218
|
+
await bounded(deps.provider.consume(record.token), 'consume');
|
|
219
|
+
} catch {
|
|
220
|
+
failed = true;
|
|
221
|
+
} finally {
|
|
222
|
+
// Released before scheduling, for the same reason as in `attempt`.
|
|
223
|
+
inFlight.delete(transactionId);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const attempts = record.attempts + 1;
|
|
227
|
+
|
|
228
|
+
if (failed) {
|
|
229
|
+
patch(transactionId, { finalizeState: 'failed', attempts });
|
|
230
|
+
return scheduleRetry(transactionId, record.productKey, attempts, 'consume');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
clearTimer(transactionId);
|
|
234
|
+
// `consumed` is what stops it being an entitlement source, so a
|
|
235
|
+
// consumable never becomes a permanent unlock.
|
|
236
|
+
patch(transactionId, {
|
|
237
|
+
state: 'consumed',
|
|
238
|
+
finalizeState: 'done',
|
|
239
|
+
attempts,
|
|
240
|
+
});
|
|
241
|
+
deps.emit({
|
|
242
|
+
type: 'transaction_consumed',
|
|
243
|
+
productKey: record.productKey,
|
|
244
|
+
});
|
|
245
|
+
return { kind: 'finalized' };
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Schedules the next attempt inside the per-session budget.
|
|
250
|
+
*
|
|
251
|
+
* Exhausting the budget is **not** giving up. The record stays `failed`, which
|
|
252
|
+
* `pendingFinalizations` still returns, so the next startup or foreground drain
|
|
253
|
+
* picks it up again. That is what keeps the package inside Play's three-day
|
|
254
|
+
* window without hammering a store that is currently refusing.
|
|
255
|
+
*/
|
|
256
|
+
const scheduleRetry = (
|
|
257
|
+
transactionId: string,
|
|
258
|
+
productKey: ProductKey,
|
|
259
|
+
attempts: number,
|
|
260
|
+
// The timer must repeat the call that failed. Retrying a failed consume
|
|
261
|
+
// through `attempt` was a no-op — `attempt` skips consumables — while the
|
|
262
|
+
// event still reported a retry as scheduled.
|
|
263
|
+
operation: 'acknowledge' | 'consume',
|
|
264
|
+
): FinalizeResult => {
|
|
265
|
+
if (deps.disposed()) {
|
|
266
|
+
return { kind: 'failed', code: 'PAYMENT_FINALIZE_FAILED' };
|
|
267
|
+
}
|
|
268
|
+
if (attempts >= deps.retry.finalize.maxAttempts) {
|
|
269
|
+
// Left queued deliberately — resumed next session, never abandoned.
|
|
270
|
+
return { kind: 'failed', code: 'PAYMENT_FINALIZE_FAILED' };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const delayMs = backoffDelayMs(attempts + 1, deps.retry.finalize);
|
|
274
|
+
clearTimer(transactionId);
|
|
275
|
+
timers.set(
|
|
276
|
+
transactionId,
|
|
277
|
+
setTimeoutFn(() => {
|
|
278
|
+
timers.delete(transactionId);
|
|
279
|
+
const retry = operation === 'consume' ? consumeAttempt : attempt;
|
|
280
|
+
retry(transactionId).catch(() => undefined);
|
|
281
|
+
}, delayMs),
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
deps.emit({
|
|
285
|
+
type: 'finalize_retry_scheduled',
|
|
286
|
+
productKey,
|
|
287
|
+
attempt: attempts + 1,
|
|
288
|
+
delayMs,
|
|
289
|
+
});
|
|
290
|
+
return { kind: 'retry_scheduled', attempts };
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
async enqueue(transactionId: string): Promise<FinalizeResult> {
|
|
295
|
+
const record = find(transactionId);
|
|
296
|
+
if (!record) {
|
|
297
|
+
return { kind: 'unknown_transaction' };
|
|
298
|
+
}
|
|
299
|
+
if (record.finalizeState === 'done') {
|
|
300
|
+
return { kind: 'already_finalized' };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// The write happens BEFORE the store call. This single ordering is what
|
|
304
|
+
// makes a kill between payment and acknowledgement recoverable.
|
|
305
|
+
if (record.finalizeState !== 'in_flight') {
|
|
306
|
+
patch(transactionId, { finalizeState: 'queued' });
|
|
307
|
+
}
|
|
308
|
+
return attempt(transactionId);
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
async drain(): Promise<void> {
|
|
312
|
+
// Runs regardless of whether a purchase happened this session: the record
|
|
313
|
+
// it is looking for was probably written by a session that died.
|
|
314
|
+
for (const record of pendingFinalizations(deps.snapshot())) {
|
|
315
|
+
if (deps.disposed()) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
await attempt(record.transactionId).catch(() => undefined);
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
consume(ref: TransactionRef): Promise<FinalizeResult> {
|
|
323
|
+
return consumeAttempt(ref.transactionId);
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
unfinalized(): UnfinalizedSummary[] {
|
|
327
|
+
return pendingFinalizations(deps.snapshot()).map(p => ({
|
|
328
|
+
productKey: p.productKey,
|
|
329
|
+
transactionId: p.transactionId,
|
|
330
|
+
attempts: p.attempts,
|
|
331
|
+
purchasedAt: p.purchasedAt,
|
|
332
|
+
}));
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
dispose(): void {
|
|
336
|
+
for (const timer of timers.values()) {
|
|
337
|
+
clearTimeoutFn(timer);
|
|
338
|
+
}
|
|
339
|
+
timers.clear();
|
|
340
|
+
inFlight.clear();
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|