@tenthbilling/expo 0.0.0-stage → 0.1.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/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
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { CurrentOffering, CustomerState, Platform } from './types';
|
|
2
|
+
export declare const DEFAULT_API_URL = "https://api.tenthbilling.com";
|
|
3
|
+
export type Identity = {
|
|
4
|
+
kind: 'token';
|
|
5
|
+
token: string;
|
|
6
|
+
} | {
|
|
7
|
+
kind: 'anonymous';
|
|
8
|
+
id: string;
|
|
9
|
+
};
|
|
10
|
+
export interface ApiClientOptions {
|
|
11
|
+
apiKey: string;
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
fetch?: typeof fetch;
|
|
15
|
+
}
|
|
16
|
+
export declare class ApiClient {
|
|
17
|
+
readonly baseUrl: string;
|
|
18
|
+
readonly origin: string;
|
|
19
|
+
private readonly apiKey;
|
|
20
|
+
private readonly timeoutMs;
|
|
21
|
+
private readonly fetchImpl;
|
|
22
|
+
constructor(options: ApiClientOptions);
|
|
23
|
+
request<T>(method: 'GET' | 'POST', path: string, identity: Identity | null, options?: {
|
|
24
|
+
body?: unknown;
|
|
25
|
+
query?: Record<string, string | number | undefined>;
|
|
26
|
+
idempotencyKey?: string;
|
|
27
|
+
}): Promise<T>;
|
|
28
|
+
getCustomer(identity: Identity): Promise<CustomerState>;
|
|
29
|
+
getOffering(identity: Identity | null, input: {
|
|
30
|
+
key: string | null;
|
|
31
|
+
platform: Platform;
|
|
32
|
+
country?: string;
|
|
33
|
+
appVersion?: string;
|
|
34
|
+
paywallSchema: number;
|
|
35
|
+
}): Promise<CurrentOffering>;
|
|
36
|
+
postReceipt(identity: Identity, body: {
|
|
37
|
+
platform: 'ios';
|
|
38
|
+
signed_transaction: string;
|
|
39
|
+
} | {
|
|
40
|
+
platform: 'android';
|
|
41
|
+
purchase_token: string;
|
|
42
|
+
product_id: string;
|
|
43
|
+
type: 'subscription' | 'one_time';
|
|
44
|
+
}, idempotencyKey: string): Promise<{
|
|
45
|
+
provisional: boolean;
|
|
46
|
+
customer: CustomerState;
|
|
47
|
+
}>;
|
|
48
|
+
restore(identity: Identity, body: {
|
|
49
|
+
platform: 'ios';
|
|
50
|
+
signed_transactions: string[];
|
|
51
|
+
} | {
|
|
52
|
+
platform: 'android';
|
|
53
|
+
purchases: {
|
|
54
|
+
purchase_token: string;
|
|
55
|
+
product_id: string;
|
|
56
|
+
type: 'subscription' | 'one_time';
|
|
57
|
+
}[];
|
|
58
|
+
}): Promise<{
|
|
59
|
+
results: {
|
|
60
|
+
transaction_id: string | null;
|
|
61
|
+
outcome: string;
|
|
62
|
+
}[];
|
|
63
|
+
customer: CustomerState;
|
|
64
|
+
}>;
|
|
65
|
+
alias(identity: Identity, anonymousId: string): Promise<{
|
|
66
|
+
outcome: string;
|
|
67
|
+
customer: CustomerState;
|
|
68
|
+
}>;
|
|
69
|
+
debit(identity: Identity, creditType: string, body: {
|
|
70
|
+
amount: number;
|
|
71
|
+
idempotency_key: string;
|
|
72
|
+
metadata?: Record<string, string>;
|
|
73
|
+
}): Promise<unknown>;
|
|
74
|
+
appleOfferSignature(identity: Identity, body: {
|
|
75
|
+
product_id: string;
|
|
76
|
+
offer_id: string;
|
|
77
|
+
}): Promise<{
|
|
78
|
+
key_identifier: string;
|
|
79
|
+
nonce: string;
|
|
80
|
+
timestamp: number;
|
|
81
|
+
signature: string;
|
|
82
|
+
app_account_token: string;
|
|
83
|
+
}>;
|
|
84
|
+
createPaymentForm(identity: Identity, body: {
|
|
85
|
+
product: string;
|
|
86
|
+
form: 'elements' | 'payment_sheet';
|
|
87
|
+
return_url?: string;
|
|
88
|
+
}): Promise<{
|
|
89
|
+
id: string;
|
|
90
|
+
form: 'elements' | 'payment_sheet';
|
|
91
|
+
client_secret: string | null;
|
|
92
|
+
publishable_key: string | null;
|
|
93
|
+
stripe_account: string | null;
|
|
94
|
+
}>;
|
|
95
|
+
createCheckoutSession(identity: Identity, body: {
|
|
96
|
+
product: string;
|
|
97
|
+
return: 'app';
|
|
98
|
+
}): Promise<{
|
|
99
|
+
id: string;
|
|
100
|
+
url: string;
|
|
101
|
+
}>;
|
|
102
|
+
managementUrl(identity: Identity, subscriptionId: string): Promise<{
|
|
103
|
+
url: string;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
/** A fresh Idempotency-Key for one user action. Retries of that action reuse it. */
|
|
107
|
+
export declare function newIdempotencyKey(prefix: string): string;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { TenthSdkError, messageFor } from '../errors';
|
|
2
|
+
export const DEFAULT_API_URL = 'https://api.tenthbilling.com';
|
|
3
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
4
|
+
export class ApiClient {
|
|
5
|
+
baseUrl;
|
|
6
|
+
origin;
|
|
7
|
+
apiKey;
|
|
8
|
+
timeoutMs;
|
|
9
|
+
fetchImpl;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
const url = new URL(options.baseUrl ?? DEFAULT_API_URL);
|
|
12
|
+
const local = url.hostname === 'localhost' || url.hostname === '127.0.0.1';
|
|
13
|
+
if (url.protocol !== 'https:' && !(url.protocol === 'http:' && local)) {
|
|
14
|
+
throw new TenthSdkError('invalid_config', 'apiUrl must be an https URL.');
|
|
15
|
+
}
|
|
16
|
+
this.baseUrl = url.origin;
|
|
17
|
+
this.origin = url.origin;
|
|
18
|
+
this.apiKey = options.apiKey;
|
|
19
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
20
|
+
this.fetchImpl = options.fetch ?? ((...args) => fetch(...args));
|
|
21
|
+
}
|
|
22
|
+
async request(method, path, identity, options = {}) {
|
|
23
|
+
const url = new URL(path, this.baseUrl);
|
|
24
|
+
for (const [k, v] of Object.entries(options.query ?? {})) {
|
|
25
|
+
if (v !== undefined)
|
|
26
|
+
url.searchParams.set(k, String(v));
|
|
27
|
+
}
|
|
28
|
+
const headers = {
|
|
29
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
30
|
+
accept: 'application/json',
|
|
31
|
+
};
|
|
32
|
+
if (identity?.kind === 'token')
|
|
33
|
+
headers['x-customer-token'] = identity.token;
|
|
34
|
+
if (identity?.kind === 'anonymous')
|
|
35
|
+
headers['x-anonymous-id'] = identity.id;
|
|
36
|
+
if (options.body !== undefined)
|
|
37
|
+
headers['content-type'] = 'application/json';
|
|
38
|
+
if (options.idempotencyKey)
|
|
39
|
+
headers['idempotency-key'] = options.idempotencyKey;
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
42
|
+
let res;
|
|
43
|
+
try {
|
|
44
|
+
res = await this.fetchImpl(url.toString(), {
|
|
45
|
+
method,
|
|
46
|
+
headers,
|
|
47
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
48
|
+
signal: controller.signal,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
const code = controller.signal.aborted ? 'timeout' : 'network_error';
|
|
53
|
+
throw new TenthSdkError(code, messageFor(code, 'Tenth could not be reached.'));
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
}
|
|
58
|
+
const requestId = res.headers.get('x-request-id');
|
|
59
|
+
let payload;
|
|
60
|
+
try {
|
|
61
|
+
payload = await res.json();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
payload = null;
|
|
65
|
+
}
|
|
66
|
+
if (!res.ok) {
|
|
67
|
+
const p = (payload ?? {});
|
|
68
|
+
const code = typeof p.code === 'string' ? p.code : res.status === 429 ? 'rate_limited' : 'http_error';
|
|
69
|
+
const detail = typeof p.detail === 'string' ? p.detail : `Tenth returned ${res.status}.`;
|
|
70
|
+
throw new TenthSdkError(code, messageFor(code, detail), res.status, requestId);
|
|
71
|
+
}
|
|
72
|
+
return payload;
|
|
73
|
+
}
|
|
74
|
+
getCustomer(identity) {
|
|
75
|
+
return this.request('GET', '/v1/customers/me', identity);
|
|
76
|
+
}
|
|
77
|
+
getOffering(identity, input) {
|
|
78
|
+
const path = input.key
|
|
79
|
+
? `/v1/offerings/${encodeURIComponent(input.key)}`
|
|
80
|
+
: '/v1/offerings/current';
|
|
81
|
+
return this.request('GET', path, identity, {
|
|
82
|
+
query: {
|
|
83
|
+
platform: input.platform,
|
|
84
|
+
country: input.key ? undefined : input.country,
|
|
85
|
+
app_version: input.key ? undefined : input.appVersion,
|
|
86
|
+
paywall_schema: input.paywallSchema,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
postReceipt(identity, body, idempotencyKey) {
|
|
91
|
+
return this.request('POST', '/v1/receipts', identity, { body, idempotencyKey });
|
|
92
|
+
}
|
|
93
|
+
restore(identity, body) {
|
|
94
|
+
return this.request('POST', '/v1/receipts/restore', identity, {
|
|
95
|
+
body,
|
|
96
|
+
idempotencyKey: newIdempotencyKey('restore'),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
alias(identity, anonymousId) {
|
|
100
|
+
return this.request('POST', '/v1/customers/me/alias', identity, {
|
|
101
|
+
body: { alias: anonymousId },
|
|
102
|
+
idempotencyKey: `alias:${anonymousId}`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
debit(identity, creditType, body) {
|
|
106
|
+
return this.request('POST', `/v1/customers/me/credits/${encodeURIComponent(creditType)}/debit`, identity, {
|
|
107
|
+
body,
|
|
108
|
+
idempotencyKey: `debit:${body.idempotency_key}`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
appleOfferSignature(identity, body) {
|
|
112
|
+
return this.request('POST', '/v1/apple/offer-signatures', identity, {
|
|
113
|
+
body,
|
|
114
|
+
idempotencyKey: newIdempotencyKey('offer'),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
createPaymentForm(identity, body) {
|
|
118
|
+
return this.request('POST', '/v1/payment-intents', identity, {
|
|
119
|
+
body,
|
|
120
|
+
idempotencyKey: newIdempotencyKey('payment'),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
createCheckoutSession(identity, body) {
|
|
124
|
+
return this.request('POST', '/v1/checkout-sessions', identity, {
|
|
125
|
+
body,
|
|
126
|
+
idempotencyKey: newIdempotencyKey('checkout'),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
managementUrl(identity, subscriptionId) {
|
|
130
|
+
return this.request('GET', '/v1/customers/me/management-url', identity, {
|
|
131
|
+
query: { subscription_id: subscriptionId },
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
let counter = 0;
|
|
136
|
+
export function newIdempotencyKey(prefix) {
|
|
137
|
+
counter = (counter + 1) % 1_000_000;
|
|
138
|
+
return `${prefix}:${Date.now().toString(36)}:${counter.toString(36)}:${Math.random().toString(36).slice(2, 10)}`;
|
|
139
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { components } from '../generated/openapi';
|
|
2
|
+
type Schemas = components['schemas'];
|
|
3
|
+
export type CustomerState = Schemas['CustomerState'];
|
|
4
|
+
export type EntitlementState = Schemas['EntitlementState'];
|
|
5
|
+
export type CreditBalance = Schemas['CreditBalance'];
|
|
6
|
+
export type SubscriptionSummary = Schemas['SubscriptionSummary'];
|
|
7
|
+
export type CurrentOffering = Schemas['CurrentOffering'];
|
|
8
|
+
export type Offering = NonNullable<CurrentOffering['offering']>;
|
|
9
|
+
export type OfferingProduct = Offering['products'][number];
|
|
10
|
+
export type PurchasePath = Offering['purchase_path'];
|
|
11
|
+
export type Platform = 'ios' | 'android' | 'web';
|
|
12
|
+
export interface Problem {
|
|
13
|
+
code?: string;
|
|
14
|
+
detail?: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CustomerState } from './api/types';
|
|
2
|
+
import type { KeyValueStore } from './storage/storage';
|
|
3
|
+
export declare const DEFAULT_OFFLINE_GRACE_HOURS = 72;
|
|
4
|
+
declare const CACHE_VERSION = 1;
|
|
5
|
+
export interface CachedState {
|
|
6
|
+
v: typeof CACHE_VERSION;
|
|
7
|
+
/** Who the state belongs to: the token's subject, or the anonymous id. */
|
|
8
|
+
identity: string;
|
|
9
|
+
state: CustomerState;
|
|
10
|
+
/** When the server last confirmed this state, ms since the epoch. */
|
|
11
|
+
confirmedAt: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function cacheKey(environment: 'test' | 'live'): string;
|
|
14
|
+
export declare function readCache(store: KeyValueStore, environment: 'test' | 'live', identity: string): Promise<CachedState | null>;
|
|
15
|
+
export declare function writeCache(store: KeyValueStore, environment: 'test' | 'live', entry: Omit<CachedState, 'v'>): Promise<void>;
|
|
16
|
+
export declare function clearCache(store: KeyValueStore, environment: 'test' | 'live'): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* The state to act on now. State the server just sent is returned as is (the server already
|
|
19
|
+
* applied grace periods and billing retry). Cached state is checked entitlement by entitlement:
|
|
20
|
+
* active only within the offline grace window and before its expires_at.
|
|
21
|
+
*/
|
|
22
|
+
export declare function effectiveState(state: CustomerState, confirmedAt: number, now: number, graceHours: number, fromCache: boolean): CustomerState;
|
|
23
|
+
export {};
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const DEFAULT_OFFLINE_GRACE_HOURS = 72;
|
|
2
|
+
const CACHE_VERSION = 1;
|
|
3
|
+
export function cacheKey(environment) {
|
|
4
|
+
return `tenth.state.${environment}`;
|
|
5
|
+
}
|
|
6
|
+
export async function readCache(store, environment, identity) {
|
|
7
|
+
const raw = await store.get(cacheKey(environment));
|
|
8
|
+
if (!raw)
|
|
9
|
+
return null;
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(raw);
|
|
12
|
+
if (parsed?.v !== CACHE_VERSION ||
|
|
13
|
+
parsed.identity !== identity ||
|
|
14
|
+
typeof parsed.confirmedAt !== 'number' ||
|
|
15
|
+
!parsed.state?.customer) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return parsed;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function writeCache(store, environment, entry) {
|
|
25
|
+
await store.set(cacheKey(environment), JSON.stringify({ v: CACHE_VERSION, ...entry }));
|
|
26
|
+
}
|
|
27
|
+
export async function clearCache(store, environment) {
|
|
28
|
+
await store.remove(cacheKey(environment));
|
|
29
|
+
}
|
|
30
|
+
export function effectiveState(state, confirmedAt, now, graceHours, fromCache) {
|
|
31
|
+
if (!fromCache)
|
|
32
|
+
return state;
|
|
33
|
+
const graceOver = now > confirmedAt + graceHours * 3_600_000;
|
|
34
|
+
const entitlements = {};
|
|
35
|
+
for (const [key, e] of Object.entries(state.entitlements)) {
|
|
36
|
+
const expired = e.expires_at !== null && Date.parse(e.expires_at) <= now;
|
|
37
|
+
entitlements[key] = e.active && (graceOver || expired) ? { ...e, active: false } : e;
|
|
38
|
+
}
|
|
39
|
+
return { ...state, entitlements };
|
|
40
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PaywallError } from '@tenthbilling/paywall';
|
|
2
|
+
export declare class TenthSdkError extends Error implements PaywallError {
|
|
3
|
+
readonly code: string;
|
|
4
|
+
/** HTTP status for API errors; null for errors raised on the device. */
|
|
5
|
+
readonly status: number | null;
|
|
6
|
+
/** x-request-id, to quote to support. */
|
|
7
|
+
readonly requestId: string | null;
|
|
8
|
+
name: string;
|
|
9
|
+
constructor(code: string, message: string,
|
|
10
|
+
/** HTTP status for API errors; null for errors raised on the device. */
|
|
11
|
+
status?: number | null,
|
|
12
|
+
/** x-request-id, to quote to support. */
|
|
13
|
+
requestId?: string | null);
|
|
14
|
+
/** Worth retrying later: the network, a timeout, rate limits, or a provider outage. */
|
|
15
|
+
get retryable(): boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function messageFor(code: string, fallback: string): string;
|
|
18
|
+
export declare function toSdkError(err: unknown): TenthSdkError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class TenthSdkError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
status;
|
|
4
|
+
requestId;
|
|
5
|
+
name = 'TenthSdkError';
|
|
6
|
+
constructor(code, message, status = null, requestId = null) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.requestId = requestId;
|
|
11
|
+
}
|
|
12
|
+
get retryable() {
|
|
13
|
+
return (this.code === 'network_error' ||
|
|
14
|
+
this.code === 'timeout' ||
|
|
15
|
+
this.code === 'store_unavailable' ||
|
|
16
|
+
this.code === 'rate_limited' ||
|
|
17
|
+
(this.status !== null && this.status >= 500));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const MESSAGES = {
|
|
21
|
+
network_error: "Tenth couldn't be reached. Check your connection and try again.",
|
|
22
|
+
timeout: 'The request took too long. Check your connection and try again.',
|
|
23
|
+
store_unavailable: "The store isn't responding right now. Your purchase is safe; try again in a few minutes.",
|
|
24
|
+
receipt_invalid: "The store couldn't confirm this purchase. Restore purchases, or try again.",
|
|
25
|
+
receipt_belongs_to_other_customer: 'This purchase belongs to a different account. Restore purchases on the original account, or contact the app’s support.',
|
|
26
|
+
purchases_unavailable: "Purchases aren't available on this device.",
|
|
27
|
+
preview_live_key: 'Purchases are simulated in preview mode, which only works with a test key. Use a development build to buy with a live key.',
|
|
28
|
+
product_unavailable: "This product isn't available in your store right now.",
|
|
29
|
+
payment_sheet_missing: 'In-app card payments need @tenthbilling/expo/payment-sheet. Import it once in your app.',
|
|
30
|
+
not_signed_in: 'Sign in to buy or restore purchases.',
|
|
31
|
+
};
|
|
32
|
+
export function messageFor(code, fallback) {
|
|
33
|
+
return MESSAGES[code] ?? fallback;
|
|
34
|
+
}
|
|
35
|
+
export function toSdkError(err) {
|
|
36
|
+
if (err instanceof TenthSdkError)
|
|
37
|
+
return err;
|
|
38
|
+
const code = err && typeof err === 'object' && 'code' in err && typeof err.code === 'string'
|
|
39
|
+
? err.code
|
|
40
|
+
: 'unknown_error';
|
|
41
|
+
return new TenthSdkError(code, messageFor(code, 'Something went wrong. Try again.'));
|
|
42
|
+
}
|