@growth-operations/ui-shared 1.5.3 → 1.5.5
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/package.json +1 -1
- package/src/billing/PlanGrid.jsx +36 -67
package/package.json
CHANGED
package/src/billing/PlanGrid.jsx
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import React, { useState } from "react";
|
|
2
2
|
import {
|
|
3
|
-
hubspot,
|
|
4
3
|
Flex,
|
|
5
4
|
AutoGrid,
|
|
6
5
|
Tile,
|
|
7
6
|
Text,
|
|
8
7
|
Heading,
|
|
9
8
|
StatusTag,
|
|
10
|
-
LoadingButton,
|
|
11
9
|
Button,
|
|
12
|
-
Link,
|
|
13
|
-
Alert,
|
|
14
10
|
ToggleGroup,
|
|
15
11
|
} from "@hubspot/ui-extensions";
|
|
16
12
|
import { fmtMoney } from "../lib/format";
|
|
@@ -41,9 +37,10 @@ import { fmtMoney } from "../lib/format";
|
|
|
41
37
|
function PlanCard({
|
|
42
38
|
plan,
|
|
43
39
|
interval,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
billingBaseUrl,
|
|
41
|
+
appKey,
|
|
42
|
+
portalId,
|
|
43
|
+
returnUrl,
|
|
47
44
|
supportUrl,
|
|
48
45
|
maxFeatures = 0,
|
|
49
46
|
}) {
|
|
@@ -56,6 +53,18 @@ function PlanCard({
|
|
|
56
53
|
? `${fmtMoney(leg.unit_amount, leg.currency)}${periodLabel}`
|
|
57
54
|
: "Free";
|
|
58
55
|
|
|
56
|
+
// Direct one-click checkout link to the billing service's redirect endpoint.
|
|
57
|
+
// Null until we have everything (billing host, appKey, portal, this interval's
|
|
58
|
+
// price) — the button is disabled until then.
|
|
59
|
+
const startUrl =
|
|
60
|
+
billingBaseUrl && appKey && portalId && leg?.price_id
|
|
61
|
+
? `${billingBaseUrl}/v1/billing/checkout/start` +
|
|
62
|
+
`?app_key=${encodeURIComponent(appKey)}` +
|
|
63
|
+
`&portal_id=${encodeURIComponent(portalId)}` +
|
|
64
|
+
`&price_id=${encodeURIComponent(leg.price_id)}` +
|
|
65
|
+
`&return_url=${encodeURIComponent(returnUrl ?? "")}`
|
|
66
|
+
: null;
|
|
67
|
+
|
|
59
68
|
const features = plan.features ?? [];
|
|
60
69
|
// Align the CTA buttons across cards. HubSpot's column Flex does NOT stretch
|
|
61
70
|
// to the Tile height (AutoGrid equalizes the card height but flex={1}/justify
|
|
@@ -107,18 +116,19 @@ function PlanCard({
|
|
|
107
116
|
<Text format={{ fontStyle: "italic" }}>
|
|
108
117
|
Not available {interval === "annual" ? "annually" : "monthly"}
|
|
109
118
|
</Text>
|
|
110
|
-
) : checkoutUrl ? (
|
|
111
|
-
<Button href={{ url: checkoutUrl, external: true }} variant="primary">
|
|
112
|
-
Continue to checkout
|
|
113
|
-
</Button>
|
|
114
119
|
) : (
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
120
|
+
// One-click: the button is a DIRECT external link to the billing
|
|
121
|
+
// service's GET /checkout/start, which creates the Stripe session
|
|
122
|
+
// server-side (in the opened tab) and 302s to Stripe. No in-iframe
|
|
123
|
+
// pre-create fetch — avoids the old two-click flow + the 15s iframe
|
|
124
|
+
// timeout. startUrl is null until billing_base_url is known.
|
|
125
|
+
<Button
|
|
126
|
+
href={startUrl ? { url: startUrl, external: true } : undefined}
|
|
127
|
+
disabled={!startUrl}
|
|
118
128
|
variant="primary"
|
|
119
129
|
>
|
|
120
130
|
Choose {plan.name ?? plan.tier}
|
|
121
|
-
</
|
|
131
|
+
</Button>
|
|
122
132
|
)}
|
|
123
133
|
</Flex>
|
|
124
134
|
</Tile>
|
|
@@ -132,15 +142,17 @@ export function PlanGrid({ context, state, appKey }) {
|
|
|
132
142
|
const annualAvailable =
|
|
133
143
|
paid.length > 0 && paid.every((p) => p.annual);
|
|
134
144
|
const [interval, setInterval] = useState(annualAvailable ? "annual" : "monthly");
|
|
135
|
-
const [choosingTier, setChoosingTier] = useState(null);
|
|
136
|
-
// Pre-created checkout URL keyed by `${tier}:${interval}` so a card only shows
|
|
137
|
-
// "Continue to checkout" for the exact price the customer picked.
|
|
138
|
-
const [checkoutUrls, setCheckoutUrls] = useState({});
|
|
139
|
-
const [error, setError] = useState(null);
|
|
140
145
|
|
|
141
146
|
if (plans.length === 0) return null;
|
|
142
147
|
|
|
143
148
|
const supportUrl = state?.helpful_links?.supportUrl ?? null;
|
|
149
|
+
const billingBaseUrl = state?.billing_base_url ?? null;
|
|
150
|
+
const portalId = context?.portal?.id;
|
|
151
|
+
// Where Stripe sends the customer back after pay/cancel — the app's Billing
|
|
152
|
+
// tab. The card builds the one-click /checkout/start link from these.
|
|
153
|
+
const returnUrl = state?.app_id
|
|
154
|
+
? `https://app.hubspot.com/app/${portalId}/${state.app_id}/billing`
|
|
155
|
+
: "https://app.hubspot.com/";
|
|
144
156
|
// Max feature count across cards — each card pads to this so all cards share
|
|
145
157
|
// content height and the CTAs align (see PlanCard comment).
|
|
146
158
|
const maxFeatures = plans.reduce(
|
|
@@ -148,44 +160,6 @@ export function PlanGrid({ context, state, appKey }) {
|
|
|
148
160
|
0
|
|
149
161
|
);
|
|
150
162
|
|
|
151
|
-
async function choose(plan, leg) {
|
|
152
|
-
setError(null);
|
|
153
|
-
setChoosingTier(plan.tier);
|
|
154
|
-
try {
|
|
155
|
-
const base = state?.billing_base_url;
|
|
156
|
-
if (!base) throw new Error("Billing service not configured");
|
|
157
|
-
if (!appKey) throw new Error("Missing appKey for checkout");
|
|
158
|
-
const portalId = context?.portal?.id;
|
|
159
|
-
|
|
160
|
-
const returnUrl = state?.app_id
|
|
161
|
-
? `https://app.hubspot.com/app/${portalId}/${state.app_id}/billing`
|
|
162
|
-
: "https://app.hubspot.com/";
|
|
163
|
-
|
|
164
|
-
// hubspot.fetch: body is a plain OBJECT (not a JSON string); only
|
|
165
|
-
// Authorization survives as a header — don't set Content-Type.
|
|
166
|
-
const res = await hubspot.fetch(`${base}/v1/billing/checkout`, {
|
|
167
|
-
method: "POST",
|
|
168
|
-
body: {
|
|
169
|
-
app_key: appKey,
|
|
170
|
-
portal_id: String(portalId),
|
|
171
|
-
price_id: leg.price_id,
|
|
172
|
-
return_url: returnUrl,
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
if (!res.ok) {
|
|
176
|
-
const text = await res.text();
|
|
177
|
-
throw new Error(`Checkout request failed (${res.status}): ${text}`);
|
|
178
|
-
}
|
|
179
|
-
const { url } = await res.json();
|
|
180
|
-
if (!url) throw new Error("No checkout URL returned");
|
|
181
|
-
setCheckoutUrls((prev) => ({ ...prev, [`${plan.tier}:${interval}`]: url }));
|
|
182
|
-
} catch (err) {
|
|
183
|
-
setError(String(err));
|
|
184
|
-
} finally {
|
|
185
|
-
setChoosingTier(null);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
163
|
return (
|
|
190
164
|
<Flex direction="column" gap="medium">
|
|
191
165
|
<Flex direction="row" gap="medium" align="center" justify="start">
|
|
@@ -205,12 +179,6 @@ export function PlanGrid({ context, state, appKey }) {
|
|
|
205
179
|
)}
|
|
206
180
|
</Flex>
|
|
207
181
|
|
|
208
|
-
{error && (
|
|
209
|
-
<Alert title="Couldn't start checkout" variant="danger">
|
|
210
|
-
<Text>{error}</Text>
|
|
211
|
-
</Alert>
|
|
212
|
-
)}
|
|
213
|
-
|
|
214
182
|
{/* AutoGrid(flexible): equal-width columns that wrap responsively. Card
|
|
215
183
|
height is equalized via the maxFeatures padding in PlanCard. */}
|
|
216
184
|
<AutoGrid columnWidth={240} flexible={true} gap="medium">
|
|
@@ -219,9 +187,10 @@ export function PlanGrid({ context, state, appKey }) {
|
|
|
219
187
|
key={plan.tier}
|
|
220
188
|
plan={plan}
|
|
221
189
|
interval={interval}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
190
|
+
billingBaseUrl={billingBaseUrl}
|
|
191
|
+
appKey={appKey}
|
|
192
|
+
portalId={portalId}
|
|
193
|
+
returnUrl={returnUrl}
|
|
225
194
|
supportUrl={supportUrl}
|
|
226
195
|
maxFeatures={maxFeatures}
|
|
227
196
|
/>
|