@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Identity } from './api/client';
|
|
2
|
+
import type { KeyValueStore } from './storage/storage';
|
|
3
|
+
/** Same format the API checks: `$tenth_anon_` + 24 base62 characters, from a CSPRNG. */
|
|
4
|
+
export declare function newAnonymousId(randomBytes?: (n: number) => Uint8Array): string;
|
|
5
|
+
export declare function isAnonymousId(v: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* The `sub` of a JWT, read without verifying it. Only used to key the device cache, so one
|
|
8
|
+
* user's cached state is never shown to another; the server verifies every token.
|
|
9
|
+
*/
|
|
10
|
+
export declare function tokenSubject(token: string): string | null;
|
|
11
|
+
export interface ResolvedIdentity {
|
|
12
|
+
identity: Identity;
|
|
13
|
+
/** Cache key: `token:<sub>` or `anon:<id>`. */
|
|
14
|
+
cacheIdentity: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class IdentityManager {
|
|
17
|
+
private readonly store;
|
|
18
|
+
private readonly environment;
|
|
19
|
+
private readonly getCustomerToken;
|
|
20
|
+
private anonymousId;
|
|
21
|
+
/** Anonymous ids already merged into a signed-in customer (or found to need no merge). */
|
|
22
|
+
private aliased;
|
|
23
|
+
constructor(store: KeyValueStore, environment: 'test' | 'live', getCustomerToken: (() => Promise<string | null | undefined>) | undefined);
|
|
24
|
+
private get anonKey();
|
|
25
|
+
anonymous(): Promise<string>;
|
|
26
|
+
/** The identity for the next call. Never throws: a failing token getter means signed out. */
|
|
27
|
+
resolve(): Promise<ResolvedIdentity>;
|
|
28
|
+
/** The anonymous id to merge into the signed-in customer, once per id. */
|
|
29
|
+
pendingAlias(): Promise<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* After a merge: forget the anonymous id, so the next signed-out session starts as a new
|
|
32
|
+
* anonymous customer instead of reusing the one now merged into this user.
|
|
33
|
+
*/
|
|
34
|
+
aliasDone(id: string): Promise<void>;
|
|
35
|
+
}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { getRandomBytes } from 'expo-crypto';
|
|
2
|
+
const ANON_PREFIX = '$tenth_anon_';
|
|
3
|
+
const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
4
|
+
export function newAnonymousId(randomBytes = getRandomBytes) {
|
|
5
|
+
let out = '';
|
|
6
|
+
while (out.length < 24) {
|
|
7
|
+
for (const byte of randomBytes(48)) {
|
|
8
|
+
if (byte < 248)
|
|
9
|
+
out += BASE62[byte % 62];
|
|
10
|
+
if (out.length === 24)
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return ANON_PREFIX + out;
|
|
15
|
+
}
|
|
16
|
+
export function isAnonymousId(v) {
|
|
17
|
+
return v.startsWith(ANON_PREFIX) && /^[0-9A-Za-z]{24}$/.test(v.slice(ANON_PREFIX.length));
|
|
18
|
+
}
|
|
19
|
+
export function tokenSubject(token) {
|
|
20
|
+
const part = token.split('.')[1];
|
|
21
|
+
if (!part)
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
const json = JSON.parse(decodeBase64Url(part));
|
|
25
|
+
return typeof json.sub === 'string' && json.sub.length > 0 && json.sub.length <= 255
|
|
26
|
+
? json.sub
|
|
27
|
+
: null;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function decodeBase64Url(s) {
|
|
34
|
+
const b64 = s.replace(/-/g, '+').replace(/_/g, '/');
|
|
35
|
+
const padded = b64 + '='.repeat((4 - (b64.length % 4)) % 4);
|
|
36
|
+
const binary = atob(padded);
|
|
37
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
38
|
+
return new TextDecoder().decode(bytes);
|
|
39
|
+
}
|
|
40
|
+
export class IdentityManager {
|
|
41
|
+
store;
|
|
42
|
+
environment;
|
|
43
|
+
getCustomerToken;
|
|
44
|
+
anonymousId = null;
|
|
45
|
+
aliased = new Set();
|
|
46
|
+
constructor(store, environment, getCustomerToken) {
|
|
47
|
+
this.store = store;
|
|
48
|
+
this.environment = environment;
|
|
49
|
+
this.getCustomerToken = getCustomerToken;
|
|
50
|
+
}
|
|
51
|
+
get anonKey() {
|
|
52
|
+
return `tenth.anon.${this.environment}`;
|
|
53
|
+
}
|
|
54
|
+
async anonymous() {
|
|
55
|
+
if (this.anonymousId)
|
|
56
|
+
return this.anonymousId;
|
|
57
|
+
const stored = await this.store.get(this.anonKey);
|
|
58
|
+
if (stored && isAnonymousId(stored)) {
|
|
59
|
+
this.anonymousId = stored;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this.anonymousId = newAnonymousId();
|
|
63
|
+
await this.store.set(this.anonKey, this.anonymousId);
|
|
64
|
+
}
|
|
65
|
+
return this.anonymousId;
|
|
66
|
+
}
|
|
67
|
+
async resolve() {
|
|
68
|
+
let token;
|
|
69
|
+
try {
|
|
70
|
+
token = this.getCustomerToken ? await this.getCustomerToken() : null;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
token = null;
|
|
74
|
+
}
|
|
75
|
+
if (token) {
|
|
76
|
+
const sub = tokenSubject(token);
|
|
77
|
+
return { identity: { kind: 'token', token }, cacheIdentity: `token:${sub ?? 'unknown'}` };
|
|
78
|
+
}
|
|
79
|
+
const id = await this.anonymous();
|
|
80
|
+
return { identity: { kind: 'anonymous', id }, cacheIdentity: `anon:${id}` };
|
|
81
|
+
}
|
|
82
|
+
async pendingAlias() {
|
|
83
|
+
const stored = await this.store.get(this.anonKey);
|
|
84
|
+
return stored && isAnonymousId(stored) && !this.aliased.has(stored) ? stored : null;
|
|
85
|
+
}
|
|
86
|
+
async aliasDone(id) {
|
|
87
|
+
this.aliased.add(id);
|
|
88
|
+
if (this.anonymousId === id)
|
|
89
|
+
this.anonymousId = null;
|
|
90
|
+
await this.store.remove(this.anonKey);
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { TenthProvider, type TenthProviderProps } from './react/TenthProvider';
|
|
2
|
+
export { useTenth } from './react/context';
|
|
3
|
+
export { useCustomer, useEntitlement, useCredits, useOffering, useTenthSnapshot, type CustomerInfo, type EntitlementInfo, type CreditsInfo, type OfferingInfo, } from './react/hooks';
|
|
4
|
+
export { usePaywall, type UsePaywallOptions } from './react/usePaywall';
|
|
5
|
+
export { Paywall, Gate, type PaywallProps, type GateProps } from './react/Paywall';
|
|
6
|
+
export { presentPaywall, presentPaywallIfNeeded, type PaywallResult, type PresentPaywallOptions, } from './react/present';
|
|
7
|
+
export { Tenth, type TenthConfig, type TenthSnapshot, type LoadedOffering } from './tenth';
|
|
8
|
+
export { TenthSdkError } from './errors';
|
|
9
|
+
export { InvalidKeyError } from './keys';
|
|
10
|
+
export type { UpdateSource, RefreshReason } from './updates';
|
|
11
|
+
export type { CustomerState, EntitlementState, CreditBalance, SubscriptionSummary, Offering, OfferingProduct, } from './api/types';
|
|
12
|
+
export { PaywallView, PaywallOption, ctaLabel, resolveCopy, type PaywallController, type PaywallProduct, type PurchaseResult, type RestoreResult, type PaywallSlots, type PaywallPresentation, type PaywallThemeOverrides, } from '@tenthbilling/paywall';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { TenthProvider } from './react/TenthProvider';
|
|
2
|
+
export { useTenth } from './react/context';
|
|
3
|
+
export { useCustomer, useEntitlement, useCredits, useOffering, useTenthSnapshot, } from './react/hooks';
|
|
4
|
+
export { usePaywall } from './react/usePaywall';
|
|
5
|
+
export { Paywall, Gate } from './react/Paywall';
|
|
6
|
+
export { presentPaywall, presentPaywallIfNeeded, } from './react/present';
|
|
7
|
+
export { Tenth } from './tenth';
|
|
8
|
+
export { TenthSdkError } from './errors';
|
|
9
|
+
export { InvalidKeyError } from './keys';
|
|
10
|
+
export { PaywallView, PaywallOption, ctaLabel, resolveCopy, } from '@tenthbilling/paywall';
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type Environment = 'test' | 'live';
|
|
2
|
+
export declare class InvalidKeyError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
/** Checks a key passed to <TenthProvider>. Throws for a secret key or anything malformed. */
|
|
6
|
+
export declare function parsePublishableKey(apiKey: string): {
|
|
7
|
+
environment: Environment;
|
|
8
|
+
};
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const KEY_RE = /^tenth_(test|live)_(pk|sk)_[0-9A-Za-z]+$/;
|
|
2
|
+
export class InvalidKeyError extends Error {
|
|
3
|
+
name = 'InvalidKeyError';
|
|
4
|
+
}
|
|
5
|
+
export function parsePublishableKey(apiKey) {
|
|
6
|
+
const m = KEY_RE.exec(typeof apiKey === 'string' ? apiKey.trim() : '');
|
|
7
|
+
if (!m) {
|
|
8
|
+
throw new InvalidKeyError('apiKey must be a Tenth publishable key (tenth_test_pk_… or tenth_live_pk_…). Copy it from the dashboard.');
|
|
9
|
+
}
|
|
10
|
+
if (m[2] === 'sk') {
|
|
11
|
+
throw new InvalidKeyError('That is a secret key. Secret keys belong on your server only; anyone can read keys shipped in an app. Use the publishable key (tenth_…_pk_…), and rotate this secret key in the dashboard now.');
|
|
12
|
+
}
|
|
13
|
+
return { environment: m[1] };
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type EventEmitter } from 'expo-modules-core';
|
|
2
|
+
import { type AndroidStoreEvents, type AndroidStoreModule, type IosStoreEvents, type IosStoreModule } from './spec';
|
|
3
|
+
export type IosNative = IosStoreModule & EventEmitter<IosStoreEvents>;
|
|
4
|
+
export type AndroidNative = AndroidStoreModule & EventEmitter<AndroidStoreEvents>;
|
|
5
|
+
export type NativeStore = {
|
|
6
|
+
platform: 'ios';
|
|
7
|
+
module: IosNative;
|
|
8
|
+
} | {
|
|
9
|
+
platform: 'android';
|
|
10
|
+
module: AndroidNative;
|
|
11
|
+
};
|
|
12
|
+
export declare function loadNativeStore(): NativeStore | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { requireOptionalNativeModule } from 'expo-modules-core';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
import { NATIVE_MODULE_NAME, } from './spec';
|
|
4
|
+
export function loadNativeStore() {
|
|
5
|
+
if (Platform.OS === 'ios') {
|
|
6
|
+
const module = requireOptionalNativeModule(NATIVE_MODULE_NAME);
|
|
7
|
+
return module ? { platform: 'ios', module } : null;
|
|
8
|
+
}
|
|
9
|
+
if (Platform.OS === 'android') {
|
|
10
|
+
const module = requireOptionalNativeModule(NATIVE_MODULE_NAME);
|
|
11
|
+
return module ? { platform: 'android', module } : null;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/** ISO 8601-style period from the store, normalized. */
|
|
2
|
+
export interface NativePeriod {
|
|
3
|
+
unit: 'day' | 'week' | 'month' | 'year';
|
|
4
|
+
value: number;
|
|
5
|
+
}
|
|
6
|
+
export interface IosProduct {
|
|
7
|
+
id: string;
|
|
8
|
+
type: 'autoRenewable' | 'nonRenewable' | 'consumable' | 'nonConsumable';
|
|
9
|
+
displayName: string;
|
|
10
|
+
/** Product.displayPrice, localized by StoreKit. */
|
|
11
|
+
displayPrice: string;
|
|
12
|
+
/** Product.price (Decimal) as a plain decimal string, such as "59.99". */
|
|
13
|
+
price: string;
|
|
14
|
+
currencyCode: string;
|
|
15
|
+
subscriptionPeriod: NativePeriod | null;
|
|
16
|
+
subscriptionGroupId: string | null;
|
|
17
|
+
/** The introductory offer, only if this customer is eligible for it. */
|
|
18
|
+
introductoryOffer: {
|
|
19
|
+
paymentMode: 'freeTrial' | 'payAsYouGo' | 'payUpFront';
|
|
20
|
+
period: NativePeriod;
|
|
21
|
+
periodCount: number;
|
|
22
|
+
displayPrice: string;
|
|
23
|
+
price: string;
|
|
24
|
+
} | null;
|
|
25
|
+
}
|
|
26
|
+
/** A promotional offer signed by Tenth (`POST /v1/apple/offer-signatures`). */
|
|
27
|
+
export interface IosPromotionalOffer {
|
|
28
|
+
offerId: string;
|
|
29
|
+
keyId: string;
|
|
30
|
+
/** UUID, lowercase. */
|
|
31
|
+
nonce: string;
|
|
32
|
+
/** Base64 signature. */
|
|
33
|
+
signature: string;
|
|
34
|
+
/** Milliseconds since the epoch. */
|
|
35
|
+
timestamp: number;
|
|
36
|
+
}
|
|
37
|
+
/** A StoreKit 2 transaction as its signed JWS, plus its id for finish(). */
|
|
38
|
+
export interface IosTransaction {
|
|
39
|
+
/** VerificationResult.jwsRepresentation. Sent to Tenth as is; the server verifies it. */
|
|
40
|
+
jws: string;
|
|
41
|
+
transactionId: string;
|
|
42
|
+
productId: string;
|
|
43
|
+
}
|
|
44
|
+
export type IosPurchaseResult = {
|
|
45
|
+
status: 'success';
|
|
46
|
+
transaction: IosTransaction;
|
|
47
|
+
} | {
|
|
48
|
+
status: 'userCancelled';
|
|
49
|
+
}
|
|
50
|
+
/** Ask to Buy or SCA: the transaction arrives later through onTransactionUpdate. */
|
|
51
|
+
| {
|
|
52
|
+
status: 'pending';
|
|
53
|
+
};
|
|
54
|
+
export interface IosStoreModule {
|
|
55
|
+
canMakePayments(): boolean;
|
|
56
|
+
/** The App Store storefront's ISO 3166-1 alpha-2 country, or null. */
|
|
57
|
+
storefrontCountry(): Promise<string | null>;
|
|
58
|
+
getProducts(productIds: string[]): Promise<IosProduct[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Product.purchase(options: [.appAccountToken(uuid), .promotionalOffer(...)?]). Does not
|
|
61
|
+
* finish the transaction: the SDK calls finishTransaction only after Tenth accepted it.
|
|
62
|
+
*/
|
|
63
|
+
purchase(productId: string, appAccountToken: string, promotionalOffer: IosPromotionalOffer | null): Promise<IosPurchaseResult>;
|
|
64
|
+
finishTransaction(transactionId: string): Promise<void>;
|
|
65
|
+
/** Transaction.currentEntitlements, verified or not (Tenth verifies). For restore. */
|
|
66
|
+
currentEntitlements(): Promise<IosTransaction[]>;
|
|
67
|
+
/** Transaction.unfinished: purchases Tenth hasn't accepted yet, retried on launch. */
|
|
68
|
+
unfinishedTransactions(): Promise<IosTransaction[]>;
|
|
69
|
+
/** AppStore.sync(): asks the customer to sign in to the App Store, then refreshes. */
|
|
70
|
+
sync(): Promise<void>;
|
|
71
|
+
/** AppStore.showManageSubscriptions(in:). */
|
|
72
|
+
showManageSubscriptions(): Promise<void>;
|
|
73
|
+
/** Starts the Transaction.updates listener; events arrive as `onTransactionUpdate`. Idempotent. */
|
|
74
|
+
startObserving(): void;
|
|
75
|
+
}
|
|
76
|
+
export type IosStoreEvents = {
|
|
77
|
+
onTransactionUpdate: (event: IosTransaction) => void;
|
|
78
|
+
};
|
|
79
|
+
export interface AndroidPricingPhase {
|
|
80
|
+
formattedPrice: string;
|
|
81
|
+
priceAmountMicros: string;
|
|
82
|
+
priceCurrencyCode: string;
|
|
83
|
+
/** ISO 8601, such as P1M. */
|
|
84
|
+
billingPeriod: string;
|
|
85
|
+
/** 1 infinite, 2 finite, 3 non-recurring (Play's RecurrenceMode). */
|
|
86
|
+
recurrenceMode: number;
|
|
87
|
+
billingCycleCount: number;
|
|
88
|
+
}
|
|
89
|
+
export interface AndroidProduct {
|
|
90
|
+
productId: string;
|
|
91
|
+
type: 'subs' | 'inapp';
|
|
92
|
+
name: string;
|
|
93
|
+
/** One-time products. */
|
|
94
|
+
oneTime: {
|
|
95
|
+
formattedPrice: string;
|
|
96
|
+
priceAmountMicros: string;
|
|
97
|
+
priceCurrencyCode: string;
|
|
98
|
+
} | null;
|
|
99
|
+
/** Subscriptions: every base plan and offer this customer can buy. */
|
|
100
|
+
subscriptionOffers: {
|
|
101
|
+
basePlanId: string;
|
|
102
|
+
offerId: string | null;
|
|
103
|
+
offerToken: string;
|
|
104
|
+
pricingPhases: AndroidPricingPhase[];
|
|
105
|
+
}[];
|
|
106
|
+
}
|
|
107
|
+
export interface AndroidPurchase {
|
|
108
|
+
purchaseToken: string;
|
|
109
|
+
/** Play allows several products per purchase; Tenth sells one. */
|
|
110
|
+
productIds: string[];
|
|
111
|
+
/**
|
|
112
|
+
* Which kind of product this is: from the queryPurchasesAsync call that returned it, or the
|
|
113
|
+
* product type of the purchase flow that produced it. Tenth's receipt call needs it.
|
|
114
|
+
*/
|
|
115
|
+
productType: 'subs' | 'inapp';
|
|
116
|
+
orderId: string | null;
|
|
117
|
+
/** 0 unspecified, 1 purchased, 2 pending (Play's PurchaseState). */
|
|
118
|
+
purchaseState: number;
|
|
119
|
+
quantity: number;
|
|
120
|
+
isAcknowledged: boolean;
|
|
121
|
+
obfuscatedAccountId: string | null;
|
|
122
|
+
}
|
|
123
|
+
export type AndroidPurchaseResult = {
|
|
124
|
+
status: 'success';
|
|
125
|
+
purchases: AndroidPurchase[];
|
|
126
|
+
} | {
|
|
127
|
+
status: 'userCancelled';
|
|
128
|
+
}
|
|
129
|
+
/** Purchased but PENDING (cash payments and similar): it completes later through onPurchasesUpdated. */
|
|
130
|
+
| {
|
|
131
|
+
status: 'pending';
|
|
132
|
+
purchases: AndroidPurchase[];
|
|
133
|
+
};
|
|
134
|
+
export interface AndroidStoreModule {
|
|
135
|
+
isReady(): Promise<boolean>;
|
|
136
|
+
/** BillingConfig countryCode, or null. */
|
|
137
|
+
storefrontCountry(): Promise<string | null>;
|
|
138
|
+
getProducts(products: {
|
|
139
|
+
productId: string;
|
|
140
|
+
type: 'subs' | 'inapp';
|
|
141
|
+
}[]): Promise<AndroidProduct[]>;
|
|
142
|
+
/**
|
|
143
|
+
* launchBillingFlow with setObfuscatedAccountId. `offerToken` is required for subscriptions.
|
|
144
|
+
* `oldPurchaseToken` and `replacementMode` (Play's ReplacementMode) are for plan changes.
|
|
145
|
+
* Never acknowledges: Tenth does that server-side after the receipt is recorded.
|
|
146
|
+
*/
|
|
147
|
+
purchase(input: {
|
|
148
|
+
productId: string;
|
|
149
|
+
type: 'subs' | 'inapp';
|
|
150
|
+
offerToken: string | null;
|
|
151
|
+
obfuscatedAccountId: string;
|
|
152
|
+
oldPurchaseToken: string | null;
|
|
153
|
+
replacementMode: number | null;
|
|
154
|
+
}): Promise<AndroidPurchaseResult>;
|
|
155
|
+
/** queryPurchasesAsync for subs and inapp. For restore and the launch retry. */
|
|
156
|
+
queryPurchases(): Promise<AndroidPurchase[]>;
|
|
157
|
+
/** Opens Play's subscription center, or one subscription's page. */
|
|
158
|
+
showManageSubscriptions(productId: string | null, packageName: string): Promise<void>;
|
|
159
|
+
}
|
|
160
|
+
export type AndroidStoreEvents = {
|
|
161
|
+
onPurchasesUpdated: (event: {
|
|
162
|
+
purchases: AndroidPurchase[];
|
|
163
|
+
}) => void;
|
|
164
|
+
};
|
|
165
|
+
/** Name the module registers with expo-modules-core on both platforms. */
|
|
166
|
+
export declare const NATIVE_MODULE_NAME = "TenthStore";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const NATIVE_MODULE_NAME = 'TenthStore';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PaywallProduct, Period } from '@tenthbilling/paywall';
|
|
2
|
+
import type { OfferingProduct } from './api/types';
|
|
3
|
+
import type { AndroidProduct, IosProduct } from './native/spec';
|
|
4
|
+
/** "59.99" → 59_990_000n. Exact; no floating point. Null for anything that isn't a plain decimal. */
|
|
5
|
+
export declare function decimalToMicros(value: string): bigint | null;
|
|
6
|
+
/** Integer minor units ("1299" in USD) → micros, using the currency's own exponent. */
|
|
7
|
+
export declare function minorToMicros(amountMinor: string, currency: string): bigint | null;
|
|
8
|
+
export declare function currencyDigits(currency: string): number;
|
|
9
|
+
/** Formats integer minor units for display in the buyer's locale (Stripe prices on web). */
|
|
10
|
+
export declare function formatMinor(amountMinor: string, currency: string, locale: string): string;
|
|
11
|
+
/** ISO 8601 durations as Play reports them: P1W, P1M, P3M, P1Y, P7D. */
|
|
12
|
+
export declare function parseIsoPeriod(iso: string): Period | null;
|
|
13
|
+
export declare function fromIosProduct(p: OfferingProduct, s: IosProduct): PaywallProduct | null;
|
|
14
|
+
export type AndroidOffer = AndroidProduct['subscriptionOffers'][number];
|
|
15
|
+
/**
|
|
16
|
+
* The subscription offer to buy. The offering may name a base plan and offer; with
|
|
17
|
+
* no offer named, the customer gets the base plan's free trial if Play says they're eligible
|
|
18
|
+
* (Play only returns offers the customer can use), else the plain base plan.
|
|
19
|
+
*/
|
|
20
|
+
export declare function chooseAndroidOffer(p: OfferingProduct, s: AndroidProduct): AndroidOffer | null;
|
|
21
|
+
export declare function fromAndroidProduct(p: OfferingProduct, s: AndroidProduct): {
|
|
22
|
+
product: PaywallProduct;
|
|
23
|
+
offerToken: string | null;
|
|
24
|
+
} | null;
|
|
25
|
+
/** The price Tenth read from Stripe for a web purchase path. */
|
|
26
|
+
export type StripeDisplayPrice = NonNullable<OfferingProduct['price']>;
|
|
27
|
+
export declare function fromStripePrice(p: OfferingProduct, price: StripeDisplayPrice | null | undefined, locale: string): PaywallProduct | null;
|
|
28
|
+
/**
|
|
29
|
+
* Preview mode has no store, so it shows clearly fake prices, so UI work isn't
|
|
30
|
+
* blocked. The paywall shows a preview notice; nothing here is ever charged.
|
|
31
|
+
*/
|
|
32
|
+
export declare function previewProduct(p: OfferingProduct, index: number, locale: string): PaywallProduct;
|
package/dist/products.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
export function decimalToMicros(value) {
|
|
2
|
+
const m = /^(\d{1,15})(?:\.(\d{1,6})\d*)?$/.exec(value.trim());
|
|
3
|
+
if (!m)
|
|
4
|
+
return null;
|
|
5
|
+
return BigInt(m[1]) * 1000000n + BigInt((m[2] ?? '').padEnd(6, '0'));
|
|
6
|
+
}
|
|
7
|
+
export function minorToMicros(amountMinor, currency) {
|
|
8
|
+
if (!/^-?\d{1,15}$/.test(amountMinor))
|
|
9
|
+
return null;
|
|
10
|
+
const digits = currencyDigits(currency);
|
|
11
|
+
return BigInt(amountMinor) * 10n ** BigInt(6 - digits);
|
|
12
|
+
}
|
|
13
|
+
export function currencyDigits(currency) {
|
|
14
|
+
try {
|
|
15
|
+
const d = new Intl.NumberFormat('en', { style: 'currency', currency }).resolvedOptions()
|
|
16
|
+
.maximumFractionDigits;
|
|
17
|
+
return Math.min(Math.max(d ?? 2, 0), 6);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return 2;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function formatMinor(amountMinor, currency, locale) {
|
|
24
|
+
const digits = currencyDigits(currency);
|
|
25
|
+
const value = Number(amountMinor) / 10 ** digits;
|
|
26
|
+
try {
|
|
27
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return `${value} ${currency.toUpperCase()}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function parseIsoPeriod(iso) {
|
|
34
|
+
const m = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?$/.exec(iso);
|
|
35
|
+
if (!m)
|
|
36
|
+
return null;
|
|
37
|
+
const [, y, mo, w, d] = m.map((x) => (x ? Number(x) : 0));
|
|
38
|
+
if (y && !mo && !w && !d)
|
|
39
|
+
return { unit: 'year', count: y };
|
|
40
|
+
if (mo && !y && !w && !d)
|
|
41
|
+
return { unit: 'month', count: mo };
|
|
42
|
+
if (w && !y && !mo && !d)
|
|
43
|
+
return { unit: 'week', count: w };
|
|
44
|
+
if (d && !y && !mo && !w)
|
|
45
|
+
return d % 7 === 0 ? { unit: 'week', count: d / 7 } : { unit: 'day', count: d };
|
|
46
|
+
if (y || mo)
|
|
47
|
+
return { unit: 'month', count: y * 12 + mo };
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
function fromNativePeriod(p) {
|
|
51
|
+
return { unit: p.unit, count: p.value };
|
|
52
|
+
}
|
|
53
|
+
function nameFor(p, storeName) {
|
|
54
|
+
return p.label ?? p.display_name ?? storeName;
|
|
55
|
+
}
|
|
56
|
+
function base(p, name) {
|
|
57
|
+
return {
|
|
58
|
+
key: p.key,
|
|
59
|
+
type: p.type,
|
|
60
|
+
name,
|
|
61
|
+
badge: p.badge,
|
|
62
|
+
highlighted: p.is_highlighted,
|
|
63
|
+
credits: p.credits.map((c) => ({ creditType: c.credit_type, amount: c.amount, per: c.per })),
|
|
64
|
+
entitlements: p.entitlements,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export function fromIosProduct(p, s) {
|
|
68
|
+
const micros = decimalToMicros(s.price);
|
|
69
|
+
if (micros === null)
|
|
70
|
+
return null;
|
|
71
|
+
const price = {
|
|
72
|
+
formatted: s.displayPrice,
|
|
73
|
+
amountMicros: micros,
|
|
74
|
+
currency: s.currencyCode,
|
|
75
|
+
};
|
|
76
|
+
let intro = null;
|
|
77
|
+
if (s.introductoryOffer) {
|
|
78
|
+
const o = s.introductoryOffer;
|
|
79
|
+
const introMicros = decimalToMicros(o.price);
|
|
80
|
+
intro = {
|
|
81
|
+
kind: o.paymentMode === 'freeTrial'
|
|
82
|
+
? 'free_trial'
|
|
83
|
+
: o.paymentMode === 'payAsYouGo'
|
|
84
|
+
? 'pay_as_you_go'
|
|
85
|
+
: 'pay_up_front',
|
|
86
|
+
period: fromNativePeriod(o.period),
|
|
87
|
+
periods: o.periodCount,
|
|
88
|
+
price: o.paymentMode === 'freeTrial' || introMicros === null
|
|
89
|
+
? null
|
|
90
|
+
: { formatted: o.displayPrice, amountMicros: introMicros, currency: s.currencyCode },
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...base(p, nameFor(p, s.displayName)),
|
|
95
|
+
price,
|
|
96
|
+
period: s.subscriptionPeriod ? fromNativePeriod(s.subscriptionPeriod) : null,
|
|
97
|
+
intro,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export function chooseAndroidOffer(p, s) {
|
|
101
|
+
const plans = s.subscriptionOffers.filter((o) => !p.google_base_plan_id || o.basePlanId === p.google_base_plan_id);
|
|
102
|
+
if (p.google_offer_id)
|
|
103
|
+
return plans.find((o) => o.offerId === p.google_offer_id) ?? null;
|
|
104
|
+
const withTrial = plans.find((o) => o.offerId !== null && o.pricingPhases.some((ph) => ph.priceAmountMicros === '0'));
|
|
105
|
+
return withTrial ?? plans.find((o) => o.offerId === null) ?? plans[0] ?? null;
|
|
106
|
+
}
|
|
107
|
+
export function fromAndroidProduct(p, s) {
|
|
108
|
+
if (s.type === 'inapp') {
|
|
109
|
+
const o = s.oneTime;
|
|
110
|
+
if (!o || !/^\d+$/.test(o.priceAmountMicros))
|
|
111
|
+
return null;
|
|
112
|
+
return {
|
|
113
|
+
product: {
|
|
114
|
+
...base(p, nameFor(p, s.name)),
|
|
115
|
+
price: {
|
|
116
|
+
formatted: o.formattedPrice,
|
|
117
|
+
amountMicros: BigInt(o.priceAmountMicros),
|
|
118
|
+
currency: o.priceCurrencyCode,
|
|
119
|
+
},
|
|
120
|
+
period: null,
|
|
121
|
+
intro: null,
|
|
122
|
+
},
|
|
123
|
+
offerToken: null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const offer = chooseAndroidOffer(p, s);
|
|
127
|
+
if (!offer || offer.pricingPhases.length === 0)
|
|
128
|
+
return null;
|
|
129
|
+
const phases = offer.pricingPhases;
|
|
130
|
+
const recurring = phases[phases.length - 1];
|
|
131
|
+
const period = parseIsoPeriod(recurring.billingPeriod);
|
|
132
|
+
if (!/^\d+$/.test(recurring.priceAmountMicros) || !period)
|
|
133
|
+
return null;
|
|
134
|
+
let intro = null;
|
|
135
|
+
if (phases.length > 1) {
|
|
136
|
+
const first = phases[0];
|
|
137
|
+
const firstPeriod = parseIsoPeriod(first.billingPeriod);
|
|
138
|
+
if (firstPeriod && /^\d+$/.test(first.priceAmountMicros)) {
|
|
139
|
+
const free = first.priceAmountMicros === '0';
|
|
140
|
+
intro = {
|
|
141
|
+
kind: free ? 'free_trial' : first.recurrenceMode === 3 ? 'pay_up_front' : 'pay_as_you_go',
|
|
142
|
+
period: firstPeriod,
|
|
143
|
+
periods: Math.max(first.billingCycleCount, 1),
|
|
144
|
+
price: free
|
|
145
|
+
? null
|
|
146
|
+
: {
|
|
147
|
+
formatted: first.formattedPrice,
|
|
148
|
+
amountMicros: BigInt(first.priceAmountMicros),
|
|
149
|
+
currency: first.priceCurrencyCode,
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
product: {
|
|
156
|
+
...base(p, nameFor(p, s.name)),
|
|
157
|
+
price: {
|
|
158
|
+
formatted: recurring.formattedPrice,
|
|
159
|
+
amountMicros: BigInt(recurring.priceAmountMicros),
|
|
160
|
+
currency: recurring.priceCurrencyCode,
|
|
161
|
+
},
|
|
162
|
+
period,
|
|
163
|
+
intro,
|
|
164
|
+
},
|
|
165
|
+
offerToken: offer.offerToken,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
export function fromStripePrice(p, price, locale) {
|
|
169
|
+
if (!price)
|
|
170
|
+
return null;
|
|
171
|
+
const currency = price.currency.toUpperCase();
|
|
172
|
+
const micros = minorToMicros(price.amount_minor, currency);
|
|
173
|
+
if (micros === null)
|
|
174
|
+
return null;
|
|
175
|
+
return {
|
|
176
|
+
...base(p, nameFor(p, p.key)),
|
|
177
|
+
price: {
|
|
178
|
+
formatted: formatMinor(price.amount_minor, currency, locale),
|
|
179
|
+
amountMicros: micros,
|
|
180
|
+
currency,
|
|
181
|
+
},
|
|
182
|
+
period: price.recurring
|
|
183
|
+
? { unit: price.recurring.interval, count: price.recurring.interval_count }
|
|
184
|
+
: null,
|
|
185
|
+
intro: price.trial_period_days
|
|
186
|
+
? {
|
|
187
|
+
kind: 'free_trial',
|
|
188
|
+
period: { unit: 'day', count: price.trial_period_days },
|
|
189
|
+
periods: 1,
|
|
190
|
+
price: null,
|
|
191
|
+
}
|
|
192
|
+
: null,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
export function previewProduct(p, index, locale) {
|
|
196
|
+
const sub = p.type === 'subscription';
|
|
197
|
+
const period = sub
|
|
198
|
+
? index % 2 === 0
|
|
199
|
+
? { unit: 'month', count: 1 }
|
|
200
|
+
: { unit: 'year', count: 1 }
|
|
201
|
+
: null;
|
|
202
|
+
const minor = sub ? (period?.unit === 'year' ? '3999' : '499') : '199';
|
|
203
|
+
return {
|
|
204
|
+
...base(p, nameFor(p, p.key)),
|
|
205
|
+
price: {
|
|
206
|
+
formatted: formatMinor(minor, 'USD', locale),
|
|
207
|
+
amountMicros: BigInt(minor) * 10000n,
|
|
208
|
+
currency: 'USD',
|
|
209
|
+
},
|
|
210
|
+
period,
|
|
211
|
+
intro: null,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type PaywallPresentation, type PaywallProduct, type PaywallSlots, type PaywallThemeOverrides } from '@tenthbilling/paywall';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
export interface PaywallProps {
|
|
4
|
+
/** Offering key, such as "black_friday". Omitted: the current, targeted offering. */
|
|
5
|
+
offering?: string;
|
|
6
|
+
/** `inline` (default) renders in place; `sheet` slides up; `modal` covers the screen. */
|
|
7
|
+
presentation?: PaywallPresentation;
|
|
8
|
+
slots?: PaywallSlots;
|
|
9
|
+
/** Developer theme overrides (brand, surface, display font); they win over remote config. */
|
|
10
|
+
theme?: PaywallThemeOverrides;
|
|
11
|
+
locale?: string;
|
|
12
|
+
onPurchased?: (product: PaywallProduct) => void;
|
|
13
|
+
onRestored?: () => void;
|
|
14
|
+
/** Close button pressed. Inline paywalls show a close button only when this is set. */
|
|
15
|
+
onDismiss?: () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function Paywall(props: PaywallProps): import("react").JSX.Element;
|
|
18
|
+
export interface GateProps {
|
|
19
|
+
/** Entitlement key, such as "pro". */
|
|
20
|
+
entitlement: string;
|
|
21
|
+
/** Shown without access. Usually <Paywall />. */
|
|
22
|
+
fallback: ReactNode;
|
|
23
|
+
/** Shown before the first state is known (cache or server). Defaults to nothing. */
|
|
24
|
+
loading?: ReactNode;
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
}
|
|
27
|
+
/** `<Gate entitlement="pro" fallback={<Paywall />}>…</Gate>` */
|
|
28
|
+
export declare function Gate({ entitlement, fallback, loading, children }: GateProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { PaywallView, } from '@tenthbilling/paywall';
|
|
3
|
+
import { useEntitlement } from './hooks';
|
|
4
|
+
import { usePaywall } from './usePaywall';
|
|
5
|
+
export function Paywall(props) {
|
|
6
|
+
const controller = usePaywall({
|
|
7
|
+
offering: props.offering,
|
|
8
|
+
locale: props.locale,
|
|
9
|
+
onPurchased: props.onPurchased,
|
|
10
|
+
onRestored: props.onRestored,
|
|
11
|
+
});
|
|
12
|
+
return (_jsx(PaywallView, { controller: controller, presentation: props.presentation, slots: props.slots, theme: props.theme, locale: props.locale, onDismiss: props.onDismiss }));
|
|
13
|
+
}
|
|
14
|
+
export function Gate({ entitlement, fallback, loading = null, children }) {
|
|
15
|
+
const e = useEntitlement(entitlement);
|
|
16
|
+
if (e.loading)
|
|
17
|
+
return _jsx(_Fragment, { children: loading });
|
|
18
|
+
return _jsx(_Fragment, { children: e.active ? children : fallback });
|
|
19
|
+
}
|