@tenthbilling/expo 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/LICENSE +21 -0
- package/README.md +41 -2
- package/android/build.gradle +23 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/tenthbilling/expo/TenthStoreModule.kt +669 -0
- package/app.plugin.js +3 -0
- package/dist/api/client.d.ts +107 -0
- package/dist/api/client.js +139 -0
- package/dist/api/types.d.ts +17 -0
- package/dist/api/types.js +1 -0
- package/dist/cache.d.ts +23 -0
- package/dist/cache.js +40 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +42 -0
- package/dist/generated/openapi.d.ts +3208 -0
- package/dist/generated/openapi.js +1 -0
- package/dist/identity.d.ts +35 -0
- package/dist/identity.js +92 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +10 -0
- package/dist/keys.d.ts +8 -0
- package/dist/keys.js +14 -0
- package/dist/native/module.d.ts +12 -0
- package/dist/native/module.js +14 -0
- package/dist/native/spec.d.ts +166 -0
- package/dist/native/spec.js +1 -0
- package/dist/products.d.ts +32 -0
- package/dist/products.js +213 -0
- package/dist/react/Paywall.d.ts +28 -0
- package/dist/react/Paywall.js +19 -0
- package/dist/react/TenthProvider.d.ts +11 -0
- package/dist/react/TenthProvider.js +23 -0
- package/dist/react/context.d.ts +4 -0
- package/dist/react/context.js +8 -0
- package/dist/react/hooks.d.ts +48 -0
- package/dist/react/hooks.js +68 -0
- package/dist/react/present.d.ts +15 -0
- package/dist/react/present.js +47 -0
- package/dist/react/usePaywall.d.ts +12 -0
- package/dist/react/usePaywall.js +109 -0
- package/dist/storage/storage.d.ts +14 -0
- package/dist/storage/storage.js +63 -0
- package/dist/storage/storage.web.d.ts +4 -0
- package/dist/storage/storage.web.js +35 -0
- package/dist/store/apple.d.ts +19 -0
- package/dist/store/apple.js +127 -0
- package/dist/store/context.d.ts +46 -0
- package/dist/store/context.js +21 -0
- package/dist/store/google.d.ts +22 -0
- package/dist/store/google.js +145 -0
- package/dist/store/preview.d.ts +16 -0
- package/dist/store/preview.js +68 -0
- package/dist/stripe/appearance.d.ts +47 -0
- package/dist/stripe/appearance.js +44 -0
- package/dist/stripe/flows.d.ts +23 -0
- package/dist/stripe/flows.js +145 -0
- package/dist/stripe/payment-sheet.d.ts +1 -0
- package/dist/stripe/payment-sheet.js +24 -0
- package/dist/stripe/sheet-registry.d.ts +16 -0
- package/dist/stripe/sheet-registry.js +7 -0
- package/dist/stripe/web-step.d.ts +10 -0
- package/dist/stripe/web-step.js +4 -0
- package/dist/stripe/web-step.web.d.ts +4 -0
- package/dist/stripe/web-step.web.js +55 -0
- package/dist/tenth.d.ts +125 -0
- package/dist/tenth.js +329 -0
- package/dist/updates.d.ts +15 -0
- package/dist/updates.js +30 -0
- package/expo-module.config.json +9 -0
- package/ios/CountryCodes.swift +52 -0
- package/ios/TenthBilling.podspec +31 -0
- package/ios/TenthStoreExceptions.swift +88 -0
- package/ios/TenthStoreMapping.swift +108 -0
- package/ios/TenthStoreModule.swift +213 -0
- package/ios/TenthStoreRecords.swift +105 -0
- package/package.json +94 -3
- package/plugin/withTenth.js +249 -0
package/dist/tenth.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { PaywallProduct, PurchaseResult, RestoreResult } from '@tenthbilling/paywall';
|
|
2
|
+
import { type ServedPaywall } from '@tenthbilling/paywall/schema';
|
|
3
|
+
import { ApiClient } from './api/client';
|
|
4
|
+
import type { CustomerState, Offering, Platform as ApiPlatform, PurchasePath } from './api/types';
|
|
5
|
+
import { TenthSdkError } from './errors';
|
|
6
|
+
import { type Environment } from './keys';
|
|
7
|
+
import { type NativeStore } from './native/module';
|
|
8
|
+
import { type KeyValueStore } from './storage/storage';
|
|
9
|
+
import { type PurchaseOptions, type StoreContext } from './store/context';
|
|
10
|
+
import { type RefreshReason, type UpdateSource } from './updates';
|
|
11
|
+
export interface TenthConfig {
|
|
12
|
+
/** Publishable key (tenth_test_pk_… / tenth_live_pk_…). Secret keys are refused. */
|
|
13
|
+
apiKey: string;
|
|
14
|
+
/** Returns the signed-in user's token (Clerk's getToken, or your OIDC provider's), or null. */
|
|
15
|
+
getCustomerToken?: () => Promise<string | null | undefined>;
|
|
16
|
+
/** Defaults to https://api.tenthbilling.com. */
|
|
17
|
+
apiUrl?: string;
|
|
18
|
+
/** How long cached access survives without reaching Tenth. Default 72 hours. */
|
|
19
|
+
offlineGraceHours?: number;
|
|
20
|
+
/** Force preview mode (simulated purchases). By default it turns on when the native store isn't available. */
|
|
21
|
+
preview?: boolean;
|
|
22
|
+
/** Locale for prices and paywall copy. Defaults to the device's. */
|
|
23
|
+
locale?: string;
|
|
24
|
+
/** URL scheme for returning from hosted Checkout and bank redirects. Defaults to expo.scheme. */
|
|
25
|
+
urlScheme?: string;
|
|
26
|
+
/** Name on Stripe's PaymentSheet. Defaults to the app's name. */
|
|
27
|
+
merchantDisplayName?: string;
|
|
28
|
+
/** For tests. */
|
|
29
|
+
storage?: KeyValueStore;
|
|
30
|
+
fetch?: typeof fetch;
|
|
31
|
+
updateSource?: UpdateSource;
|
|
32
|
+
nativeStore?: NativeStore | null;
|
|
33
|
+
platform?: ApiPlatform;
|
|
34
|
+
now?: () => number;
|
|
35
|
+
}
|
|
36
|
+
export interface TenthSnapshot {
|
|
37
|
+
status: 'loading' | 'ready' | 'error';
|
|
38
|
+
/** What the app should act on: the latest state, with offline grace and preview purchases applied. */
|
|
39
|
+
customer: CustomerState | null;
|
|
40
|
+
/** True while showing the device cache, before the first successful refresh. */
|
|
41
|
+
fromCache: boolean;
|
|
42
|
+
/** When Tenth last confirmed the state (ms since the epoch). */
|
|
43
|
+
confirmedAt: number | null;
|
|
44
|
+
error: TenthSdkError | null;
|
|
45
|
+
preview: boolean;
|
|
46
|
+
environment: Environment;
|
|
47
|
+
}
|
|
48
|
+
export interface LoadedOffering {
|
|
49
|
+
offering: Offering | null;
|
|
50
|
+
/** The published paywall this SDK can render, or null (use the bundled default). */
|
|
51
|
+
paywall: ServedPaywall | null;
|
|
52
|
+
/** Products the customer can buy here, with store or Stripe prices. */
|
|
53
|
+
products: PaywallProduct[];
|
|
54
|
+
path: PurchasePath | null;
|
|
55
|
+
}
|
|
56
|
+
export declare class Tenth implements StoreContext {
|
|
57
|
+
private readonly config;
|
|
58
|
+
readonly environment: Environment;
|
|
59
|
+
readonly preview: boolean;
|
|
60
|
+
readonly locale: string;
|
|
61
|
+
readonly api: ApiClient;
|
|
62
|
+
private readonly platform;
|
|
63
|
+
private readonly store;
|
|
64
|
+
private readonly identities;
|
|
65
|
+
private readonly graceHours;
|
|
66
|
+
private readonly now;
|
|
67
|
+
private readonly updateSource;
|
|
68
|
+
private readonly native;
|
|
69
|
+
private readonly urlScheme;
|
|
70
|
+
private readonly merchantDisplayName;
|
|
71
|
+
private apple;
|
|
72
|
+
private google;
|
|
73
|
+
private previewStore;
|
|
74
|
+
private web;
|
|
75
|
+
private raw;
|
|
76
|
+
private rawFromCache;
|
|
77
|
+
private confirmedAt;
|
|
78
|
+
private cacheIdentity;
|
|
79
|
+
private status;
|
|
80
|
+
private error;
|
|
81
|
+
private snapshot;
|
|
82
|
+
private listeners;
|
|
83
|
+
private inflight;
|
|
84
|
+
private offerings;
|
|
85
|
+
private stops;
|
|
86
|
+
constructor(config: TenthConfig);
|
|
87
|
+
/** Lets the provider swap in the latest getCustomerToken without recreating the SDK. */
|
|
88
|
+
setTokenGetter(getCustomerToken: TenthConfig['getCustomerToken']): void;
|
|
89
|
+
subscribe: (listener: () => void) => (() => void);
|
|
90
|
+
getSnapshot: () => TenthSnapshot;
|
|
91
|
+
private buildSnapshot;
|
|
92
|
+
private emit;
|
|
93
|
+
/** Serves the cache, then refreshes on launch and foreground, and starts store listeners. */
|
|
94
|
+
start(): () => void;
|
|
95
|
+
private loadCache;
|
|
96
|
+
identity(): Promise<import("./identity").ResolvedIdentity>;
|
|
97
|
+
customer(): Promise<CustomerState>;
|
|
98
|
+
apply(state: CustomerState, cacheIdentity?: string | null): void;
|
|
99
|
+
refreshUntil(done: (state: CustomerState) => boolean, options?: {
|
|
100
|
+
timeoutMs?: number;
|
|
101
|
+
}): Promise<boolean>;
|
|
102
|
+
/** Fetches the customer's state from Tenth. Concurrent calls share one request. */
|
|
103
|
+
refresh(reason?: RefreshReason): Promise<CustomerState | null>;
|
|
104
|
+
private doRefresh;
|
|
105
|
+
private flowFor;
|
|
106
|
+
private country;
|
|
107
|
+
/** The offering for this customer and platform (`key` null: the current, targeted one). */
|
|
108
|
+
getOffering(key?: string | null, options?: {
|
|
109
|
+
force?: boolean;
|
|
110
|
+
}): Promise<LoadedOffering>;
|
|
111
|
+
private findProduct;
|
|
112
|
+
/** Buys a product from a loaded offering (loads the current offering if needed). Never throws. */
|
|
113
|
+
purchase(productKey: string, options?: PurchaseOptions & {
|
|
114
|
+
offering?: string | null;
|
|
115
|
+
}): Promise<PurchaseResult>;
|
|
116
|
+
/** Restore purchases: the store's current purchases, sent to Tenth. */
|
|
117
|
+
restore(): Promise<RestoreResult>;
|
|
118
|
+
/** App-side credit spend, when the credit type allows it. */
|
|
119
|
+
debitCredits(creditType: string, amount: number, options: {
|
|
120
|
+
idempotencyKey: string;
|
|
121
|
+
metadata?: Record<string, string>;
|
|
122
|
+
}): Promise<void>;
|
|
123
|
+
/** Opens where the customer manages a subscription: App Store, Google Play or Stripe's portal. */
|
|
124
|
+
manageSubscription(subscriptionId?: string): Promise<void>;
|
|
125
|
+
}
|
package/dist/tenth.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { PAYWALL_SCHEMA_VERSION, parseServedPaywall, } from '@tenthbilling/paywall/schema';
|
|
2
|
+
import Constants from 'expo-constants';
|
|
3
|
+
import * as WebBrowser from 'expo-web-browser';
|
|
4
|
+
import { Platform } from 'react-native';
|
|
5
|
+
import { ApiClient } from './api/client';
|
|
6
|
+
import { DEFAULT_OFFLINE_GRACE_HOURS, effectiveState, readCache, writeCache } from './cache';
|
|
7
|
+
import { TenthSdkError, messageFor, toSdkError } from './errors';
|
|
8
|
+
import { IdentityManager } from './identity';
|
|
9
|
+
import { parsePublishableKey } from './keys';
|
|
10
|
+
import { loadNativeStore } from './native/module';
|
|
11
|
+
import { defaultStore } from './storage/storage';
|
|
12
|
+
import { AppleStore } from './store/apple';
|
|
13
|
+
import { failed, restoreFailed, } from './store/context';
|
|
14
|
+
import { GoogleStore } from './store/google';
|
|
15
|
+
import { PreviewStore, previewCustomer } from './store/preview';
|
|
16
|
+
import { LinkOutFlow, PaymentSheetFlow, StripeWebFlow } from './stripe/flows';
|
|
17
|
+
import { AppStateUpdateSource, pollUntil } from './updates';
|
|
18
|
+
const OFFERING_TTL_MS = 5 * 60_000;
|
|
19
|
+
export class Tenth {
|
|
20
|
+
config;
|
|
21
|
+
environment;
|
|
22
|
+
preview;
|
|
23
|
+
locale;
|
|
24
|
+
api;
|
|
25
|
+
platform;
|
|
26
|
+
store;
|
|
27
|
+
identities;
|
|
28
|
+
graceHours;
|
|
29
|
+
now;
|
|
30
|
+
updateSource;
|
|
31
|
+
native;
|
|
32
|
+
urlScheme;
|
|
33
|
+
merchantDisplayName;
|
|
34
|
+
apple = null;
|
|
35
|
+
google = null;
|
|
36
|
+
previewStore;
|
|
37
|
+
web = new StripeWebFlow();
|
|
38
|
+
raw = null;
|
|
39
|
+
rawFromCache = false;
|
|
40
|
+
confirmedAt = null;
|
|
41
|
+
cacheIdentity = null;
|
|
42
|
+
status = 'loading';
|
|
43
|
+
error = null;
|
|
44
|
+
snapshot;
|
|
45
|
+
listeners = new Set();
|
|
46
|
+
inflight = null;
|
|
47
|
+
offerings = new Map();
|
|
48
|
+
stops = [];
|
|
49
|
+
constructor(config) {
|
|
50
|
+
this.config = config;
|
|
51
|
+
this.environment = parsePublishableKey(config.apiKey).environment;
|
|
52
|
+
this.api = new ApiClient({
|
|
53
|
+
apiKey: config.apiKey.trim(),
|
|
54
|
+
baseUrl: config.apiUrl,
|
|
55
|
+
fetch: config.fetch,
|
|
56
|
+
});
|
|
57
|
+
this.platform =
|
|
58
|
+
config.platform ?? (Platform.OS === 'ios' || Platform.OS === 'android' ? Platform.OS : 'web');
|
|
59
|
+
this.native = config.nativeStore !== undefined ? config.nativeStore : loadNativeStore();
|
|
60
|
+
this.preview = config.preview ?? (this.platform !== 'web' && this.native === null);
|
|
61
|
+
this.store = config.storage ?? defaultStore();
|
|
62
|
+
this.identities = new IdentityManager(this.store, this.environment, () => this.config.getCustomerToken ? this.config.getCustomerToken() : Promise.resolve(null));
|
|
63
|
+
this.graceHours = config.offlineGraceHours ?? DEFAULT_OFFLINE_GRACE_HOURS;
|
|
64
|
+
this.now = config.now ?? Date.now;
|
|
65
|
+
this.locale = config.locale ?? deviceLocale();
|
|
66
|
+
this.updateSource = config.updateSource ?? new AppStateUpdateSource();
|
|
67
|
+
const expo = Constants.expoConfig;
|
|
68
|
+
const scheme = config.urlScheme ?? (Array.isArray(expo?.scheme) ? expo.scheme[0] : expo?.scheme);
|
|
69
|
+
this.urlScheme = scheme && /^[a-z][a-z0-9+.-]{1,63}$/.test(scheme) ? scheme : null;
|
|
70
|
+
this.merchantDisplayName = config.merchantDisplayName ?? expo?.name ?? 'Store';
|
|
71
|
+
this.previewStore = new PreviewStore(this.environment, () => this.emit());
|
|
72
|
+
if (!this.preview && this.native?.platform === 'ios')
|
|
73
|
+
this.apple = new AppleStore(this.native.module);
|
|
74
|
+
if (!this.preview && this.native?.platform === 'android') {
|
|
75
|
+
this.google = new GoogleStore(this.native.module, expo?.android?.package ?? null);
|
|
76
|
+
}
|
|
77
|
+
this.snapshot = this.buildSnapshot();
|
|
78
|
+
}
|
|
79
|
+
setTokenGetter(getCustomerToken) {
|
|
80
|
+
this.config.getCustomerToken = getCustomerToken;
|
|
81
|
+
}
|
|
82
|
+
subscribe = (listener) => {
|
|
83
|
+
this.listeners.add(listener);
|
|
84
|
+
return () => this.listeners.delete(listener);
|
|
85
|
+
};
|
|
86
|
+
getSnapshot = () => this.snapshot;
|
|
87
|
+
buildSnapshot() {
|
|
88
|
+
let customer = null;
|
|
89
|
+
if (this.raw && this.confirmedAt !== null) {
|
|
90
|
+
customer = effectiveState(this.raw, this.confirmedAt, this.now(), this.graceHours, this.rawFromCache);
|
|
91
|
+
}
|
|
92
|
+
if (this.preview)
|
|
93
|
+
customer = this.previewStore.overlay(customer ?? previewCustomer());
|
|
94
|
+
return {
|
|
95
|
+
status: this.status,
|
|
96
|
+
customer,
|
|
97
|
+
fromCache: this.rawFromCache,
|
|
98
|
+
confirmedAt: this.confirmedAt,
|
|
99
|
+
error: this.error,
|
|
100
|
+
preview: this.preview,
|
|
101
|
+
environment: this.environment,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
emit() {
|
|
105
|
+
this.snapshot = this.buildSnapshot();
|
|
106
|
+
for (const l of this.listeners)
|
|
107
|
+
l();
|
|
108
|
+
}
|
|
109
|
+
start() {
|
|
110
|
+
void this.loadCache();
|
|
111
|
+
this.stops.push(this.updateSource.start((reason) => void this.refresh(reason)));
|
|
112
|
+
if (this.apple)
|
|
113
|
+
this.stops.push(this.apple.start(this));
|
|
114
|
+
if (this.google)
|
|
115
|
+
this.stops.push(this.google.start(this));
|
|
116
|
+
return () => {
|
|
117
|
+
for (const stop of this.stops.splice(0))
|
|
118
|
+
stop();
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async loadCache() {
|
|
122
|
+
const { cacheIdentity } = await this.identities.resolve();
|
|
123
|
+
if (this.raw)
|
|
124
|
+
return;
|
|
125
|
+
const cached = await readCache(this.store, this.environment, cacheIdentity);
|
|
126
|
+
if (cached && !this.raw) {
|
|
127
|
+
this.raw = cached.state;
|
|
128
|
+
this.rawFromCache = true;
|
|
129
|
+
this.confirmedAt = cached.confirmedAt;
|
|
130
|
+
this.cacheIdentity = cacheIdentity;
|
|
131
|
+
this.status = 'ready';
|
|
132
|
+
this.emit();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
identity() {
|
|
136
|
+
return this.identities.resolve();
|
|
137
|
+
}
|
|
138
|
+
async customer() {
|
|
139
|
+
const state = this.raw && !this.rawFromCache ? this.raw : await this.refresh('purchase');
|
|
140
|
+
if (!state)
|
|
141
|
+
throw this.error ?? new TenthSdkError('network_error', messageFor('network_error', ''));
|
|
142
|
+
return state;
|
|
143
|
+
}
|
|
144
|
+
apply(state, cacheIdentity = this.cacheIdentity) {
|
|
145
|
+
this.raw = state;
|
|
146
|
+
this.rawFromCache = false;
|
|
147
|
+
this.confirmedAt = this.now();
|
|
148
|
+
this.status = 'ready';
|
|
149
|
+
this.error = null;
|
|
150
|
+
if (cacheIdentity) {
|
|
151
|
+
this.cacheIdentity = cacheIdentity;
|
|
152
|
+
void writeCache(this.store, this.environment, {
|
|
153
|
+
identity: cacheIdentity,
|
|
154
|
+
state,
|
|
155
|
+
confirmedAt: this.confirmedAt,
|
|
156
|
+
}).catch(() => undefined);
|
|
157
|
+
}
|
|
158
|
+
this.emit();
|
|
159
|
+
}
|
|
160
|
+
refreshUntil(done, options = {}) {
|
|
161
|
+
return pollUntil(async () => {
|
|
162
|
+
const s = await this.refresh('purchase');
|
|
163
|
+
return s ? done(s) : false;
|
|
164
|
+
}, { timeoutMs: options.timeoutMs });
|
|
165
|
+
}
|
|
166
|
+
refresh(reason = 'manual') {
|
|
167
|
+
void reason;
|
|
168
|
+
this.inflight ??= this.doRefresh().finally(() => {
|
|
169
|
+
this.inflight = null;
|
|
170
|
+
});
|
|
171
|
+
return this.inflight;
|
|
172
|
+
}
|
|
173
|
+
async doRefresh() {
|
|
174
|
+
const resolved = await this.identities.resolve();
|
|
175
|
+
if (this.cacheIdentity && resolved.cacheIdentity !== this.cacheIdentity) {
|
|
176
|
+
this.raw = null;
|
|
177
|
+
this.confirmedAt = null;
|
|
178
|
+
this.rawFromCache = false;
|
|
179
|
+
this.status = 'loading';
|
|
180
|
+
this.offerings.clear();
|
|
181
|
+
this.emit();
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
let state = null;
|
|
185
|
+
if (resolved.identity.kind === 'token') {
|
|
186
|
+
const anon = await this.identities.pendingAlias();
|
|
187
|
+
if (anon) {
|
|
188
|
+
try {
|
|
189
|
+
state = (await this.api.alias(resolved.identity, anon)).customer;
|
|
190
|
+
await this.identities.aliasDone(anon);
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
const e = toSdkError(err);
|
|
194
|
+
if (!e.retryable)
|
|
195
|
+
await this.identities.aliasDone(anon);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
state ??= await this.api.getCustomer(resolved.identity);
|
|
200
|
+
this.apply(state, resolved.cacheIdentity);
|
|
201
|
+
return state;
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
this.error = toSdkError(err);
|
|
205
|
+
this.status = this.raw ? 'ready' : 'error';
|
|
206
|
+
this.emit();
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
flowFor(path) {
|
|
211
|
+
if (!path || path === 'none')
|
|
212
|
+
return null;
|
|
213
|
+
if (this.preview)
|
|
214
|
+
return this.previewStore;
|
|
215
|
+
if (this.platform === 'web')
|
|
216
|
+
return this.web;
|
|
217
|
+
if (path === 'web_link_out')
|
|
218
|
+
return new LinkOutFlow(this.urlScheme);
|
|
219
|
+
if (path === 'in_app_stripe')
|
|
220
|
+
return new PaymentSheetFlow(this.merchantDisplayName, this.urlScheme);
|
|
221
|
+
return this.apple ?? this.google;
|
|
222
|
+
}
|
|
223
|
+
async country() {
|
|
224
|
+
const c = await (this.apple ?? this.google)?.storefrontCountry();
|
|
225
|
+
return c && /^[A-Z]{2}$/.test(c) ? c : undefined;
|
|
226
|
+
}
|
|
227
|
+
async getOffering(key = null, options = {}) {
|
|
228
|
+
const cacheKey = key ?? '';
|
|
229
|
+
const hit = this.offerings.get(cacheKey);
|
|
230
|
+
if (hit && !options.force && this.now() - hit.at < OFFERING_TTL_MS)
|
|
231
|
+
return hit.value;
|
|
232
|
+
const { identity } = await this.identities.resolve();
|
|
233
|
+
const res = await this.api.getOffering(identity, {
|
|
234
|
+
key,
|
|
235
|
+
platform: this.platform,
|
|
236
|
+
country: await this.country(),
|
|
237
|
+
appVersion: Constants.expoConfig?.version,
|
|
238
|
+
paywallSchema: PAYWALL_SCHEMA_VERSION,
|
|
239
|
+
});
|
|
240
|
+
const offering = res.offering;
|
|
241
|
+
const flow = this.flowFor(offering?.purchase_path ?? null);
|
|
242
|
+
const products = offering && flow ? await flow.loadProducts(offering.products, this) : [];
|
|
243
|
+
const value = {
|
|
244
|
+
offering,
|
|
245
|
+
paywall: parseServedPaywall(res.paywall ?? null, this.api.origin),
|
|
246
|
+
products,
|
|
247
|
+
path: offering?.purchase_path ?? null,
|
|
248
|
+
};
|
|
249
|
+
this.offerings.set(cacheKey, { at: this.now(), value });
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
findProduct(productKey) {
|
|
253
|
+
for (const { value } of this.offerings.values()) {
|
|
254
|
+
const product = value.offering?.products.find((p) => p.key === productKey);
|
|
255
|
+
if (product && value.path)
|
|
256
|
+
return { product, path: value.path };
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
async purchase(productKey, options = {}) {
|
|
261
|
+
try {
|
|
262
|
+
let found = this.findProduct(productKey);
|
|
263
|
+
if (!found) {
|
|
264
|
+
await this.getOffering(options.offering ?? null);
|
|
265
|
+
found = this.findProduct(productKey);
|
|
266
|
+
}
|
|
267
|
+
const flow = found ? this.flowFor(found.path) : null;
|
|
268
|
+
if (!found || !flow) {
|
|
269
|
+
throw new TenthSdkError('product_unavailable', messageFor('product_unavailable', ''));
|
|
270
|
+
}
|
|
271
|
+
const result = await flow.purchase(found.product, this, options);
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
return failed(err);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async restore() {
|
|
279
|
+
try {
|
|
280
|
+
if (this.preview)
|
|
281
|
+
return await this.previewStore.restore();
|
|
282
|
+
const store = this.apple ?? this.google;
|
|
283
|
+
const result = store ? await store.restore(this) : { status: 'nothing_to_restore' };
|
|
284
|
+
await this.refresh('manual');
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
return restoreFailed(err);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async debitCredits(creditType, amount, options) {
|
|
292
|
+
const { identity } = await this.identities.resolve();
|
|
293
|
+
await this.api.debit(identity, creditType, {
|
|
294
|
+
amount,
|
|
295
|
+
idempotency_key: options.idempotencyKey,
|
|
296
|
+
metadata: options.metadata,
|
|
297
|
+
});
|
|
298
|
+
await this.refresh('manual');
|
|
299
|
+
}
|
|
300
|
+
async manageSubscription(subscriptionId) {
|
|
301
|
+
const subs = this.raw?.subscriptions ?? [];
|
|
302
|
+
const sub = subscriptionId
|
|
303
|
+
? subs.find((s) => s.id === subscriptionId)
|
|
304
|
+
: (subs.find((s) => s.will_renew) ?? subs[0]);
|
|
305
|
+
if (sub?.store === 'apple' && this.apple)
|
|
306
|
+
return this.apple.manage();
|
|
307
|
+
if (sub?.store === 'google' && this.google) {
|
|
308
|
+
const product = Object.values(Object.fromEntries(this.offerings))
|
|
309
|
+
.flatMap((o) => o.value.offering?.products ?? [])
|
|
310
|
+
.find((p) => p.key === sub.product);
|
|
311
|
+
return this.google.manage(product?.store_product_id ?? null);
|
|
312
|
+
}
|
|
313
|
+
if (!sub)
|
|
314
|
+
return;
|
|
315
|
+
const { identity } = await this.identities.resolve();
|
|
316
|
+
const { url } = await this.api.managementUrl(identity, sub.id);
|
|
317
|
+
if (!url.startsWith('https://'))
|
|
318
|
+
throw new TenthSdkError('invalid_url', 'The management link is not secure.');
|
|
319
|
+
await WebBrowser.openBrowserAsync(url);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function deviceLocale() {
|
|
323
|
+
try {
|
|
324
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || 'en';
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
return 'en';
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface UpdateSource {
|
|
2
|
+
/** Starts delivering refresh triggers. Returns a function that stops them. */
|
|
3
|
+
start(trigger: (reason: RefreshReason) => void): () => void;
|
|
4
|
+
}
|
|
5
|
+
export type RefreshReason = 'launch' | 'foreground' | 'purchase' | 'manual' | 'identity';
|
|
6
|
+
/** Launch plus every return to the foreground. */
|
|
7
|
+
export declare class AppStateUpdateSource implements UpdateSource {
|
|
8
|
+
start(trigger: (reason: RefreshReason) => void): () => void;
|
|
9
|
+
}
|
|
10
|
+
/** Post-payment polling: every 2 s for up to 30 s, until `done` says so. */
|
|
11
|
+
export declare function pollUntil(check: () => Promise<boolean>, options?: {
|
|
12
|
+
intervalMs?: number;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
sleep?: (ms: number) => Promise<void>;
|
|
15
|
+
}): Promise<boolean>;
|
package/dist/updates.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AppState } from 'react-native';
|
|
2
|
+
export class AppStateUpdateSource {
|
|
3
|
+
start(trigger) {
|
|
4
|
+
let last = AppState.currentState ?? 'active';
|
|
5
|
+
trigger('launch');
|
|
6
|
+
const sub = AppState.addEventListener('change', (next) => {
|
|
7
|
+
if (last.match(/inactive|background/) && next === 'active')
|
|
8
|
+
trigger('foreground');
|
|
9
|
+
last = next;
|
|
10
|
+
});
|
|
11
|
+
return () => sub.remove();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export async function pollUntil(check, options = {}) {
|
|
15
|
+
const interval = options.intervalMs ?? 2000;
|
|
16
|
+
const timeout = options.timeoutMs ?? 30_000;
|
|
17
|
+
const sleep = options.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
18
|
+
for (let waited = 0; waited <= timeout; waited += interval) {
|
|
19
|
+
try {
|
|
20
|
+
if (await check())
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
}
|
|
25
|
+
if (waited + interval > timeout)
|
|
26
|
+
break;
|
|
27
|
+
await sleep(interval);
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// StoreKit's Storefront.countryCode is ISO 3166-1 alpha-3 ("USA"), while Tenth and the JS contract
|
|
2
|
+
// use alpha-2 ("US"). Foundation has no alpha-3 → alpha-2 conversion (Locale.Region takes alpha-2
|
|
3
|
+
// or UN M.49 codes only), so this is the full ISO 3166-1 table: all 249 officially assigned codes,
|
|
4
|
+
// plus Kosovo's user-assigned codes (XKX, and XKS, which the App Store uses), both → XK.
|
|
5
|
+
// An unknown code yields nil rather than a guess.
|
|
6
|
+
|
|
7
|
+
enum CountryCodes {
|
|
8
|
+
static func alpha2(fromAlpha3 code: String) -> String? {
|
|
9
|
+
return table[code.uppercased()]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private static let table: [String: String] = [
|
|
13
|
+
"ABW": "AW", "AFG": "AF", "AGO": "AO", "AIA": "AI", "ALA": "AX", "ALB": "AL", "AND": "AD",
|
|
14
|
+
"ARE": "AE", "ARG": "AR", "ARM": "AM", "ASM": "AS", "ATA": "AQ", "ATF": "TF", "ATG": "AG",
|
|
15
|
+
"AUS": "AU", "AUT": "AT", "AZE": "AZ", "BDI": "BI", "BEL": "BE", "BEN": "BJ", "BES": "BQ",
|
|
16
|
+
"BFA": "BF", "BGD": "BD", "BGR": "BG", "BHR": "BH", "BHS": "BS", "BIH": "BA", "BLM": "BL",
|
|
17
|
+
"BLR": "BY", "BLZ": "BZ", "BMU": "BM", "BOL": "BO", "BRA": "BR", "BRB": "BB", "BRN": "BN",
|
|
18
|
+
"BTN": "BT", "BVT": "BV", "BWA": "BW", "CAF": "CF", "CAN": "CA", "CCK": "CC", "CHE": "CH",
|
|
19
|
+
"CHL": "CL", "CHN": "CN", "CIV": "CI", "CMR": "CM", "COD": "CD", "COG": "CG", "COK": "CK",
|
|
20
|
+
"COL": "CO", "COM": "KM", "CPV": "CV", "CRI": "CR", "CUB": "CU", "CUW": "CW", "CXR": "CX",
|
|
21
|
+
"CYM": "KY", "CYP": "CY", "CZE": "CZ", "DEU": "DE", "DJI": "DJ", "DMA": "DM", "DNK": "DK",
|
|
22
|
+
"DOM": "DO", "DZA": "DZ", "ECU": "EC", "EGY": "EG", "ERI": "ER", "ESH": "EH", "ESP": "ES",
|
|
23
|
+
"EST": "EE", "ETH": "ET", "FIN": "FI", "FJI": "FJ", "FLK": "FK", "FRA": "FR", "FRO": "FO",
|
|
24
|
+
"FSM": "FM", "GAB": "GA", "GBR": "GB", "GEO": "GE", "GGY": "GG", "GHA": "GH", "GIB": "GI",
|
|
25
|
+
"GIN": "GN", "GLP": "GP", "GMB": "GM", "GNB": "GW", "GNQ": "GQ", "GRC": "GR", "GRD": "GD",
|
|
26
|
+
"GRL": "GL", "GTM": "GT", "GUF": "GF", "GUM": "GU", "GUY": "GY", "HKG": "HK", "HMD": "HM",
|
|
27
|
+
"HND": "HN", "HRV": "HR", "HTI": "HT", "HUN": "HU", "IDN": "ID", "IMN": "IM", "IND": "IN",
|
|
28
|
+
"IOT": "IO", "IRL": "IE", "IRN": "IR", "IRQ": "IQ", "ISL": "IS", "ISR": "IL", "ITA": "IT",
|
|
29
|
+
"JAM": "JM", "JEY": "JE", "JOR": "JO", "JPN": "JP", "KAZ": "KZ", "KEN": "KE", "KGZ": "KG",
|
|
30
|
+
"KHM": "KH", "KIR": "KI", "KNA": "KN", "KOR": "KR", "KWT": "KW", "LAO": "LA", "LBN": "LB",
|
|
31
|
+
"LBR": "LR", "LBY": "LY", "LCA": "LC", "LIE": "LI", "LKA": "LK", "LSO": "LS", "LTU": "LT",
|
|
32
|
+
"LUX": "LU", "LVA": "LV", "MAC": "MO", "MAF": "MF", "MAR": "MA", "MCO": "MC", "MDA": "MD",
|
|
33
|
+
"MDG": "MG", "MDV": "MV", "MEX": "MX", "MHL": "MH", "MKD": "MK", "MLI": "ML", "MLT": "MT",
|
|
34
|
+
"MMR": "MM", "MNE": "ME", "MNG": "MN", "MNP": "MP", "MOZ": "MZ", "MRT": "MR", "MSR": "MS",
|
|
35
|
+
"MTQ": "MQ", "MUS": "MU", "MWI": "MW", "MYS": "MY", "MYT": "YT", "NAM": "NA", "NCL": "NC",
|
|
36
|
+
"NER": "NE", "NFK": "NF", "NGA": "NG", "NIC": "NI", "NIU": "NU", "NLD": "NL", "NOR": "NO",
|
|
37
|
+
"NPL": "NP", "NRU": "NR", "NZL": "NZ", "OMN": "OM", "PAK": "PK", "PAN": "PA", "PCN": "PN",
|
|
38
|
+
"PER": "PE", "PHL": "PH", "PLW": "PW", "PNG": "PG", "POL": "PL", "PRI": "PR", "PRK": "KP",
|
|
39
|
+
"PRT": "PT", "PRY": "PY", "PSE": "PS", "PYF": "PF", "QAT": "QA", "REU": "RE", "ROU": "RO",
|
|
40
|
+
"RUS": "RU", "RWA": "RW", "SAU": "SA", "SDN": "SD", "SEN": "SN", "SGP": "SG", "SGS": "GS",
|
|
41
|
+
"SHN": "SH", "SJM": "SJ", "SLB": "SB", "SLE": "SL", "SLV": "SV", "SMR": "SM", "SOM": "SO",
|
|
42
|
+
"SPM": "PM", "SRB": "RS", "SSD": "SS", "STP": "ST", "SUR": "SR", "SVK": "SK", "SVN": "SI",
|
|
43
|
+
"SWE": "SE", "SWZ": "SZ", "SXM": "SX", "SYC": "SC", "SYR": "SY", "TCA": "TC", "TCD": "TD",
|
|
44
|
+
"TGO": "TG", "THA": "TH", "TJK": "TJ", "TKL": "TK", "TKM": "TM", "TLS": "TL", "TON": "TO",
|
|
45
|
+
"TTO": "TT", "TUN": "TN", "TUR": "TR", "TUV": "TV", "TWN": "TW", "TZA": "TZ", "UGA": "UG",
|
|
46
|
+
"UKR": "UA", "UMI": "UM", "URY": "UY", "USA": "US", "UZB": "UZ", "VAT": "VA", "VCT": "VC",
|
|
47
|
+
"VEN": "VE", "VGB": "VG", "VIR": "VI", "VNM": "VN", "VUT": "VU", "WLF": "WF", "WSM": "WS",
|
|
48
|
+
"YEM": "YE", "ZAF": "ZA", "ZMB": "ZM", "ZWE": "ZW",
|
|
49
|
+
// Kosovo (user-assigned, not in ISO 3166-1): XKX is the common form, XKS the App Store's.
|
|
50
|
+
"XKX": "XK", "XKS": "XK",
|
|
51
|
+
]
|
|
52
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'TenthBilling'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license'] || 'UNLICENSED'
|
|
11
|
+
s.author = package['author'] || 'Tenth'
|
|
12
|
+
s.homepage = package['homepage'] || 'https://app.tenthbilling.com'
|
|
13
|
+
# Same minimum as expo-modules-core 58 (StoreKit 2 itself needs iOS 15).
|
|
14
|
+
s.platforms = {
|
|
15
|
+
:ios => '16.4'
|
|
16
|
+
}
|
|
17
|
+
# Swift 5 language mode, as Expo's own modules use (expo-secure-store, expo-web-browser), so
|
|
18
|
+
# Swift 6 strict-concurrency diagnostics stay warnings.
|
|
19
|
+
s.swift_version = '5.9'
|
|
20
|
+
s.source = { git: 'https://github.com/tenthbilling/tenth.git' }
|
|
21
|
+
s.static_framework = true
|
|
22
|
+
|
|
23
|
+
s.dependency 'ExpoModulesCore'
|
|
24
|
+
s.frameworks = 'StoreKit'
|
|
25
|
+
|
|
26
|
+
s.source_files = "**/*.{h,m,swift}"
|
|
27
|
+
s.pod_target_xcconfig = {
|
|
28
|
+
'DEFINES_MODULE' => 'YES',
|
|
29
|
+
'SWIFT_COMPILATION_MODE' => 'wholemodule'
|
|
30
|
+
}
|
|
31
|
+
end
|