@fantastic.dev/better-stripe 0.61.0 → 0.62.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/CHANGELOG.md +6 -0
- package/README.md +3 -1
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +3 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/workflows/UsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/UsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.js +19 -0
- package/dist/react/workflows/UsageConfiguration.js.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts +65 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.js +2 -0
- package/dist/react/workflows/UsageConfiguration.types.js.map +1 -0
- package/dist/react/workflows/UsagePriceRow.d.ts +26 -0
- package/dist/react/workflows/UsagePriceRow.d.ts.map +1 -0
- package/dist/react/workflows/UsagePriceRow.js +40 -0
- package/dist/react/workflows/UsagePriceRow.js.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts +4 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.js +116 -0
- package/dist/react/workflows/usage-configuration-validation.js.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.js +84 -0
- package/dist/react/workflows/useUsageConfiguration.js.map +1 -0
- package/docs/README.md +2 -0
- package/docs/react-api.md +2 -0
- package/docs/usage-configuration.md +150 -0
- package/package.json +3 -2
- package/src/react/index.ts +16 -0
- package/src/react/workflows/UsageConfiguration.test.tsx +393 -0
- package/src/react/workflows/UsageConfiguration.tsx +135 -0
- package/src/react/workflows/UsageConfiguration.types.ts +67 -0
- package/src/react/workflows/UsagePriceRow.test.tsx +140 -0
- package/src/react/workflows/UsagePriceRow.tsx +109 -0
- package/src/react/workflows/usage-configuration-validation.test.ts +107 -0
- package/src/react/workflows/usage-configuration-validation.ts +152 -0
- package/src/react/workflows/useUsageConfiguration.ts +99 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useId } from "react";
|
|
3
|
+
import { useUsageConfiguration } from "./useUsageConfiguration.js";
|
|
4
|
+
import type {
|
|
5
|
+
UsageConfigurationFields,
|
|
6
|
+
UsageConfigurationProps,
|
|
7
|
+
} from "./UsageConfiguration.types.js";
|
|
8
|
+
/** Unstyled native catalog configuration. Host adapters own authorization and provider operations. */
|
|
9
|
+
export function UsageConfiguration(props: UsageConfigurationProps) {
|
|
10
|
+
const id = useId();
|
|
11
|
+
const state = useUsageConfiguration(props);
|
|
12
|
+
if (props.children) return <>{props.children(state)}</>;
|
|
13
|
+
if (props.scope.capability !== "issuer_native")
|
|
14
|
+
return (
|
|
15
|
+
<p role="status">
|
|
16
|
+
Native usage configuration unavailable for this account.
|
|
17
|
+
</p>
|
|
18
|
+
);
|
|
19
|
+
const input = (
|
|
20
|
+
field: keyof UsageConfigurationFields,
|
|
21
|
+
label: string,
|
|
22
|
+
inputMode?: "decimal" | "numeric",
|
|
23
|
+
) => (
|
|
24
|
+
<label htmlFor={`${id}-${field}`}>
|
|
25
|
+
{label}
|
|
26
|
+
<input
|
|
27
|
+
id={`${id}-${field}`}
|
|
28
|
+
value={state.fields[field]}
|
|
29
|
+
inputMode={inputMode}
|
|
30
|
+
disabled={state.immutable}
|
|
31
|
+
onChange={(event) => state.setField(field, event.target.value)}
|
|
32
|
+
/>
|
|
33
|
+
</label>
|
|
34
|
+
);
|
|
35
|
+
return (
|
|
36
|
+
<form
|
|
37
|
+
className={props.className}
|
|
38
|
+
data-better-stripe="usage-configuration"
|
|
39
|
+
aria-label="Usage configuration"
|
|
40
|
+
aria-busy={state.isPending}
|
|
41
|
+
onSubmit={(event) => {
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
void state.save();
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
<h3>Usage configuration</h3>
|
|
47
|
+
<p>
|
|
48
|
+
Owner: {props.scope.ownershipLabel}. Authorized {props.scope.role}{" "}
|
|
49
|
+
configuration.
|
|
50
|
+
</p>
|
|
51
|
+
<p>
|
|
52
|
+
Monthly billing with sum aggregation. Existing subscriptions keep their
|
|
53
|
+
original prices. Changed terms require a new version, event name and
|
|
54
|
+
price.
|
|
55
|
+
</p>
|
|
56
|
+
<fieldset disabled={state.disabled || state.isPending}>
|
|
57
|
+
<legend>Meter and immutable billing terms</legend>
|
|
58
|
+
<label htmlFor={`${id}-meter`}>Meter</label>
|
|
59
|
+
<select
|
|
60
|
+
id={`${id}-meter`}
|
|
61
|
+
value={state.meterId}
|
|
62
|
+
onChange={(event) => state.selectMeter(event.target.value)}
|
|
63
|
+
>
|
|
64
|
+
<option value="">Create new meter version</option>
|
|
65
|
+
{props.meters.map((meter) => (
|
|
66
|
+
<option key={meter.id} value={meter.id} disabled={!meter.active}>
|
|
67
|
+
{meter.configuration.displayName} ·{" "}
|
|
68
|
+
{meter.configuration.plan.version}
|
|
69
|
+
{meter.active ? "" : " (inactive)"}
|
|
70
|
+
</option>
|
|
71
|
+
))}
|
|
72
|
+
</select>
|
|
73
|
+
<label htmlFor={`${id}-model`}>Billing model</label>
|
|
74
|
+
<select
|
|
75
|
+
id={`${id}-model`}
|
|
76
|
+
value={state.fields.model}
|
|
77
|
+
disabled={state.immutable}
|
|
78
|
+
onChange={(event) =>
|
|
79
|
+
state.setField(
|
|
80
|
+
"model",
|
|
81
|
+
event.target.value as UsageConfigurationFields["model"],
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
>
|
|
85
|
+
<option value="pay_as_you_go">Pay as you go</option>
|
|
86
|
+
<option value="included_overage">
|
|
87
|
+
Included monthly units plus overages
|
|
88
|
+
</option>
|
|
89
|
+
<option value="prepaid">Prepaid paid credits</option>
|
|
90
|
+
</select>
|
|
91
|
+
{input("key", "Plan key")}
|
|
92
|
+
{input("version", "Plan version")}
|
|
93
|
+
{input("eventName", "Meter event name")}
|
|
94
|
+
{input("displayName", "Meter display name")}
|
|
95
|
+
{input("unit", "Billable unit")}
|
|
96
|
+
{input("precision", "Quantity precision", "numeric")}
|
|
97
|
+
{input("currency", "Currency")}
|
|
98
|
+
{input(
|
|
99
|
+
"price",
|
|
100
|
+
`Price per whole unit (${state.fields.currency.toUpperCase()})`,
|
|
101
|
+
"decimal",
|
|
102
|
+
)}
|
|
103
|
+
{state.fields.model === "included_overage" && (
|
|
104
|
+
<>
|
|
105
|
+
{input("included", "Included units per billing period", "decimal")}
|
|
106
|
+
<p>
|
|
107
|
+
Graduated pricing: free included units, then the configured
|
|
108
|
+
overage rate. Reset at each original monthly billing period.
|
|
109
|
+
</p>
|
|
110
|
+
</>
|
|
111
|
+
)}
|
|
112
|
+
{state.fields.model === "prepaid" && (
|
|
113
|
+
<>
|
|
114
|
+
{input("packageQuantity", "Credit package quantity", "decimal")}
|
|
115
|
+
<p>
|
|
116
|
+
Manual top-up with a verified payment for the issuer's own
|
|
117
|
+
services. Retain original credit terms. Reject work when credits
|
|
118
|
+
are insufficient. No automatic top-up or monetary wallet.
|
|
119
|
+
</p>
|
|
120
|
+
</>
|
|
121
|
+
)}
|
|
122
|
+
<p>
|
|
123
|
+
Only managed monthly sum meters and exact per-unit rates or
|
|
124
|
+
included-unit graduated tiers are supported. Other tiers,
|
|
125
|
+
transformations and intervals require a supported server adapter.
|
|
126
|
+
</p>
|
|
127
|
+
<button type="submit">
|
|
128
|
+
{state.isPending ? "Saving…" : "Save configuration version"}
|
|
129
|
+
</button>
|
|
130
|
+
</fieldset>
|
|
131
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
132
|
+
{state.message && <p role="status">{state.message}</p>}
|
|
133
|
+
</form>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
UsagePlan,
|
|
4
|
+
UsageBillingModel,
|
|
5
|
+
} from "../../client/usage/contracts.js";
|
|
6
|
+
/** Display/cache context from an authenticated host. Never use this as server authorization. */
|
|
7
|
+
export type UsageConfigurationScope = {
|
|
8
|
+
identityKey: string;
|
|
9
|
+
accountKey: string;
|
|
10
|
+
productKey: string;
|
|
11
|
+
role: "admin" | "seller";
|
|
12
|
+
ownershipLabel: string;
|
|
13
|
+
canConfigure: boolean;
|
|
14
|
+
/** Host has verified the configured issuer realm and supported native catalog. */
|
|
15
|
+
capability: "issuer_native" | "unsupported";
|
|
16
|
+
};
|
|
17
|
+
export type UsageConfigurationValue = {
|
|
18
|
+
plan: UsagePlan;
|
|
19
|
+
eventName: string;
|
|
20
|
+
displayName: string;
|
|
21
|
+
meterId?: string;
|
|
22
|
+
creditPackage?: { quantity: string; topUp: "manual" };
|
|
23
|
+
};
|
|
24
|
+
export type UsageMeterChoice = {
|
|
25
|
+
id: string;
|
|
26
|
+
active: boolean;
|
|
27
|
+
/** Original managed terms, derived and authorized by the host. */
|
|
28
|
+
configuration: UsageConfigurationValue;
|
|
29
|
+
};
|
|
30
|
+
export type UsageConfigurationFields = {
|
|
31
|
+
model: UsageBillingModel;
|
|
32
|
+
key: string;
|
|
33
|
+
version: string;
|
|
34
|
+
unit: string;
|
|
35
|
+
precision: string;
|
|
36
|
+
currency: string;
|
|
37
|
+
price: string;
|
|
38
|
+
included: string;
|
|
39
|
+
packageQuantity: string;
|
|
40
|
+
eventName: string;
|
|
41
|
+
displayName: string;
|
|
42
|
+
};
|
|
43
|
+
export type UsageConfigurationProps = {
|
|
44
|
+
scope: UsageConfigurationScope;
|
|
45
|
+
initial: UsageConfigurationValue;
|
|
46
|
+
/** A scoped page/subset of managed meters. Never treat a partial list as complete inventory. */
|
|
47
|
+
meters: readonly UsageMeterChoice[];
|
|
48
|
+
/** Reauthorize, derive ownership/product/realm and validate immutable terms on the server. */
|
|
49
|
+
onSave: (value: UsageConfigurationValue) => Promise<unknown>;
|
|
50
|
+
className?: string;
|
|
51
|
+
children?: (state: UsageConfigurationState) => ReactNode;
|
|
52
|
+
};
|
|
53
|
+
export type UsageConfigurationState = {
|
|
54
|
+
fields: UsageConfigurationFields;
|
|
55
|
+
setField: <K extends keyof UsageConfigurationFields>(
|
|
56
|
+
key: K,
|
|
57
|
+
value: UsageConfigurationFields[K],
|
|
58
|
+
) => void;
|
|
59
|
+
meterId: string;
|
|
60
|
+
selectMeter: (id: string) => void;
|
|
61
|
+
immutable: boolean;
|
|
62
|
+
disabled: boolean;
|
|
63
|
+
isPending: boolean;
|
|
64
|
+
error: string | null;
|
|
65
|
+
message: string | undefined;
|
|
66
|
+
save: () => Promise<unknown>;
|
|
67
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, expect, it } from "vitest";
|
|
4
|
+
import * as ui from "../index.js";
|
|
5
|
+
import type { UsagePriceBinding } from "../../client/usage/catalogRead.js";
|
|
6
|
+
afterEach(cleanup);
|
|
7
|
+
const price: UsagePriceBinding = {
|
|
8
|
+
pricingMode: "metered",
|
|
9
|
+
stripePriceId: "price_usage",
|
|
10
|
+
stripeProductId: "prod_usage",
|
|
11
|
+
meterId: "mtr_usage",
|
|
12
|
+
active: true,
|
|
13
|
+
currency: "usd",
|
|
14
|
+
billingScheme: "per_unit",
|
|
15
|
+
unitAmountDecimal: "0.00125",
|
|
16
|
+
tiers: [],
|
|
17
|
+
interval: "month",
|
|
18
|
+
plan: {
|
|
19
|
+
key: "calls",
|
|
20
|
+
version: "v1",
|
|
21
|
+
model: "pay_as_you_go",
|
|
22
|
+
unit: { name: "call", precision: 2 },
|
|
23
|
+
rate: { currency: "usd", unitAmountDecimal: "0.125" },
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
it("reuses ordinary PriceCard formatting for fixed prices", () => {
|
|
27
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
28
|
+
render(
|
|
29
|
+
<ui.UsagePriceRow
|
|
30
|
+
value={{
|
|
31
|
+
kind: "fixed",
|
|
32
|
+
price: {
|
|
33
|
+
stripePriceId: "price_fixed",
|
|
34
|
+
unitAmount: 900,
|
|
35
|
+
currency: "usd",
|
|
36
|
+
type: "recurring",
|
|
37
|
+
interval: "month",
|
|
38
|
+
active: true,
|
|
39
|
+
},
|
|
40
|
+
}}
|
|
41
|
+
/>,
|
|
42
|
+
);
|
|
43
|
+
expect(screen.getByText("Fixed price")).toBeTruthy();
|
|
44
|
+
expect(screen.getByText("$9.00/month")).toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
it("shows exact whole-unit metered rates and the original version instead of treating null amount as free", () => {
|
|
47
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
48
|
+
render(<ui.UsagePriceRow value={{ kind: "metered", price }} />);
|
|
49
|
+
expect(screen.getByText(/0.00125 USD per call/)).toBeTruthy();
|
|
50
|
+
expect(screen.getByText(/v1/)).toBeTruthy();
|
|
51
|
+
expect(screen.getByText(/sum aggregation/)).toBeTruthy();
|
|
52
|
+
});
|
|
53
|
+
it("labels supported managed graduated tiers separately from other tiers and transformations", () => {
|
|
54
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
55
|
+
const { rerender } = render(
|
|
56
|
+
<ui.UsagePriceRow
|
|
57
|
+
value={{
|
|
58
|
+
kind: "metered",
|
|
59
|
+
price: {
|
|
60
|
+
...price,
|
|
61
|
+
billingScheme: "tiered",
|
|
62
|
+
plan: {
|
|
63
|
+
...price.plan,
|
|
64
|
+
model: "included_overage",
|
|
65
|
+
includedQuantity: "100",
|
|
66
|
+
reset: "billing_period",
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}}
|
|
70
|
+
/>,
|
|
71
|
+
);
|
|
72
|
+
expect(screen.getByText(/100 included/)).toBeTruthy();
|
|
73
|
+
rerender(
|
|
74
|
+
<ui.UsagePriceRow value={{ kind: "unsupported", reason: "tiered" }} />,
|
|
75
|
+
);
|
|
76
|
+
expect(screen.getByText(/Unsupported tiered pricing/)).toBeTruthy();
|
|
77
|
+
expect(screen.queryByText(/0.00/)).toBeNull();
|
|
78
|
+
rerender(
|
|
79
|
+
<ui.UsagePriceRow value={{ kind: "unsupported", reason: "transformed" }} />,
|
|
80
|
+
);
|
|
81
|
+
expect(screen.getByText(/Unsupported transformed pricing/)).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
it("does not let legacy fixed rows misrepresent metered, tiered, transformed or missing amounts", () => {
|
|
84
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
85
|
+
const fixed = {
|
|
86
|
+
stripePriceId: "price_bad",
|
|
87
|
+
currency: "usd",
|
|
88
|
+
type: "recurring",
|
|
89
|
+
interval: "month",
|
|
90
|
+
active: true,
|
|
91
|
+
unitAmount: 0,
|
|
92
|
+
};
|
|
93
|
+
const { rerender } = render(
|
|
94
|
+
<ui.UsagePriceRow
|
|
95
|
+
value={{ kind: "fixed", price: { ...fixed, usageType: "metered" } }}
|
|
96
|
+
/>,
|
|
97
|
+
);
|
|
98
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
99
|
+
rerender(
|
|
100
|
+
<ui.UsagePriceRow
|
|
101
|
+
value={{ kind: "fixed", price: { ...fixed, billingScheme: "tiered" } }}
|
|
102
|
+
/>,
|
|
103
|
+
);
|
|
104
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
105
|
+
rerender(
|
|
106
|
+
<ui.UsagePriceRow
|
|
107
|
+
value={{
|
|
108
|
+
kind: "fixed",
|
|
109
|
+
price: { ...fixed, transformQuantity: { divideBy: 100 } },
|
|
110
|
+
}}
|
|
111
|
+
/>,
|
|
112
|
+
);
|
|
113
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
114
|
+
rerender(
|
|
115
|
+
<ui.UsagePriceRow
|
|
116
|
+
value={{ kind: "fixed", price: { ...fixed, unitAmount: null } }}
|
|
117
|
+
/>,
|
|
118
|
+
);
|
|
119
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
it("keeps unsupported fixed charge currency and increments unavailable without crashing", () => {
|
|
122
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
123
|
+
const price = {
|
|
124
|
+
stripePriceId: "price_bad",
|
|
125
|
+
unitAmount: 1,
|
|
126
|
+
currency: "bhd",
|
|
127
|
+
type: "one_time",
|
|
128
|
+
active: true,
|
|
129
|
+
};
|
|
130
|
+
const { rerender } = render(
|
|
131
|
+
<ui.UsagePriceRow value={{ kind: "fixed", price }} />,
|
|
132
|
+
);
|
|
133
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
134
|
+
rerender(
|
|
135
|
+
<ui.UsagePriceRow
|
|
136
|
+
value={{ kind: "fixed", price: { ...price, currency: "isk" } }}
|
|
137
|
+
/>,
|
|
138
|
+
);
|
|
139
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
140
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import type { UsagePriceBinding } from "../../client/usage/catalogRead.js";
|
|
4
|
+
import { PriceCard, type PriceCardPrice } from "../components/PriceCard.js";
|
|
5
|
+
import { chargeAmountToDecimal } from "../../client/charge-currency.js";
|
|
6
|
+
import { formatUsageEstimate } from "./usage-format.js";
|
|
7
|
+
export type UsageCatalogPrice =
|
|
8
|
+
| {
|
|
9
|
+
kind: "fixed";
|
|
10
|
+
price: PriceCardPrice & {
|
|
11
|
+
usageType?: string;
|
|
12
|
+
billingScheme?: string;
|
|
13
|
+
transformQuantity?: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
| { kind: "metered"; price: UsagePriceBinding }
|
|
17
|
+
| { kind: "unsupported"; reason: "tiered" | "transformed" | "other" };
|
|
18
|
+
export type UsagePriceRowProps = {
|
|
19
|
+
value: UsageCatalogPrice;
|
|
20
|
+
productName?: string;
|
|
21
|
+
className?: string;
|
|
22
|
+
children?: (value: UsageCatalogPrice) => ReactNode;
|
|
23
|
+
};
|
|
24
|
+
/** Read-only catalog row. Only managed metered bindings carry original exact terms. */
|
|
25
|
+
export function UsagePriceRow({
|
|
26
|
+
value,
|
|
27
|
+
className,
|
|
28
|
+
productName,
|
|
29
|
+
children,
|
|
30
|
+
}: UsagePriceRowProps) {
|
|
31
|
+
if (children) return <>{children(value)}</>;
|
|
32
|
+
let contents: ReactNode;
|
|
33
|
+
if (value.kind === "fixed") {
|
|
34
|
+
const price = value.price;
|
|
35
|
+
const supported =
|
|
36
|
+
price.unitAmount != null &&
|
|
37
|
+
Number.isSafeInteger(price.unitAmount) &&
|
|
38
|
+
price.unitAmount >= 0 &&
|
|
39
|
+
chargeAmountToDecimal(price.unitAmount, price.currency) !== null &&
|
|
40
|
+
price.usageType !== "metered" &&
|
|
41
|
+
(!price.billingScheme || price.billingScheme === "per_unit") &&
|
|
42
|
+
price.transformQuantity == null;
|
|
43
|
+
contents = supported ? (
|
|
44
|
+
<>
|
|
45
|
+
<p>Fixed price</p>
|
|
46
|
+
<PriceCard price={price}>
|
|
47
|
+
{({ formattedPrice }) => <p>{formattedPrice}</p>}
|
|
48
|
+
</PriceCard>
|
|
49
|
+
</>
|
|
50
|
+
) : (
|
|
51
|
+
<p>
|
|
52
|
+
Unsupported pricing terms. Read the original provider terms before
|
|
53
|
+
configuration.
|
|
54
|
+
</p>
|
|
55
|
+
);
|
|
56
|
+
} else if (value.kind === "unsupported") {
|
|
57
|
+
contents = (
|
|
58
|
+
<p>
|
|
59
|
+
{value.reason === "tiered"
|
|
60
|
+
? "Unsupported tiered pricing"
|
|
61
|
+
: value.reason === "transformed"
|
|
62
|
+
? "Unsupported transformed pricing"
|
|
63
|
+
: "Unsupported pricing terms"}
|
|
64
|
+
. No fixed or zero price is inferred.
|
|
65
|
+
</p>
|
|
66
|
+
);
|
|
67
|
+
} else {
|
|
68
|
+
const { plan } = value.price;
|
|
69
|
+
const amount = formatUsageEstimate(
|
|
70
|
+
plan.rate.unitAmountDecimal,
|
|
71
|
+
plan.rate.currency,
|
|
72
|
+
);
|
|
73
|
+
contents = (
|
|
74
|
+
<>
|
|
75
|
+
<p>
|
|
76
|
+
{plan.model === "included_overage"
|
|
77
|
+
? "Included monthly units plus overages"
|
|
78
|
+
: plan.model === "prepaid"
|
|
79
|
+
? "Prepaid paid credits"
|
|
80
|
+
: "Pay as you go"}
|
|
81
|
+
</p>
|
|
82
|
+
<p>Version {plan.version}. Monthly billing, sum aggregation.</p>
|
|
83
|
+
<p>
|
|
84
|
+
{amount ? `${amount} per ${plan.unit.name}` : "Rate unavailable"}.
|
|
85
|
+
Quantity precision {plan.unit.precision}.
|
|
86
|
+
</p>
|
|
87
|
+
{plan.model === "included_overage" && (
|
|
88
|
+
<p>
|
|
89
|
+
{plan.includedQuantity} included per billing period. Graduated free
|
|
90
|
+
tier, then overages.
|
|
91
|
+
</p>
|
|
92
|
+
)}
|
|
93
|
+
{plan.model === "prepaid" && (
|
|
94
|
+
<p>
|
|
95
|
+
Payment-backed issuer credits. Consumption is not a second cash
|
|
96
|
+
collection.
|
|
97
|
+
</p>
|
|
98
|
+
)}
|
|
99
|
+
<p>{value.price.active ? "Active" : "Inactive"} immutable price</p>
|
|
100
|
+
</>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return (
|
|
104
|
+
<div className={className} data-better-stripe="usage-price-row">
|
|
105
|
+
{productName && <h3>{productName}</h3>}
|
|
106
|
+
{contents}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
configurationFields,
|
|
4
|
+
validateConfiguration,
|
|
5
|
+
} from "./usage-configuration-validation.js";
|
|
6
|
+
import type { UsageConfigurationValue } from "./UsageConfiguration.types.js";
|
|
7
|
+
const original: UsageConfigurationValue = {
|
|
8
|
+
plan: {
|
|
9
|
+
key: "calls",
|
|
10
|
+
version: "v1",
|
|
11
|
+
model: "pay_as_you_go",
|
|
12
|
+
unit: { name: "call", precision: 0 },
|
|
13
|
+
rate: { currency: "usd", unitAmountDecimal: "2" },
|
|
14
|
+
},
|
|
15
|
+
eventName: "calls_v1",
|
|
16
|
+
displayName: "Calls",
|
|
17
|
+
};
|
|
18
|
+
it.each([
|
|
19
|
+
["precision", "1.5"],
|
|
20
|
+
["precision", "7"],
|
|
21
|
+
["key", ""],
|
|
22
|
+
["version", " "],
|
|
23
|
+
["unit", ""],
|
|
24
|
+
["eventName", "has spaces"],
|
|
25
|
+
["displayName", ""],
|
|
26
|
+
["currency", "bhd"],
|
|
27
|
+
["currency", "zzz"],
|
|
28
|
+
["price", "-1"],
|
|
29
|
+
["price", "1e2"],
|
|
30
|
+
["price", "0.000000000000001"],
|
|
31
|
+
["price", "99999999999999"],
|
|
32
|
+
["model", "unsupported"],
|
|
33
|
+
])(
|
|
34
|
+
"rejects invalid %s=%s instead of provisioning or rounding",
|
|
35
|
+
(key, value) => {
|
|
36
|
+
expect(() =>
|
|
37
|
+
validateConfiguration({ ...configurationFields(original), [key]: value }),
|
|
38
|
+
).toThrow();
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
it("rejects rates that fit the shared contract but exceed native scaled decimal precision", () => {
|
|
42
|
+
expect(() =>
|
|
43
|
+
validateConfiguration({
|
|
44
|
+
...configurationFields(original),
|
|
45
|
+
precision: "6",
|
|
46
|
+
price: "0.000000001",
|
|
47
|
+
}),
|
|
48
|
+
).toThrow(/precision/);
|
|
49
|
+
});
|
|
50
|
+
it.each(["0", "0.001", "9999999999999999999"])(
|
|
51
|
+
"rejects invalid credit package %s",
|
|
52
|
+
(packageQuantity) => {
|
|
53
|
+
expect(() =>
|
|
54
|
+
validateConfiguration({
|
|
55
|
+
...configurationFields(original),
|
|
56
|
+
model: "prepaid",
|
|
57
|
+
packageQuantity,
|
|
58
|
+
}),
|
|
59
|
+
).toThrow(/Paid credits/);
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
it("roundtrips fractional zero-decimal currency rates as exact major units", () => {
|
|
63
|
+
const value = {
|
|
64
|
+
...original,
|
|
65
|
+
plan: {
|
|
66
|
+
...original.plan,
|
|
67
|
+
rate: { currency: "jpy", unitAmountDecimal: "0.125" },
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
const fields = configurationFields(value);
|
|
71
|
+
expect(fields.price).toBe("0.125");
|
|
72
|
+
expect(validateConfiguration(fields)).toEqual(value);
|
|
73
|
+
});
|
|
74
|
+
it("rejects prepaid packages which violate special currency charge increments", () => {
|
|
75
|
+
expect(() =>
|
|
76
|
+
validateConfiguration({
|
|
77
|
+
...configurationFields(original),
|
|
78
|
+
currency: "isk",
|
|
79
|
+
price: "0.02",
|
|
80
|
+
model: "prepaid",
|
|
81
|
+
packageQuantity: "1",
|
|
82
|
+
}),
|
|
83
|
+
).toThrow(/Paid credits/);
|
|
84
|
+
});
|
|
85
|
+
it.each(["jpy", "usd", "isk"])(
|
|
86
|
+
"preserves large exact native per-unit rates in %s",
|
|
87
|
+
(currency) => {
|
|
88
|
+
const value = {
|
|
89
|
+
...original,
|
|
90
|
+
plan: {
|
|
91
|
+
...original.plan,
|
|
92
|
+
rate: { currency, unitAmountDecimal: "999999999999.125" },
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
expect(validateConfiguration(configurationFields(value))).toEqual(value);
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
it("allows exact paid packages for fractional rates when consumption uses whole-backed batches", () => {
|
|
99
|
+
const result = validateConfiguration({
|
|
100
|
+
...configurationFields(original),
|
|
101
|
+
model: "prepaid",
|
|
102
|
+
price: "0.001",
|
|
103
|
+
packageQuantity: "1000",
|
|
104
|
+
});
|
|
105
|
+
expect(result.plan.rate.unitAmountDecimal).toBe("0.1");
|
|
106
|
+
expect(result.creditPackage).toEqual({ quantity: "1000", topUp: "manual" });
|
|
107
|
+
});
|