@chipmobilesdk/rn-payment 0.1.0 → 0.2.1
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 +113 -1
- package/README.md +885 -761
- package/package.json +54 -54
- package/skills/sdk-rn-payment/SKILL.md +33 -6
- package/src/adapter/createStoreProvider.ts +117 -117
- package/src/adapter/iap-adapter.ts +547 -516
- package/src/adapter/port.ts +12 -0
- package/src/config/types.ts +40 -0
- package/src/diagnostics/events.ts +9 -0
- package/src/engine/engine.ts +951 -854
- package/src/engine/finalize.ts +298 -298
- package/src/engine/pending.ts +118 -0
- package/src/engine/products.ts +259 -259
- package/src/engine/purchase.ts +526 -495
- package/src/engine/subscriptions.ts +157 -157
- package/src/entitlements/aggregate.ts +21 -4
- package/src/index.ts +4 -0
- package/src/persistence/mmkv-store.ts +16 -8
- package/src/react/hooks.ts +274 -238
- package/src/testing/fakeStore.ts +33 -0
|
@@ -1,516 +1,547 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Android adapter over `react-native-iap` (Play Billing).
|
|
3
|
-
*
|
|
4
|
-
* **The only file in this package that touches the billing library.** Everything
|
|
5
|
-
* above it talks to `ProviderPort`, which is what keeps the binding replaceable
|
|
6
|
-
* and lets an iOS adapter be added without changing a consumer call (FR-002,
|
|
7
|
-
* FR-007). ESLint enforces that boundary.
|
|
8
|
-
*
|
|
9
|
-
* ## Verification status
|
|
10
|
-
*
|
|
11
|
-
* The binding is **not yet installed** — the root `react-native-nitro-modules`
|
|
12
|
-
* bump it requires is held pending approval (see specs/013-rn-payment-sdk/plan.md,
|
|
13
|
-
* Post-Implementation Notes). So the call shapes below are written against
|
|
14
|
-
* `react-native-iap` 16.x as documented, and **every one must be verified against
|
|
15
|
-
* the installed package** before this adapter is trusted on a device. The module
|
|
16
|
-
* is required lazily and the surface it needs is declared explicitly in
|
|
17
|
-
* `IapModule`, so a mismatch surfaces here rather than deep in the engine.
|
|
18
|
-
*
|
|
19
|
-
* No test exercises this file: the whole suite runs against the fake store, and
|
|
20
|
-
* the IAP test double throws on every call precisely so that reaching this code in
|
|
21
|
-
* a test fails loudly instead of passing against a stub.
|
|
22
|
-
*
|
|
23
|
-
* ## Known upstream defect this guards against
|
|
24
|
-
*
|
|
25
|
-
* openiap#408: the Android bridge did not clear its internal `isInitialized` flag
|
|
26
|
-
* on disconnect, so `connected` stayed true, later calls failed with
|
|
27
|
-
* `E_NOT_PREPARED`, and `reconnect()` reported success without rebuilding
|
|
28
|
-
* anything. This adapter therefore (a) tracks connection state itself rather than
|
|
29
|
-
* trusting the binding's flag, and (b) always performs a full
|
|
30
|
-
* `endConnection` + `initConnection` cycle instead of calling any reconnect
|
|
31
|
-
* shortcut.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
import type { ProductType, Unsubscribe } from '../config/types';
|
|
35
|
-
import type {
|
|
36
|
-
ProviderCapabilities,
|
|
37
|
-
ProviderConnectOutcome,
|
|
38
|
-
ProviderFailureReason,
|
|
39
|
-
ProviderPort,
|
|
40
|
-
ProviderPriceChangeOutcome,
|
|
41
|
-
ProviderProduct,
|
|
42
|
-
ProviderPurchase,
|
|
43
|
-
ProviderPurchaseOutcome,
|
|
44
|
-
ProviderPurchaseRequest,
|
|
45
|
-
ProviderToken,
|
|
46
|
-
StoreProductId,
|
|
47
|
-
} from './port';
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Exactly what this adapter needs from the binding.
|
|
51
|
-
*
|
|
52
|
-
* Declared narrowly on purpose: it documents the coupling, and it means a binding
|
|
53
|
-
* upgrade that changes one of these shapes fails to typecheck here instead of
|
|
54
|
-
* failing silently at runtime on a user's device.
|
|
55
|
-
*/
|
|
56
|
-
interface IapModule {
|
|
57
|
-
initConnection(): Promise<boolean>;
|
|
58
|
-
endConnection(): Promise<void>;
|
|
59
|
-
fetchProducts(params: {
|
|
60
|
-
skus: string[];
|
|
61
|
-
type: 'in-app' | 'subs';
|
|
62
|
-
}): Promise<RawProduct[]>;
|
|
63
|
-
getAvailablePurchases(): Promise<RawPurchase[]>;
|
|
64
|
-
requestPurchase(params: unknown): Promise<unknown>;
|
|
65
|
-
finishTransaction(params: {
|
|
66
|
-
purchase: unknown;
|
|
67
|
-
isConsumable?: boolean;
|
|
68
|
-
}): Promise<unknown>;
|
|
69
|
-
purchaseUpdatedListener(
|
|
70
|
-
listener: (purchase: RawPurchase) => void,
|
|
71
|
-
): { remove(): void };
|
|
72
|
-
purchaseErrorListener(
|
|
73
|
-
listener: (error: { code?: string; message?: string }) => void,
|
|
74
|
-
): { remove(): void };
|
|
75
|
-
deepLinkToSubscriptions?(params: { skuAndroid?: string }): Promise<void>;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
interface RawProduct {
|
|
79
|
-
id?: string;
|
|
80
|
-
productId?: string;
|
|
81
|
-
title?: string;
|
|
82
|
-
name?: string;
|
|
83
|
-
description?: string;
|
|
84
|
-
displayPrice?: string;
|
|
85
|
-
localizedPrice?: string;
|
|
86
|
-
currency?: string;
|
|
87
|
-
currencyCode?: string;
|
|
88
|
-
price?: string | number;
|
|
89
|
-
oneTimePurchaseOfferDetails?: {
|
|
90
|
-
formattedPrice?: string;
|
|
91
|
-
priceAmountMicros?: string | number;
|
|
92
|
-
priceCurrencyCode?: string;
|
|
93
|
-
};
|
|
94
|
-
subscriptionOfferDetails?: RawSubscriptionOffer[];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
interface RawSubscriptionOffer {
|
|
98
|
-
offerToken?: string;
|
|
99
|
-
offerId?: string;
|
|
100
|
-
basePlanId?: string;
|
|
101
|
-
pricingPhases?: {
|
|
102
|
-
pricingPhaseList?: Array<{
|
|
103
|
-
formattedPrice?: string;
|
|
104
|
-
priceAmountMicros?: string | number;
|
|
105
|
-
priceCurrencyCode?: string;
|
|
106
|
-
billingPeriod?: string;
|
|
107
|
-
recurrenceMode?: number;
|
|
108
|
-
billingCycleCount?: number;
|
|
109
|
-
}>;
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface RawPurchase {
|
|
114
|
-
id?: string;
|
|
115
|
-
transactionId?: string;
|
|
116
|
-
productId?: string;
|
|
117
|
-
ids?: string[];
|
|
118
|
-
purchaseToken?: string;
|
|
119
|
-
purchaseStateAndroid?: number;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
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
|
-
const
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
//
|
|
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
|
-
case '
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
case '
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
return '
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
300
|
-
|
|
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
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
notifyConnection(
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
})
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Android adapter over `react-native-iap` (Play Billing).
|
|
3
|
+
*
|
|
4
|
+
* **The only file in this package that touches the billing library.** Everything
|
|
5
|
+
* above it talks to `ProviderPort`, which is what keeps the binding replaceable
|
|
6
|
+
* and lets an iOS adapter be added without changing a consumer call (FR-002,
|
|
7
|
+
* FR-007). ESLint enforces that boundary.
|
|
8
|
+
*
|
|
9
|
+
* ## Verification status
|
|
10
|
+
*
|
|
11
|
+
* The binding is **not yet installed** — the root `react-native-nitro-modules`
|
|
12
|
+
* bump it requires is held pending approval (see specs/013-rn-payment-sdk/plan.md,
|
|
13
|
+
* Post-Implementation Notes). So the call shapes below are written against
|
|
14
|
+
* `react-native-iap` 16.x as documented, and **every one must be verified against
|
|
15
|
+
* the installed package** before this adapter is trusted on a device. The module
|
|
16
|
+
* is required lazily and the surface it needs is declared explicitly in
|
|
17
|
+
* `IapModule`, so a mismatch surfaces here rather than deep in the engine.
|
|
18
|
+
*
|
|
19
|
+
* No test exercises this file: the whole suite runs against the fake store, and
|
|
20
|
+
* the IAP test double throws on every call precisely so that reaching this code in
|
|
21
|
+
* a test fails loudly instead of passing against a stub.
|
|
22
|
+
*
|
|
23
|
+
* ## Known upstream defect this guards against
|
|
24
|
+
*
|
|
25
|
+
* openiap#408: the Android bridge did not clear its internal `isInitialized` flag
|
|
26
|
+
* on disconnect, so `connected` stayed true, later calls failed with
|
|
27
|
+
* `E_NOT_PREPARED`, and `reconnect()` reported success without rebuilding
|
|
28
|
+
* anything. This adapter therefore (a) tracks connection state itself rather than
|
|
29
|
+
* trusting the binding's flag, and (b) always performs a full
|
|
30
|
+
* `endConnection` + `initConnection` cycle instead of calling any reconnect
|
|
31
|
+
* shortcut.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import type { ProductType, Unsubscribe } from '../config/types';
|
|
35
|
+
import type {
|
|
36
|
+
ProviderCapabilities,
|
|
37
|
+
ProviderConnectOutcome,
|
|
38
|
+
ProviderFailureReason,
|
|
39
|
+
ProviderPort,
|
|
40
|
+
ProviderPriceChangeOutcome,
|
|
41
|
+
ProviderProduct,
|
|
42
|
+
ProviderPurchase,
|
|
43
|
+
ProviderPurchaseOutcome,
|
|
44
|
+
ProviderPurchaseRequest,
|
|
45
|
+
ProviderToken,
|
|
46
|
+
StoreProductId,
|
|
47
|
+
} from './port';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Exactly what this adapter needs from the binding.
|
|
51
|
+
*
|
|
52
|
+
* Declared narrowly on purpose: it documents the coupling, and it means a binding
|
|
53
|
+
* upgrade that changes one of these shapes fails to typecheck here instead of
|
|
54
|
+
* failing silently at runtime on a user's device.
|
|
55
|
+
*/
|
|
56
|
+
interface IapModule {
|
|
57
|
+
initConnection(): Promise<boolean>;
|
|
58
|
+
endConnection(): Promise<void>;
|
|
59
|
+
fetchProducts(params: {
|
|
60
|
+
skus: string[];
|
|
61
|
+
type: 'in-app' | 'subs';
|
|
62
|
+
}): Promise<RawProduct[]>;
|
|
63
|
+
getAvailablePurchases(): Promise<RawPurchase[]>;
|
|
64
|
+
requestPurchase(params: unknown): Promise<unknown>;
|
|
65
|
+
finishTransaction(params: {
|
|
66
|
+
purchase: unknown;
|
|
67
|
+
isConsumable?: boolean;
|
|
68
|
+
}): Promise<unknown>;
|
|
69
|
+
purchaseUpdatedListener(
|
|
70
|
+
listener: (purchase: RawPurchase) => void,
|
|
71
|
+
): { remove(): void };
|
|
72
|
+
purchaseErrorListener(
|
|
73
|
+
listener: (error: { code?: string; message?: string }) => void,
|
|
74
|
+
): { remove(): void };
|
|
75
|
+
deepLinkToSubscriptions?(params: { skuAndroid?: string }): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface RawProduct {
|
|
79
|
+
id?: string;
|
|
80
|
+
productId?: string;
|
|
81
|
+
title?: string;
|
|
82
|
+
name?: string;
|
|
83
|
+
description?: string;
|
|
84
|
+
displayPrice?: string;
|
|
85
|
+
localizedPrice?: string;
|
|
86
|
+
currency?: string;
|
|
87
|
+
currencyCode?: string;
|
|
88
|
+
price?: string | number;
|
|
89
|
+
oneTimePurchaseOfferDetails?: {
|
|
90
|
+
formattedPrice?: string;
|
|
91
|
+
priceAmountMicros?: string | number;
|
|
92
|
+
priceCurrencyCode?: string;
|
|
93
|
+
};
|
|
94
|
+
subscriptionOfferDetails?: RawSubscriptionOffer[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface RawSubscriptionOffer {
|
|
98
|
+
offerToken?: string;
|
|
99
|
+
offerId?: string;
|
|
100
|
+
basePlanId?: string;
|
|
101
|
+
pricingPhases?: {
|
|
102
|
+
pricingPhaseList?: Array<{
|
|
103
|
+
formattedPrice?: string;
|
|
104
|
+
priceAmountMicros?: string | number;
|
|
105
|
+
priceCurrencyCode?: string;
|
|
106
|
+
billingPeriod?: string;
|
|
107
|
+
recurrenceMode?: number;
|
|
108
|
+
billingCycleCount?: number;
|
|
109
|
+
}>;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface RawPurchase {
|
|
114
|
+
id?: string;
|
|
115
|
+
transactionId?: string;
|
|
116
|
+
productId?: string;
|
|
117
|
+
ids?: string[];
|
|
118
|
+
purchaseToken?: string;
|
|
119
|
+
purchaseStateAndroid?: number | null;
|
|
120
|
+
/** A string in the 16.x binding: `'pending' | 'purchased' | 'unknown'`. */
|
|
121
|
+
purchaseState?: number | string;
|
|
122
|
+
transactionDate?: number;
|
|
123
|
+
isAcknowledgedAndroid?: boolean;
|
|
124
|
+
isAcknowledged?: boolean;
|
|
125
|
+
autoRenewingAndroid?: boolean;
|
|
126
|
+
isAutoRenewing?: boolean;
|
|
127
|
+
expirationDateAndroid?: number;
|
|
128
|
+
transactionReceipt?: string;
|
|
129
|
+
dataAndroid?: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Play's `PurchaseState`: 0 unspecified, 1 purchased, 2 pending. */
|
|
133
|
+
const PLAY_STATE_PURCHASED = 1;
|
|
134
|
+
const PLAY_STATE_PENDING = 2;
|
|
135
|
+
|
|
136
|
+
function loadModule(): IapModule {
|
|
137
|
+
// Required lazily so importing the package does not require the native module.
|
|
138
|
+
const mod = require('react-native-iap') as IapModule;
|
|
139
|
+
return mod;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function toMicros(value: string | number | undefined): number {
|
|
143
|
+
if (typeof value === 'number') {
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
if (typeof value === 'string') {
|
|
147
|
+
const parsed = Number.parseInt(value, 10);
|
|
148
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
149
|
+
}
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function normalizeProduct(
|
|
154
|
+
raw: RawProduct,
|
|
155
|
+
productType: ProductType,
|
|
156
|
+
): ProviderProduct {
|
|
157
|
+
const oneTime = raw.oneTimePurchaseOfferDetails;
|
|
158
|
+
const currencyCode =
|
|
159
|
+
oneTime?.priceCurrencyCode ?? raw.currencyCode ?? raw.currency ?? '';
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
storeProductId: raw.id ?? raw.productId ?? '',
|
|
163
|
+
productType,
|
|
164
|
+
title: raw.title ?? raw.name ?? '',
|
|
165
|
+
description: raw.description ?? '',
|
|
166
|
+
// Store-formatted, never built here: only the store knows the right currency
|
|
167
|
+
// symbol, separator, and placement for the user's locale (FR-028).
|
|
168
|
+
formattedPrice:
|
|
169
|
+
oneTime?.formattedPrice ?? raw.displayPrice ?? raw.localizedPrice ?? '',
|
|
170
|
+
currencyCode,
|
|
171
|
+
priceAmountMicros: toMicros(oneTime?.priceAmountMicros ?? raw.price),
|
|
172
|
+
offers: (raw.subscriptionOfferDetails ?? []).map(offer =>
|
|
173
|
+
normalizeOffer(offer, currencyCode),
|
|
174
|
+
),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function normalizeOffer(
|
|
179
|
+
offer: RawSubscriptionOffer,
|
|
180
|
+
fallbackCurrency: string,
|
|
181
|
+
): ProviderProduct['offers'][number] {
|
|
182
|
+
const phases = offer.pricingPhases?.pricingPhaseList ?? [];
|
|
183
|
+
// The last phase is the recurring price; earlier ones are trial or
|
|
184
|
+
// introductory. Play orders them that way.
|
|
185
|
+
const recurring = phases[phases.length - 1];
|
|
186
|
+
const intro = phases.length > 1 ? phases[0] : undefined;
|
|
187
|
+
const isFreeTrial = intro !== undefined && toMicros(intro.priceAmountMicros) === 0;
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
offerKey: offer.offerToken ?? offer.offerId ?? offer.basePlanId ?? '',
|
|
191
|
+
billingPeriod: recurring?.billingPeriod ?? '',
|
|
192
|
+
formattedPrice: recurring?.formattedPrice ?? '',
|
|
193
|
+
priceAmountMicros: toMicros(recurring?.priceAmountMicros),
|
|
194
|
+
currencyCode: recurring?.priceCurrencyCode ?? fallbackCurrency,
|
|
195
|
+
trialPeriod: isFreeTrial ? intro?.billingPeriod : undefined,
|
|
196
|
+
introductoryPrice:
|
|
197
|
+
intro && !isFreeTrial
|
|
198
|
+
? {
|
|
199
|
+
formattedPrice: intro.formattedPrice ?? '',
|
|
200
|
+
period: intro.billingPeriod ?? '',
|
|
201
|
+
cycles: intro.billingCycleCount ?? 1,
|
|
202
|
+
}
|
|
203
|
+
: undefined,
|
|
204
|
+
// Play decides eligibility by only returning offers the user qualifies for,
|
|
205
|
+
// so forwarding `true` reports the store's determination rather than
|
|
206
|
+
// computing our own (FR-026).
|
|
207
|
+
isEligible: true,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Reads the purchase state from whichever field the binding filled.
|
|
213
|
+
*
|
|
214
|
+
* Pending is preserved, never flattened to purchased: that distinction is what
|
|
215
|
+
* stops an entitlement being granted for money that has not arrived (FR-037).
|
|
216
|
+
* And an unrecognized or missing state is `unspecified`, never `purchased`.
|
|
217
|
+
*
|
|
218
|
+
* 0.1.0 read only Play's numeric state and treated anything else as purchased.
|
|
219
|
+
* The 16.x binding hands JavaScript the *string* state and does not carry the
|
|
220
|
+
* numeric one across, so on a device every pending purchase arrived as a string,
|
|
221
|
+
* fell through to the default, and was granted. Unit tests could not see it: they
|
|
222
|
+
* drive the fake store, which never passes through here.
|
|
223
|
+
*/
|
|
224
|
+
export function toProviderPurchaseState(
|
|
225
|
+
raw: Pick<RawPurchase, 'purchaseStateAndroid' | 'purchaseState'>,
|
|
226
|
+
): ProviderPurchase['state'] {
|
|
227
|
+
let numeric: number | undefined;
|
|
228
|
+
if (typeof raw.purchaseStateAndroid === 'number') {
|
|
229
|
+
numeric = raw.purchaseStateAndroid;
|
|
230
|
+
} else if (typeof raw.purchaseState === 'number') {
|
|
231
|
+
numeric = raw.purchaseState;
|
|
232
|
+
}
|
|
233
|
+
if (numeric !== undefined) {
|
|
234
|
+
if (numeric === PLAY_STATE_PENDING) {
|
|
235
|
+
return 'pending';
|
|
236
|
+
}
|
|
237
|
+
return numeric === PLAY_STATE_PURCHASED ? 'purchased' : 'unspecified';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
switch (typeof raw.purchaseState === 'string' ? raw.purchaseState.toLowerCase() : '') {
|
|
241
|
+
case 'purchased':
|
|
242
|
+
case 'restored':
|
|
243
|
+
return 'purchased';
|
|
244
|
+
case 'pending':
|
|
245
|
+
case 'deferred':
|
|
246
|
+
return 'pending';
|
|
247
|
+
default:
|
|
248
|
+
return 'unspecified';
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function normalizePurchase(raw: RawPurchase): ProviderPurchase {
|
|
253
|
+
return {
|
|
254
|
+
storeProductId: raw.productId ?? raw.ids?.[0] ?? '',
|
|
255
|
+
transactionId: raw.id ?? raw.transactionId ?? raw.purchaseToken ?? '',
|
|
256
|
+
token: raw.purchaseToken ?? '',
|
|
257
|
+
state: toProviderPurchaseState(raw),
|
|
258
|
+
purchasedAt: raw.transactionDate ?? Date.now(),
|
|
259
|
+
isAcknowledged: raw.isAcknowledgedAndroid ?? raw.isAcknowledged ?? false,
|
|
260
|
+
expiresAt: raw.expirationDateAndroid,
|
|
261
|
+
willAutoRenew: raw.autoRenewingAndroid ?? raw.isAutoRenewing,
|
|
262
|
+
// Opaque material for the app's backend. Carried here and handed to the
|
|
263
|
+
// validation hook only — never logged or put in an event (FR-074).
|
|
264
|
+
verificationPayload: raw.transactionReceipt ?? raw.dataAndroid,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Maps a binding error code onto the port's vocabulary, so no store code escapes. */
|
|
269
|
+
function mapErrorCode(code: string | undefined): ProviderFailureReason {
|
|
270
|
+
switch (code) {
|
|
271
|
+
case 'E_USER_CANCELLED':
|
|
272
|
+
case 'user-cancelled':
|
|
273
|
+
return 'user_cancelled';
|
|
274
|
+
case 'E_ALREADY_OWNED':
|
|
275
|
+
case 'item-already-owned':
|
|
276
|
+
return 'already_owned';
|
|
277
|
+
case 'E_ITEM_UNAVAILABLE':
|
|
278
|
+
case 'item-unavailable':
|
|
279
|
+
return 'unknown_product';
|
|
280
|
+
case 'E_NOT_PREPARED':
|
|
281
|
+
case 'not-prepared':
|
|
282
|
+
return 'not_ready';
|
|
283
|
+
case 'E_NETWORK_ERROR':
|
|
284
|
+
case 'network-error':
|
|
285
|
+
return 'network_error';
|
|
286
|
+
case 'E_DEFERRED_PAYMENT':
|
|
287
|
+
// Not a failure — a pending purchase. The caller resolves it from the
|
|
288
|
+
// purchase update rather than from this error.
|
|
289
|
+
return 'store_error';
|
|
290
|
+
default:
|
|
291
|
+
return 'store_error';
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const ANDROID_CAPABILITIES: ProviderCapabilities = {
|
|
296
|
+
supportsSubscriptions: true,
|
|
297
|
+
supportsPlanChange: true,
|
|
298
|
+
supportsPriceChangeConfirmation: false,
|
|
299
|
+
/**
|
|
300
|
+
* False, and this is a platform fact rather than a gap in this adapter.
|
|
301
|
+
*
|
|
302
|
+
* Client-side Play Billing cannot separate grace period from account hold: a
|
|
303
|
+
* grace-period subscription is still returned as active, and one in account
|
|
304
|
+
* hold stops being returned at all — indistinguishable from expired, cancelled,
|
|
305
|
+
* or refunded. The entitlement layer reads this flag and reports
|
|
306
|
+
* `indeterminate` rather than inventing a distinction (research.md R5).
|
|
307
|
+
*/
|
|
308
|
+
reportsGraceAndHoldDistinctly: false,
|
|
309
|
+
supportsExternalTransactions: false,
|
|
310
|
+
/** This one really does move money, which is what arms the environment guard. */
|
|
311
|
+
createsRealTransactions: true,
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
export interface IapAdapterOptions {
|
|
315
|
+
/** Injected in tests that need to drive the mapping without the native module. */
|
|
316
|
+
module?: IapModule;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function createIapAdapter(options: IapAdapterOptions = {}): ProviderPort {
|
|
320
|
+
let iap: IapModule | undefined = options.module;
|
|
321
|
+
|
|
322
|
+
/** Tracked here, never read from the binding — see the openiap#408 note above. */
|
|
323
|
+
let connected = false;
|
|
324
|
+
|
|
325
|
+
const connectionListeners = new Set<
|
|
326
|
+
(connected: boolean, reason?: string) => void
|
|
327
|
+
>();
|
|
328
|
+
const purchaseListeners = new Set<(purchase: ProviderPurchase) => void>();
|
|
329
|
+
let updateSub: { remove(): void } | undefined;
|
|
330
|
+
let errorSub: { remove(): void } | undefined;
|
|
331
|
+
|
|
332
|
+
const module = (): IapModule => {
|
|
333
|
+
if (!iap) {
|
|
334
|
+
iap = loadModule();
|
|
335
|
+
}
|
|
336
|
+
return iap;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const notifyConnection = (next: boolean, reason?: string): void => {
|
|
340
|
+
if (connected === next) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
connected = next;
|
|
344
|
+
for (const listener of connectionListeners) {
|
|
345
|
+
listener(next, reason);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const attachListeners = (): void => {
|
|
350
|
+
const mod = module();
|
|
351
|
+
updateSub = mod.purchaseUpdatedListener(raw => {
|
|
352
|
+
const purchase = normalizePurchase(raw);
|
|
353
|
+
for (const listener of purchaseListeners) {
|
|
354
|
+
listener(purchase);
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
errorSub = mod.purchaseErrorListener(error => {
|
|
358
|
+
// A `not-prepared` error is the one signal the binding gives that the
|
|
359
|
+
// service went away underneath us, so it is treated as a disconnect
|
|
360
|
+
// rather than as a purchase failure.
|
|
361
|
+
if (mapErrorCode(error.code) === 'not_ready') {
|
|
362
|
+
notifyConnection(false, 'not_prepared');
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const detachListeners = (): void => {
|
|
368
|
+
updateSub?.remove();
|
|
369
|
+
updateSub = undefined;
|
|
370
|
+
errorSub?.remove();
|
|
371
|
+
errorSub = undefined;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
async connect(): Promise<ProviderConnectOutcome> {
|
|
376
|
+
try {
|
|
377
|
+
const mod = module();
|
|
378
|
+
// Always a full cycle. The binding's own reconnect has reported success
|
|
379
|
+
// without rebuilding anything, so we never rely on it (openiap#408).
|
|
380
|
+
if (connected) {
|
|
381
|
+
await mod.endConnection().catch(() => undefined);
|
|
382
|
+
connected = false;
|
|
383
|
+
}
|
|
384
|
+
const ok = await mod.initConnection();
|
|
385
|
+
if (!ok) {
|
|
386
|
+
return {
|
|
387
|
+
ok: false,
|
|
388
|
+
reason: 'no_billing_service',
|
|
389
|
+
retryable: true,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
attachListeners();
|
|
393
|
+
notifyConnection(true);
|
|
394
|
+
return { ok: true };
|
|
395
|
+
} catch (error) {
|
|
396
|
+
const code = (error as { code?: string }).code;
|
|
397
|
+
// A missing or too-old Play Services install is not the same as a
|
|
398
|
+
// transient error, and the app shows different things for each (FR-021).
|
|
399
|
+
if (code === 'E_IAP_NOT_AVAILABLE' || code === 'iap-not-available') {
|
|
400
|
+
return { ok: false, reason: 'no_billing_service', retryable: false };
|
|
401
|
+
}
|
|
402
|
+
if (code === 'E_SERVICE_ERROR') {
|
|
403
|
+
return {
|
|
404
|
+
ok: false,
|
|
405
|
+
reason: 'billing_service_outdated',
|
|
406
|
+
retryable: true,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
return { ok: false, reason: 'store_error', retryable: true };
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
|
|
413
|
+
async disconnect(): Promise<void> {
|
|
414
|
+
detachListeners();
|
|
415
|
+
try {
|
|
416
|
+
await module().endConnection();
|
|
417
|
+
} catch {
|
|
418
|
+
// Already down; nothing to tear down.
|
|
419
|
+
}
|
|
420
|
+
notifyConnection(false, 'disconnect');
|
|
421
|
+
},
|
|
422
|
+
|
|
423
|
+
observeConnection(listener): Unsubscribe {
|
|
424
|
+
connectionListeners.add(listener);
|
|
425
|
+
let removed = false;
|
|
426
|
+
return () => {
|
|
427
|
+
if (removed) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
removed = true;
|
|
431
|
+
connectionListeners.delete(listener);
|
|
432
|
+
};
|
|
433
|
+
},
|
|
434
|
+
|
|
435
|
+
async queryProducts(
|
|
436
|
+
storeProductIds: StoreProductId[],
|
|
437
|
+
productType: ProductType,
|
|
438
|
+
): Promise<ProviderProduct[]> {
|
|
439
|
+
const raw = await module().fetchProducts({
|
|
440
|
+
skus: storeProductIds,
|
|
441
|
+
type: productType === 'subscription' ? 'subs' : 'in-app',
|
|
442
|
+
});
|
|
443
|
+
return raw.map(product => normalizeProduct(product, productType));
|
|
444
|
+
},
|
|
445
|
+
|
|
446
|
+
async queryOwned(): Promise<ProviderPurchase[]> {
|
|
447
|
+
const raw = await module().getAvailablePurchases();
|
|
448
|
+
// Pending purchases are mapped through, flagged rather than filtered: the
|
|
449
|
+
// engine needs to see them to raise the pending signal (FR-038).
|
|
450
|
+
return raw.map(normalizePurchase);
|
|
451
|
+
},
|
|
452
|
+
|
|
453
|
+
async launchPurchase(
|
|
454
|
+
request: ProviderPurchaseRequest,
|
|
455
|
+
): Promise<ProviderPurchaseOutcome> {
|
|
456
|
+
try {
|
|
457
|
+
const isSubscription = request.productType === 'subscription';
|
|
458
|
+
const result = await module().requestPurchase({
|
|
459
|
+
request: {
|
|
460
|
+
android: {
|
|
461
|
+
skus: [request.storeProductId],
|
|
462
|
+
obfuscatedAccountIdAndroid: request.obfuscatedAccountId,
|
|
463
|
+
...(isSubscription && request.offerKey
|
|
464
|
+
? {
|
|
465
|
+
subscriptionOffers: [
|
|
466
|
+
{
|
|
467
|
+
sku: request.storeProductId,
|
|
468
|
+
offerToken: request.offerKey,
|
|
469
|
+
},
|
|
470
|
+
],
|
|
471
|
+
}
|
|
472
|
+
: {}),
|
|
473
|
+
...(request.replaceStoreProductId
|
|
474
|
+
? {
|
|
475
|
+
purchaseTokenAndroid: request.replaceStoreProductId,
|
|
476
|
+
replacementModeAndroid: request.prorationMode,
|
|
477
|
+
}
|
|
478
|
+
: {}),
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
type: isSubscription ? 'subs' : 'in-app',
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
// The binding may resolve with the purchase, or resolve empty and deliver
|
|
485
|
+
// it through the update listener. Both are handled: the engine also
|
|
486
|
+
// ingests listener-delivered purchases, so neither path loses one.
|
|
487
|
+
const raw = Array.isArray(result) ? result[0] : result;
|
|
488
|
+
if (!raw) {
|
|
489
|
+
return { ok: false, reason: 'store_error', detail: 'no purchase returned' };
|
|
490
|
+
}
|
|
491
|
+
return { ok: true, purchase: normalizePurchase(raw as RawPurchase) };
|
|
492
|
+
} catch (error) {
|
|
493
|
+
const code = (error as { code?: string }).code;
|
|
494
|
+
return { ok: false, reason: mapErrorCode(code), detail: code };
|
|
495
|
+
}
|
|
496
|
+
},
|
|
497
|
+
|
|
498
|
+
observePurchases(listener): Unsubscribe {
|
|
499
|
+
purchaseListeners.add(listener);
|
|
500
|
+
let removed = false;
|
|
501
|
+
return () => {
|
|
502
|
+
if (removed) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
removed = true;
|
|
506
|
+
purchaseListeners.delete(listener);
|
|
507
|
+
};
|
|
508
|
+
},
|
|
509
|
+
|
|
510
|
+
async acknowledge(token: ProviderToken): Promise<void> {
|
|
511
|
+
// Idempotent at the store: acknowledging an already-acknowledged purchase
|
|
512
|
+
// is accepted, which is what FR-044 relies on.
|
|
513
|
+
await module().finishTransaction({
|
|
514
|
+
purchase: { purchaseToken: token },
|
|
515
|
+
isConsumable: false,
|
|
516
|
+
});
|
|
517
|
+
},
|
|
518
|
+
|
|
519
|
+
async consume(token: ProviderToken): Promise<void> {
|
|
520
|
+
await module().finishTransaction({
|
|
521
|
+
purchase: { purchaseToken: token },
|
|
522
|
+
isConsumable: true,
|
|
523
|
+
});
|
|
524
|
+
},
|
|
525
|
+
|
|
526
|
+
async openSubscriptionManagement(
|
|
527
|
+
storeProductId?: StoreProductId,
|
|
528
|
+
): Promise<void> {
|
|
529
|
+
// The store's own screen. Platforms forbid a self-built cancellation
|
|
530
|
+
// screen, so there is deliberately no alternative path here (FR-066).
|
|
531
|
+
const mod = module();
|
|
532
|
+
if (!mod.deepLinkToSubscriptions) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
await mod.deepLinkToSubscriptions({ skuAndroid: storeProductId });
|
|
536
|
+
},
|
|
537
|
+
|
|
538
|
+
async confirmPriceChange(): Promise<ProviderPriceChangeOutcome> {
|
|
539
|
+
// Play has no client-side price-change confirmation flow; it notifies the
|
|
540
|
+
// user itself. Reported as unsupported rather than faked, matching
|
|
541
|
+
// `supportsPriceChangeConfirmation: false` (FR-067).
|
|
542
|
+
return { ok: false, reason: 'store_error' };
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
capabilities: () => ANDROID_CAPABILITIES,
|
|
546
|
+
};
|
|
547
|
+
}
|