@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,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entitlement aggregation — the package's primary output.
|
|
3
|
+
*
|
|
4
|
+
* Everything else exists to make this one answer correct. The most damaging
|
|
5
|
+
* defect class in a payment SDK lives here: collapsing "not yet synced" into "not
|
|
6
|
+
* entitled" makes a paying user watch ads flash on cold start or see a purchased
|
|
7
|
+
* feature sit locked.
|
|
8
|
+
*
|
|
9
|
+
* Two rules do most of the work:
|
|
10
|
+
*
|
|
11
|
+
* - **A pending purchase is never a source** (FR-037). Enforced here, in
|
|
12
|
+
* aggregation, rather than at each call site, so no future code path can grant
|
|
13
|
+
* on money that has not arrived.
|
|
14
|
+
* - **Uncertainty resolves to `unknown`, never `not_entitled`** (FR-047,
|
|
15
|
+
* FR-055a). Never synced, stale, or scope-changed all mean "we do not know",
|
|
16
|
+
* and the package says so rather than asserting the user did not pay.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { ResolvedConfig } from '../config/validate';
|
|
20
|
+
import type { EntitlementKey, ProductKey } from '../config/types';
|
|
21
|
+
import type { PersistedPurchase, PersistedSnapshot } from '../persistence/snapshot';
|
|
22
|
+
import { entitlementsFor } from './mapping';
|
|
23
|
+
import {
|
|
24
|
+
deriveRenewalState,
|
|
25
|
+
grantsUnderPolicy,
|
|
26
|
+
hasExpired,
|
|
27
|
+
type RenewalState,
|
|
28
|
+
} from './policy';
|
|
29
|
+
|
|
30
|
+
export type EntitlementStatus = 'unknown' | 'entitled' | 'not_entitled';
|
|
31
|
+
|
|
32
|
+
export interface SubscriptionDetail {
|
|
33
|
+
expiresAt?: number;
|
|
34
|
+
willAutoRenew: boolean;
|
|
35
|
+
isCancelledButValid: boolean;
|
|
36
|
+
renewalState: RenewalState;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface EntitlementState {
|
|
40
|
+
key: EntitlementKey;
|
|
41
|
+
status: EntitlementStatus;
|
|
42
|
+
/** The app's own product keys currently granting this. Never raw transactions (FR-051). */
|
|
43
|
+
sources: ProductKey[];
|
|
44
|
+
subscription?: SubscriptionDetail;
|
|
45
|
+
lastSyncedAt?: number;
|
|
46
|
+
/** True once the snapshot is older than `staleAfterMs`; status is then `unknown`. */
|
|
47
|
+
isStale: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type EntitlementMap = Record<EntitlementKey, EntitlementState>;
|
|
51
|
+
|
|
52
|
+
export interface AggregateInput {
|
|
53
|
+
config: ResolvedConfig;
|
|
54
|
+
snapshot: PersistedSnapshot;
|
|
55
|
+
/** False until the first successful sync of this session or a hydrated one. */
|
|
56
|
+
hasSynced: boolean;
|
|
57
|
+
reportsGraceAndHoldDistinctly: boolean;
|
|
58
|
+
now: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A purchase that currently confers its entitlements. */
|
|
62
|
+
function isValidSource(
|
|
63
|
+
purchase: PersistedPurchase,
|
|
64
|
+
input: AggregateInput,
|
|
65
|
+
): boolean {
|
|
66
|
+
// Pending money is not money. No configuration and no code path may grant here.
|
|
67
|
+
if (purchase.state === 'pending') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (purchase.state === 'revoked' || purchase.state === 'consumed') {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
// A backend that said no is final (FR-071). A queued re-validation still
|
|
74
|
+
// grants, which is the documented `grant_and_queue` fallback (FR-072).
|
|
75
|
+
if (purchase.validationState === 'rejected') {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (purchase.productType === 'subscription') {
|
|
80
|
+
if (hasExpired(purchase.expiresAt, input.now)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const renewalState = deriveRenewalState({
|
|
84
|
+
expiresAt: purchase.expiresAt,
|
|
85
|
+
willAutoRenew: purchase.willAutoRenew,
|
|
86
|
+
reportsGraceAndHoldDistinctly: input.reportsGraceAndHoldDistinctly,
|
|
87
|
+
});
|
|
88
|
+
return grantsUnderPolicy(renewalState, {
|
|
89
|
+
gracePolicy: input.config.gracePolicy,
|
|
90
|
+
holdPolicy: input.config.holdPolicy,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// A consumable that has not been consumed yet is owned, but it must never
|
|
95
|
+
// become a permanent entitlement — consumption flips it to `consumed` above.
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function subscriptionDetailFor(
|
|
100
|
+
purchases: PersistedPurchase[],
|
|
101
|
+
input: AggregateInput,
|
|
102
|
+
): SubscriptionDetail | undefined {
|
|
103
|
+
const subs = purchases.filter(p => p.productType === 'subscription');
|
|
104
|
+
if (subs.length === 0) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Where several subscriptions grant one entitlement, the one running longest
|
|
109
|
+
// is what the user experiences, so report that one.
|
|
110
|
+
const furthest = subs.reduce((best, candidate) => {
|
|
111
|
+
const bestAt = best.expiresAt ?? Number.MAX_SAFE_INTEGER;
|
|
112
|
+
const candidateAt = candidate.expiresAt ?? Number.MAX_SAFE_INTEGER;
|
|
113
|
+
return candidateAt > bestAt ? candidate : best;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const renewalState = deriveRenewalState({
|
|
117
|
+
expiresAt: furthest.expiresAt,
|
|
118
|
+
willAutoRenew: furthest.willAutoRenew,
|
|
119
|
+
reportsGraceAndHoldDistinctly: input.reportsGraceAndHoldDistinctly,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
expiresAt: furthest.expiresAt,
|
|
124
|
+
willAutoRenew: furthest.willAutoRenew === true,
|
|
125
|
+
isCancelledButValid: renewalState === 'cancelled_valid',
|
|
126
|
+
renewalState,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Computes the full entitlement map.
|
|
132
|
+
*
|
|
133
|
+
* Pure and synchronous by design: it runs on render paths, so it must never
|
|
134
|
+
* touch I/O (PERF-001). The engine calls it after any state change and caches the
|
|
135
|
+
* result.
|
|
136
|
+
*/
|
|
137
|
+
export function aggregate(input: AggregateInput): EntitlementMap {
|
|
138
|
+
const { config, snapshot, hasSynced, now } = input;
|
|
139
|
+
|
|
140
|
+
const isStale =
|
|
141
|
+
snapshot.lastSyncedAt !== undefined &&
|
|
142
|
+
now - snapshot.lastSyncedAt > config.staleAfterMs;
|
|
143
|
+
|
|
144
|
+
// Both roads lead to `unknown`, never to `not_entitled`. Never having synced
|
|
145
|
+
// and having synced too long ago are the same epistemic state: we cannot
|
|
146
|
+
// currently support a claim either way.
|
|
147
|
+
const undetermined = !hasSynced || isStale;
|
|
148
|
+
|
|
149
|
+
const valid = snapshot.purchases.filter(p => isValidSource(p, input));
|
|
150
|
+
|
|
151
|
+
const result: EntitlementMap = {};
|
|
152
|
+
for (const key of config.entitlementKeys) {
|
|
153
|
+
const granting = valid.filter(p =>
|
|
154
|
+
entitlementsFor(config, p.productKey).includes(key),
|
|
155
|
+
);
|
|
156
|
+
const sources = [...new Set(granting.map(p => p.productKey))];
|
|
157
|
+
|
|
158
|
+
result[key] = {
|
|
159
|
+
key,
|
|
160
|
+
// An entitlement stays valid while at least one source remains (FR-052).
|
|
161
|
+
status: undetermined
|
|
162
|
+
? 'unknown'
|
|
163
|
+
: sources.length > 0
|
|
164
|
+
? 'entitled'
|
|
165
|
+
: 'not_entitled',
|
|
166
|
+
sources,
|
|
167
|
+
subscription: subscriptionDetailFor(granting, input),
|
|
168
|
+
lastSyncedAt: snapshot.lastSyncedAt,
|
|
169
|
+
isStale,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The initial map, before anything is known. Every key starts at `unknown` (FR-047). */
|
|
177
|
+
export function initialEntitlements(config: ResolvedConfig): EntitlementMap {
|
|
178
|
+
const result: EntitlementMap = {};
|
|
179
|
+
for (const key of config.entitlementKeys) {
|
|
180
|
+
result[key] = {
|
|
181
|
+
key,
|
|
182
|
+
status: 'unknown',
|
|
183
|
+
sources: [],
|
|
184
|
+
lastSyncedAt: undefined,
|
|
185
|
+
isStale: false,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface EntitlementChange {
|
|
192
|
+
key: EntitlementKey;
|
|
193
|
+
previous: EntitlementStatus;
|
|
194
|
+
next: EntitlementStatus;
|
|
195
|
+
sources: ProductKey[];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Diffs two maps to produce change signals.
|
|
200
|
+
*
|
|
201
|
+
* Emitting from a diff rather than from grant call sites is what makes the
|
|
202
|
+
* exactly-once guarantees structural (FR-079): a successful subscription renewal
|
|
203
|
+
* changes no status, so it produces no event and cannot double-signal (FR-068);
|
|
204
|
+
* and an entitlement with three sources fires once on transition, not once per
|
|
205
|
+
* source.
|
|
206
|
+
*/
|
|
207
|
+
export function diffEntitlements(
|
|
208
|
+
previous: EntitlementMap,
|
|
209
|
+
next: EntitlementMap,
|
|
210
|
+
): EntitlementChange[] {
|
|
211
|
+
const changes: EntitlementChange[] = [];
|
|
212
|
+
for (const key of Object.keys(next)) {
|
|
213
|
+
const before = previous[key]?.status ?? 'unknown';
|
|
214
|
+
const after = next[key].status;
|
|
215
|
+
if (before !== after) {
|
|
216
|
+
changes.push({
|
|
217
|
+
key,
|
|
218
|
+
previous: before,
|
|
219
|
+
next: after,
|
|
220
|
+
sources: next[key].sources,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return changes;
|
|
225
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Product ↔ entitlement indexing.
|
|
3
|
+
*
|
|
4
|
+
* The relation is many-to-many (FR-010): monthly, yearly, and lifetime products
|
|
5
|
+
* may all grant `no_ads`, and one bundle product may grant several entitlements.
|
|
6
|
+
*
|
|
7
|
+
* Both directions are indexed once at configure time. The reverse direction is
|
|
8
|
+
* what makes two requirements cheap on hot paths: naming which product currently
|
|
9
|
+
* grants an entitlement (FR-051), and keeping an entitlement valid while *any*
|
|
10
|
+
* source remains (FR-052).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ResolvedConfig } from '../config/validate';
|
|
14
|
+
import type { EntitlementKey, ProductKey } from '../config/types';
|
|
15
|
+
|
|
16
|
+
/** Entitlements a product confers. Empty for an unknown key rather than throwing. */
|
|
17
|
+
export function entitlementsFor(
|
|
18
|
+
config: ResolvedConfig,
|
|
19
|
+
productKey: ProductKey,
|
|
20
|
+
): readonly EntitlementKey[] {
|
|
21
|
+
return config.grants.get(productKey) ?? [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Products that could grant an entitlement, whether or not any is currently valid. */
|
|
25
|
+
export function sourcesFor(
|
|
26
|
+
config: ResolvedConfig,
|
|
27
|
+
entitlementKey: EntitlementKey,
|
|
28
|
+
): readonly ProductKey[] {
|
|
29
|
+
return config.sources.get(entitlementKey) ?? [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Translates a store product id back to the app's own product key.
|
|
34
|
+
*
|
|
35
|
+
* Needed because store callbacks and `queryOwned` speak in store ids, while
|
|
36
|
+
* everything above the adapter speaks in product keys (FR-003). Returns
|
|
37
|
+
* `undefined` for an id absent from the catalog — which happens legitimately when
|
|
38
|
+
* a user owns something from a build whose catalog has since changed, so it must
|
|
39
|
+
* be handled rather than treated as impossible.
|
|
40
|
+
*/
|
|
41
|
+
export function productKeyForStoreId(
|
|
42
|
+
config: ResolvedConfig,
|
|
43
|
+
storeProductId: string,
|
|
44
|
+
): ProductKey | undefined {
|
|
45
|
+
return config.byStoreId.get(storeProductId);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function storeIdForProductKey(
|
|
49
|
+
config: ResolvedConfig,
|
|
50
|
+
productKey: ProductKey,
|
|
51
|
+
): string | undefined {
|
|
52
|
+
return config.entries.get(productKey)?.storeProductIds[config.platform];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Which entitlements a set of granting products covers, de-duplicated. */
|
|
56
|
+
export function entitlementsCoveredBy(
|
|
57
|
+
config: ResolvedConfig,
|
|
58
|
+
productKeys: Iterable<ProductKey>,
|
|
59
|
+
): Set<EntitlementKey> {
|
|
60
|
+
const covered = new Set<EntitlementKey>();
|
|
61
|
+
for (const productKey of productKeys) {
|
|
62
|
+
for (const entitlement of entitlementsFor(config, productKey)) {
|
|
63
|
+
covered.add(entitlement);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return covered;
|
|
67
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grace and hold entitlement policy.
|
|
3
|
+
*
|
|
4
|
+
* This is a **business decision, not a technical constant** (FR-049), so it is
|
|
5
|
+
* configurable with documented defaults.
|
|
6
|
+
*
|
|
7
|
+
* ## The defaults, and why
|
|
8
|
+
*
|
|
9
|
+
* `gracePolicy: 'grant'` — a grace period means the store is still retrying a
|
|
10
|
+
* payment method that failed for reasons usually outside the user's control: an
|
|
11
|
+
* expired card, a bank decline, a temporary hold. Revoking immediately punishes a
|
|
12
|
+
* paying customer for a billing hiccup they may not know about yet.
|
|
13
|
+
*
|
|
14
|
+
* `holdPolicy: 'withhold'` — account hold means the store has given up retrying.
|
|
15
|
+
* The subscription is effectively unpaid, and continuing to serve paid features
|
|
16
|
+
* indefinitely is giving away the product.
|
|
17
|
+
*
|
|
18
|
+
* ## What Android can actually tell us
|
|
19
|
+
*
|
|
20
|
+
* Client-side Play Billing **cannot** separate grace from hold: a grace-period
|
|
21
|
+
* subscription is still returned as active, and one in account hold stops being
|
|
22
|
+
* returned at all — indistinguishable from expired, cancelled, or refunded.
|
|
23
|
+
*
|
|
24
|
+
* Fortunately the platform's own behavior matches the defaults above. Play keeps
|
|
25
|
+
* returning a grace-period purchase, so the entitlement holds. Play stops
|
|
26
|
+
* returning a held one, so it lapses at the next sync. **The default policy is
|
|
27
|
+
* therefore correct with no backend at all.**
|
|
28
|
+
*
|
|
29
|
+
* What is *not* achievable client-side is a non-default policy — withholding
|
|
30
|
+
* during grace, or granting during hold — because that needs the state the client
|
|
31
|
+
* cannot see. Such an app supplies it through the validation hook (FR-069). This
|
|
32
|
+
* limitation is documented rather than papered over (FR-058).
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { LapsePolicy } from '../config/types';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A subscription's renewal state as far as the package can honestly tell.
|
|
39
|
+
*
|
|
40
|
+
* `indeterminate` is not a failure — it is the truthful answer when the platform
|
|
41
|
+
* reports a subscription as active but cannot say whether that is a healthy
|
|
42
|
+
* renewal, a cancelled-but-still-paid period, or a grace period.
|
|
43
|
+
*/
|
|
44
|
+
export type RenewalState =
|
|
45
|
+
| 'active'
|
|
46
|
+
| 'cancelled_valid'
|
|
47
|
+
| 'grace'
|
|
48
|
+
| 'hold'
|
|
49
|
+
| 'indeterminate';
|
|
50
|
+
|
|
51
|
+
export interface LapsePolicyConfig {
|
|
52
|
+
gracePolicy: LapsePolicy;
|
|
53
|
+
holdPolicy: LapsePolicy;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SubscriptionFacts {
|
|
57
|
+
expiresAt?: number;
|
|
58
|
+
willAutoRenew?: boolean;
|
|
59
|
+
/** True only where the provider genuinely distinguishes grace/hold. */
|
|
60
|
+
reportsGraceAndHoldDistinctly: boolean;
|
|
61
|
+
/** Provider-reported, only meaningful when the flag above is true. */
|
|
62
|
+
providerRenewalState?: 'active' | 'grace' | 'hold';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Derives the renewal state without inventing information.
|
|
67
|
+
*
|
|
68
|
+
* The temptation is to read `willAutoRenew === false` as "grace period", but that
|
|
69
|
+
* same value is what a deliberate cancellation looks like. Guessing would mean
|
|
70
|
+
* showing "there's a problem with your payment method" to someone who simply
|
|
71
|
+
* chose not to renew — so when the provider cannot distinguish, neither do we.
|
|
72
|
+
*/
|
|
73
|
+
export function deriveRenewalState(facts: SubscriptionFacts): RenewalState {
|
|
74
|
+
if (facts.reportsGraceAndHoldDistinctly && facts.providerRenewalState) {
|
|
75
|
+
switch (facts.providerRenewalState) {
|
|
76
|
+
case 'grace':
|
|
77
|
+
return 'grace';
|
|
78
|
+
case 'hold':
|
|
79
|
+
return 'hold';
|
|
80
|
+
case 'active':
|
|
81
|
+
return facts.willAutoRenew === false ? 'cancelled_valid' : 'active';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (facts.willAutoRenew === false) {
|
|
86
|
+
// Still inside a paid period with auto-renew off. Genuinely ambiguous
|
|
87
|
+
// between "cancelled" and "in grace", so report the one that is certainly
|
|
88
|
+
// true — the period is paid for — and let the app decide how to present it.
|
|
89
|
+
return 'cancelled_valid';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (facts.willAutoRenew === true) {
|
|
93
|
+
return 'active';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return 'indeterminate';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Whether a subscription in this state should confer its entitlement.
|
|
101
|
+
*
|
|
102
|
+
* `hold` reaching this function at all requires a provider that reports it,
|
|
103
|
+
* which Android does not. On Android a held subscription simply stops being
|
|
104
|
+
* returned, so it never becomes a source and the entitlement lapses — the same
|
|
105
|
+
* result the `withhold` default expresses.
|
|
106
|
+
*/
|
|
107
|
+
export function grantsUnderPolicy(
|
|
108
|
+
state: RenewalState,
|
|
109
|
+
policy: LapsePolicyConfig,
|
|
110
|
+
): boolean {
|
|
111
|
+
switch (state) {
|
|
112
|
+
case 'active':
|
|
113
|
+
case 'cancelled_valid':
|
|
114
|
+
return true;
|
|
115
|
+
case 'grace':
|
|
116
|
+
return policy.gracePolicy === 'grant';
|
|
117
|
+
case 'hold':
|
|
118
|
+
return policy.holdPolicy === 'grant';
|
|
119
|
+
case 'indeterminate':
|
|
120
|
+
// The store is still reporting the purchase as owned. Withholding here
|
|
121
|
+
// would revoke from a paying user because of our own uncertainty.
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** True once a subscription's paid period has genuinely elapsed (FR-048). */
|
|
127
|
+
export function hasExpired(expiresAt: number | undefined, now: number): boolean {
|
|
128
|
+
return expiresAt !== undefined && expiresAt <= now;
|
|
129
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@chipmobilesdk/rn-payment` — reusable in-app payment and entitlement
|
|
3
|
+
* capability for ChipMobileSdk-family React Native apps.
|
|
4
|
+
*
|
|
5
|
+
* This is the public entrypoint (LIB-003). Modules under `config/`, `core/`,
|
|
6
|
+
* `engine/`, `entitlements/`, `persistence/`, `adapter/`, `diagnostics/`, and
|
|
7
|
+
* `react/` are private; import them only through this file. The fake store lives
|
|
8
|
+
* behind `@chipmobilesdk/rn-payment/testing` so test doubles cannot reach a
|
|
9
|
+
* production bundle.
|
|
10
|
+
*
|
|
11
|
+
* Nothing exported here names Google, Play Billing, or `react-native-iap`
|
|
12
|
+
* (FR-002). `src/__tests__/public-api.test.ts` asserts that mechanically.
|
|
13
|
+
*
|
|
14
|
+
* The package owns **capability**: a safe, consistent way to sell, finalize,
|
|
15
|
+
* restore, and report entitlements. The consuming app owns **policy** — what is
|
|
16
|
+
* sold, at what price, on which screen, at which moment, what each entitlement
|
|
17
|
+
* unlocks, and how generous the grace policy is. There is no product identifier,
|
|
18
|
+
* no screen name, and no business rule in this package (FR-016).
|
|
19
|
+
*
|
|
20
|
+
* The one output that matters most is the **entitlement key**: an app-chosen key
|
|
21
|
+
* such as `no_ads`. Business logic asks about entitlement keys and never about
|
|
22
|
+
* store product identifiers, product types, or which store sold the thing.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// --- Configuration and catalog ---------------------------------------------
|
|
26
|
+
export type {
|
|
27
|
+
BackoffConfig,
|
|
28
|
+
BlockedOperation,
|
|
29
|
+
CatalogEntry,
|
|
30
|
+
EntitlementKey,
|
|
31
|
+
Environment,
|
|
32
|
+
FinalizeResult,
|
|
33
|
+
LapsePolicy,
|
|
34
|
+
LoggerAdapter,
|
|
35
|
+
PaymentConfig,
|
|
36
|
+
PaymentStatus,
|
|
37
|
+
PaymentStatusKind,
|
|
38
|
+
ProductKey,
|
|
39
|
+
ProductType,
|
|
40
|
+
PurchaseOptions,
|
|
41
|
+
PurchaseResult,
|
|
42
|
+
RetryConfig,
|
|
43
|
+
StoreProductIds,
|
|
44
|
+
TimeoutConfig,
|
|
45
|
+
TransactionRef,
|
|
46
|
+
UnavailableReason,
|
|
47
|
+
UnfinalizedSummary,
|
|
48
|
+
Unsubscribe,
|
|
49
|
+
ValidationCandidate,
|
|
50
|
+
ValidationDecision,
|
|
51
|
+
ValidationHook,
|
|
52
|
+
ValidationUnavailablePolicy,
|
|
53
|
+
} from './config/types';
|
|
54
|
+
|
|
55
|
+
// --- Policy defaults and bounds --------------------------------------------
|
|
56
|
+
// Exported so a consumer can read the values it is bound by rather than
|
|
57
|
+
// discovering them from a timeout in production.
|
|
58
|
+
export {
|
|
59
|
+
BACKOFF_ATTEMPT_BOUNDS,
|
|
60
|
+
BACKOFF_MAX_DELAY_BOUNDS,
|
|
61
|
+
CONNECT_TIMEOUT_BOUNDS,
|
|
62
|
+
DEFAULT_CONNECT_TIMEOUT_MS,
|
|
63
|
+
DEFAULT_FINALIZE_ATTEMPT_TIMEOUT_MS,
|
|
64
|
+
DEFAULT_FINALIZE_BACKOFF,
|
|
65
|
+
DEFAULT_FOREGROUND_SYNC_MIN_INTERVAL_MS,
|
|
66
|
+
DEFAULT_PRODUCT_CACHE_TTL_MS,
|
|
67
|
+
DEFAULT_PRODUCT_QUERY_TIMEOUT_MS,
|
|
68
|
+
DEFAULT_PURCHASE_LAUNCH_TIMEOUT_MS,
|
|
69
|
+
DEFAULT_RECONNECT_BACKOFF,
|
|
70
|
+
DEFAULT_STALE_AFTER_MS,
|
|
71
|
+
DEFAULT_SYNC_TIMEOUT_MS,
|
|
72
|
+
FINALIZE_ATTEMPT_TIMEOUT_BOUNDS,
|
|
73
|
+
FOREGROUND_SYNC_MIN_INTERVAL_BOUNDS,
|
|
74
|
+
PRODUCT_CACHE_TTL_BOUNDS,
|
|
75
|
+
PRODUCT_QUERY_TIMEOUT_BOUNDS,
|
|
76
|
+
PURCHASE_LAUNCH_TIMEOUT_BOUNDS,
|
|
77
|
+
STALE_AFTER_BOUNDS,
|
|
78
|
+
SYNC_TIMEOUT_BOUNDS,
|
|
79
|
+
} from './core/timers';
|
|
80
|
+
|
|
81
|
+
// --- Products, pricing, and offers -----------------------------------------
|
|
82
|
+
export type {
|
|
83
|
+
MissingProduct,
|
|
84
|
+
MissingProductReason,
|
|
85
|
+
Offer,
|
|
86
|
+
ProductInfo,
|
|
87
|
+
ProductQueryResult,
|
|
88
|
+
} from './engine/products';
|
|
89
|
+
|
|
90
|
+
// --- Subscriptions ---------------------------------------------------------
|
|
91
|
+
export type {
|
|
92
|
+
PriceChangeResult,
|
|
93
|
+
ProrationMode,
|
|
94
|
+
} from './engine/subscriptions';
|
|
95
|
+
|
|
96
|
+
// --- Entitlements ----------------------------------------------------------
|
|
97
|
+
export type {
|
|
98
|
+
EntitlementMap,
|
|
99
|
+
EntitlementState,
|
|
100
|
+
EntitlementStatus,
|
|
101
|
+
SubscriptionDetail,
|
|
102
|
+
} from './entitlements/aggregate';
|
|
103
|
+
export type { RenewalState } from './entitlements/policy';
|
|
104
|
+
|
|
105
|
+
// --- Store provider --------------------------------------------------------
|
|
106
|
+
// Named for the concept, not the store: the same call returns the StoreKit
|
|
107
|
+
// adapter once iOS exists, so no consumer line changes (FR-004, FR-007). On a
|
|
108
|
+
// platform with no adapter it returns a stub that reports `unavailable` rather
|
|
109
|
+
// than throwing, so a host app still starts and runs (FR-021, FR-023).
|
|
110
|
+
export { createStoreProvider } from './adapter/createStoreProvider';
|
|
111
|
+
export type { StoreProviderOptions } from './adapter/createStoreProvider';
|
|
112
|
+
|
|
113
|
+
// --- Engine ----------------------------------------------------------------
|
|
114
|
+
export { createEngine } from './engine/engine';
|
|
115
|
+
export type {
|
|
116
|
+
EngineDeps,
|
|
117
|
+
InitOutcome,
|
|
118
|
+
PaymentEngine,
|
|
119
|
+
RestoreOutcome,
|
|
120
|
+
SyncOutcome,
|
|
121
|
+
} from './engine/engine';
|
|
122
|
+
|
|
123
|
+
// --- React -----------------------------------------------------------------
|
|
124
|
+
export { PaymentProvider, usePaymentEngine } from './react/PaymentProvider';
|
|
125
|
+
export type { PaymentProviderProps } from './react/PaymentProvider';
|
|
126
|
+
export {
|
|
127
|
+
useEntitlement,
|
|
128
|
+
useEntitlements,
|
|
129
|
+
usePaymentStatus,
|
|
130
|
+
useProducts,
|
|
131
|
+
usePurchase,
|
|
132
|
+
useSync,
|
|
133
|
+
} from './react/hooks';
|
|
134
|
+
export type {
|
|
135
|
+
UseProductsResult,
|
|
136
|
+
UsePurchaseResult,
|
|
137
|
+
UseSyncResult,
|
|
138
|
+
} from './react/hooks';
|
|
139
|
+
|
|
140
|
+
// --- Diagnostics -----------------------------------------------------------
|
|
141
|
+
export { PaymentConfigError, PaymentError, PAYMENT_ERROR_CODES } from './diagnostics/errors';
|
|
142
|
+
export type { PaymentErrorCode, PaymentOperation } from './diagnostics/errors';
|
|
143
|
+
export type {
|
|
144
|
+
PaymentEvent,
|
|
145
|
+
PaymentEventListener,
|
|
146
|
+
PaymentEventType,
|
|
147
|
+
SyncTrigger,
|
|
148
|
+
} from './diagnostics/events';
|
|
149
|
+
|
|
150
|
+
// --- Localizable copy ------------------------------------------------------
|
|
151
|
+
// Keys and English defaults only. The package takes no i18n dependency and
|
|
152
|
+
// resolves nothing itself; apps map these through their own i18n (FR-085).
|
|
153
|
+
export {
|
|
154
|
+
DEFAULT_DEVELOPER_MESSAGES,
|
|
155
|
+
DEFAULT_MESSAGES,
|
|
156
|
+
DEFAULT_USER_MESSAGES,
|
|
157
|
+
DEVELOPER_MESSAGE_KEYS,
|
|
158
|
+
USER_MESSAGE_KEYS,
|
|
159
|
+
isUserMessageKey,
|
|
160
|
+
} from './diagnostics/messageKeys';
|
|
161
|
+
export type {
|
|
162
|
+
PaymentDeveloperMessageKey,
|
|
163
|
+
PaymentDeveloperMessageMap,
|
|
164
|
+
PaymentMessageKey,
|
|
165
|
+
PaymentMessageMap,
|
|
166
|
+
PaymentUserMessageKey,
|
|
167
|
+
PaymentUserMessageMap,
|
|
168
|
+
} from './diagnostics/messageKeys';
|