@meterbox/react 0.1.8 → 0.1.9
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 -20
- package/package.json +2 -2
- package/src/BillingDetailsButton.js +5 -3
- package/src/CheckoutButton.js +5 -3
- package/src/CreditBalance.js +5 -4
- package/src/CreditGrantsTable.js +5 -4
- package/src/CreditUtilization.js +5 -3
- package/src/InvoiceHistory.js +5 -4
- package/src/OutcomeHistory.js +5 -4
- package/src/Paywall.js +6 -4
- package/src/PlanChangeCard.js +5 -3
- package/src/PricingTable.js +6 -4
- package/src/SubscriptionPreview.js +5 -4
- package/src/TrialBanner.js +5 -3
- package/src/UsageProgress.js +5 -3
- package/src/config.js +26 -0
- package/src/index.js +7 -0
package/README.md
CHANGED
|
@@ -14,8 +14,8 @@ Each component supports two modes:
|
|
|
14
14
|
`<CreditBalance>`, `<CreditGrantsTable>`, and `<CreditUtilization>` can
|
|
15
15
|
fetch their own data from the `/v1/customer-portal/*` endpoints using a
|
|
16
16
|
short-lived customer-scoped JWT minted server-side
|
|
17
|
-
(`POST /v1/tokens/:id`). Pass `
|
|
18
|
-
|
|
17
|
+
(`POST /v1/tokens/:id`). Pass `customerId` + `customerToken` instead of
|
|
18
|
+
the data prop.
|
|
19
19
|
|
|
20
20
|
For a complete hosted-feel portal in one component, see
|
|
21
21
|
[`@meterbox/customer-portal`](https://www.npmjs.com/package/@meterbox/customer-portal).
|
|
@@ -45,9 +45,9 @@ import {
|
|
|
45
45
|
} from "@meterbox/react";
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? branding? />` or `<PricingTable
|
|
48
|
+
### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? branding? />` or `<PricingTable publishableKey … />`
|
|
49
49
|
|
|
50
|
-
On a public page with no backend of its own, pass
|
|
50
|
+
On a public page with no backend of its own, pass a
|
|
51
51
|
publishable key (`mb_pub_...`, from Settings > API keys) instead of `plans`.
|
|
52
52
|
The table then reads the public pricing snapshot,
|
|
53
53
|
`GET /v1/catalog/pricing-page`: published plans with display fields only. A
|
|
@@ -79,6 +79,13 @@ its `bullets`: the tenant's own pricing-page bullets, else its features' names.
|
|
|
79
79
|
plan with a public promotion shows its promotional price, the regular one struck through, and the
|
|
80
80
|
code to use ("Save 50% with code LAUNCH50 for 3 months").
|
|
81
81
|
|
|
82
|
+
### Base URL: `<MeterboxProvider baseUrl>`
|
|
83
|
+
|
|
84
|
+
The components call `https://meterbox.ai`, so there's nothing to set. To
|
|
85
|
+
point them elsewhere, wrap them once in `<MeterboxProvider baseUrl="…">`; a
|
|
86
|
+
component's own `baseUrl` prop wins over both. `useBaseUrl()` reads the
|
|
87
|
+
resolved URL for your own calls.
|
|
88
|
+
|
|
82
89
|
### Branding: `<BrandingProvider branding>`
|
|
83
90
|
|
|
84
91
|
Every component styles itself from `--mbx-*` CSS variables (`--mbx-accent`,
|
|
@@ -86,7 +93,7 @@ Every component styles itself from `--mbx-*` CSS variables (`--mbx-accent`,
|
|
|
86
93
|
variables), with Meterbox's defaults as fallbacks. Wrap them in
|
|
87
94
|
`<BrandingProvider branding={branding}>` to apply your branding from
|
|
88
95
|
Settings → Branding (`GET /v1/settings/branding`): logo, colors, font and
|
|
89
|
-
button style. `<PricingTable
|
|
96
|
+
button style. `<PricingTable publishableKey>` applies it from the
|
|
90
97
|
pricing snapshot by itself, and any component's surroundings can set the
|
|
91
98
|
variables in CSS. `useBranding()` reads the values (e.g. the logo), and
|
|
92
99
|
`theme` exports the variable references for styling your own elements to
|
|
@@ -105,7 +112,7 @@ keys and English text are in [`src/i18n.js`](./src/i18n.js). Plan names,
|
|
|
105
112
|
features and offers are shown as you entered them. `useI18n()` gives your own
|
|
106
113
|
elements the same `t(key, vars)`, `date(iso)` and `money(amount, currency)`.
|
|
107
114
|
|
|
108
|
-
### `<BillingDetailsButton
|
|
115
|
+
### `<BillingDetailsButton customerId customerToken returnUrl? label? onSession? onError? />`
|
|
109
116
|
|
|
110
117
|
Sends the customer to Stripe's own Billing Portal to update their payment
|
|
111
118
|
method and billing details (address, tax IDs, email, name, phone) and see
|
|
@@ -122,7 +129,7 @@ works once the customer has a Stripe account and the tenant has enabled
|
|
|
122
129
|
Side-by-side comparison of two plan records with added / removed feature
|
|
123
130
|
deltas. Pair with `<SubscriptionPreview>` for the prorated math.
|
|
124
131
|
|
|
125
|
-
### `<CheckoutButton planId successUrl cancelUrl
|
|
132
|
+
### `<CheckoutButton planId successUrl cancelUrl customerId customerToken billingInterval? label? onSession? onError? />`
|
|
126
133
|
|
|
127
134
|
One-tap Stripe Checkout for a plan change. POSTs to
|
|
128
135
|
`/v1/customer-portal/:id/checkout` (customer JWT, no API key in the
|
|
@@ -131,13 +138,13 @@ browser) and redirects to the returned Stripe URL. `billingInterval`
|
|
|
131
138
|
`annual_free_months` configured; requesting "annual" on a plan that
|
|
132
139
|
doesn't offer it surfaces a 400 via `onError` rather than redirecting.
|
|
133
140
|
|
|
134
|
-
### `<SubscriptionPreview preview? |
|
|
141
|
+
### `<SubscriptionPreview preview? | customerId customerToken planId planVersion? />`
|
|
135
142
|
|
|
136
143
|
Renders the response of `POST /v1/customers/:id/subscription/preview` (admin)
|
|
137
144
|
or `POST /v1/customer-portal/:id/subscription/preview` (customer JWT):
|
|
138
145
|
prorated charge, MRR delta, effective date.
|
|
139
146
|
|
|
140
|
-
### `<PlanChangeCard preview? scheduledChange? allowSchedule? |
|
|
147
|
+
### `<PlanChangeCard preview? scheduledChange? allowSchedule? | customerId customerToken planId planVersion? fetch? onConfirm? onCancelScheduled? onChanged? />`
|
|
141
148
|
|
|
142
149
|
Composes `<SubscriptionPreview>` with the actions it doesn't have: actually
|
|
143
150
|
applying or scheduling the change, and cancelling a pending scheduled one.
|
|
@@ -147,7 +154,7 @@ Two modes:
|
|
|
147
154
|
`onConfirm(effective)` / `onCancelScheduled()` callbacks that return
|
|
148
155
|
promises; you own how the request is actually made (e.g. proxied through
|
|
149
156
|
your own backend, session-authenticated, holding no customer JWT).
|
|
150
|
-
- **Self-fetching**: `
|
|
157
|
+
- **Self-fetching**: `customerId` + `customerToken` + `planId`,
|
|
151
158
|
no `onConfirm`/`onCancelScheduled` supplied: fetches its own preview AND
|
|
152
159
|
posts confirm/cancel directly to `POST /v1/customer-portal/:id/subscription/change`
|
|
153
160
|
and `DELETE /v1/customer-portal/:id/subscription/scheduled`.
|
|
@@ -163,23 +170,23 @@ mounted; keep whatever render condition shows the card independent of the
|
|
|
163
170
|
plan id it just changed to, or the card (and the confirmation message it's
|
|
164
171
|
showing) unmounts the instant your own state catches up.
|
|
165
172
|
|
|
166
|
-
### `<CreditBalance balance? |
|
|
173
|
+
### `<CreditBalance balance? | customerId customerToken; threshold? onTopUp? />`
|
|
167
174
|
|
|
168
175
|
Compact balance widget. If `balance_usd < threshold`, shows a top-up CTA
|
|
169
176
|
wired to `onTopUp`. Self-fetches `GET /v1/customer-portal/:id/credits` in
|
|
170
177
|
self-fetch mode.
|
|
171
178
|
|
|
172
|
-
### `<CreditGrantsTable grants? balance_usd? |
|
|
179
|
+
### `<CreditGrantsTable grants? balance_usd? | customerId customerToken; filter? />`
|
|
173
180
|
|
|
174
181
|
Table of active grants: source, amount, remaining, expiry, status. Pass
|
|
175
182
|
`filter={(g) => ...}` to slice the rows.
|
|
176
183
|
|
|
177
|
-
### `<CreditUtilization series? |
|
|
184
|
+
### `<CreditUtilization series? | customerId customerToken from? to? granularity?; height? />`
|
|
178
185
|
|
|
179
186
|
Inline SVG line + area chart of credit balance over time, fed by
|
|
180
187
|
`GET /v1/customer-portal/:id/credits/timeseries`.
|
|
181
188
|
|
|
182
|
-
### `<InvoiceHistory invoices? |
|
|
189
|
+
### `<InvoiceHistory invoices? | customerId customerToken />`
|
|
183
190
|
|
|
184
191
|
The invoices the customer received, newest first: date, number, status and
|
|
185
192
|
amount, with a link to download each Stripe invoice's PDF and to its hosted
|
|
@@ -200,10 +207,7 @@ from `GET /v1/ledger/:id?grain=day`. `valueKey` picks the dimension
|
|
|
200
207
|
// app/server.tsx (Next.js Server Component, or any backend handler)
|
|
201
208
|
import { getV1Plans, MeterboxClient } from "@meterbox/node";
|
|
202
209
|
|
|
203
|
-
new MeterboxClient({
|
|
204
|
-
baseUrl: process.env.METERBOX_BASE_URL, // e.g. https://meterbox.ai
|
|
205
|
-
apiKey: process.env.METERBOX_API_KEY,
|
|
206
|
-
});
|
|
210
|
+
new MeterboxClient({ apiKey: process.env.METERBOX_API_KEY });
|
|
207
211
|
|
|
208
212
|
export default async function PricingPage() {
|
|
209
213
|
const { data } = await getV1Plans();
|
|
@@ -237,13 +241,11 @@ export function PortalBits({ customerJwt }) {
|
|
|
237
241
|
return (
|
|
238
242
|
<>
|
|
239
243
|
<SubscriptionPreview
|
|
240
|
-
baseUrl="https://meterbox.ai"
|
|
241
244
|
customerId="cus_42"
|
|
242
245
|
customerToken={customerJwt}
|
|
243
246
|
planId="pro"
|
|
244
247
|
/>
|
|
245
248
|
<CreditBalance
|
|
246
|
-
baseUrl="https://meterbox.ai"
|
|
247
249
|
customerId="cus_42"
|
|
248
250
|
customerToken={customerJwt}
|
|
249
251
|
threshold={10}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterbox/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Embeddable React components for Meterbox: pricing tables, subscription preview, credit balance and usage charts. Presentational by default, with data fetched by your backend via @meterbox/node.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://meterbox.ai",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@meterbox/node": "0.1.
|
|
20
|
+
"@meterbox/node": "0.1.1"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": ">=17",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <BillingDetailsButton
|
|
2
|
+
* <BillingDetailsButton customerId customerToken returnUrl? label? />
|
|
3
3
|
*
|
|
4
4
|
* Sends the customer to Stripe's own Billing Portal to update their payment
|
|
5
5
|
* method and billing details (address, tax IDs, email, name, phone) and see
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
const React = require("react");
|
|
19
19
|
const theme = require("./theme");
|
|
20
|
+
const { useBaseUrl } = require("./config");
|
|
20
21
|
const { useI18n } = require("./i18n");
|
|
21
22
|
|
|
22
23
|
const styles = {
|
|
@@ -50,7 +51,7 @@ async function openBillingDetails({ baseUrl, customerId, customerToken, returnUr
|
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
function BillingDetailsButton({
|
|
53
|
-
baseUrl,
|
|
54
|
+
baseUrl: baseUrlProp,
|
|
54
55
|
customerId,
|
|
55
56
|
customerToken,
|
|
56
57
|
returnUrl,
|
|
@@ -59,9 +60,10 @@ function BillingDetailsButton({
|
|
|
59
60
|
onSession,
|
|
60
61
|
onError,
|
|
61
62
|
}) {
|
|
63
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
62
64
|
const [busy, setBusy] = React.useState(false);
|
|
63
65
|
const { t } = useI18n();
|
|
64
|
-
const ready =
|
|
66
|
+
const ready = customerId && customerToken;
|
|
65
67
|
|
|
66
68
|
async function onClick() {
|
|
67
69
|
setBusy(true);
|
package/src/CheckoutButton.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* <CheckoutButton planId="pro" successUrl="..." cancelUrl="..."
|
|
3
|
-
* customerId customerToken
|
|
3
|
+
* customerId customerToken billingInterval? />
|
|
4
4
|
*
|
|
5
5
|
* One-tap Stripe Checkout for a plan change. POSTs to
|
|
6
6
|
* `/v1/customer-portal/:id/checkout` and redirects the browser to the
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
const React = require("react");
|
|
22
22
|
const theme = require("./theme");
|
|
23
|
+
const { useBaseUrl } = require("./config");
|
|
23
24
|
|
|
24
25
|
const styles = {
|
|
25
26
|
btn: {
|
|
@@ -74,13 +75,14 @@ function CheckoutButton({
|
|
|
74
75
|
cancelUrl,
|
|
75
76
|
billingInterval,
|
|
76
77
|
label,
|
|
77
|
-
baseUrl,
|
|
78
|
+
baseUrl: baseUrlProp,
|
|
78
79
|
customerId,
|
|
79
80
|
customerToken,
|
|
80
81
|
fetch: fetchImpl,
|
|
81
82
|
onSession,
|
|
82
83
|
onError,
|
|
83
84
|
}) {
|
|
85
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
84
86
|
const [busy, setBusy] = React.useState(false);
|
|
85
87
|
|
|
86
88
|
const onClick = React.useCallback(async () => {
|
|
@@ -118,7 +120,7 @@ function CheckoutButton({
|
|
|
118
120
|
onError,
|
|
119
121
|
]);
|
|
120
122
|
|
|
121
|
-
const ready =
|
|
123
|
+
const ready = customerId && customerToken && planId && successUrl && cancelUrl;
|
|
122
124
|
return React.createElement(
|
|
123
125
|
"button",
|
|
124
126
|
{
|
package/src/CreditBalance.js
CHANGED
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
* — `{balance_usd, grants}`.
|
|
10
10
|
*
|
|
11
11
|
* 2. **Self-fetching** (browser-safe):
|
|
12
|
-
* <CreditBalance
|
|
13
|
-
* customerId="cus_42"
|
|
12
|
+
* <CreditBalance customerId="cus_42"
|
|
14
13
|
* customerToken={jwt}
|
|
15
14
|
* threshold={10} onTopUp={...} />
|
|
16
15
|
* Fetches `GET /v1/customer-portal/:id/credits` with the customer JWT.
|
|
@@ -21,6 +20,7 @@
|
|
|
21
20
|
|
|
22
21
|
const React = require("react");
|
|
23
22
|
const theme = require("./theme");
|
|
23
|
+
const { useBaseUrl } = require("./config");
|
|
24
24
|
const { useI18n } = require("./i18n");
|
|
25
25
|
|
|
26
26
|
async function fetchBalance({ baseUrl, customerId, customerToken, fetchImpl }) {
|
|
@@ -72,12 +72,13 @@ function CreditBalance({
|
|
|
72
72
|
balance,
|
|
73
73
|
threshold,
|
|
74
74
|
onTopUp,
|
|
75
|
-
baseUrl,
|
|
75
|
+
baseUrl: baseUrlProp,
|
|
76
76
|
customerId,
|
|
77
77
|
customerToken,
|
|
78
78
|
fetch: fetchImpl,
|
|
79
79
|
}) {
|
|
80
|
-
const
|
|
80
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
81
|
+
const selfFetch = !balance && customerId && customerToken;
|
|
81
82
|
const [fetched, setFetched] = React.useState(null);
|
|
82
83
|
const [error, setError] = React.useState(null);
|
|
83
84
|
const [loading, setLoading] = React.useState(false);
|
package/src/CreditGrantsTable.js
CHANGED
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
* in as props.
|
|
11
11
|
*
|
|
12
12
|
* 2. **Self-fetching** (browser-safe):
|
|
13
|
-
* <CreditGrantsTable
|
|
14
|
-
* customerId="cus_42"
|
|
13
|
+
* <CreditGrantsTable customerId="cus_42"
|
|
15
14
|
* customerToken={jwt} />
|
|
16
15
|
* Fetches `GET /v1/customer-portal/:id/credits` directly with the
|
|
17
16
|
* customer-scoped JWT. Re-fetches when `customerId` changes.
|
|
@@ -22,6 +21,7 @@
|
|
|
22
21
|
|
|
23
22
|
const React = require("react");
|
|
24
23
|
const theme = require("./theme");
|
|
24
|
+
const { useBaseUrl } = require("./config");
|
|
25
25
|
const { useI18n } = require("./i18n");
|
|
26
26
|
|
|
27
27
|
const styles = {
|
|
@@ -80,12 +80,13 @@ function CreditGrantsTable({
|
|
|
80
80
|
grants,
|
|
81
81
|
balance_usd,
|
|
82
82
|
filter,
|
|
83
|
-
baseUrl,
|
|
83
|
+
baseUrl: baseUrlProp,
|
|
84
84
|
customerId,
|
|
85
85
|
customerToken,
|
|
86
86
|
fetch: fetchImpl,
|
|
87
87
|
}) {
|
|
88
|
-
const
|
|
88
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
89
|
+
const selfFetch = !grants && customerId && customerToken;
|
|
89
90
|
const [fetched, setFetched] = React.useState(null);
|
|
90
91
|
const [error, setError] = React.useState(null);
|
|
91
92
|
const [loading, setLoading] = React.useState(false);
|
package/src/CreditUtilization.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* <CreditUtilization series={timeseries} />
|
|
12
12
|
*
|
|
13
13
|
* 2. **Self-fetching**:
|
|
14
|
-
* <CreditUtilization
|
|
14
|
+
* <CreditUtilization customerId="cus_42"
|
|
15
15
|
* customerToken={jwt}
|
|
16
16
|
* from="2026-05-01" to="2026-06-01"
|
|
17
17
|
* granularity="day" />
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
const React = require("react");
|
|
21
21
|
const theme = require("./theme");
|
|
22
|
+
const { useBaseUrl } = require("./config");
|
|
22
23
|
const { useI18n } = require("./i18n");
|
|
23
24
|
|
|
24
25
|
const styles = {
|
|
@@ -71,7 +72,7 @@ async function fetchTimeseries({
|
|
|
71
72
|
|
|
72
73
|
function CreditUtilization({
|
|
73
74
|
series,
|
|
74
|
-
baseUrl,
|
|
75
|
+
baseUrl: baseUrlProp,
|
|
75
76
|
customerId,
|
|
76
77
|
customerToken,
|
|
77
78
|
from,
|
|
@@ -80,7 +81,8 @@ function CreditUtilization({
|
|
|
80
81
|
height = 120,
|
|
81
82
|
fetch: fetchImpl,
|
|
82
83
|
}) {
|
|
83
|
-
const
|
|
84
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
85
|
+
const selfFetch = !series && customerId && customerToken;
|
|
84
86
|
const [fetched, setFetched] = React.useState(null);
|
|
85
87
|
const [error, setError] = React.useState(null);
|
|
86
88
|
const [loading, setLoading] = React.useState(false);
|
package/src/InvoiceHistory.js
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
* 1. **Presentational**: <InvoiceHistory invoices={...} />, with the
|
|
8
8
|
* `invoices` array from `GET /v1/customer-portal/:id/invoices`.
|
|
9
9
|
* 2. **Self-fetching** (browser-safe):
|
|
10
|
-
* <InvoiceHistory
|
|
11
|
-
* customerId="cus_42"
|
|
10
|
+
* <InvoiceHistory customerId="cus_42"
|
|
12
11
|
* customerToken={jwt} />
|
|
13
12
|
* Fetches that endpoint with the customer-scoped JWT.
|
|
14
13
|
*/
|
|
15
14
|
|
|
16
15
|
const React = require("react");
|
|
17
16
|
const theme = require("./theme");
|
|
17
|
+
const { useBaseUrl } = require("./config");
|
|
18
18
|
const { useI18n } = require("./i18n");
|
|
19
19
|
|
|
20
20
|
const styles = {
|
|
@@ -69,8 +69,9 @@ async function fetchInvoices({ baseUrl, customerId, customerToken, fetchImpl })
|
|
|
69
69
|
return json.invoices || [];
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
function InvoiceHistory({ invoices, baseUrl, customerId, customerToken, fetch: fetchImpl }) {
|
|
73
|
-
const
|
|
72
|
+
function InvoiceHistory({ invoices, baseUrl: baseUrlProp, customerId, customerToken, fetch: fetchImpl }) {
|
|
73
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
74
|
+
const selfFetch = !invoices && customerId && customerToken;
|
|
74
75
|
const [fetched, setFetched] = React.useState(null);
|
|
75
76
|
const [error, setError] = React.useState(null);
|
|
76
77
|
const { t, locale, date } = useI18n();
|
package/src/OutcomeHistory.js
CHANGED
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
* `@meterbox/node` and passes `outcomes` in as a prop.
|
|
10
10
|
*
|
|
11
11
|
* 2. **Self-fetching** (browser-safe):
|
|
12
|
-
* <OutcomeHistory
|
|
13
|
-
* customerId="cus_42"
|
|
12
|
+
* <OutcomeHistory customerId="cus_42"
|
|
14
13
|
* customerToken={jwt} />
|
|
15
14
|
* Fetches `GET /v1/customer-portal/:id/outcomes` directly with the
|
|
16
15
|
* customer-scoped JWT. Re-fetches when `customerId` changes.
|
|
@@ -26,6 +25,7 @@
|
|
|
26
25
|
|
|
27
26
|
const React = require("react");
|
|
28
27
|
const theme = require("./theme");
|
|
28
|
+
const { useBaseUrl } = require("./config");
|
|
29
29
|
|
|
30
30
|
const styles = {
|
|
31
31
|
card: {
|
|
@@ -96,12 +96,13 @@ async function fetchOutcomes({ baseUrl, customerId, customerToken, fetchImpl })
|
|
|
96
96
|
function OutcomeHistory({
|
|
97
97
|
outcomes,
|
|
98
98
|
filter,
|
|
99
|
-
baseUrl,
|
|
99
|
+
baseUrl: baseUrlProp,
|
|
100
100
|
customerId,
|
|
101
101
|
customerToken,
|
|
102
102
|
fetch: fetchImpl,
|
|
103
103
|
}) {
|
|
104
|
-
const
|
|
104
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
105
|
+
const selfFetch = !outcomes && customerId && customerToken;
|
|
105
106
|
const [fetched, setFetched] = React.useState(null);
|
|
106
107
|
const [error, setError] = React.useState(null);
|
|
107
108
|
const [loading, setLoading] = React.useState(false);
|
package/src/Paywall.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <Paywall feature="vision" customerId="..." customerToken="..."
|
|
2
|
+
* <Paywall feature="vision" customerId="..." customerToken="...">
|
|
3
3
|
* <YourFeature />
|
|
4
4
|
* </Paywall>
|
|
5
5
|
*
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* prompt). Self-fetches from `GET /v1/customer-portal/:id/entitlements`.
|
|
9
9
|
*
|
|
10
10
|
* Modes:
|
|
11
|
-
* • **Self-fetching** (default) — provide `
|
|
11
|
+
* • **Self-fetching** (default) — provide `customerId` and
|
|
12
12
|
* `customerToken`. Fetches the entitlements once on mount and again
|
|
13
13
|
* whenever the customer or token changes.
|
|
14
14
|
* • **Presentational** — pass `features` (array of feature ids the customer
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
const React = require("react");
|
|
25
25
|
const theme = require("./theme");
|
|
26
|
+
const { useBaseUrl } = require("./config");
|
|
26
27
|
|
|
27
28
|
const defaultStyles = {
|
|
28
29
|
fallback: {
|
|
@@ -67,13 +68,14 @@ function Paywall({
|
|
|
67
68
|
children,
|
|
68
69
|
fallback,
|
|
69
70
|
onUpgradeClick,
|
|
70
|
-
baseUrl,
|
|
71
|
+
baseUrl: baseUrlProp,
|
|
71
72
|
customerId,
|
|
72
73
|
customerToken,
|
|
73
74
|
fetch: fetchImpl,
|
|
74
75
|
failOpen = true,
|
|
75
76
|
}) {
|
|
76
|
-
const
|
|
77
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
78
|
+
const selfFetch = !features && customerId && customerToken;
|
|
77
79
|
const [fetched, setFetched] = React.useState(null);
|
|
78
80
|
const [error, setError] = React.useState(null);
|
|
79
81
|
|
package/src/PlanChangeCard.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* (e.g. Meterbox's own Settings page proxies through its control
|
|
18
18
|
* plane, session-authenticated, holding no customer JWT at all).
|
|
19
19
|
*
|
|
20
|
-
* 2. **Self-fetching** — `
|
|
20
|
+
* 2. **Self-fetching** — `customerId` + `customerToken` +
|
|
21
21
|
* `planId`, no `onConfirm`/`onCancelScheduled` supplied. The panel
|
|
22
22
|
* fetches its own preview AND posts the confirm/cancel actions
|
|
23
23
|
* directly to `/v1/customer-portal/:id/subscription/change` and
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
|
|
39
39
|
const React = require("react");
|
|
40
40
|
const theme = require("./theme");
|
|
41
|
+
const { useBaseUrl } = require("./config");
|
|
41
42
|
const SubscriptionPreview = require("./SubscriptionPreview");
|
|
42
43
|
const { useI18n } = require("./i18n");
|
|
43
44
|
|
|
@@ -125,7 +126,7 @@ function PlanChangeCard({
|
|
|
125
126
|
preview,
|
|
126
127
|
scheduledChange,
|
|
127
128
|
allowSchedule = true,
|
|
128
|
-
baseUrl,
|
|
129
|
+
baseUrl: baseUrlProp,
|
|
129
130
|
customerId,
|
|
130
131
|
customerToken,
|
|
131
132
|
planId,
|
|
@@ -135,8 +136,9 @@ function PlanChangeCard({
|
|
|
135
136
|
onCancelScheduled,
|
|
136
137
|
onChanged,
|
|
137
138
|
}) {
|
|
139
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
138
140
|
const fetcher = fetchImpl || globalThis.fetch;
|
|
139
|
-
const canSelfFetch = Boolean(
|
|
141
|
+
const canSelfFetch = Boolean(customerId && customerToken);
|
|
140
142
|
const [busy, setBusy] = React.useState(false);
|
|
141
143
|
const [err, setErr] = React.useState(null);
|
|
142
144
|
const [message, setMessage] = React.useState(null);
|
package/src/PricingTable.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* <PricingTable plans={...} currentPlanId? onSelect? />
|
|
3
|
-
* <PricingTable
|
|
3
|
+
* <PricingTable publishableKey />
|
|
4
4
|
*
|
|
5
5
|
* Renders the tenant's plan catalog as a comparison grid. `plans` is the
|
|
6
6
|
* array your backend fetched from `GET /v1/plans` (via
|
|
7
7
|
* `@meterbox/node`'s `getV1Plans`). Or, on a public page with no backend of
|
|
8
|
-
* its own, pass `
|
|
8
|
+
* its own, pass `publishableKey` (`mb_pub_...`, Settings > API
|
|
9
9
|
* keys) and it reads the public pricing snapshot instead
|
|
10
10
|
* (`GET /v1/catalog/pricing-page`: published plans, display fields only).
|
|
11
11
|
* Highlights `currentPlanId` if
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
|
|
49
49
|
const React = require("react");
|
|
50
50
|
const theme = require("./theme");
|
|
51
|
+
const { useBaseUrl } = require("./config");
|
|
51
52
|
const { brandingVars } = require("./branding");
|
|
52
53
|
const { useI18n } = require("./i18n");
|
|
53
54
|
|
|
@@ -293,11 +294,12 @@ function PricingTable({
|
|
|
293
294
|
onSelect,
|
|
294
295
|
customerEmail,
|
|
295
296
|
branding,
|
|
296
|
-
baseUrl,
|
|
297
|
+
baseUrl: baseUrlProp,
|
|
297
298
|
publishableKey,
|
|
298
299
|
fetch: fetchImpl,
|
|
299
300
|
}) {
|
|
300
|
-
const
|
|
301
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
302
|
+
const selfFetch = !Array.isArray(plans) && !!publishableKey;
|
|
301
303
|
const [fetched, setFetched] = React.useState(null);
|
|
302
304
|
const [billingInterval, setBillingInterval] = React.useState("monthly");
|
|
303
305
|
const i18n = useI18n();
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
* key) and passes the body in as `preview`.
|
|
11
11
|
*
|
|
12
12
|
* 2. **Self-fetching** (browser-safe):
|
|
13
|
-
* <SubscriptionPreview
|
|
14
|
-
* customerId="cus_42"
|
|
13
|
+
* <SubscriptionPreview customerId="cus_42"
|
|
15
14
|
* customerToken={jwt}
|
|
16
15
|
* planId="pro" />
|
|
17
16
|
* The component calls `POST /v1/customer-portal/:id/subscription/preview`
|
|
@@ -22,6 +21,7 @@
|
|
|
22
21
|
|
|
23
22
|
const React = require("react");
|
|
24
23
|
const theme = require("./theme");
|
|
24
|
+
const { useBaseUrl } = require("./config");
|
|
25
25
|
const { useI18n } = require("./i18n");
|
|
26
26
|
|
|
27
27
|
const styles = {
|
|
@@ -76,14 +76,15 @@ async function fetchPreview({ baseUrl, customerId, customerToken, planId, planVe
|
|
|
76
76
|
|
|
77
77
|
function SubscriptionPreview({
|
|
78
78
|
preview,
|
|
79
|
-
baseUrl,
|
|
79
|
+
baseUrl: baseUrlProp,
|
|
80
80
|
customerId,
|
|
81
81
|
customerToken,
|
|
82
82
|
planId,
|
|
83
83
|
planVersion,
|
|
84
84
|
fetch: fetchImpl,
|
|
85
85
|
}) {
|
|
86
|
-
const
|
|
86
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
87
|
+
const selfFetch = !preview && customerId && customerToken && planId;
|
|
87
88
|
const [fetched, setFetched] = React.useState(null);
|
|
88
89
|
const [error, setError] = React.useState(null);
|
|
89
90
|
const [loading, setLoading] = React.useState(false);
|
package/src/TrialBanner.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <TrialBanner customerId customerToken
|
|
2
|
+
* <TrialBanner customerId customerToken onConvert={(planId) => …} />
|
|
3
3
|
*
|
|
4
4
|
* Renders a thin banner showing time remaining on the customer's trial, with
|
|
5
5
|
* a "Convert now" CTA that the host can wire to its checkout flow. Hides
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
const React = require("react");
|
|
13
13
|
const theme = require("./theme");
|
|
14
|
+
const { useBaseUrl } = require("./config");
|
|
14
15
|
|
|
15
16
|
const styles = {
|
|
16
17
|
banner: {
|
|
@@ -55,13 +56,14 @@ function daysRemaining(ends) {
|
|
|
55
56
|
function TrialBanner({
|
|
56
57
|
trialEndsAt,
|
|
57
58
|
status,
|
|
58
|
-
baseUrl,
|
|
59
|
+
baseUrl: baseUrlProp,
|
|
59
60
|
customerId,
|
|
60
61
|
customerToken,
|
|
61
62
|
fetch: fetchImpl,
|
|
62
63
|
onConvert,
|
|
63
64
|
}) {
|
|
64
|
-
const
|
|
65
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
66
|
+
const selfFetch = (!trialEndsAt || !status) && customerId && customerToken;
|
|
65
67
|
const [me, setMe] = React.useState(null);
|
|
66
68
|
|
|
67
69
|
React.useEffect(() => {
|
package/src/UsageProgress.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <UsageProgress metric="input_tokens_per_month" customerId customerToken
|
|
2
|
+
* <UsageProgress metric="input_tokens_per_month" customerId customerToken />
|
|
3
3
|
*
|
|
4
4
|
* Real-time bar chart of consumption against the customer's current plan
|
|
5
5
|
* limit. Self-fetches from `GET /v1/customer-portal/:id/usage`, or accept
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
const React = require("react");
|
|
19
19
|
const theme = require("./theme");
|
|
20
|
+
const { useBaseUrl } = require("./config");
|
|
20
21
|
|
|
21
22
|
const styles = {
|
|
22
23
|
wrap: {
|
|
@@ -58,12 +59,13 @@ function UsageProgress({
|
|
|
58
59
|
limits,
|
|
59
60
|
warnAt = 0.8,
|
|
60
61
|
dangerAt = 1.0,
|
|
61
|
-
baseUrl,
|
|
62
|
+
baseUrl: baseUrlProp,
|
|
62
63
|
customerId,
|
|
63
64
|
customerToken,
|
|
64
65
|
fetch: fetchImpl,
|
|
65
66
|
}) {
|
|
66
|
-
const
|
|
67
|
+
const baseUrl = useBaseUrl(baseUrlProp);
|
|
68
|
+
const selfFetch = (!usage || !limits) && customerId && customerToken;
|
|
67
69
|
const [fetched, setFetched] = React.useState(null);
|
|
68
70
|
const [stale, setStale] = React.useState(false);
|
|
69
71
|
|
package/src/config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* config.js — where the components reach Meterbox.
|
|
3
|
+
*
|
|
4
|
+
* <MeterboxProvider baseUrl="https://billing.example.com">…</MeterboxProvider>
|
|
5
|
+
*
|
|
6
|
+
* sets the API's base URL once for every component inside it. Without one,
|
|
7
|
+
* they call https://meterbox.ai; a component's own `baseUrl` prop still wins.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const React = require("react");
|
|
11
|
+
|
|
12
|
+
const DEFAULT_BASE_URL = "https://meterbox.ai";
|
|
13
|
+
|
|
14
|
+
const BaseUrlContext = React.createContext(null);
|
|
15
|
+
|
|
16
|
+
function MeterboxProvider({ baseUrl, children }) {
|
|
17
|
+
return React.createElement(BaseUrlContext.Provider, { value: baseUrl || null }, children);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The base URL to call, without a trailing slash: `override`, else the provider's, else meterbox.ai. */
|
|
21
|
+
function useBaseUrl(override) {
|
|
22
|
+
const provided = React.useContext(BaseUrlContext);
|
|
23
|
+
return String(override || provided || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { MeterboxProvider, useBaseUrl, DEFAULT_BASE_URL };
|
package/src/index.js
CHANGED
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
* `/v1/customer-portal/*` endpoints using a short-lived customer-scoped
|
|
12
12
|
* JWT minted server-side (`POST /v1/tokens/:id`).
|
|
13
13
|
*
|
|
14
|
+
* Base URL: the components call https://meterbox.ai; `<MeterboxProvider
|
|
15
|
+
* baseUrl>` points every component inside it elsewhere, and a component's own
|
|
16
|
+
* `baseUrl` prop wins over both.
|
|
17
|
+
*
|
|
14
18
|
* Branding: `<BrandingProvider branding>` themes every component inside it
|
|
15
19
|
* with the tenant's branding (GET /v1/settings/branding); `theme` is the
|
|
16
20
|
* --mbx-* CSS variables they read, for matching your own elements.
|
|
@@ -43,6 +47,7 @@ const CheckoutButton = require("./CheckoutButton");
|
|
|
43
47
|
const BillingDetailsButton = require("./BillingDetailsButton");
|
|
44
48
|
const { BrandingProvider, useBranding, brandingVars, logoUrl } = require("./branding");
|
|
45
49
|
const { LocaleProvider, useI18n } = require("./i18n");
|
|
50
|
+
const { MeterboxProvider, useBaseUrl } = require("./config");
|
|
46
51
|
const theme = require("./theme");
|
|
47
52
|
|
|
48
53
|
module.exports = {
|
|
@@ -67,6 +72,8 @@ module.exports = {
|
|
|
67
72
|
logoUrl,
|
|
68
73
|
LocaleProvider,
|
|
69
74
|
useI18n,
|
|
75
|
+
MeterboxProvider,
|
|
76
|
+
useBaseUrl,
|
|
70
77
|
theme,
|
|
71
78
|
sdk,
|
|
72
79
|
MeterboxClient: sdk.MeterboxClient,
|