@meterbox/react 0.1.5 → 0.1.6

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
@@ -87,6 +87,18 @@ variables in CSS. `useBranding()` reads the values (e.g. the logo), and
87
87
  `theme` exports the variable references for styling your own elements to
88
88
  match.
89
89
 
90
+ ### `<BillingDetailsButton baseUrl customerId customerToken returnUrl? label? onSession? onError? />`
91
+
92
+ Sends the customer to Stripe's own Billing Portal to update their payment
93
+ method and billing details (address, tax IDs, email, name, phone) and see
94
+ their invoices, so payment details never pass through your app or Meterbox.
95
+ POSTs to `/v1/customer-portal/:id/billing-details` and redirects; Stripe
96
+ sends them back to `returnUrl` (default: the current page). Cancelling and
97
+ plan changes aren't offered there: they stay in Meterbox's own flows. It
98
+ works once the customer has a Stripe account and the tenant has enabled
99
+ `checkout_via_meterbox`, which `GET /v1/customer-portal/:id/me` reports as
100
+ `billing_details_available`.
101
+
90
102
  ### `<PlanComparison from to onSwitch? />`
91
103
 
92
104
  Side-by-side comparison of two plan records with added / removed feature
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterbox/react",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
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",
@@ -0,0 +1,96 @@
1
+ /**
2
+ * <BillingDetailsButton baseUrl customerId customerToken returnUrl? label? />
3
+ *
4
+ * Sends the customer to Stripe's own Billing Portal to update their payment
5
+ * method and billing details (address, tax IDs, email, name, phone) and see
6
+ * their invoices, so payment details never pass through your app or
7
+ * Meterbox. POSTs to `/v1/customer-portal/:id/billing-details` and redirects
8
+ * to the returned URL; Stripe sends them back to `returnUrl` (default: this
9
+ * page). Cancelling and plan changes aren't offered there: they stay in
10
+ * Meterbox's own flows.
11
+ *
12
+ * Needs the customer to have a Stripe account and the tenant to have enabled
13
+ * `checkout_via_meterbox`; `GET /v1/customer-portal/:id/me` says whether it
14
+ * will work (`billing_details_available`). Otherwise `onError` receives the
15
+ * 409. Pass `onSession({ url })` to intercept the redirect.
16
+ */
17
+
18
+ const React = require("react");
19
+ const theme = require("./theme");
20
+
21
+ const styles = {
22
+ btn: {
23
+ padding: "8px 14px",
24
+ ...theme.button,
25
+ borderRadius: theme.radius(8),
26
+ fontWeight: 600,
27
+ cursor: "pointer",
28
+ fontFamily: theme.font,
29
+ },
30
+ disabled: { opacity: 0.6, cursor: "default" },
31
+ };
32
+
33
+ async function openBillingDetails({ baseUrl, customerId, customerToken, returnUrl, fetchImpl }) {
34
+ const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
35
+ customerId,
36
+ )}/billing-details`;
37
+ const res = await fetchImpl(url, {
38
+ method: "POST",
39
+ headers: { Authorization: `Bearer ${customerToken}`, "Content-Type": "application/json" },
40
+ body: JSON.stringify({ return_url: returnUrl }),
41
+ });
42
+ const body = await res.json().catch(() => ({}));
43
+ if (!res.ok) {
44
+ const err = new Error(body.error || `billing-details: ${res.status}`);
45
+ err.status = res.status;
46
+ throw err;
47
+ }
48
+ return body;
49
+ }
50
+
51
+ function BillingDetailsButton({
52
+ baseUrl,
53
+ customerId,
54
+ customerToken,
55
+ returnUrl,
56
+ label = "Update payment method and billing details",
57
+ fetch: fetchImpl,
58
+ onSession,
59
+ onError,
60
+ }) {
61
+ const [busy, setBusy] = React.useState(false);
62
+ const ready = baseUrl && customerId && customerToken;
63
+
64
+ async function onClick() {
65
+ setBusy(true);
66
+ try {
67
+ const session = await openBillingDetails({
68
+ baseUrl,
69
+ customerId,
70
+ customerToken,
71
+ returnUrl: returnUrl || globalThis.window?.location?.href,
72
+ fetchImpl: fetchImpl || globalThis.fetch,
73
+ });
74
+ if (onSession) onSession(session);
75
+ else if (session.url && globalThis.window) globalThis.window.location.href = session.url;
76
+ } catch (err) {
77
+ if (onError) onError(err);
78
+ } finally {
79
+ setBusy(false);
80
+ }
81
+ }
82
+
83
+ return React.createElement(
84
+ "button",
85
+ {
86
+ type: "button",
87
+ className: "meterbox-billing-details-button",
88
+ style: !ready || busy ? { ...styles.btn, ...styles.disabled } : styles.btn,
89
+ disabled: !ready || busy,
90
+ onClick,
91
+ },
92
+ busy ? "Redirecting…" : label,
93
+ );
94
+ }
95
+
96
+ module.exports = BillingDetailsButton;
package/src/index.js CHANGED
@@ -36,6 +36,7 @@ const Paywall = require("./Paywall");
36
36
  const UsageProgress = require("./UsageProgress");
37
37
  const TrialBanner = require("./TrialBanner");
38
38
  const CheckoutButton = require("./CheckoutButton");
39
+ const BillingDetailsButton = require("./BillingDetailsButton");
39
40
  const { BrandingProvider, useBranding, brandingVars, logoUrl } = require("./branding");
40
41
  const theme = require("./theme");
41
42
 
@@ -54,6 +55,7 @@ module.exports = {
54
55
  UsageProgress,
55
56
  TrialBanner,
56
57
  CheckoutButton,
58
+ BillingDetailsButton,
57
59
  BrandingProvider,
58
60
  useBranding,
59
61
  brandingVars,