@revenuecat/purchases-js 0.0.18 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -160
- package/dist/Purchases.es.d.ts +93 -92
- package/dist/Purchases.es.js +2401 -1990
- package/dist/Purchases.umd.js +8 -15
- package/dist/style.css +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,178 +10,29 @@ Sign up to [get started for free](https://app.revenuecat.com/signup).
|
|
|
10
10
|
|
|
11
11
|
Login @ [app.revenuecat.com](https://app.revenuecat.com)
|
|
12
12
|
|
|
13
|
+
- Connect your Stripe account if you haven't already (More payment gateways are coming soon)
|
|
13
14
|
- Create a Project (if you haven't already)
|
|
14
|
-
|
|
15
|
-
### ======> Only while testing <======
|
|
16
|
-
|
|
17
|
-
- Add the private project id (a.k.a. app_id) to the following feature flags
|
|
18
|
-
- RCBILLING
|
|
19
|
-
- GENERATE_V2_SUBSCRIPTION_MODELS_FOR_RCBILLING
|
|
20
|
-
- GENERATE_V2_SUBSCRIPTION_MODELS
|
|
21
|
-
|
|
22
|
-
### ======> Only while testing <======
|
|
23
|
-
|
|
24
15
|
- Add a new RCBilling app
|
|
25
|
-
|
|
26
|
-
- Connect your Stripe account (More payment gateways are coming soon)
|
|
16
|
+
- Get the sandbox API key or production API key (depending on the environment)
|
|
27
17
|
- Create some products for the RCBilling App
|
|
28
18
|
- Create an offering and add packages with RCBilling products
|
|
29
19
|
- Create the entitlements you need in your app and link them to the RCBilling products
|
|
30
20
|
|
|
31
21
|
# Installation
|
|
32
22
|
|
|
33
|
-
### ======> Only during testing <======
|
|
34
|
-
|
|
35
|
-
- Get a token to download the sdk from our private npm registry
|
|
36
|
-
- Set the environment variable `NODE_AUTH_TOKEN`
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
export NODE_AUTH_TOKEN="the token you got from the npm registry"
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### ======> Only during testing <======
|
|
43
|
-
|
|
44
23
|
- Add the library to your project's dependencies
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
npm
|
|
48
|
-
```
|
|
24
|
+
- npm
|
|
25
|
+
```
|
|
26
|
+
npm install --save @revenuecat/purchases-js
|
|
27
|
+
```
|
|
28
|
+
- yarn
|
|
29
|
+
```
|
|
30
|
+
yarn add --save @revenuecat/purchases-js
|
|
31
|
+
```
|
|
49
32
|
|
|
50
33
|
# Usage
|
|
51
34
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
By downloading the current Offerings you can easily build a Paywall page using the embedded Packages and their
|
|
55
|
-
associated `rcBillingProduct` and price.
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
const purchases = Purchases.configure(
|
|
59
|
-
"your RC_PUBLISHABLE_API_KEY",
|
|
60
|
-
"the unique id of the user in your systems",
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
purchases.getOfferings().then((offerings) => {
|
|
64
|
-
// Get current offering
|
|
65
|
-
console.log(offerings.current);
|
|
66
|
-
// Or a dictionary of all offerings
|
|
67
|
-
console.log(offerings.all);
|
|
68
|
-
});
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
This should print the current offerings you have set up in your RC Account.
|
|
72
|
-
|
|
73
|
-
Please check out [this file](https://github.com/RevenueCat/purchases-js/blob/main/src/entities/offerings.ts) for the
|
|
74
|
-
Offering's data structure
|
|
75
|
-
|
|
76
|
-
## Check User Entitlements
|
|
77
|
-
|
|
78
|
-
You can check the entitlements granted to your users throughout all the platforms, now
|
|
79
|
-
also on your website!
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
const entitlementId = "the entitlementId you set up in RC";
|
|
83
|
-
|
|
84
|
-
const purchases = Purchases.configure(
|
|
85
|
-
"your RC_PUBLISHABLE_API_KEY",
|
|
86
|
-
"the unique id of the user in your systems",
|
|
87
|
-
);
|
|
88
|
-
const appUserID = purchases.getAppUserId();
|
|
89
|
-
|
|
90
|
-
purchases.isEntitledTo(entitlementId).then((isEntitled) => {
|
|
91
|
-
if (isEntitled == true) {
|
|
92
|
-
console.log(`User ${appUserID} is entitled to ${entitlementId}`);
|
|
93
|
-
} else {
|
|
94
|
-
console.log(`User ${appUserID} is not entitled to ${entitlementId}`);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
As example, you can build a cool React component with it:
|
|
100
|
-
|
|
101
|
-
```tsx
|
|
102
|
-
Purchases.configure(
|
|
103
|
-
"your RC_PUBLISHABLE_API_KEY",
|
|
104
|
-
"the unique id of the user in your systems",
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
const WithEntitlement = ({ entitlementId, children }) => {
|
|
108
|
-
const [isEntitled, setIsEntitled] = useState<boolean | null>(null);
|
|
109
|
-
|
|
110
|
-
useEffect(() => {
|
|
111
|
-
Purchases.getSharedInstance()
|
|
112
|
-
.isEntitledTo(entitlementId)
|
|
113
|
-
.then((isEntitled) => {
|
|
114
|
-
setIsEntitled(isEntitled);
|
|
115
|
-
});
|
|
116
|
-
}, [entitlementId]);
|
|
117
|
-
|
|
118
|
-
if (isEntitled === null) {
|
|
119
|
-
return <>"loading..."</>;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (isEntitled === true) {
|
|
123
|
-
return <>{children}</>;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return <>You are not entitled!</>;
|
|
127
|
-
};
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
And then use it in your app:
|
|
131
|
-
|
|
132
|
-
```tsx
|
|
133
|
-
const App = () => (
|
|
134
|
-
<>
|
|
135
|
-
<WithEntitlement entitlementId={"functionality5"}>
|
|
136
|
-
<Functionality5 />
|
|
137
|
-
</WithEntitlement>
|
|
138
|
-
</>
|
|
139
|
-
);
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
If you need further information about the user's entitlements, you can use the `getCustomerInfo` method:
|
|
143
|
-
|
|
144
|
-
```ts
|
|
145
|
-
const customerInfo = await purchases.getCustomerInfo();
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Important note
|
|
149
|
-
|
|
150
|
-
Please be aware that the information about the entitlements can be manipulated by malicious actors, so make sure
|
|
151
|
-
you protect your apps against attacks that modify the entitlements by validating access through your servers.
|
|
152
|
-
|
|
153
|
-
## Subscribe a User to an entitlement and allow payment with Stripe
|
|
154
|
-
|
|
155
|
-
RCBilling allows you to use your payment gateway for payments.
|
|
156
|
-
In this example we will show Stripe, more will be supported soon!
|
|
157
|
-
|
|
158
|
-
### Context
|
|
159
|
-
|
|
160
|
-
You built your paywall, and your user just clicked on the offer they want to subscribe to.
|
|
161
|
-
|
|
162
|
-
```tsx
|
|
163
|
-
const purchases = Purchases.configure(
|
|
164
|
-
"your RC_PUBLISHABLE_API_KEY",
|
|
165
|
-
"the unique id of the user in your systems",
|
|
166
|
-
);
|
|
167
|
-
// You can retrieve the package from the offerings through `getOfferings`:
|
|
168
|
-
const rcBillingPackage = offerings.current.availablePackages[0];
|
|
169
|
-
const appUserId = purchases.getAppUserId();
|
|
170
|
-
const entitlementIdToCheck =
|
|
171
|
-
"the entitlementId you set up in RC for your product"; // TODO: remove once this is not needed
|
|
172
|
-
|
|
173
|
-
purchase.purchasePackage(rcBillingPackage).then((response) => {
|
|
174
|
-
const isEntitled =
|
|
175
|
-
entitlementIdToCheck in response.customerInfo.entitlements.active;
|
|
176
|
-
if (isEntitled == true) {
|
|
177
|
-
console.log(`User ${appUserID} is entitled to ${entitlementId}`);
|
|
178
|
-
} else {
|
|
179
|
-
console.log(
|
|
180
|
-
`User ${appUserID} is not entitled to ${entitlementId}, even after ${numberOfAttempts} attempts`,
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
```
|
|
35
|
+
See the [RevenueCat docs](https://www.revenuecat.com/docs/web/revenuecat-billing) and the [SDK Reference](https://revenuecat.github.io/purchases-js-docs).
|
|
185
36
|
|
|
186
37
|
# Development
|
|
187
38
|
|
|
@@ -218,6 +69,17 @@ npm run prettier
|
|
|
218
69
|
npm run lint
|
|
219
70
|
```
|
|
220
71
|
|
|
72
|
+
## Running E2E tests
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
npm run build
|
|
76
|
+
cd examples/rcbilling-demo
|
|
77
|
+
npm run build
|
|
78
|
+
# In a different terminal or background the process
|
|
79
|
+
npm run dev
|
|
80
|
+
npm run test
|
|
81
|
+
```
|
|
82
|
+
|
|
221
83
|
## Update API specs
|
|
222
84
|
|
|
223
85
|
```bash
|
package/dist/Purchases.es.d.ts
CHANGED
|
@@ -421,108 +421,109 @@ export declare class Purchases {
|
|
|
421
421
|
* keep the returned instance around for use throughout your application.
|
|
422
422
|
* @param apiKey - RevenueCat API Key. Can be obtained from the RevenueCat dashboard.
|
|
423
423
|
* @param appUserId - Your unique id for identifying the user.
|
|
424
|
-
|
|
425
|
-
static configure(apiKey: string, appUserId: string): Purchases;
|
|
426
|
-
/**
|
|
427
|
-
* Fetch the configured offerings for this user. You can configure these
|
|
428
|
-
* in the RevenueCat dashboard.
|
|
429
|
-
*/
|
|
430
|
-
getOfferings(): Promise<Offerings>;
|
|
431
|
-
/**
|
|
432
|
-
* Convenience method to check whether a user is entitled to a specific
|
|
433
|
-
* entitlement. This will use {@link Purchases.getCustomerInfo} under the hood.
|
|
434
|
-
* @param entitlementIdentifier - The entitlement identifier you want to check.
|
|
435
|
-
* @returns Whether the user is entitled to the specified entitlement
|
|
436
|
-
* @throws {@link PurchasesError} if there is an error while fetching the customer info.
|
|
437
|
-
* @see {@link Purchases.getCustomerInfo}
|
|
424
|
+
* @throws {@link PurchasesError} if the API key or user id are invalid.
|
|
438
425
|
*/
|
|
439
|
-
|
|
426
|
+
static configure(apiKey: string, appUserId: string): Purchases;
|
|
427
|
+
/**
|
|
428
|
+
* Fetch the configured offerings for this user. You can configure these
|
|
429
|
+
* in the RevenueCat dashboard.
|
|
430
|
+
*/
|
|
431
|
+
getOfferings(): Promise<Offerings>;
|
|
440
432
|
/**
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
* @
|
|
446
|
-
|
|
447
|
-
* @param htmlTarget - The HTML element where the billing view should be added. If null, a new div will be created at the root of the page and appended to the body.
|
|
448
|
-
* @returns The customer info after the purchase is completed successfuly.
|
|
449
|
-
* @throws {@link PurchasesError} if there is an error while performing the purchase. If the {@link PurchasesError.errorCode} is {@link ErrorCode.UserCancelledError}, the user cancelled the purchase.
|
|
433
|
+
* Convenience method to check whether a user is entitled to a specific
|
|
434
|
+
* entitlement. This will use {@link Purchases.getCustomerInfo} under the hood.
|
|
435
|
+
* @param entitlementIdentifier - The entitlement identifier you want to check.
|
|
436
|
+
* @returns Whether the user is entitled to the specified entitlement
|
|
437
|
+
* @throws {@link PurchasesError} if there is an error while fetching the customer info.
|
|
438
|
+
* @see {@link Purchases.getCustomerInfo}
|
|
450
439
|
*/
|
|
451
|
-
|
|
452
|
-
customerInfo: CustomerInfo;
|
|
453
|
-
}>;
|
|
440
|
+
isEntitledTo(entitlementIdentifier: string): Promise<boolean>;
|
|
454
441
|
/**
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
442
|
+
* Method to perform a purchase for a given package. You can obtain the
|
|
443
|
+
* package from {@link Purchases.getOfferings}. This method will present the purchase
|
|
444
|
+
* form on your site, using the given HTML element as the mount point, if
|
|
445
|
+
* provided, or as a modal if not.
|
|
446
|
+
* @param rcPackage - The package you want to purchase. Obtained from {@link Purchases.getOfferings}.
|
|
447
|
+
* @param customerEmail - The email of the user. If null, RevenueCat will ask the customer for their email.
|
|
448
|
+
* @param htmlTarget - The HTML element where the billing view should be added. If null, a new div will be created at the root of the page and appended to the body.
|
|
449
|
+
* @returns The customer info after the purchase is completed successfuly.
|
|
450
|
+
* @throws {@link PurchasesError} if there is an error while performing the purchase. If the {@link PurchasesError.errorCode} is {@link ErrorCode.UserCancelledError}, the user cancelled the purchase.
|
|
458
451
|
*/
|
|
459
|
-
|
|
452
|
+
purchasePackage(rcPackage: Package, customerEmail?: string, htmlTarget?: HTMLElement): Promise<{
|
|
453
|
+
customerInfo: CustomerInfo;
|
|
454
|
+
}>;
|
|
460
455
|
/**
|
|
461
|
-
* Gets
|
|
462
|
-
|
|
463
|
-
|
|
456
|
+
* Gets latest available {@link CustomerInfo}.
|
|
457
|
+
* @returns The latest {@link CustomerInfo}.
|
|
458
|
+
* @throws {@link PurchasesError} if there is an error while fetching the customer info.
|
|
459
|
+
*/
|
|
460
|
+
getCustomerInfo(): Promise<CustomerInfo>;
|
|
461
|
+
/**
|
|
462
|
+
* Gets the current app user id.
|
|
463
|
+
*/
|
|
464
|
+
getAppUserId(): string;
|
|
465
|
+
/**
|
|
466
|
+
* Change the current app user id. Returns the customer info for the new
|
|
467
|
+
* user id.
|
|
468
|
+
* @param newAppUserId - The user id to change to.
|
|
469
|
+
*/
|
|
470
|
+
changeUser(newAppUserId: string): Promise<CustomerInfo>;
|
|
471
|
+
/**
|
|
472
|
+
* @returns Whether the SDK is using a sandbox API Key.
|
|
473
|
+
*/
|
|
474
|
+
isSandbox(): boolean;
|
|
475
|
+
/**
|
|
476
|
+
* Closes the Purchases instance. You should never have to do this normally.
|
|
477
|
+
*/
|
|
478
|
+
close(): void;
|
|
479
|
+
}
|
|
480
|
+
|
|
464
481
|
/**
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
* @
|
|
482
|
+
* Error class for Purchases SDK. You should handle these errors and react
|
|
483
|
+
* accordingly in your app.
|
|
484
|
+
* @public
|
|
468
485
|
*/
|
|
469
|
-
|
|
486
|
+
export declare class PurchasesError extends Error {
|
|
487
|
+
/**
|
|
488
|
+
* Error code for the error. This is useful to appropriately react to
|
|
489
|
+
* different error situations.
|
|
490
|
+
*/
|
|
491
|
+
readonly errorCode: ErrorCode;
|
|
492
|
+
/**
|
|
493
|
+
* Underlying error message. This provides more details on the error and
|
|
494
|
+
* can be useful for debugging and logging.
|
|
495
|
+
*/
|
|
496
|
+
readonly underlyingErrorMessage?: string | null | undefined;
|
|
497
|
+
constructor(
|
|
498
|
+
/**
|
|
499
|
+
* Error code for the error. This is useful to appropriately react to
|
|
500
|
+
* different error situations.
|
|
501
|
+
*/
|
|
502
|
+
errorCode: ErrorCode,
|
|
503
|
+
/**
|
|
504
|
+
* Message for the error. This is useful for debugging and logging.
|
|
505
|
+
*/
|
|
506
|
+
message?: string,
|
|
507
|
+
/**
|
|
508
|
+
* Underlying error message. This provides more details on the error and
|
|
509
|
+
* can be useful for debugging and logging.
|
|
510
|
+
*/
|
|
511
|
+
underlyingErrorMessage?: string | null | undefined);
|
|
512
|
+
toString: () => string;
|
|
513
|
+
}
|
|
514
|
+
|
|
470
515
|
/**
|
|
471
|
-
*
|
|
516
|
+
* The store where the user originally subscribed.
|
|
517
|
+
* @public
|
|
472
518
|
*/
|
|
473
|
-
|
|
519
|
+
export declare type Store = "app_store" | "mac_app_store" | "play_store" | "amazon" | "stripe" | "rc_billing" | "promotional" | "unknown";
|
|
520
|
+
|
|
474
521
|
/**
|
|
475
|
-
*
|
|
522
|
+
* Error indicating that the SDK was accessed before it was initialized.
|
|
523
|
+
* @public
|
|
476
524
|
*/
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Error class for Purchases SDK. You should handle these errors and react
|
|
482
|
-
* accordingly in your app.
|
|
483
|
-
* @public
|
|
484
|
-
*/
|
|
485
|
-
export declare class PurchasesError extends Error {
|
|
486
|
-
/**
|
|
487
|
-
* Error code for the error. This is useful to appropriately react to
|
|
488
|
-
* different error situations.
|
|
489
|
-
*/
|
|
490
|
-
readonly errorCode: ErrorCode;
|
|
491
|
-
/**
|
|
492
|
-
* Underlying error message. This provides more details on the error and
|
|
493
|
-
* can be useful for debugging and logging.
|
|
494
|
-
*/
|
|
495
|
-
readonly underlyingErrorMessage?: string | null | undefined;
|
|
496
|
-
constructor(
|
|
497
|
-
/**
|
|
498
|
-
* Error code for the error. This is useful to appropriately react to
|
|
499
|
-
* different error situations.
|
|
500
|
-
*/
|
|
501
|
-
errorCode: ErrorCode,
|
|
502
|
-
/**
|
|
503
|
-
* Message for the error. This is useful for debugging and logging.
|
|
504
|
-
*/
|
|
505
|
-
message?: string,
|
|
506
|
-
/**
|
|
507
|
-
* Underlying error message. This provides more details on the error and
|
|
508
|
-
* can be useful for debugging and logging.
|
|
509
|
-
*/
|
|
510
|
-
underlyingErrorMessage?: string | null | undefined);
|
|
511
|
-
toString: () => string;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* The store where the user originally subscribed.
|
|
516
|
-
* @public
|
|
517
|
-
*/
|
|
518
|
-
export declare type Store = "app_store" | "mac_app_store" | "play_store" | "amazon" | "stripe" | "rc_billing" | "promotional" | "unknown";
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
* Error indicating that the SDK was accessed before it was initialized.
|
|
522
|
-
* @public
|
|
523
|
-
*/
|
|
524
|
-
export declare class UninitializedPurchasesError extends Error {
|
|
525
|
-
constructor();
|
|
526
|
-
}
|
|
525
|
+
export declare class UninitializedPurchasesError extends Error {
|
|
526
|
+
constructor();
|
|
527
|
+
}
|
|
527
528
|
|
|
528
|
-
|
|
529
|
+
export { }
|