@chipmobilesdk/rn-payment 0.0.0-stage → 0.1.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 +112 -0
- package/README.md +760 -2
- package/package.json +54 -6
- package/skills/sdk-rn-payment/SKILL.md +137 -0
- package/src/adapter/createStoreProvider.ts +117 -0
- package/src/adapter/iap-adapter.ts +516 -0
- package/src/adapter/port.ts +186 -0
- package/src/config/types.ts +255 -0
- package/src/config/validate.ts +232 -0
- package/src/core/mutex.ts +128 -0
- package/src/core/retry.ts +133 -0
- package/src/core/timers.ts +249 -0
- package/src/diagnostics/errors.ts +110 -0
- package/src/diagnostics/events.ts +257 -0
- package/src/diagnostics/messageKeys.ts +95 -0
- package/src/engine/engine.ts +854 -0
- package/src/engine/finalize.ts +298 -0
- package/src/engine/products.ts +259 -0
- package/src/engine/purchase.ts +495 -0
- package/src/engine/subscriptions.ts +157 -0
- package/src/entitlements/aggregate.ts +225 -0
- package/src/entitlements/mapping.ts +67 -0
- package/src/entitlements/policy.ts +129 -0
- package/src/index.ts +168 -0
- package/src/persistence/mmkv-store.ts +150 -0
- package/src/persistence/snapshot.ts +247 -0
- package/src/react/PaymentProvider.tsx +79 -0
- package/src/react/hooks.ts +238 -0
- package/src/testing/fakeStore.ts +341 -0
- package/src/testing/index.ts +10 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fake store — a full `ProviderPort` implementation for automated tests.
|
|
3
|
+
*
|
|
4
|
+
* Lives behind `@chipmobilesdk/rn-payment/testing` so test doubles cannot reach a
|
|
5
|
+
* production bundle.
|
|
6
|
+
*
|
|
7
|
+
* Nearly every requirement in this package concerns a rare ordering or failure
|
|
8
|
+
* path — a pending purchase settling two sessions later, an app killed between
|
|
9
|
+
* payment and acknowledgement, a billing connection dropping mid-sync. None of
|
|
10
|
+
* those are reachable on a real device on demand, so this is what makes them
|
|
11
|
+
* testable at all (NFR-007, SC-008).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
ProviderCapabilities,
|
|
16
|
+
ProviderConnectOutcome,
|
|
17
|
+
ProviderFailureReason,
|
|
18
|
+
ProviderPort,
|
|
19
|
+
ProviderPriceChangeOutcome,
|
|
20
|
+
ProviderProduct,
|
|
21
|
+
ProviderPurchase,
|
|
22
|
+
ProviderPurchaseOutcome,
|
|
23
|
+
ProviderPurchaseRequest,
|
|
24
|
+
ProviderToken,
|
|
25
|
+
StoreProductId,
|
|
26
|
+
} from '../adapter/port';
|
|
27
|
+
import type { ProductType, Unsubscribe } from '../config/types';
|
|
28
|
+
|
|
29
|
+
export interface FakeStoreOptions {
|
|
30
|
+
/** Fails `connect` until `setConnectOutcome` says otherwise. */
|
|
31
|
+
connectOutcome?: ProviderConnectOutcome;
|
|
32
|
+
capabilities?: Partial<ProviderCapabilities>;
|
|
33
|
+
products?: ProviderProduct[];
|
|
34
|
+
owned?: ProviderPurchase[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FakeStore extends ProviderPort {
|
|
38
|
+
// --- Scripting the next purchase -------------------------------------------
|
|
39
|
+
/** Next `launchPurchase` succeeds and yields this purchase. */
|
|
40
|
+
scriptPurchaseSuccess(overrides?: Partial<ProviderPurchase>): void;
|
|
41
|
+
/** Next `launchPurchase` fails with this reason — cancellation included (FR-032). */
|
|
42
|
+
scriptPurchaseFailure(reason: ProviderFailureReason): void;
|
|
43
|
+
/** Next `launchPurchase` returns a purchase in the pending state (FR-037). */
|
|
44
|
+
scriptPurchasePending(overrides?: Partial<ProviderPurchase>): void;
|
|
45
|
+
|
|
46
|
+
// --- Store-side state ------------------------------------------------------
|
|
47
|
+
setOwned(purchases: ProviderPurchase[]): void;
|
|
48
|
+
/** Push a purchase through `observePurchases`, as a settling pending one does. */
|
|
49
|
+
pushPurchaseUpdate(purchase: ProviderPurchase): void;
|
|
50
|
+
/** Stop reporting a transaction as valid — a refund or revocation (FR-064). */
|
|
51
|
+
revoke(transactionId: string): void;
|
|
52
|
+
setProducts(products: ProviderProduct[]): void;
|
|
53
|
+
|
|
54
|
+
// --- Connection ------------------------------------------------------------
|
|
55
|
+
setConnectOutcome(outcome: ProviderConnectOutcome): void;
|
|
56
|
+
/** Drop the connection, as the billing service does (FR-020). */
|
|
57
|
+
dropConnection(reason?: string): void;
|
|
58
|
+
restoreConnection(): void;
|
|
59
|
+
|
|
60
|
+
// --- Finalization ----------------------------------------------------------
|
|
61
|
+
/** Make `acknowledge`/`consume` fail, to exercise bounded retry (FR-045). */
|
|
62
|
+
setFinalizeFailure(failing: boolean): void;
|
|
63
|
+
|
|
64
|
+
// --- Inspection ------------------------------------------------------------
|
|
65
|
+
acknowledged(): ProviderToken[];
|
|
66
|
+
consumed(): ProviderToken[];
|
|
67
|
+
connectCount(): number;
|
|
68
|
+
launchCount(): number;
|
|
69
|
+
queryOwnedCount(): number;
|
|
70
|
+
disconnectCount(): number;
|
|
71
|
+
subscriptionManagementOpenedFor(): (StoreProductId | undefined)[];
|
|
72
|
+
reset(): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DEFAULT_CAPABILITIES: ProviderCapabilities = {
|
|
76
|
+
supportsSubscriptions: true,
|
|
77
|
+
supportsPlanChange: true,
|
|
78
|
+
supportsPriceChangeConfirmation: true,
|
|
79
|
+
// Matches Android reality: grace and hold are not separable client-side.
|
|
80
|
+
reportsGraceAndHoldDistinctly: false,
|
|
81
|
+
supportsExternalTransactions: false,
|
|
82
|
+
// The whole point of the fake: it moves no money, so the environment guard
|
|
83
|
+
// lets a test complete a purchase while still blocking the real adapter
|
|
84
|
+
// outside production (FR-014).
|
|
85
|
+
createsRealTransactions: false,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
let sequence = 0;
|
|
89
|
+
|
|
90
|
+
/** A purchase with sensible defaults, so tests state only what they care about. */
|
|
91
|
+
export function makePurchase(
|
|
92
|
+
overrides: Partial<ProviderPurchase> = {},
|
|
93
|
+
): ProviderPurchase {
|
|
94
|
+
sequence += 1;
|
|
95
|
+
return {
|
|
96
|
+
storeProductId: overrides.storeProductId ?? 'store.product.default',
|
|
97
|
+
transactionId: overrides.transactionId ?? `txn_${sequence}`,
|
|
98
|
+
token: overrides.token ?? `token_${sequence}`,
|
|
99
|
+
state: overrides.state ?? 'purchased',
|
|
100
|
+
purchasedAt: overrides.purchasedAt ?? 1_700_000_000_000,
|
|
101
|
+
isAcknowledged: overrides.isAcknowledged ?? false,
|
|
102
|
+
expiresAt: overrides.expiresAt,
|
|
103
|
+
willAutoRenew: overrides.willAutoRenew,
|
|
104
|
+
verificationPayload: overrides.verificationPayload ?? 'fake-verification',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function createFakeStore(options: FakeStoreOptions = {}): FakeStore {
|
|
109
|
+
let connectOutcome: ProviderConnectOutcome = options.connectOutcome ?? {
|
|
110
|
+
ok: true,
|
|
111
|
+
};
|
|
112
|
+
let capabilities: ProviderCapabilities = {
|
|
113
|
+
...DEFAULT_CAPABILITIES,
|
|
114
|
+
...options.capabilities,
|
|
115
|
+
};
|
|
116
|
+
let products = [...(options.products ?? [])];
|
|
117
|
+
let owned = [...(options.owned ?? [])];
|
|
118
|
+
|
|
119
|
+
let scripted:
|
|
120
|
+
| { kind: 'success' | 'pending'; overrides?: Partial<ProviderPurchase> }
|
|
121
|
+
| { kind: 'failure'; reason: ProviderFailureReason }
|
|
122
|
+
| undefined;
|
|
123
|
+
|
|
124
|
+
let finalizeFails = false;
|
|
125
|
+
let connected = false;
|
|
126
|
+
|
|
127
|
+
const acknowledgedTokens: ProviderToken[] = [];
|
|
128
|
+
const consumedTokens: ProviderToken[] = [];
|
|
129
|
+
const managementOpenedFor: (StoreProductId | undefined)[] = [];
|
|
130
|
+
const connectionListeners = new Set<
|
|
131
|
+
(connected: boolean, reason?: string) => void
|
|
132
|
+
>();
|
|
133
|
+
const purchaseListeners = new Set<(purchase: ProviderPurchase) => void>();
|
|
134
|
+
|
|
135
|
+
let connectCalls = 0;
|
|
136
|
+
let launchCalls = 0;
|
|
137
|
+
let queryOwnedCalls = 0;
|
|
138
|
+
let disconnectCalls = 0;
|
|
139
|
+
|
|
140
|
+
const notifyConnection = (next: boolean, reason?: string): void => {
|
|
141
|
+
connected = next;
|
|
142
|
+
for (const listener of [...connectionListeners]) {
|
|
143
|
+
listener(next, reason);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
// --- ProviderPort ------------------------------------------------------
|
|
149
|
+
async connect(): Promise<ProviderConnectOutcome> {
|
|
150
|
+
connectCalls += 1;
|
|
151
|
+
if (connectOutcome.ok) {
|
|
152
|
+
notifyConnection(true);
|
|
153
|
+
}
|
|
154
|
+
return connectOutcome;
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
async disconnect(): Promise<void> {
|
|
158
|
+
disconnectCalls += 1;
|
|
159
|
+
if (connected) {
|
|
160
|
+
notifyConnection(false, 'disconnect');
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
observeConnection(listener): Unsubscribe {
|
|
165
|
+
connectionListeners.add(listener);
|
|
166
|
+
let removed = false;
|
|
167
|
+
return () => {
|
|
168
|
+
if (removed) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
removed = true;
|
|
172
|
+
connectionListeners.delete(listener);
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
async queryProducts(
|
|
177
|
+
storeProductIds: StoreProductId[],
|
|
178
|
+
productType: ProductType,
|
|
179
|
+
): Promise<ProviderProduct[]> {
|
|
180
|
+
return products.filter(
|
|
181
|
+
p =>
|
|
182
|
+
storeProductIds.includes(p.storeProductId) &&
|
|
183
|
+
p.productType === productType,
|
|
184
|
+
);
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
async queryOwned(): Promise<ProviderPurchase[]> {
|
|
188
|
+
queryOwnedCalls += 1;
|
|
189
|
+
// Pending purchases are returned flagged, never filtered out: the engine
|
|
190
|
+
// must see them to raise the pending signal while never granting.
|
|
191
|
+
return [...owned];
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
async launchPurchase(
|
|
195
|
+
request: ProviderPurchaseRequest,
|
|
196
|
+
): Promise<ProviderPurchaseOutcome> {
|
|
197
|
+
launchCalls += 1;
|
|
198
|
+
const script = scripted;
|
|
199
|
+
scripted = undefined;
|
|
200
|
+
|
|
201
|
+
if (!script) {
|
|
202
|
+
return { ok: false, reason: 'store_error', detail: 'no script set' };
|
|
203
|
+
}
|
|
204
|
+
if (script.kind === 'failure') {
|
|
205
|
+
return { ok: false, reason: script.reason };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const purchase = makePurchase({
|
|
209
|
+
storeProductId: request.storeProductId,
|
|
210
|
+
state: script.kind === 'pending' ? 'pending' : 'purchased',
|
|
211
|
+
...script.overrides,
|
|
212
|
+
});
|
|
213
|
+
owned = [...owned.filter(p => p.transactionId !== purchase.transactionId), purchase];
|
|
214
|
+
return { ok: true, purchase };
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
observePurchases(listener): Unsubscribe {
|
|
218
|
+
purchaseListeners.add(listener);
|
|
219
|
+
let removed = false;
|
|
220
|
+
return () => {
|
|
221
|
+
if (removed) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
removed = true;
|
|
225
|
+
purchaseListeners.delete(listener);
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
async acknowledge(token: ProviderToken): Promise<void> {
|
|
230
|
+
if (finalizeFails) {
|
|
231
|
+
throw new Error('fake acknowledge failure');
|
|
232
|
+
}
|
|
233
|
+
// Safe to call for an already-acknowledged transaction (FR-044).
|
|
234
|
+
if (!acknowledgedTokens.includes(token)) {
|
|
235
|
+
acknowledgedTokens.push(token);
|
|
236
|
+
}
|
|
237
|
+
owned = owned.map(p =>
|
|
238
|
+
p.token === token ? { ...p, isAcknowledged: true } : p,
|
|
239
|
+
);
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
async consume(token: ProviderToken): Promise<void> {
|
|
243
|
+
if (finalizeFails) {
|
|
244
|
+
throw new Error('fake consume failure');
|
|
245
|
+
}
|
|
246
|
+
if (!consumedTokens.includes(token)) {
|
|
247
|
+
consumedTokens.push(token);
|
|
248
|
+
}
|
|
249
|
+
// A consumed purchase stops being owned, which is what lets the same
|
|
250
|
+
// product be bought again (FR-043).
|
|
251
|
+
owned = owned.filter(p => p.token !== token);
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
async openSubscriptionManagement(
|
|
255
|
+
storeProductId?: StoreProductId,
|
|
256
|
+
): Promise<void> {
|
|
257
|
+
managementOpenedFor.push(storeProductId);
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
async confirmPriceChange(): Promise<ProviderPriceChangeOutcome> {
|
|
261
|
+
return { ok: true, confirmed: true };
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
capabilities: () => capabilities,
|
|
265
|
+
|
|
266
|
+
// --- Scripting ---------------------------------------------------------
|
|
267
|
+
scriptPurchaseSuccess(overrides) {
|
|
268
|
+
scripted = { kind: 'success', overrides };
|
|
269
|
+
},
|
|
270
|
+
scriptPurchaseFailure(reason) {
|
|
271
|
+
scripted = { kind: 'failure', reason };
|
|
272
|
+
},
|
|
273
|
+
scriptPurchasePending(overrides) {
|
|
274
|
+
scripted = { kind: 'pending', overrides };
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
// --- Store state -------------------------------------------------------
|
|
278
|
+
setOwned(purchases) {
|
|
279
|
+
owned = [...purchases];
|
|
280
|
+
},
|
|
281
|
+
pushPurchaseUpdate(purchase) {
|
|
282
|
+
owned = [
|
|
283
|
+
...owned.filter(p => p.transactionId !== purchase.transactionId),
|
|
284
|
+
purchase,
|
|
285
|
+
];
|
|
286
|
+
for (const listener of [...purchaseListeners]) {
|
|
287
|
+
listener(purchase);
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
revoke(transactionId) {
|
|
291
|
+
owned = owned.filter(p => p.transactionId !== transactionId);
|
|
292
|
+
},
|
|
293
|
+
setProducts(next) {
|
|
294
|
+
products = [...next];
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
// --- Connection --------------------------------------------------------
|
|
298
|
+
setConnectOutcome(outcome) {
|
|
299
|
+
connectOutcome = outcome;
|
|
300
|
+
},
|
|
301
|
+
dropConnection(reason = 'service_disconnected') {
|
|
302
|
+
notifyConnection(false, reason);
|
|
303
|
+
},
|
|
304
|
+
restoreConnection() {
|
|
305
|
+
notifyConnection(true);
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
// --- Finalization ------------------------------------------------------
|
|
309
|
+
setFinalizeFailure(failing) {
|
|
310
|
+
finalizeFails = failing;
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
// --- Inspection --------------------------------------------------------
|
|
314
|
+
acknowledged: () => [...acknowledgedTokens],
|
|
315
|
+
consumed: () => [...consumedTokens],
|
|
316
|
+
connectCount: () => connectCalls,
|
|
317
|
+
launchCount: () => launchCalls,
|
|
318
|
+
queryOwnedCount: () => queryOwnedCalls,
|
|
319
|
+
disconnectCount: () => disconnectCalls,
|
|
320
|
+
subscriptionManagementOpenedFor: () => [...managementOpenedFor],
|
|
321
|
+
|
|
322
|
+
reset() {
|
|
323
|
+
connectOutcome = { ok: true };
|
|
324
|
+
capabilities = { ...DEFAULT_CAPABILITIES, ...options.capabilities };
|
|
325
|
+
products = [];
|
|
326
|
+
owned = [];
|
|
327
|
+
scripted = undefined;
|
|
328
|
+
finalizeFails = false;
|
|
329
|
+
connected = false;
|
|
330
|
+
acknowledgedTokens.length = 0;
|
|
331
|
+
consumedTokens.length = 0;
|
|
332
|
+
managementOpenedFor.length = 0;
|
|
333
|
+
connectionListeners.clear();
|
|
334
|
+
purchaseListeners.clear();
|
|
335
|
+
connectCalls = 0;
|
|
336
|
+
launchCalls = 0;
|
|
337
|
+
queryOwnedCalls = 0;
|
|
338
|
+
disconnectCalls = 0;
|
|
339
|
+
},
|
|
340
|
+
};
|
|
341
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@chipmobilesdk/rn-payment/testing` — test doubles.
|
|
3
|
+
*
|
|
4
|
+
* A separate entrypoint so the fake store cannot reach a production bundle
|
|
5
|
+
* (LIB-003). Nothing here is part of the package's public runtime contract.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { createFakeStore, makePurchase } from './fakeStore';
|
|
9
|
+
export type { FakeStore, FakeStoreOptions } from './fakeStore';
|
|
10
|
+
export { createMemorySnapshotStore } from '../persistence/mmkv-store';
|