@baukit/purchases-revenuecat 0.0.0-stage → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +17 -2
- package/package.json +52 -4
- package/src/index.test.ts +243 -0
- package/src/index.ts +148 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Patrick Koss
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@baukit/purchases-revenuecat`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `react-native-purchases` implementation of the `@baukit/purchases-core` port. It imports only types from `react-native-purchases`. The app passes the module in.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import Purchases from 'react-native-purchases';
|
|
7
|
+
import { createRevenueCatPurchasesPort } from '@baukit/purchases-revenuecat';
|
|
8
|
+
|
|
9
|
+
const port = createRevenueCatPurchasesPort(Purchases, { apiKey: platformApiKey });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- The factory calls `Purchases.configure({ apiKey })` once and throws `unavailable` for an empty key. Use `createNoopPurchasesPort()` when the build has no key.
|
|
13
|
+
- `identify` calls `logIn` with the suite subject `<identity_domain>|<hub sub>`. The hub resolves RevenueCat webhooks against that string.
|
|
14
|
+
- `reset` calls `logOut` unless the user is already anonymous.
|
|
15
|
+
- `offerings` maps RevenueCat offerings. Offering and product IDs come from the RevenueCat dashboard and app configuration. `NON_SUBSCRIPTION` products become `one_time` packages for credit packs.
|
|
16
|
+
- RevenueCat error codes map to `cancelled`, `pending`, `network` or `store`.
|
|
17
|
+
|
|
18
|
+
`react-native-purchases` 10.14.0 or later is a peer dependency. It needs no Expo config plugin. Expo Go has no store access, so use a development build.
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/purchases-revenuecat",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.1",
|
|
4
|
+
"description": "Optional react-native-purchases adapter for @baukit/purchases-core that identifies RevenueCat customers by suite subject.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Patrick Koss",
|
|
7
|
+
"homepage": "https://github.com/PatrickKoss/baukit#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/PatrickKoss/baukit.git",
|
|
11
|
+
"directory": "typescript/packages/purchases-revenuecat"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/PatrickKoss/baukit/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=24.0.0"
|
|
22
|
+
},
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.json",
|
|
39
|
+
"prepare": "pnpm run build",
|
|
40
|
+
"test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts && node ../../scripts/test-packed-exports.mjs",
|
|
41
|
+
"lint": "eslint src --max-warnings=0",
|
|
42
|
+
"format:check": "prettier --check src package.json tsconfig.json tsconfig.test.json"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@baukit/purchases-core": "workspace:*",
|
|
46
|
+
"react-native-purchases": "10.14.0",
|
|
47
|
+
"typescript": "6.0.3",
|
|
48
|
+
"vitest": "5.0.3"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@baukit/purchases-core": "^0.12.1",
|
|
52
|
+
"react-native-purchases": "^10.14.0"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { PurchaseFlow, type PurchaseOffering, type PurchasePackage } from '@baukit/purchases-core';
|
|
2
|
+
import type {
|
|
3
|
+
PurchasesOfferings,
|
|
4
|
+
PurchasesPackage,
|
|
5
|
+
PURCHASES_ERROR_CODE as ErrorCodeEnum,
|
|
6
|
+
} from 'react-native-purchases';
|
|
7
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { createRevenueCatPurchasesPort, type RevenueCatModule } from './index.js';
|
|
10
|
+
|
|
11
|
+
const SUBJECT = 'suite|person-1';
|
|
12
|
+
const API_KEY = 'appl_public_key';
|
|
13
|
+
|
|
14
|
+
const CODES = {
|
|
15
|
+
PURCHASE_CANCELLED_ERROR: '1',
|
|
16
|
+
STORE_PROBLEM_ERROR: '2',
|
|
17
|
+
NETWORK_ERROR: '10',
|
|
18
|
+
PAYMENT_PENDING_ERROR: '20',
|
|
19
|
+
OFFLINE_CONNECTION_ERROR: '35',
|
|
20
|
+
} as unknown as typeof ErrorCodeEnum;
|
|
21
|
+
|
|
22
|
+
function nativePackage(
|
|
23
|
+
offering: string,
|
|
24
|
+
id: string,
|
|
25
|
+
product: string,
|
|
26
|
+
category: string | null,
|
|
27
|
+
): PurchasesPackage {
|
|
28
|
+
return {
|
|
29
|
+
identifier: id,
|
|
30
|
+
offeringIdentifier: offering,
|
|
31
|
+
presentedOfferingContext: { offeringIdentifier: offering },
|
|
32
|
+
product: {
|
|
33
|
+
identifier: product,
|
|
34
|
+
title: `${product} title`,
|
|
35
|
+
description: `${product} description`,
|
|
36
|
+
priceString: '€4.99',
|
|
37
|
+
productCategory: category,
|
|
38
|
+
},
|
|
39
|
+
} as unknown as PurchasesPackage;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const monthly = nativePackage('default', '$rc_monthly', 'app.premium.monthly', 'SUBSCRIPTION');
|
|
43
|
+
const small = nativePackage('packs', 'small', 'app.pack.small', 'NON_SUBSCRIPTION');
|
|
44
|
+
const OFFERINGS = {
|
|
45
|
+
current: { identifier: 'default', availablePackages: [monthly] },
|
|
46
|
+
all: {
|
|
47
|
+
default: { identifier: 'default', availablePackages: [monthly] },
|
|
48
|
+
packs: { identifier: 'packs', availablePackages: [small] },
|
|
49
|
+
},
|
|
50
|
+
} as unknown as PurchasesOfferings;
|
|
51
|
+
|
|
52
|
+
function firstPackage(offerings: { readonly current: PurchaseOffering | null }): PurchasePackage {
|
|
53
|
+
const item = offerings.current?.packages[0];
|
|
54
|
+
if (item === undefined) throw new Error('offering has no package');
|
|
55
|
+
return item;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function mockPurchases() {
|
|
59
|
+
const calls: string[] = [];
|
|
60
|
+
const listeners = new Set<() => void>();
|
|
61
|
+
let anonymous = true;
|
|
62
|
+
const record =
|
|
63
|
+
<T>(name: string, result: () => T) =>
|
|
64
|
+
(...args: unknown[]) => {
|
|
65
|
+
calls.push(args.length ? `${name}:${JSON.stringify(args[0])}` : name);
|
|
66
|
+
return result();
|
|
67
|
+
};
|
|
68
|
+
const module = {
|
|
69
|
+
PURCHASES_ERROR_CODE: CODES,
|
|
70
|
+
configure: vi.fn(record('configure', () => undefined)),
|
|
71
|
+
logIn: vi.fn(
|
|
72
|
+
record('logIn', () => {
|
|
73
|
+
anonymous = false;
|
|
74
|
+
return Promise.resolve({ customerInfo: {}, created: false });
|
|
75
|
+
}),
|
|
76
|
+
),
|
|
77
|
+
logOut: vi.fn(
|
|
78
|
+
record('logOut', () => {
|
|
79
|
+
anonymous = true;
|
|
80
|
+
return Promise.resolve({});
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
isAnonymous: vi.fn(() => Promise.resolve(anonymous)),
|
|
84
|
+
getOfferings: vi.fn(record('getOfferings', () => Promise.resolve(OFFERINGS))),
|
|
85
|
+
purchasePackage: vi.fn((item: PurchasesPackage) => {
|
|
86
|
+
calls.push(`purchasePackage:${item.identifier}`);
|
|
87
|
+
return Promise.resolve({});
|
|
88
|
+
}),
|
|
89
|
+
restorePurchases: vi.fn(record('restorePurchases', () => Promise.resolve({}))),
|
|
90
|
+
addCustomerInfoUpdateListener: vi.fn((listener: () => void) => {
|
|
91
|
+
listeners.add(listener);
|
|
92
|
+
}),
|
|
93
|
+
removeCustomerInfoUpdateListener: vi.fn((listener: () => void) => listeners.delete(listener)),
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
module: module as unknown as RevenueCatModule,
|
|
97
|
+
raw: module,
|
|
98
|
+
calls,
|
|
99
|
+
emit: () => {
|
|
100
|
+
for (const listener of listeners) listener();
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
describe('createRevenueCatPurchasesPort', () => {
|
|
106
|
+
it('configures once with the platform key and no app user ID', () => {
|
|
107
|
+
const { module, raw } = mockPurchases();
|
|
108
|
+
|
|
109
|
+
createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
110
|
+
|
|
111
|
+
expect(raw.configure).toHaveBeenCalledTimes(1);
|
|
112
|
+
expect(raw.configure).toHaveBeenCalledWith({ apiKey: API_KEY });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('refuses an empty key', () => {
|
|
116
|
+
const { module, raw } = mockPurchases();
|
|
117
|
+
|
|
118
|
+
expect(() => createRevenueCatPurchasesPort(module, { apiKey: ' ' })).toThrow('unavailable');
|
|
119
|
+
expect(raw.configure).not.toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('logs in with the suite subject and logs out only identified customers', async () => {
|
|
123
|
+
const { module, raw, calls } = mockPurchases();
|
|
124
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
125
|
+
|
|
126
|
+
await port.reset();
|
|
127
|
+
expect(raw.logOut).not.toHaveBeenCalled();
|
|
128
|
+
await port.identify(SUBJECT);
|
|
129
|
+
await port.reset();
|
|
130
|
+
|
|
131
|
+
expect(calls).toEqual([
|
|
132
|
+
`configure:${JSON.stringify({ apiKey: API_KEY })}`,
|
|
133
|
+
`logIn:${JSON.stringify(SUBJECT)}`,
|
|
134
|
+
'logOut',
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('maps offerings and purchases the matching native package', async () => {
|
|
139
|
+
const { module, raw } = mockPurchases();
|
|
140
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
141
|
+
|
|
142
|
+
const offerings = await port.offerings();
|
|
143
|
+
|
|
144
|
+
expect(offerings.current?.id).toBe('default');
|
|
145
|
+
expect(offerings.all['packs']?.packages).toEqual([
|
|
146
|
+
{
|
|
147
|
+
id: 'small',
|
|
148
|
+
offeringId: 'packs',
|
|
149
|
+
productId: 'app.pack.small',
|
|
150
|
+
kind: 'one_time',
|
|
151
|
+
title: 'app.pack.small title',
|
|
152
|
+
description: 'app.pack.small description',
|
|
153
|
+
priceLabel: '€4.99',
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
const subscription = firstPackage(offerings);
|
|
157
|
+
expect(subscription.kind).toBe('subscription');
|
|
158
|
+
await port.purchase(subscription);
|
|
159
|
+
expect(raw.purchasePackage).toHaveBeenCalledWith(monthly);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('treats an offering without a current entry as none', async () => {
|
|
163
|
+
const { module, raw } = mockPurchases();
|
|
164
|
+
raw.getOfferings.mockResolvedValueOnce({ ...OFFERINGS, current: null });
|
|
165
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
166
|
+
|
|
167
|
+
await expect(port.offerings()).resolves.toMatchObject({ current: null });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('rejects a package that the last offerings did not contain', async () => {
|
|
171
|
+
const { module, raw } = mockPurchases();
|
|
172
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
173
|
+
const offerings = await port.offerings();
|
|
174
|
+
const item = firstPackage(offerings);
|
|
175
|
+
await port.reset();
|
|
176
|
+
|
|
177
|
+
await expect(port.purchase(item)).rejects.toMatchObject({ code: 'store' });
|
|
178
|
+
await expect(port.purchase({ ...item, id: 'unknown' })).rejects.toMatchObject({
|
|
179
|
+
code: 'store',
|
|
180
|
+
});
|
|
181
|
+
expect(raw.purchasePackage).not.toHaveBeenCalled();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it.each([
|
|
185
|
+
[{ code: '1' }, 'cancelled'],
|
|
186
|
+
[{ code: '20' }, 'pending'],
|
|
187
|
+
[{ code: '10' }, 'network'],
|
|
188
|
+
[{ code: '35' }, 'network'],
|
|
189
|
+
[{ code: '2' }, 'store'],
|
|
190
|
+
['opaque', 'store'],
|
|
191
|
+
])('maps RevenueCat error %j to %s', async (error, code) => {
|
|
192
|
+
const { module, raw } = mockPurchases();
|
|
193
|
+
raw.restorePurchases.mockRejectedValueOnce(error);
|
|
194
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
195
|
+
|
|
196
|
+
await expect(port.restore()).rejects.toMatchObject({ code, cause: error });
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('forwards and removes customer info listeners', () => {
|
|
200
|
+
const { module, raw, emit } = mockPurchases();
|
|
201
|
+
const port = createRevenueCatPurchasesPort(module, { apiKey: API_KEY });
|
|
202
|
+
const listener = vi.fn();
|
|
203
|
+
|
|
204
|
+
const stop = port.onCustomerUpdate(listener);
|
|
205
|
+
emit();
|
|
206
|
+
stop();
|
|
207
|
+
emit();
|
|
208
|
+
|
|
209
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
210
|
+
expect(raw.removeCustomerInfoUpdateListener).toHaveBeenCalledTimes(1);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('logs in before the flow buys and logs out on sign-out', async () => {
|
|
214
|
+
const { module, calls } = mockPurchases();
|
|
215
|
+
let revision = 1;
|
|
216
|
+
const flow = new PurchaseFlow({
|
|
217
|
+
port: createRevenueCatPurchasesPort(module, { apiKey: API_KEY }),
|
|
218
|
+
backend: {
|
|
219
|
+
refreshEntitlement: () =>
|
|
220
|
+
Promise.resolve({ suiteSubject: SUBJECT, tier: 'premium', features: [], revision }),
|
|
221
|
+
},
|
|
222
|
+
sleep: () => {
|
|
223
|
+
revision = 2;
|
|
224
|
+
return Promise.resolve();
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
const offerings = await flow.offerings();
|
|
228
|
+
const item = firstPackage(offerings);
|
|
229
|
+
|
|
230
|
+
await expect(flow.purchase(item)).rejects.toMatchObject({ code: 'not_identified' });
|
|
231
|
+
await flow.identify(SUBJECT);
|
|
232
|
+
await expect(flow.purchase(item)).resolves.toMatchObject({ status: 'granted' });
|
|
233
|
+
await flow.signOut();
|
|
234
|
+
|
|
235
|
+
expect(calls).toEqual([
|
|
236
|
+
`configure:${JSON.stringify({ apiKey: API_KEY })}`,
|
|
237
|
+
'getOfferings',
|
|
238
|
+
`logIn:${JSON.stringify(SUBJECT)}`,
|
|
239
|
+
'purchasePackage:$rc_monthly',
|
|
240
|
+
'logOut',
|
|
241
|
+
]);
|
|
242
|
+
});
|
|
243
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isPurchaseError,
|
|
3
|
+
PurchaseError,
|
|
4
|
+
type PurchaseOffering,
|
|
5
|
+
type PurchaseOfferings,
|
|
6
|
+
type PurchasePackage,
|
|
7
|
+
type PurchasesPort,
|
|
8
|
+
} from '@baukit/purchases-core';
|
|
9
|
+
import type {
|
|
10
|
+
CustomerInfoUpdateListener,
|
|
11
|
+
PURCHASES_ERROR_CODE,
|
|
12
|
+
PurchasesConfiguration,
|
|
13
|
+
PurchasesError,
|
|
14
|
+
PurchasesOffering,
|
|
15
|
+
PurchasesOfferings,
|
|
16
|
+
PurchasesPackage,
|
|
17
|
+
} from 'react-native-purchases';
|
|
18
|
+
|
|
19
|
+
/** The calls this adapter makes on the `react-native-purchases` default export. */
|
|
20
|
+
export interface RevenueCatModule {
|
|
21
|
+
readonly PURCHASES_ERROR_CODE: typeof PURCHASES_ERROR_CODE;
|
|
22
|
+
configure(configuration: PurchasesConfiguration): void;
|
|
23
|
+
logIn(appUserID: string): Promise<unknown>;
|
|
24
|
+
logOut(): Promise<unknown>;
|
|
25
|
+
isAnonymous(): Promise<boolean>;
|
|
26
|
+
getOfferings(): Promise<PurchasesOfferings>;
|
|
27
|
+
purchasePackage(aPackage: PurchasesPackage): Promise<unknown>;
|
|
28
|
+
restorePurchases(): Promise<unknown>;
|
|
29
|
+
addCustomerInfoUpdateListener(listener: CustomerInfoUpdateListener): void;
|
|
30
|
+
removeCustomerInfoUpdateListener(listener: CustomerInfoUpdateListener): boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface RevenueCatPurchasesOptions {
|
|
34
|
+
/** Public RevenueCat SDK key for the running platform, from app configuration. */
|
|
35
|
+
readonly apiKey: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type ErrorCodes = RevenueCatModule['PURCHASES_ERROR_CODE'];
|
|
39
|
+
|
|
40
|
+
function packageKey(offeringId: string, packageId: string): string {
|
|
41
|
+
return `${offeringId}\u0000${packageId}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function translate(codes: ErrorCodes, error: unknown): PurchaseError {
|
|
45
|
+
if (isPurchaseError(error)) return error;
|
|
46
|
+
if (typeof error !== 'object' || error === null)
|
|
47
|
+
return new PurchaseError('store', { cause: error });
|
|
48
|
+
const { code } = error as Partial<PurchasesError>;
|
|
49
|
+
if (code === codes.PURCHASE_CANCELLED_ERROR)
|
|
50
|
+
return new PurchaseError('cancelled', { cause: error });
|
|
51
|
+
if (code === codes.PAYMENT_PENDING_ERROR) return new PurchaseError('pending', { cause: error });
|
|
52
|
+
if (code === codes.NETWORK_ERROR || code === codes.OFFLINE_CONNECTION_ERROR)
|
|
53
|
+
return new PurchaseError('network', { cause: error });
|
|
54
|
+
return new PurchaseError('store', { cause: error });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const NON_SUBSCRIPTION = 'NON_SUBSCRIPTION';
|
|
58
|
+
|
|
59
|
+
function mapPackage(offeringId: string, item: PurchasesPackage): PurchasePackage {
|
|
60
|
+
const category: string | null = item.product.productCategory;
|
|
61
|
+
return {
|
|
62
|
+
id: item.identifier,
|
|
63
|
+
offeringId,
|
|
64
|
+
productId: item.product.identifier,
|
|
65
|
+
kind: category === NON_SUBSCRIPTION ? 'one_time' : 'subscription',
|
|
66
|
+
title: item.product.title,
|
|
67
|
+
description: item.product.description,
|
|
68
|
+
priceLabel: item.product.priceString,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class PackageCatalog {
|
|
73
|
+
readonly #packages = new Map<string, PurchasesPackage>();
|
|
74
|
+
|
|
75
|
+
replace(offerings: PurchasesOfferings): PurchaseOfferings {
|
|
76
|
+
this.#packages.clear();
|
|
77
|
+
const all: Record<string, PurchaseOffering> = {};
|
|
78
|
+
for (const [id, offering] of Object.entries(offerings.all)) all[id] = this.#offering(offering);
|
|
79
|
+
const current = offerings.current ? (all[offerings.current.identifier] ?? null) : null;
|
|
80
|
+
return { current, all };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
find(item: PurchasePackage): PurchasesPackage {
|
|
84
|
+
const found = this.#packages.get(packageKey(item.offeringId, item.id));
|
|
85
|
+
if (found === undefined) throw new PurchaseError('store');
|
|
86
|
+
return found;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
clear(): void {
|
|
90
|
+
this.#packages.clear();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
#offering(offering: PurchasesOffering): PurchaseOffering {
|
|
94
|
+
const packages = offering.availablePackages.map((item) => {
|
|
95
|
+
this.#packages.set(packageKey(offering.identifier, item.identifier), item);
|
|
96
|
+
return mapPackage(offering.identifier, item);
|
|
97
|
+
});
|
|
98
|
+
return { id: offering.identifier, packages };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Configures RevenueCat once. Pass the `react-native-purchases` default export. */
|
|
103
|
+
export function createRevenueCatPurchasesPort(
|
|
104
|
+
purchases: RevenueCatModule,
|
|
105
|
+
options: RevenueCatPurchasesOptions,
|
|
106
|
+
): PurchasesPort {
|
|
107
|
+
if (options.apiKey.trim() === '') throw new PurchaseError('unavailable');
|
|
108
|
+
purchases.configure({ apiKey: options.apiKey });
|
|
109
|
+
const catalog = new PackageCatalog();
|
|
110
|
+
const call = async <T>(operation: () => Promise<T>): Promise<T> => {
|
|
111
|
+
try {
|
|
112
|
+
return await operation();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw translate(purchases.PURCHASES_ERROR_CODE, error);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
identify: (suiteSubject) =>
|
|
120
|
+
call(async () => {
|
|
121
|
+
await purchases.logIn(suiteSubject);
|
|
122
|
+
}),
|
|
123
|
+
reset: () =>
|
|
124
|
+
call(async () => {
|
|
125
|
+
catalog.clear();
|
|
126
|
+
if (await purchases.isAnonymous()) return;
|
|
127
|
+
await purchases.logOut();
|
|
128
|
+
}),
|
|
129
|
+
offerings: () => call(async () => catalog.replace(await purchases.getOfferings())),
|
|
130
|
+
purchase: (item) =>
|
|
131
|
+
call(async () => {
|
|
132
|
+
await purchases.purchasePackage(catalog.find(item));
|
|
133
|
+
}),
|
|
134
|
+
restore: () =>
|
|
135
|
+
call(async () => {
|
|
136
|
+
await purchases.restorePurchases();
|
|
137
|
+
}),
|
|
138
|
+
onCustomerUpdate: (listener) => {
|
|
139
|
+
const forward = () => {
|
|
140
|
+
listener();
|
|
141
|
+
};
|
|
142
|
+
purchases.addCustomerInfoUpdateListener(forward);
|
|
143
|
+
return () => {
|
|
144
|
+
purchases.removeCustomerInfoUpdateListener(forward);
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|