@octane-xplat/billing 0.18.0 → 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 CHANGED
@@ -1,29 +1,80 @@
1
- # `@octane-xplat/billing`
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:
2
11
 
3
12
  ```sh
4
- pnpm add @octane-xplat/billing
13
+ bun add @octane-xplat/billing
5
14
  ```
6
15
 
7
- In-app purchases and subscriptions — StoreKit 2 and Play Billing through
8
- `@nativescript/payments`. Owns the store transaction loop; paywall UI and
9
- server-side receipt validation stay in the app.
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:
10
20
 
11
21
  ```ts
12
22
  import { billing } from '@octane-xplat/billing'
13
23
 
14
- billing.connect()
15
- const unsubscribe = billing.onPurchase(async (purchases) => {
16
- for (const p of purchases) {
17
- await grantEntitlement(p)
18
- await billing.finishTransaction(p)
19
- }
20
- })
21
-
22
- const products = await billing.fetchProducts(['com.app.pro'], 'subs')
23
- await billing.requestPurchase('com.app.pro')
24
- const owned = await billing.getAvailablePurchases() // restore
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)
25
74
  ```
26
75
 
27
- Mirrors `expo-iap`/`react-native-iap` naming (fetchProducts, requestPurchase,
28
- finishTransaction, getAvailablePurchases). Web/desktop report
29
- `supported: false`.
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,97 +1,90 @@
1
1
  {
2
- "name": "@octane-xplat/billing",
3
- "version": "0.18.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",
18
- "license": "MIT",
19
- "repository": {
20
- "type": "git",
21
- "url": "https://github.com/octane-xplat/octane-xplat",
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
- "macos": {
35
- "types": "./types/generated/index.macos.d.ts",
36
- "default": "./src/index.macos.ts"
37
- },
38
- "web": {
39
- "types": "./types/generated/index.web.d.ts",
40
- "default": "./src/index.web.ts"
41
- },
42
- "native": {
43
- "types": "./types/generated/index.d.ts",
44
- "default": "./src/index.ts"
45
- },
46
- "types": "./types/generated/index.d.ts",
47
- "default": "./src/index.ts"
48
- },
49
- "./*": {
50
- "types": "./types/generated/*.d.ts",
51
- "default": "./src/*"
52
- },
53
- "./*.ts": {
54
- "types": "./types/generated/*.d.ts",
55
- "default": "./src/*.ts"
56
- }
57
- },
58
- "publishConfig": {
59
- "access": "public"
60
- },
61
- "devDependencies": {
62
- "@nativescript-community/octane": "0.2.5",
63
- "@nativescript/core": "9.1.3",
64
- "@nativescript/types": "9.1.4",
65
- "@tsrx/typescript-plugin": "0.4.11",
66
- "octane": "0.14.0",
67
- "tsrx-typegen": "0.4.0",
68
- "typescript": "5.9.3"
69
- },
70
- "peerDependencies": {
71
- "@nativescript-community/octane": ">=0.2.1 <1",
72
- "@nativescript/core": ">=9.1.0 <10",
73
- "octane": ">=0.6.3 <1"
74
- },
75
- "peerDependenciesMeta": {
76
- "@nativescript-community/octane": {
77
- "optional": true
78
- },
79
- "@nativescript/core": {
80
- "optional": true
81
- }
82
- },
83
- "nativescript": {
84
- "platforms": {
85
- "android": "9.1.0",
86
- "ios": "9.1.0"
87
- }
88
- },
89
- "dependencies": {
90
- "@nativescript/payments": "4.0.1"
91
- },
92
- "scripts": {
93
- "typegen": "tsrx-typegen --target octane",
94
- "pack:check": "tsrx-typegen --pack-check",
95
- "typecheck": "node ../../scripts/typecheck.ts"
96
- }
97
- }
2
+ "name": "@octane-xplat/billing",
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",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/octane-xplat/octane-xplat",
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"
89
+ }
90
+ }
@@ -4,9 +4,10 @@ import type { Billing } from './types'
4
4
 
5
5
  const unsupported = () => Promise.reject(new Error('unsupported: billing is not wired on macOS'))
6
6
 
