@octane-xplat/billing 0.0.1 → 0.19.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/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @octane-xplat/billing
2
+
3
+ Display store products and manage verified one-time purchases on iOS and Android
4
+ through StoreKit and Google Play Billing. Your app owns the screen, backend
5
+ verification and entitlement ledger. Web and desktop report `supported: false`.
6
+ For the complete purchase/restore path, follow [Sell and restore an in-app purchase](https://octane-xplat.goddardai.org/in-app-purchases).
7
+
8
+ ## Show a store product
9
+
10
+ Install in an existing Xplat app:
11
+
12
+ ```sh
13
+ bun add @octane-xplat/billing
14
+ ```
15
+
16
+ Register a non-consumable product and test account first, following the
17
+ [guide's store setup](https://octane-xplat.goddardai.org/in-app-purchases#register-one-product).
18
+ Use its exact product ID in a screen action. The result contains the store's
19
+ title and localized price; keep the connection for the billing owner's lifetime:
20
+
21
+ ```ts
22
+ import { billing } from '@octane-xplat/billing'
23
+
24
+ export async function readProduct(productId: string) {
25
+ if (!billing.supported) return { status: 'unavailable' as const }
26
+ await billing.connect()
27
+ if (!billing.canMakePayments()) return { status: 'retry' as const }
28
+ const products = await billing.fetchProducts([productId], 'inapp')
29
+ return { status: 'loaded' as const, product: products[0] ?? null }
30
+ }
31
+
32
+ export function closeBillingOwner() {
33
+ billing.disconnect()
34
+ }
35
+ ```
36
+
37
+ Handle a rejected read in your screen's error state. `connect()` resolves once
38
+ the store reports ready, including reconnects after `disconnect()`. A `retry`
39
+ result after a resolved connect means the store setup failed or payments are
40
+ restricted; offer Reload store after checking the store account. If a loaded
41
+ product is absent, check its ID, app identity, availability and tester setup.
42
+ Release the owner when it ends; do not disconnect immediately after initiating
43
+ a purchase.
44
+
45
+ ## Complete and restore safely
46
+
47
+ Use the [maintained controller and screen](https://octane-xplat.goddardai.org/in-app-purchases#own-the-store-connection-and-screen)
48
+ for listeners installed before purchase initiation, authenticated verification,
49
+ pending/cancelled states, duplicate events, restore and disposal. A returned
50
+ purchase request is not proof of payment. The backend must verify and durably
51
+ grant before the app finishes a purchased transaction:
52
+
53
+ ```ts
54
+ import { billing, type Purchase } from '@octane-xplat/billing'
55
+
56
+ export async function finishVerifiedPurchase(purchase: Purchase) {
57
+ // Only after server verification and durable entitlement commit.
58
+ await billing.finishTransaction(purchase, { consume: false })
59
+ }
60
+ ```
61
+
62
+ The purchase object must retain its native transaction reference. Query the
63
+ store again after restart instead of persisting this object as JSON. The guide
64
+ specifies the [backend contract](https://octane-xplat.goddardai.org/in-app-purchases#implement-the-backend-contract-before-buying)
65
+ and recovery checks; the package supplies neither a server verifier nor an
66
+ entitlement database.
67
+
68
+ Subscriptions initiate through the product returned by a `'subs'` fetch, which
69
+ carries the store's offer details into checkout:
70
+
71
+ ```ts
72
+ const [plan] = await billing.fetchProducts(['com.example.plan.monthly'], 'subs')
73
+ if (plan) await billing.requestPurchase(plan)
74
+ ```
75
+
76
+ On Google Play the store applies the product's first offer; choosing between
77
+ base plans or offers and changing an active subscription are not exposed at
78
+ this level. Listing a subscription does not qualify its checkout. Use the
79
+ guide's [support and recovery boundaries](https://octane-xplat.goddardai.org/in-app-purchases#buy-finish-and-recover)
80
+ before choosing consumables or subscriptions.
package/package.json CHANGED
@@ -1,11 +1,90 @@
1
1
  {
2
2
  "name": "@octane-xplat/billing",
3
- "version": "0.0.1",
4
- "description": "Placeholder release — no supported API. The first real version publishes via octane-xplat CI.",
3
+ "version": "0.19.0",
4
+ "description": "In-app purchases and subscriptions for Octane xplat — StoreKit 2 and Play Billing via @nativescript/payments",
5
+ "keywords": [
6
+ "android",
7
+ "cross-platform",
8
+ "nativescript",
9
+ "octane",
10
+ "xplat",
11
+ "iap",
12
+ "billing",
13
+ "storekit",
14
+ "purchases",
15
+ "subscriptions"
16
+ ],
17
+ "homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/billing",
5
18
  "license": "MIT",
6
19
  "repository": {
7
20
  "type": "git",
8
21
  "url": "https://github.com/octane-xplat/octane-xplat",
9
22
  "directory": "packages/billing"
23
+ },
24
+ "files": [
25
+ "src",
26
+ "platforms",
27
+ "types",
28
+ "!src/**/*.test.*",
29
+ "!src/**/*.spec.*"
30
+ ],
31
+ "type": "module",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./types/generated/index.d.ts",
35
+ "macos": "./src/index.macos.ts",
36
+ "web": "./src/index.web.ts",
37
+ "native": "./src/index.ts",
38
+ "default": "./src/index.ts"
39
+ },
40
+ "./*": {
41
+ "types": "./types/generated/*.d.ts",
42
+ "default": "./src/*"
43
+ },
44
+ "./*.ts": {
45
+ "types": "./types/generated/*.d.ts",
46
+ "default": "./src/*.ts"
47
+ }
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "test": "node --test tests/*.test.mjs",
54
+ "typegen": "tsrx-typegen --target octane",
55
+ "prepack": "bun run typegen && tsrx-typegen --pack-check",
56
+ "pack:check": "tsrx-typegen --pack-check",
57
+ "typecheck": "node ../../scripts/typecheck.ts"
58
+ },
59
+ "devDependencies": {
60
+ "@nativescript-community/octane": "0.2.5",
61
+ "@nativescript/core": "9.1.3",
62
+ "@nativescript/types": "9.1.4",
63
+ "@tsrx/typescript-plugin": "0.4.11",
64
+ "octane": "0.16.3",
65
+ "tsrx-typegen": "0.5.0",
66
+ "typescript": "5.9.3"
67
+ },
68
+ "peerDependencies": {
69
+ "@nativescript-community/octane": ">=0.2.1 <1",
70
+ "@nativescript/core": ">=9.1.0 <10",
71
+ "octane": ">=0.6.3 <1"
72
+ },
73
+ "peerDependenciesMeta": {
74
+ "@nativescript-community/octane": {
75
+ "optional": true
76
+ },
77
+ "@nativescript/core": {
78
+ "optional": true
79
+ }
80
+ },
81
+ "nativescript": {
82
+ "platforms": {
83
+ "android": "9.1.0",
84
+ "ios": "9.1.0"
85
+ }
86
+ },
87
+ "dependencies": {
88
+ "@nativescript/payments": "4.0.1"
10
89
  }
11
90
  }
@@ -0,0 +1,2 @@
1
+ // Linux's system webview runs the shared web implementation.
2
+ export { billing } from './billing.web'
@@ -0,0 +1,20 @@
1
+ // Billing — macOS leaf. StoreKit is bridgeable through the AppKit host but
2
+ // not wired; report unsupported.
3
+ import type { Billing } from './types'
4
+
5
+ const unsupported = () => Promise.reject(new Error('unsupported: billing is not wired on macOS'))
6
+
7
+ /** Store billing service — Play Billing / StoreKit. */
8
+ export const billing: Billing = {
9
+ supported: false,
10
+ connect: () => Promise.resolve(),
11
+ disconnect: () => {},
12
+ canMakePayments: () => false,
13
+ fetchProducts: unsupported,
14
+ requestPurchase: unsupported,
15
+ onPurchase: () => () => {},
16
+ onPurchaseError: () => () => {},
17
+ getAvailablePurchases: unsupported,
18
+ finishTransaction: unsupported,
19
+ showManageSubscriptions: unsupported,
20
+ }
package/src/billing.ts ADDED
@@ -0,0 +1,155 @@
1
+ // Billing — native leaf over @nativescript/payments (StoreKit 2 + Play
2
+ // Billing). One Payment client behind the flat service; purchase outcomes
3
+ // fan out to registered listeners.
4
+ import { Payment, type Product as NativeProduct, type Transaction } from '@nativescript/payments'
5
+ import type { Billing, Product, Purchase, RequestPurchaseOptions } from './types'
6
+
7
+ let client: Payment | null = null
8
+ let ready = false
9
+ const readyWaiters = new Set<() => void>()
10
+ const purchaseListeners = new Set<(purchases: Purchase[]) => void>()
11
+ const errorListeners = new Set<(error: { code: string; message: string }) => void>()
12
+
13
+ function asProduct(p: NativeProduct): Product {
14
+ return {
15
+ id: p.id,
16
+ title: p.localizedTitle ?? p.title ?? p.name,
17
+ description: p.description,
18
+ type: p.type,
19
+ displayPrice: p.priceFormatted,
20
+ priceMicros: p.priceAmountMicros,
21
+ native: p,
22
+ }
23
+ }
24
+
25
+ function asPurchase(t: Transaction): Purchase {
26
+ return {
27
+ productId: t.productId,
28
+ transactionId: t.orderId,
29
+ transactionDate: t.orderDate ?? null,
30
+ state: t.state,
31
+ type: t.type,
32
+ isAcknowledged: t.isAcknowledged,
33
+ receiptToken: t.receiptToken,
34
+ native: t,
35
+ }
36
+ }
37
+
38
+ function getClient(): Payment {
39
+ if (!client) {
40
+ client = new Payment()
41
+ client.onReady = () => {
42
+ ready = true
43
+ for (const resolve of readyWaiters) {
44
+ resolve()
45
+ }
46
+ readyWaiters.clear()
47
+ }
48
+ client.onPurchaseUpdate = (purchases, error) => {
49
+ if (error) {
50
+ const out = {
51
+ code: (error as any)?.code ?? 'ERROR',
52
+ message: error.message ?? String(error),
53
+ }
54
+
55
+ for (const listener of errorListeners) {
56
+ listener(out)
57
+ }
58
+
59
+ return
60
+ }
61
+ const mapped = (purchases ?? []).map(asPurchase)
62
+ for (const listener of purchaseListeners) {
63
+ listener(mapped)
64
+ }
65
+ }
66
+ }
67
+ return client
68
+ }
69
+
70
+ /** Store billing service — Play Billing / StoreKit. */
71
+ export const billing: Billing = {
72
+ supported: Payment.isSupported(),
73
+
74
+ connect(): Promise<void> {
75
+ const c = getClient()
76
+ if (ready) {
77
+ return Promise.resolve()
78
+ }
79
+ const pending = new Promise<void>((resolve) => readyWaiters.add(resolve))
80
+ c.connect()
81
+ return pending
82
+ },
83
+
84
+ disconnect(): void {
85
+ ready = false
86
+ client?.disconnect()
87
+ // @nativescript/payments 4.0.1 never re-fires onReady on a manually
88
+ // disconnected Payment (its internal flag stays set), so reconnecting
89
+ // needs a fresh client — a new Payment's init reconnects and re-arms
90
+ // the callback.
91
+ client = null
92
+ },
93
+
94
+ canMakePayments(): boolean {
95
+ return getClient().canMakePayments()
96
+ },
97
+
98
+ async fetchProducts(productIds: string[], type: 'inapp' | 'subs' = 'inapp'): Promise<Product[]> {
99
+ const products = await getClient().fetchProducts(productIds, type)
100
+ return (products ?? []).map(asProduct)
101
+ },
102
+
103
+ async requestPurchase(
104
+ product: Product | string,
105
+ options?: RequestPurchaseOptions,
106
+ ): Promise<void> {
107
+ const c = getClient()
108
+ const { type, ...purchaseOptions } = options ?? {}
109
+ let native: NativeProduct | undefined
110
+ if (typeof product === 'string') {
111
+ const [fetched] = await c.fetchProducts([product], type ?? 'inapp')
112
+ if (!fetched) {
113
+ throw Object.assign(new Error(`billing.requestPurchase: product '${product}' not found`), {
114
+ code: 'PRODUCT_UNAVAILABLE',
115
+ })
116
+ }
117
+ native = fetched
118
+ } else {
119
+ native = product.native as NativeProduct | undefined
120
+ if (!native) {
121
+ throw new Error('billing.requestPurchase: product lacks its native store reference')
122
+ }
123
+ }
124
+ await c.purchaseProduct(native, purchaseOptions)
125
+ },
126
+
127
+ onPurchase(listener: (purchases: Purchase[]) => void): () => void {
128
+ purchaseListeners.add(listener)
129
+ return () => purchaseListeners.delete(listener)
130
+ },
131
+
132
+ onPurchaseError(listener): () => void {
133
+ errorListeners.add(listener)
134
+ return () => errorListeners.delete(listener)
135
+ },
136
+
137
+ async getAvailablePurchases(): Promise<Purchase[]> {
138
+ const transactions = await getClient().fetchPurchases()
139
+ return (transactions ?? []).map(asPurchase)
140
+ },
141
+
142
+ async finishTransaction(purchase: Purchase, options?: { consume?: boolean }): Promise<void> {
143
+ const native = purchase.native as Transaction | undefined
144
+ if (!native) {
145
+ throw new Error('billing.finishTransaction: purchase lacks its native transaction')
146
+ }
147
+ await native.finish(options)
148
+ },
149
+
150
+ async showManageSubscriptions(options?: { subscriptionGroupId?: string }): Promise<void> {
151
+ await getClient().showSubscriptionsManagement({
152
+ ios: { subscriptionGroupID: options?.subscriptionGroupId },
153
+ })
154
+ },
155
+ }
@@ -0,0 +1,21 @@
1
+ // Billing — web leaf. No web store backend exists yet (a delegated-checkout
2
+ // seam like RevenueCat Web Billing is a possible future). supported: false;
3
+ // calls throw rather than silently doing nothing.
4
+ import type { Billing } from './types'
5
+
6
+ const unsupported = () => Promise.reject(new Error('unsupported: no store backend on web'))
7
+
8
+ /** Store billing service — Play Billing / StoreKit. */
9
+ export const billing: Billing = {
10
+ supported: false,
11
+ connect: () => Promise.resolve(),
12
+ disconnect: () => {},
13
+ canMakePayments: () => false,
14
+ fetchProducts: unsupported,
15
+ requestPurchase: unsupported,
16
+ onPurchase: () => () => {},
17
+ onPurchaseError: () => () => {},
18
+ getAvailablePurchases: unsupported,
19
+ finishTransaction: unsupported,
20
+ showManageSubscriptions: unsupported,
21
+ }
@@ -0,0 +1,20 @@
1
+ // Windows leaf — no store backend; report unsupported.
2
+ import type { Billing } from './types'
3
+
4
+ const unsupported = () =>
5
+ Promise.reject(new Error('unsupported: billing is not available on windows'))
6
+
7
+ /** Store billing service — Play Billing / StoreKit. */
8
+ export const billing: Billing = {
9
+ supported: false,
10
+ connect: () => Promise.resolve(),
11
+ disconnect: () => {},
12
+ canMakePayments: () => false,
13
+ fetchProducts: unsupported,
14
+ requestPurchase: unsupported,
15
+ onPurchase: () => () => {},
16
+ onPurchaseError: () => () => {},
17
+ getAvailablePurchases: unsupported,
18
+ finishTransaction: unsupported,
19
+ showManageSubscriptions: unsupported,
20
+ }
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { billing } from './billing.macos'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { billing } from './billing'
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { billing } from './billing.web'
package/src/types.ts ADDED
@@ -0,0 +1,114 @@
1
+ /** Store product kind: one-time 'inapp' or 'subs' subscription; 'unknown'
2
+ * when the store reports something else. */
3
+ export type ProductType = 'inapp' | 'subs' | 'unknown'
4
+
5
+ /** A store product as fetched from the app store. Prices are null until
6
+ * the store reports them (and priceMicros stays null on iOS). */
7
+ export interface Product {
8
+ /** Store product identifier as registered in the console. */
9
+ id: string
10
+ /** Store-localized title. */
11
+ title: string
12
+ /** Store-localized description. */
13
+ description: string
14
+ /** 'inapp' or 'subs' as queried. */
15
+ type: ProductType
16
+ /** Localized price string ('$4.99') — null until the store reports it. */
17
+ displayPrice: string | null
18
+ /** Price in micros (1_000_000 = 1 unit) — null on iOS, which reports
19
+ * formatted price only at this API level. */
20
+ priceMicros: number | null
21
+ /** Underlying plugin product — pass the fetched Product back to
22
+ * requestPurchase so subscription offer details reach checkout. */
23
+ native?: unknown
24
+ }
25
+
26
+ /** Transaction lifecycle: 'pending' awaits external payment action
27
+ * (ask-to-buy, deferred); 'unknown' covers unmapped store states. */
28
+ export type PurchaseState = 'pending' | 'purchased' | 'unknown'
29
+
30
+ /** One store transaction returned by purchase updates or restore queries. */
31
+ export interface Purchase {
32
+ /** The Product.id this transaction is for. */
33
+ productId: string
34
+ /** Play order id / StoreKit transaction id. */
35
+ transactionId: string
36
+ /** When the store recorded the transaction; null when unreported. */
37
+ transactionDate: Date | null
38
+ /** Normalized transaction state. */
39
+ state: PurchaseState
40
+ /** 'inapp' or 'subs'. */
41
+ type: ProductType
42
+ /** Play: acknowledged; StoreKit: finished. Unacknowledged consumables
43
+ * are re-delivered by the store until finishTransaction completes them. */
44
+ isAcknowledged: boolean
45
+ /** Receipt token for server-side verification (Play purchase token /
46
+ * StoreKit receipt data). */
47
+ receiptToken: string
48
+ /** Underlying plugin transaction — pass to finishTransaction internals
49
+ * when holding the raw reference is required. */
50
+ native?: unknown
51
+ }
52
+
53
+ /** Options for requestPurchase(); platform bags pass through to the store. */
54
+ export interface PurchaseOptions {
55
+ /** Android obfuscated account/profile ids. */
56
+ accountId?: string
57
+ /** Play Billing overrides — obfuscated ids plus the EU personalized-price flag. */
58
+ android?: { accountId?: string; profileId?: string; isOfferPersonalized?: boolean }
59
+ /** StoreKit overrides — quantity and sandbox ask-to-buy simulation. */
60
+ ios?: { quantity?: number; simulatesAskToBuyInSandbox?: boolean }
61
+ }
62
+
63
+ /** requestPurchase() options: store options plus the product kind used to
64
+ * fetch an id before checkout. */
65
+ export interface RequestPurchaseOptions extends PurchaseOptions {
66
+ /** Product kind fetched when `requestPurchase` receives an id rather than
67
+ * a fetched Product; defaults to 'inapp'. Use 'subs' for subscriptions. */
68
+ type?: 'inapp' | 'subs'
69
+ }
70
+
71
+ /** A store-reported failure. */
72
+ export interface PurchaseError {
73
+ /** Normalized store error code (Play Billing response / StoreKit error
74
+ * mapped to shared spellings where possible). */
75
+ code: string
76
+ /** Human-readable store message. */
77
+ message: string
78
+ }
79
+
80
+ /** Store billing façade — Play Billing on Android, StoreKit on iOS.
81
+ * False `supported` on web/desktop leaves. */
82
+ export interface Billing {
83
+ /** StoreKit/Play Billing client exists (false on web/desktop leaves). */
84
+ readonly supported: boolean
85
+ /** Connect the store client — call once at app start, before other calls.
86
+ * Resolves once the store reports ready; check canMakePayments() after it
87
+ * resolves. A call after disconnect() awaits the next readiness signal. */
88
+ connect(): Promise<void>
89
+ /** Release the store client connection. */
90
+ disconnect(): void
91
+ /** The device can make payments (parental controls off, store reachable). */
92
+ canMakePayments(): boolean
93
+ /** Fetch store metadata for console-registered product ids. */
94
+ fetchProducts(productIds: string[], type?: 'inapp' | 'subs'): Promise<Product[]>
95
+ /** Kick the OS purchase sheet. Pass the Product returned by fetchProducts —
96
+ * required for subscriptions, whose 'subs' fetch carries the store's offer
97
+ * details — or a product id, fetched internally as options.type
98
+ * (default 'inapp'). Outcomes arrive on onPurchase/onPurchaseError and
99
+ * must be completed with finishTransaction. */
100
+ requestPurchase(product: Product | string, options?: RequestPurchaseOptions): Promise<void>
101
+ /** Purchase-update listener; returns the unsubscribe function. */
102
+ onPurchase(listener: (purchases: Purchase[]) => void): () => void
103
+ /** Purchase-failure listener; returns the unsubscribe function. */
104
+ onPurchaseError(listener: (error: PurchaseError) => void): () => void
105
+ /** Owned/unconsumed purchases — the "restore" query on both stores. */
106
+ getAvailablePurchases(): Promise<Purchase[]>
107
+ /** Consume/acknowledge a completed purchase (Play: consume or
108
+ * acknowledge; StoreKit: finish). Options.consume = consumable flow. */
109
+ finishTransaction(purchase: Purchase, options?: { consume?: boolean }): Promise<void>
110
+ /** Native subscription management UI. */
111
+ showManageSubscriptions(options?: { subscriptionGroupId?: string }): Promise<void>
112
+ }
113
+
114
+ export declare const billing: Billing
@@ -0,0 +1,14 @@
1
+ {
2
+ "version": 1,
3
+ "files": [
4
+ "billing.d.ts",
5
+ "billing.linux.d.ts",
6
+ "billing.macos.d.ts",
7
+ "billing.web.d.ts",
8
+ "billing.windows.d.ts",
9
+ "index.d.ts",
10
+ "index.macos.d.ts",
11
+ "index.web.d.ts",
12
+ "types.d.ts"
13
+ ]
14
+ }
@@ -0,0 +1,3 @@
1
+ import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
3
+ export declare const billing: Billing;
@@ -0,0 +1 @@
1
+ export { billing } from './billing.web.js';
@@ -0,0 +1,3 @@
1
+ import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
3
+ export declare const billing: Billing;
@@ -0,0 +1,3 @@
1
+ import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
3
+ export declare const billing: Billing;
@@ -0,0 +1,3 @@
1
+ import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
3
+ export declare const billing: Billing;
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { billing } from './billing.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { billing } from './billing.macos.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { billing } from './billing.web.js';
@@ -0,0 +1,117 @@
1
+ /** Store product kind: one-time 'inapp' or 'subs' subscription; 'unknown'
2
+ * when the store reports something else. */
3
+ export type ProductType = 'inapp' | 'subs' | 'unknown';
4
+ /** A store product as fetched from the app store. Prices are null until
5
+ * the store reports them (and priceMicros stays null on iOS). */
6
+ export interface Product {
7
+ /** Store product identifier as registered in the console. */
8
+ id: string;
9
+ /** Store-localized title. */
10
+ title: string;
11
+ /** Store-localized description. */
12
+ description: string;
13
+ /** 'inapp' or 'subs' as queried. */
14
+ type: ProductType;
15
+ /** Localized price string ('$4.99') — null until the store reports it. */
16
+ displayPrice: string | null;
17
+ /** Price in micros (1_000_000 = 1 unit) — null on iOS, which reports
18
+ * formatted price only at this API level. */
19
+ priceMicros: number | null;
20
+ /** Underlying plugin product — pass the fetched Product back to
21
+ * requestPurchase so subscription offer details reach checkout. */
22
+ native?: unknown;
23
+ }
24
+ /** Transaction lifecycle: 'pending' awaits external payment action
25
+ * (ask-to-buy, deferred); 'unknown' covers unmapped store states. */
26
+ export type PurchaseState = 'pending' | 'purchased' | 'unknown';
27
+ /** One store transaction returned by purchase updates or restore queries. */
28
+ export interface Purchase {
29
+ /** The Product.id this transaction is for. */
30
+ productId: string;
31
+ /** Play order id / StoreKit transaction id. */
32
+ transactionId: string;
33
+ /** When the store recorded the transaction; null when unreported. */
34
+ transactionDate: Date | null;
35
+ /** Normalized transaction state. */
36
+ state: PurchaseState;
37
+ /** 'inapp' or 'subs'. */
38
+ type: ProductType;
39
+ /** Play: acknowledged; StoreKit: finished. Unacknowledged consumables
40
+ * are re-delivered by the store until finishTransaction completes them. */
41
+ isAcknowledged: boolean;
42
+ /** Receipt token for server-side verification (Play purchase token /
43
+ * StoreKit receipt data). */
44
+ receiptToken: string;
45
+ /** Underlying plugin transaction — pass to finishTransaction internals
46
+ * when holding the raw reference is required. */
47
+ native?: unknown;
48
+ }
49
+ /** Options for requestPurchase(); platform bags pass through to the store. */
50
+ export interface PurchaseOptions {
51
+ /** Android obfuscated account/profile ids. */
52
+ accountId?: string;
53
+ /** Play Billing overrides — obfuscated ids plus the EU personalized-price flag. */
54
+ android?: {
55
+ accountId?: string;
56
+ profileId?: string;
57
+ isOfferPersonalized?: boolean;
58
+ };
59
+ /** StoreKit overrides — quantity and sandbox ask-to-buy simulation. */
60
+ ios?: {
61
+ quantity?: number;
62
+ simulatesAskToBuyInSandbox?: boolean;
63
+ };
64
+ }
65
+ /** requestPurchase() options: store options plus the product kind used to
66
+ * fetch an id before checkout. */
67
+ export interface RequestPurchaseOptions extends PurchaseOptions {
68
+ /** Product kind fetched when `requestPurchase` receives an id rather than
69
+ * a fetched Product; defaults to 'inapp'. Use 'subs' for subscriptions. */
70
+ type?: 'inapp' | 'subs';
71
+ }
72
+ /** A store-reported failure. */
73
+ export interface PurchaseError {
74
+ /** Normalized store error code (Play Billing response / StoreKit error
75
+ * mapped to shared spellings where possible). */
76
+ code: string;
77
+ /** Human-readable store message. */
78
+ message: string;
79
+ }
80
+ /** Store billing façade — Play Billing on Android, StoreKit on iOS.
81
+ * False `supported` on web/desktop leaves. */
82
+ export interface Billing {
83
+ /** StoreKit/Play Billing client exists (false on web/desktop leaves). */
84
+ readonly supported: boolean;
85
+ /** Connect the store client — call once at app start, before other calls.
86
+ * Resolves once the store reports ready; check canMakePayments() after it
87
+ * resolves. A call after disconnect() awaits the next readiness signal. */
88
+ connect(): Promise<void>;
89
+ /** Release the store client connection. */
90
+ disconnect(): void;
91
+ /** The device can make payments (parental controls off, store reachable). */
92
+ canMakePayments(): boolean;
93
+ /** Fetch store metadata for console-registered product ids. */
94
+ fetchProducts(productIds: string[], type?: 'inapp' | 'subs'): Promise<Product[]>;
95
+ /** Kick the OS purchase sheet. Pass the Product returned by fetchProducts —
96
+ * required for subscriptions, whose 'subs' fetch carries the store's offer
97
+ * details — or a product id, fetched internally as options.type
98
+ * (default 'inapp'). Outcomes arrive on onPurchase/onPurchaseError and
99
+ * must be completed with finishTransaction. */
100
+ requestPurchase(product: Product | string, options?: RequestPurchaseOptions): Promise<void>;
101
+ /** Purchase-update listener; returns the unsubscribe function. */
102
+ onPurchase(listener: (purchases: Purchase[]) => void): () => void;
103
+ /** Purchase-failure listener; returns the unsubscribe function. */
104
+ onPurchaseError(listener: (error: PurchaseError) => void): () => void;
105
+ /** Owned/unconsumed purchases — the "restore" query on both stores. */
106
+ getAvailablePurchases(): Promise<Purchase[]>;
107
+ /** Consume/acknowledge a completed purchase (Play: consume or
108
+ * acknowledge; StoreKit: finish). Options.consume = consumable flow. */
109
+ finishTransaction(purchase: Purchase, options?: {
110
+ consume?: boolean;
111
+ }): Promise<void>;
112
+ /** Native subscription management UI. */
113
+ showManageSubscriptions(options?: {
114
+ subscriptionGroupId?: string;
115
+ }): Promise<void>;
116
+ }
117
+ export declare const billing: Billing;