@chipmobilesdk/rn-payment 0.0.0-stage → 0.2.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/CHANGELOG.md +201 -0
- package/README.md +885 -3
- package/package.json +53 -5
- package/skills/sdk-rn-payment/SKILL.md +164 -0
- package/src/adapter/createStoreProvider.ts +117 -0
- package/src/adapter/iap-adapter.ts +547 -0
- package/src/adapter/port.ts +198 -0
- package/src/config/types.ts +295 -0
- package/src/config/validate.ts +232 -0
- package/src/core/mutex.ts +128 -0
- package/src/core/retry.ts +133 -0
- package/src/core/timers.ts +249 -0
- package/src/diagnostics/errors.ts +110 -0
- package/src/diagnostics/events.ts +266 -0
- package/src/diagnostics/messageKeys.ts +95 -0
- package/src/engine/engine.ts +951 -0
- package/src/engine/finalize.ts +298 -0
- package/src/engine/pending.ts +118 -0
- package/src/engine/products.ts +259 -0
- package/src/engine/purchase.ts +526 -0
- package/src/engine/subscriptions.ts +157 -0
- package/src/entitlements/aggregate.ts +242 -0
- package/src/entitlements/mapping.ts +67 -0
- package/src/entitlements/policy.ts +129 -0
- package/src/index.ts +172 -0
- package/src/persistence/mmkv-store.ts +150 -0
- package/src/persistence/snapshot.ts +247 -0
- package/src/react/PaymentProvider.tsx +79 -0
- package/src/react/hooks.ts +274 -0
- package/src/testing/fakeStore.ts +374 -0
- package/src/testing/index.ts +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@chipmobilesdk/rn-payment",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"description": "Reusable in-app payment and entitlement capability for ChipMobileSdk React Native apps",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"types": "src/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.ts",
|
|
12
|
+
"react-native": "./src/index.ts",
|
|
13
|
+
"default": "./src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"./testing": {
|
|
16
|
+
"types": "./src/testing/index.ts",
|
|
17
|
+
"react-native": "./src/testing/index.ts",
|
|
18
|
+
"default": "./src/testing/index.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src/index.ts",
|
|
23
|
+
"src/adapter",
|
|
24
|
+
"src/config",
|
|
25
|
+
"src/core",
|
|
26
|
+
"src/diagnostics",
|
|
27
|
+
"src/engine",
|
|
28
|
+
"src/entitlements",
|
|
29
|
+
"src/persistence",
|
|
30
|
+
"src/react",
|
|
31
|
+
"src/testing",
|
|
32
|
+
"skills",
|
|
33
|
+
"README.md",
|
|
34
|
+
"CHANGELOG.md"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"scripts": {
|
|
38
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
39
|
+
"pack:dry-run": "npm pack --dry-run"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"react": ">=19",
|
|
43
|
+
"react-native": ">=0.85",
|
|
44
|
+
"react-native-iap": ">=16.6.1 <17",
|
|
45
|
+
"react-native-mmkv": ">=4.0.0",
|
|
46
|
+
"react-native-nitro-modules": ">=0.36.5"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"react-native",
|
|
50
|
+
"in-app-purchase",
|
|
51
|
+
"subscriptions",
|
|
52
|
+
"entitlements"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sdk-rn-payment
|
|
3
|
+
description: "In-app payments via @chipmobilesdk/rn-payment: Play Billing one-time/consumable/subscription behind a store-neutral contract, entitlement keys with a required unknown state, pending purchases visible across sessions, guaranteed transaction finalization, per-environment production-transaction blocking. Use when selling a feature, gating on an entitlement, showing a payment still pending after a restart, debugging a purchase that returns environment_blocked or an entitlement stuck at unknown, mapping Play products to entitlement keys, or answering a Data safety question about purchases."
|
|
4
|
+
metadata:
|
|
5
|
+
package: "@chipmobilesdk/rn-payment"
|
|
6
|
+
packageVersion: "0.2.0"
|
|
7
|
+
user-invocable: true
|
|
8
|
+
disable-model-invocation: false
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# @chipmobilesdk/rn-payment
|
|
12
|
+
|
|
13
|
+
One-time, consumable and subscription purchases on Android via Google Play Billing,
|
|
14
|
+
behind a contract that names no store. The app asks about **entitlement keys** it chose;
|
|
15
|
+
the package owns the transaction. Consuming code never imports the billing library.
|
|
16
|
+
|
|
17
|
+
## Use this when
|
|
18
|
+
|
|
19
|
+
- Selling a feature, or gating one on an entitlement
|
|
20
|
+
- Showing "payment pending" — in this session or after a restart — and withholding Buy
|
|
21
|
+
- Mapping Play product ids to entitlement keys
|
|
22
|
+
- Debugging a purchase that returns `environment_blocked`, or an entitlement stuck at
|
|
23
|
+
`unknown`
|
|
24
|
+
- Wiring `no_ads` (or any entitlement) to another system
|
|
25
|
+
- Answering a Data safety / Play policy question about purchases
|
|
26
|
+
|
|
27
|
+
## Do not use this for
|
|
28
|
+
|
|
29
|
+
- **Paywall UI, pricing strategy, or when to offer** — the package ships no paywall, no
|
|
30
|
+
price table and no purchase button, and chooses no moment.
|
|
31
|
+
- **What an entitlement unlocks** — it reports `entitled`; the meaning is the app's.
|
|
32
|
+
- **Cancellation UI** — platforms forbid a self-built one. Use
|
|
33
|
+
`openSubscriptionManagement()`, which opens the store's.
|
|
34
|
+
- **Receipt verification** — `validateTransaction` is a hook for *your* backend; the
|
|
35
|
+
package calls no endpoint but the billing service.
|
|
36
|
+
- iOS — contract only. `createStoreProvider()` returns an `unavailable` stub.
|
|
37
|
+
|
|
38
|
+
## `unknown` is a real state, and collapsing it is the defect this package exists to prevent
|
|
39
|
+
|
|
40
|
+
`getEntitlement()` returns `unknown | entitled | not_entitled`. On a cold start the answer
|
|
41
|
+
is genuinely not known yet, and the package says so rather than guessing. Treat `unknown`
|
|
42
|
+
as "wait", never as "unpaid":
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
if (state.status === 'unknown') return <Loading />; // not the unpaid branch
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Collapse it and a paying user watches ads flash before they vanish, or sees a purchased
|
|
49
|
+
feature locked for a second. It is also **re-entrant** — a cached answer returns to
|
|
50
|
+
`unknown` past its staleness ceiling (7 days by default), so it is not merely a startup
|
|
51
|
+
flicker. See `## The entitlement model`.
|
|
52
|
+
|
|
53
|
+
## Pending is observable across sessions — never keep your own marker
|
|
54
|
+
|
|
55
|
+
New in 0.2.0. `usePendingPurchases(productKeys?)` (or `getPendingPurchases()` /
|
|
56
|
+
`subscribePendingPurchases()`) lists `{ productKey, since }` for payments still in flight,
|
|
57
|
+
read from the persisted snapshot, so it survives an app kill and an offline restart. Same
|
|
58
|
+
rule as entitlements: check `status` first — `unknown` is not "nothing pending". Show the
|
|
59
|
+
existing `payment.state.pending` message key and withhold Buy while listed.
|
|
60
|
+
`purchase_pending_resolved` (`settled` → then `entitlement_granted`, or `cancelled` /
|
|
61
|
+
`expired`) fires once when it ends. On Android an unpaid end is always `cancelled`: Play
|
|
62
|
+
does not say why. See `### A payment that stays pending` under `## Purchase flow`.
|
|
63
|
+
|
|
64
|
+
## A debug build cannot buy anything, and that is the guard working
|
|
65
|
+
|
|
66
|
+
`purchase()` returning `environment_blocked` in development is correct, not a bug. The
|
|
67
|
+
rule: a chargeable purchase is permitted only when `environment: 'production'` **and**
|
|
68
|
+
`isProductionBuild: true`. A non-production build declaring `production` throws
|
|
69
|
+
`PAYMENT_PRODUCTION_CONFIG_IN_NON_PRODUCTION_BUILD` at construction and never connects.
|
|
70
|
+
|
|
71
|
+
Consequence worth knowing before you plan device testing: **licence-tester purchases are
|
|
72
|
+
real Play transactions**, so verifying them needs a release-configuration build
|
|
73
|
+
(`isProductionBuild: !__DEV__`, `environment: 'production'`) installed from a Play track.
|
|
74
|
+
Debug builds are structurally unable to reach the store. Drive every behaviour that is
|
|
75
|
+
not "does Play accept this" through the fake store instead. See
|
|
76
|
+
`## Environments and the production guard`.
|
|
77
|
+
|
|
78
|
+
Unlike `@chipmobilesdk/rn-ads`, there is **no test-id substitution**: Play has no test
|
|
79
|
+
*product* id, so blocking is the only honest option.
|
|
80
|
+
|
|
81
|
+
## Two more things not to "fix"
|
|
82
|
+
|
|
83
|
+
- **Finalization is not yours to call** for non-consumables. Google auto-refunds an
|
|
84
|
+
unacknowledged purchase after three days, so the package owns a persisted queue drained
|
|
85
|
+
at startup and on foreground. A **consumable** is the exception: call `consume()` after
|
|
86
|
+
*your* data records the item, because only you know it was banked.
|
|
87
|
+
- **`renewalState: 'indeterminate'` on Android is honest.** Play returns a grace-period
|
|
88
|
+
subscription as active and stops returning a held one, so the two are not separable
|
|
89
|
+
client-side. The default policy (`grant` in grace, `withhold` on hold) is nonetheless
|
|
90
|
+
correct with no backend, because Play's own behaviour matches it. A *non-default* policy
|
|
91
|
+
needs server data through the validation hook.
|
|
92
|
+
|
|
93
|
+
Call `handleForeground()` yourself on app resume — the package will not guess what
|
|
94
|
+
"foreground" means for a host backgrounded by its own modal or the store's purchase sheet.
|
|
95
|
+
|
|
96
|
+
## Public API (v0.2.0)
|
|
97
|
+
|
|
98
|
+
Runtime: `createEngine` (throws `PaymentConfigError` on a bad catalog or an unsafe
|
|
99
|
+
environment; everything else returns a typed result) · `createStoreProvider` ·
|
|
100
|
+
`PaymentProvider`, `usePaymentEngine` · `useEntitlement`, `useEntitlements`,
|
|
101
|
+
`usePendingPurchases`, `usePaymentStatus`, `usePurchase`, `useProducts`, `useSync` ·
|
|
102
|
+
`PaymentError`,
|
|
103
|
+
`PaymentConfigError`, `PAYMENT_ERROR_CODES` · `DEFAULT_MESSAGES`,
|
|
104
|
+
`DEFAULT_USER_MESSAGES`, `DEFAULT_DEVELOPER_MESSAGES`, `USER_MESSAGE_KEYS`,
|
|
105
|
+
`DEVELOPER_MESSAGE_KEYS`, `isUserMessageKey` · timeout/retry/cache defaults and bounds
|
|
106
|
+
(`DEFAULT_STALE_AFTER_MS`, `DEFAULT_PURCHASE_LAUNCH_TIMEOUT_MS`, `STALE_AFTER_BOUNDS`, …)
|
|
107
|
+
· `createFakeStore`, `makePurchase` from `@chipmobilesdk/rn-payment/testing` (test
|
|
108
|
+
doubles only).
|
|
109
|
+
|
|
110
|
+
Engine: `initialize`, `cleanup`, `getStatus`, `subscribeStatus`, `getEntitlement`,
|
|
111
|
+
`getAllEntitlements`, `subscribeEntitlements`, `getPendingPurchases`,
|
|
112
|
+
`subscribePendingPurchases`, `purchase`, `consume`, `getUnfinalized`,
|
|
113
|
+
`getProducts`, `refreshProducts`, `changePlan`, `openSubscriptionManagement`,
|
|
114
|
+
`confirmPriceChange`, `sync`, `restore`, `getLastSyncedAt`, `clearLocalData`,
|
|
115
|
+
`handleForeground`, `subscribeEvents`.
|
|
116
|
+
|
|
117
|
+
Types: `PaymentConfig`, `CatalogEntry`, `ProductType`, `StoreProductIds`,
|
|
118
|
+
`EntitlementKey`, `ProductKey`, `EntitlementState`, `EntitlementStatus`,
|
|
119
|
+
`EntitlementMap`, `SubscriptionDetail`, `RenewalState`, `PendingPurchase`,
|
|
120
|
+
`PendingPurchasesState`, `PendingResolution`, `PurchaseResult`,
|
|
121
|
+
`PurchaseOptions`, `TransactionRef`, `FinalizeResult`, `UnfinalizedSummary`,
|
|
122
|
+
`ProductInfo`, `Offer`, `ProductQueryResult`, `MissingProduct`, `ProrationMode`,
|
|
123
|
+
`PriceChangeResult`, `PaymentStatus`, `PaymentStatusKind`, `UnavailableReason`,
|
|
124
|
+
`Environment`, `LapsePolicy`, `ValidationHook`, `ValidationCandidate`,
|
|
125
|
+
`ValidationDecision`, `ValidationUnavailablePolicy`, `SyncOutcome`, `RestoreOutcome`,
|
|
126
|
+
`InitOutcome`, `PaymentEvent`, `PaymentErrorCode`, `LoggerAdapter`, `Unsubscribe`.
|
|
127
|
+
|
|
128
|
+
`cleanup()` is idempotent, and so is every `Unsubscribe`. After cleanup no event fires.
|
|
129
|
+
|
|
130
|
+
## Upgrading 0.1.0 → 0.2.0
|
|
131
|
+
|
|
132
|
+
One compile-time change: `PaymentEventType` gained `'purchase_pending_resolved'`, so an
|
|
133
|
+
exhaustive `switch` needs the case or a `default`. A hand-written `PaymentEngine` mock
|
|
134
|
+
needs the two pending methods. No storage migration — `0.1.0` already persisted pending
|
|
135
|
+
records. Replace any app-side "pending since" marker with `usePendingPurchases`.
|
|
136
|
+
|
|
137
|
+
**Upgrade before shipping on Android.** In `0.1.0` the adapter misread `react-native-iap`
|
|
138
|
+
16.x's string purchase state, so on a device a pending purchase was granted. `0.2.0`
|
|
139
|
+
reads it correctly, and an unrecognized (`unspecified`) state never grants or finalizes.
|
|
140
|
+
|
|
141
|
+
## Where the detail lives
|
|
142
|
+
|
|
143
|
+
Read one section of `node_modules/@chipmobilesdk/rn-payment/README.md`:
|
|
144
|
+
|
|
145
|
+
| Need | Section |
|
|
146
|
+
|---|---|
|
|
147
|
+
| First integration | `## Install`, `## Quick start` |
|
|
148
|
+
| Catalog and Play Console product setup | `## Catalog and entitlement mapping` |
|
|
149
|
+
| Entitlement states and staleness | `## The entitlement model` |
|
|
150
|
+
| Every purchase outcome and how to handle it | `## Purchase flow` |
|
|
151
|
+
| A payment pending across sessions, and how it ends | `### A payment that stays pending` |
|
|
152
|
+
| Why acknowledgement is not yours | `## Finalization: the invariant you cannot opt out of` |
|
|
153
|
+
| Consumables | `## Consumables` |
|
|
154
|
+
| Grace/hold, plan change, cancellation | `## Subscriptions` |
|
|
155
|
+
| Prices, offers, trial eligibility | `## Prices and offers` |
|
|
156
|
+
| Restore, offline, logout, account switch | `## Sync, restore, and offline` |
|
|
157
|
+
| `environment_blocked`, release config | `## Environments and the production guard` |
|
|
158
|
+
| Backend verification | `## Server-side validation` |
|
|
159
|
+
| Events, ordering, error codes | `## Events and errors` |
|
|
160
|
+
| Wiring an entitlement to ads | `## Wiring an entitlement to another system` |
|
|
161
|
+
| Tests and licence testers | `## Testing` |
|
|
162
|
+
| Data safety, permissions | `## What is stored on the device`, `## Play policy checklist` |
|
|
163
|
+
| Before release | `## Production release checklist` |
|
|
164
|
+
| What is deferred | `## Known limitations`, `## Adding iOS later` |
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform selection for the store adapter.
|
|
3
|
+
*
|
|
4
|
+
* This exists so the public surface never names a store. An app writes
|
|
5
|
+
* `createStoreProvider()`, not `createIapAdapter()` — the same call will pick the
|
|
6
|
+
* StoreKit adapter once iOS exists, and no consumer line changes (FR-002, FR-004,
|
|
7
|
+
* FR-007).
|
|
8
|
+
*
|
|
9
|
+
* On a platform with no adapter the result is a stub that reports
|
|
10
|
+
* `platform_unsupported` rather than throwing. That is what lets a host app start
|
|
11
|
+
* and run normally where selling is impossible, losing only the ability to sell
|
|
12
|
+
* (FR-021, FR-023).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Platform } from 'react-native';
|
|
16
|
+
import type { ProductType, Unsubscribe } from '../config/types';
|
|
17
|
+
import { createIapAdapter } from './iap-adapter';
|
|
18
|
+
import type {
|
|
19
|
+
ProviderCapabilities,
|
|
20
|
+
ProviderConnectOutcome,
|
|
21
|
+
ProviderPort,
|
|
22
|
+
ProviderPriceChangeOutcome,
|
|
23
|
+
ProviderProduct,
|
|
24
|
+
ProviderPurchase,
|
|
25
|
+
ProviderPurchaseOutcome,
|
|
26
|
+
} from './port';
|
|
27
|
+
|
|
28
|
+
const UNSUPPORTED_CAPABILITIES: ProviderCapabilities = {
|
|
29
|
+
supportsSubscriptions: false,
|
|
30
|
+
supportsPlanChange: false,
|
|
31
|
+
supportsPriceChangeConfirmation: false,
|
|
32
|
+
reportsGraceAndHoldDistinctly: false,
|
|
33
|
+
supportsExternalTransactions: false,
|
|
34
|
+
/** Nothing here can charge anyone, so the environment guard has nothing to block. */
|
|
35
|
+
createsRealTransactions: false,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The no-adapter stub.
|
|
40
|
+
*
|
|
41
|
+
* Every operation fails safely and predictably. Notably `queryOwned` returns an
|
|
42
|
+
* empty list rather than throwing: the engine then computes `not_entitled` after a
|
|
43
|
+
* successful-but-empty sync, which is honest — on a platform with no store, the
|
|
44
|
+
* user genuinely holds nothing this package can verify.
|
|
45
|
+
*/
|
|
46
|
+
function createUnsupportedProvider(): ProviderPort {
|
|
47
|
+
return {
|
|
48
|
+
async connect(): Promise<ProviderConnectOutcome> {
|
|
49
|
+
return { ok: false, reason: 'platform_unsupported', retryable: false };
|
|
50
|
+
},
|
|
51
|
+
async disconnect(): Promise<void> {
|
|
52
|
+
// Nothing was ever connected.
|
|
53
|
+
},
|
|
54
|
+
observeConnection(): Unsubscribe {
|
|
55
|
+
return () => undefined;
|
|
56
|
+
},
|
|
57
|
+
async queryProducts(): Promise<ProviderProduct[]> {
|
|
58
|
+
return [];
|
|
59
|
+
},
|
|
60
|
+
async queryOwned(): Promise<ProviderPurchase[]> {
|
|
61
|
+
return [];
|
|
62
|
+
},
|
|
63
|
+
async launchPurchase(): Promise<ProviderPurchaseOutcome> {
|
|
64
|
+
return { ok: false, reason: 'not_ready', detail: 'platform_unsupported' };
|
|
65
|
+
},
|
|
66
|
+
observePurchases(): Unsubscribe {
|
|
67
|
+
return () => undefined;
|
|
68
|
+
},
|
|
69
|
+
async acknowledge(): Promise<void> {
|
|
70
|
+
// No transaction can exist to acknowledge.
|
|
71
|
+
},
|
|
72
|
+
async consume(): Promise<void> {
|
|
73
|
+
// As above.
|
|
74
|
+
},
|
|
75
|
+
async openSubscriptionManagement(): Promise<void> {
|
|
76
|
+
// No store screen to open.
|
|
77
|
+
},
|
|
78
|
+
async confirmPriceChange(): Promise<ProviderPriceChangeOutcome> {
|
|
79
|
+
return { ok: false, reason: 'not_ready' };
|
|
80
|
+
},
|
|
81
|
+
capabilities: () => UNSUPPORTED_CAPABILITIES,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface StoreProviderOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Force the unsupported stub regardless of platform.
|
|
88
|
+
*
|
|
89
|
+
* Useful for an app that wants to ship one build with selling switched off at
|
|
90
|
+
* the provider level rather than through the `enabled` flag.
|
|
91
|
+
*/
|
|
92
|
+
forceUnsupported?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The platform's store adapter.
|
|
97
|
+
*
|
|
98
|
+
* iOS deliberately returns the stub today rather than throwing: the contract is
|
|
99
|
+
* prepared for StoreKit, the implementation is not, and an app running on iOS
|
|
100
|
+
* should degrade to "cannot sell here" instead of crashing.
|
|
101
|
+
*/
|
|
102
|
+
export function createStoreProvider(
|
|
103
|
+
options: StoreProviderOptions = {},
|
|
104
|
+
): ProviderPort {
|
|
105
|
+
if (options.forceUnsupported) {
|
|
106
|
+
return createUnsupportedProvider();
|
|
107
|
+
}
|
|
108
|
+
if (Platform.OS === 'android') {
|
|
109
|
+
return createIapAdapter();
|
|
110
|
+
}
|
|
111
|
+
return createUnsupportedProvider();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Exported for a test or a screen that needs the stub explicitly. */
|
|
115
|
+
export { createUnsupportedProvider };
|
|
116
|
+
|
|
117
|
+
export type { ProductType };
|