7
+ /** Store billing service — Play Billing / StoreKit. */
7
8
  export const billing: Billing = {
8
9
  supported: false,
9
- connect: () => {},
10
+ connect: () => Promise.resolve(),
10
11
  disconnect: () => {},
11
12
  canMakePayments: () => false,
12
13
  fetchProducts: unsupported,
package/src/billing.ts CHANGED
@@ -2,9 +2,11 @@
2
2
  // Billing). One Payment client behind the flat service; purchase outcomes
3
3
  // fan out to registered listeners.
4
4
  import { Payment, type Product as NativeProduct, type Transaction } from '@nativescript/payments'
5
- import type { Billing, Product, Purchase, PurchaseOptions } from './types'
5
+ import type { Billing, Product, Purchase, RequestPurchaseOptions } from './types'
6
6
 
7
7
  let client: Payment | null = null
8
+ let ready = false
9
+ const readyWaiters = new Set<() => void>()
8
10
  const purchaseListeners = new Set<(purchases: Purchase[]) => void>()
9
11
  const errorListeners = new Set<(error: { code: string; message: string }) => void>()
10
12
 
@@ -16,6 +18,7 @@ function asProduct(p: NativeProduct): Product {
16
18
  type: p.type,
17
19
  displayPrice: p.priceFormatted,
18
20
  priceMicros: p.priceAmountMicros,
21
+ native: p,
19
22
  }
20
23
  }
21
24
 
@@ -35,12 +38,24 @@ function asPurchase(t: Transaction): Purchase {
35
38
  function getClient(): Payment {
36
39
  if (!client) {
37
40
  client = new Payment()
41
+ client.onReady = () => {
42
+ ready = true
43
+ for (const resolve of readyWaiters) {
44
+ resolve()
45
+ }
46
+ readyWaiters.clear()
47
+ }
38
48
  client.onPurchaseUpdate = (purchases, error) => {
39
49
  if (error) {
40
- const out = { code: (error as any)?.code ?? 'ERROR', message: error.message ?? String(error) }
50
+ const out = {
51
+ code: (error as any)?.code ?? 'ERROR',
52
+ message: error.message ?? String(error),
53
+ }
54
+
41
55
  for (const listener of errorListeners) {
42
56
  listener(out)
43
57
  }
58
+
44
59
  return
45
60
  }
46
61
  const mapped = (purchases ?? []).map(asPurchase)
@@ -52,15 +67,28 @@ function getClient(): Payment {
52
67
  return client
53
68
  }
54
69
 
70
+ /** Store billing service — Play Billing / StoreKit. */
55
71
  export const billing: Billing = {
56
72
  supported: Payment.isSupported(),
57
73
 
58
- connect(): void {
59
- getClient().connect()
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
60
82
  },
61
83
 
62
84
  disconnect(): void {
85
+ ready = false
63
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
64
92
  },
65
93
 
66
94
  canMakePayments(): boolean {
@@ -72,15 +100,28 @@ export const billing: Billing = {
72
100
  return (products ?? []).map(asProduct)
73
101
  },
74
102
 
75
- async requestPurchase(productId: string, options?: PurchaseOptions): Promise<void> {
103
+ async requestPurchase(
104
+ product: Product | string,
105
+ options?: RequestPurchaseOptions,
106
+ ): Promise<void> {
76
107
  const c = getClient()
77
- const [product] = await c.fetchProducts([productId], 'inapp')
78
- if (!product) {
79
- throw Object.assign(new Error(`billing.requestPurchase: product '${productId}' not found`), {
80
- code: 'PRODUCT_UNAVAILABLE',
81
- })
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
+ }
82
123
  }
83
- await c.purchaseProduct(product, options as any)
124
+ await c.purchaseProduct(native, purchaseOptions)
84
125
  },
85
126
 
86
127
  onPurchase(listener: (purchases: Purchase[]) => void): () => void {
@@ -5,9 +5,10 @@ import type { Billing } from './types'
5
5
 
6
6
  const unsupported = () => Promise.reject(new Error('unsupported: no store backend on web'))
7
7
 
8
+ /** Store billing service — Play Billing / StoreKit. */
8
9
  export const billing: Billing = {
9
10
  supported: false,
10
- connect: () => {},
11
+ connect: () => Promise.resolve(),
11
12
  disconnect: () => {},
12
13
  canMakePayments: () => false,
13
14
  fetchProducts: unsupported,
@@ -1,11 +1,13 @@
1
1
  // Windows leaf — no store backend; report unsupported.
2
2
  import type { Billing } from './types'
3
3
 
4
- const unsupported = () => Promise.reject(new Error('unsupported: billing is not available on windows'))
4
+ const unsupported = () =>
5
+ Promise.reject(new Error('unsupported: billing is not available on windows'))
5
6
 
7
+ /** Store billing service — Play Billing / StoreKit. */
6
8
  export const billing: Billing = {
7
9
  supported: false,
8
- connect: () => {},
10
+ connect: () => Promise.resolve(),
9
11
  disconnect: () => {},
10
12
  canMakePayments: () => false,
11
13
  fetchProducts: unsupported,
package/src/types.ts CHANGED
@@ -1,27 +1,46 @@
1
+ /** Store product kind: one-time 'inapp' or 'subs' subscription; 'unknown'
2
+ * when the store reports something else. */
1
3
  export type ProductType = 'inapp' | 'subs' | 'unknown'
2
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). */
3
7
  export interface Product {
8
+ /** Store product identifier as registered in the console. */
4
9
  id: string
5
10
  /** Store-localized title. */
6
11
  title: string
12
+ /** Store-localized description. */
7
13
  description: string
14
+ /** 'inapp' or 'subs' as queried. */
8
15
  type: ProductType
9
16
  /** Localized price string ('$4.99') — null until the store reports it. */
10
17
  displayPrice: string | null
11
18
  /** Price in micros (1_000_000 = 1 unit) — null on iOS, which reports
12
19
  * formatted price only at this API level. */
13
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
14
24
  }
15
25
 
26
+ /** Transaction lifecycle: 'pending' awaits external payment action
27
+ * (ask-to-buy, deferred); 'unknown' covers unmapped store states. */
16
28
  export type PurchaseState = 'pending' | 'purchased' | 'unknown'
17
29
 
30
+ /** One store transaction returned by purchase updates or restore queries. */
18
31
  export interface Purchase {
32
+ /** The Product.id this transaction is for. */
19
33
  productId: string
20
34
  /** Play order id / StoreKit transaction id. */
21
35
  transactionId: string
36
+ /** When the store recorded the transaction; null when unreported. */
22
37
  transactionDate: Date | null
38
+ /** Normalized transaction state. */
23
39
  state: PurchaseState
40
+ /** 'inapp' or 'subs'. */
24
41
  type: ProductType
42
+ /** Play: acknowledged; StoreKit: finished. Unacknowledged consumables
43
+ * are re-delivered by the store until finishTransaction completes them. */
25
44
  isAcknowledged: boolean
26
45
  /** Receipt token for server-side verification (Play purchase token /
27
46
  * StoreKit receipt data). */
@@ -31,33 +50,57 @@ export interface Purchase {
31
50
  native?: unknown
32
51
  }
33
52
 
53
+ /** Options for requestPurchase(); platform bags pass through to the store. */
34
54
  export interface PurchaseOptions {
35
55
  /** Android obfuscated account/profile ids. */
36
56
  accountId?: string
57
+ /** Play Billing overrides — obfuscated ids plus the EU personalized-price flag. */
37
58
  android?: { accountId?: string; profileId?: string; isOfferPersonalized?: boolean }
59
+ /** StoreKit overrides — quantity and sandbox ask-to-buy simulation. */
38
60
  ios?: { quantity?: number; simulatesAskToBuyInSandbox?: boolean }
39
61
  }
40
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. */
41
72
  export interface PurchaseError {
73
+ /** Normalized store error code (Play Billing response / StoreKit error
74
+ * mapped to shared spellings where possible). */
42
75
  code: string
76
+ /** Human-readable store message. */
43
77
  message: string
44
78
  }
45
79
 
80
+ /** Store billing façade — Play Billing on Android, StoreKit on iOS.
81
+ * False `supported` on web/desktop leaves. */
46
82
  export interface Billing {
47
83
  /** StoreKit/Play Billing client exists (false on web/desktop leaves). */
48
84
  readonly supported: boolean
49
- /** Connect the store client — call once at app start, before other calls. */
50
- connect(): void
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. */
51
90
  disconnect(): void
52
91
  /** The device can make payments (parental controls off, store reachable). */
53
92
  canMakePayments(): boolean
93
+ /** Fetch store metadata for console-registered product ids. */
54
94
  fetchProducts(productIds: string[], type?: 'inapp' | 'subs'): Promise<Product[]>
55
- /** Kick the OS purchase sheet for `productId` — the product is fetched
56
- * internally first. Outcomes arrive on onPurchase/onPurchaseError and
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
57
99
  * must be completed with finishTransaction. */
58
- requestPurchase(productId: string, options?: PurchaseOptions): Promise<void>
100
+ requestPurchase(product: Product | string, options?: RequestPurchaseOptions): Promise<void>
59
101
  /** Purchase-update listener; returns the unsubscribe function. */
60
102
  onPurchase(listener: (purchases: Purchase[]) => void): () => void
103
+ /** Purchase-failure listener; returns the unsubscribe function. */
61
104
  onPurchaseError(listener: (error: PurchaseError) => void): () => void
62
105
  /** Owned/unconsumed purchases — the "restore" query on both stores. */
63
106
  getAvailablePurchases(): Promise<Purchase[]>
@@ -1,2 +1,3 @@
1
1
  import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
2
3
  export declare const billing: Billing;
@@ -1,2 +1,3 @@
1
1
  import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
2
3
  export declare const billing: Billing;
@@ -1,2 +1,3 @@
1
1
  import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
2
3
  export declare const billing: Billing;
@@ -1,2 +1,3 @@
1
1
  import type { Billing } from './types.js';
2
+ /** Store billing service — Play Billing / StoreKit. */
2
3
  export declare const billing: Billing;
@@ -1,24 +1,43 @@
1
+ /** Store product kind: one-time 'inapp' or 'subs' subscription; 'unknown'
2
+ * when the store reports something else. */
1
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). */
2
6
  export interface Product {
7
+ /** Store product identifier as registered in the console. */
3
8
  id: string;
4
9
  /** Store-localized title. */
5
10
  title: string;
11
+ /** Store-localized description. */
6
12
  description: string;
13
+ /** 'inapp' or 'subs' as queried. */
7
14
  type: ProductType;
8
15
  /** Localized price string ('$4.99') — null until the store reports it. */
9
16
  displayPrice: string | null;
10
17
  /** Price in micros (1_000_000 = 1 unit) — null on iOS, which reports
11
18
  * formatted price only at this API level. */
12
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;
13
23
  }
24
+ /** Transaction lifecycle: 'pending' awaits external payment action
25
+ * (ask-to-buy, deferred); 'unknown' covers unmapped store states. */
14
26
  export type PurchaseState = 'pending' | 'purchased' | 'unknown';
27
+ /** One store transaction returned by purchase updates or restore queries. */
15
28
  export interface Purchase {
29
+ /** The Product.id this transaction is for. */
16
30
  productId: string;
17
31
  /** Play order id / StoreKit transaction id. */
18
32
  transactionId: string;
33
+ /** When the store recorded the transaction; null when unreported. */
19
34
  transactionDate: Date | null;
35
+ /** Normalized transaction state. */
20
36
  state: PurchaseState;
37
+ /** 'inapp' or 'subs'. */
21
38
  type: ProductType;
39
+ /** Play: acknowledged; StoreKit: finished. Unacknowledged consumables
40
+ * are re-delivered by the store until finishTransaction completes them. */
22
41
  isAcknowledged: boolean;
23
42
  /** Receipt token for server-side verification (Play purchase token /
24
43
  * StoreKit receipt data). */
@@ -27,38 +46,61 @@ export interface Purchase {
27
46
  * when holding the raw reference is required. */
28
47
  native?: unknown;
29
48
  }
49
+ /** Options for requestPurchase(); platform bags pass through to the store. */
30
50
  export interface PurchaseOptions {
31
51
  /** Android obfuscated account/profile ids. */
32
52
  accountId?: string;
53
+ /** Play Billing overrides — obfuscated ids plus the EU personalized-price flag. */
33
54
  android?: {
34
55
  accountId?: string;
35
56
  profileId?: string;
36
57
  isOfferPersonalized?: boolean;
37
58
  };
59
+ /** StoreKit overrides — quantity and sandbox ask-to-buy simulation. */
38
60
  ios?: {
39
61
  quantity?: number;
40
62
  simulatesAskToBuyInSandbox?: boolean;
41
63
  };
42
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. */
43
73
  export interface PurchaseError {
74
+ /** Normalized store error code (Play Billing response / StoreKit error
75
+ * mapped to shared spellings where possible). */
44
76
  code: string;
77
+ /** Human-readable store message. */
45
78
  message: string;
46
79
  }
80
+ /** Store billing façade — Play Billing on Android, StoreKit on iOS.
81
+ * False `supported` on web/desktop leaves. */
47
82
  export interface Billing {
48
83
  /** StoreKit/Play Billing client exists (false on web/desktop leaves). */
49
84
  readonly supported: boolean;
50
- /** Connect the store client — call once at app start, before other calls. */
51
- connect(): void;
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. */
52
90
  disconnect(): void;
53
91
  /** The device can make payments (parental controls off, store reachable). */
54
92
  canMakePayments(): boolean;
93
+ /** Fetch store metadata for console-registered product ids. */
55
94
  fetchProducts(productIds: string[], type?: 'inapp' | 'subs'): Promise<Product[]>;
56
- /** Kick the OS purchase sheet for `productId` — the product is fetched
57
- * internally first. Outcomes arrive on onPurchase/onPurchaseError and
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
58
99
  * must be completed with finishTransaction. */
59
- requestPurchase(productId: string, options?: PurchaseOptions): Promise<void>;
100
+ requestPurchase(product: Product | string, options?: RequestPurchaseOptions): Promise<void>;
60
101
  /** Purchase-update listener; returns the unsubscribe function. */
61
102
  onPurchase(listener: (purchases: Purchase[]) => void): () => void;
103
+ /** Purchase-failure listener; returns the unsubscribe function. */
62
104
  onPurchaseError(listener: (error: PurchaseError) => void): () => void;
63
105
  /** Owned/unconsumed purchases — the "restore" query on both stores. */
64
106
  getAvailablePurchases(): Promise<Purchase[]>;
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2026 Alec Larson
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.