@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,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chargeAmountToDecimal,
|
|
3
|
+
getChargeCurrencyMetadata,
|
|
4
|
+
} from "../../client/charge-currency.js";
|
|
5
|
+
import {
|
|
6
|
+
defineUsagePlan,
|
|
7
|
+
parseUsageQuantity,
|
|
8
|
+
} from "../../client/usage/contracts.js";
|
|
9
|
+
import { scaledUsageRate } from "../../client/usage/catalogMath.js";
|
|
10
|
+
import { creditCost } from "../../client/usage/creditTerms.js";
|
|
11
|
+
import type {
|
|
12
|
+
UsageConfigurationFields,
|
|
13
|
+
UsageConfigurationValue,
|
|
14
|
+
} from "./UsageConfiguration.types.js";
|
|
15
|
+
function invalid(message: string): never {
|
|
16
|
+
throw new Error(message);
|
|
17
|
+
}
|
|
18
|
+
/** Exact major-currency input to the existing decimal minor-unit rate contract. */
|
|
19
|
+
function minorRate(price: string, currency: string): string {
|
|
20
|
+
const metadata = getChargeCurrencyMetadata(currency);
|
|
21
|
+
if (!metadata) return invalid("Choose a supported charge currency.");
|
|
22
|
+
if (!/^(0|[1-9]\d{0,13})(\.\d{1,14})?$/.test(price))
|
|
23
|
+
return invalid("Enter a nonnegative decimal price without rounding.");
|
|
24
|
+
const [whole, fraction = ""] = price.split(".");
|
|
25
|
+
const padded = fraction.padEnd(metadata.apiDecimals, "0");
|
|
26
|
+
const integer = BigInt(
|
|
27
|
+
whole + padded.slice(0, metadata.apiDecimals),
|
|
28
|
+
).toString();
|
|
29
|
+
const decimal = padded.slice(metadata.apiDecimals).replace(/0+$/, "");
|
|
30
|
+
return decimal ? `${integer}.${decimal}` : integer;
|
|
31
|
+
}
|
|
32
|
+
export function configurationFields(
|
|
33
|
+
value: UsageConfigurationValue,
|
|
34
|
+
): UsageConfigurationFields {
|
|
35
|
+
const { plan } = value;
|
|
36
|
+
const metadata = getChargeCurrencyMetadata(plan.rate.currency);
|
|
37
|
+
const [whole, fraction = ""] = plan.rate.unitAmountDecimal.split(".");
|
|
38
|
+
const scale = (metadata?.apiDecimals ?? 0) + fraction.length;
|
|
39
|
+
const digits = (whole + fraction).padStart(scale + 1, "0");
|
|
40
|
+
const integer = BigInt(scale ? digits.slice(0, -scale) : digits).toString();
|
|
41
|
+
const decimal = (scale ? digits.slice(-scale) : "").replace(/0+$/, "");
|
|
42
|
+
const formatted = metadata
|
|
43
|
+
? integer +
|
|
44
|
+
(decimal ? `.${decimal}` : metadata.displayDecimals ? ".00" : "")
|
|
45
|
+
: "";
|
|
46
|
+
return {
|
|
47
|
+
model: plan.model,
|
|
48
|
+
key: plan.key,
|
|
49
|
+
version: plan.version,
|
|
50
|
+
unit: plan.unit.name,
|
|
51
|
+
precision: String(plan.unit.precision),
|
|
52
|
+
currency: plan.rate.currency,
|
|
53
|
+
price: formatted,
|
|
54
|
+
included:
|
|
55
|
+
plan.model === "included_overage" ? plan.includedQuantity : "1000",
|
|
56
|
+
packageQuantity: value.creditPackage?.quantity ?? "100",
|
|
57
|
+
eventName: value.eventName,
|
|
58
|
+
displayName: value.displayName,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function validateConfiguration(
|
|
62
|
+
fields: UsageConfigurationFields,
|
|
63
|
+
meterId?: string,
|
|
64
|
+
): UsageConfigurationValue {
|
|
65
|
+
const precision = Number(fields.precision);
|
|
66
|
+
if (!/^[0-6]$/.test(fields.precision))
|
|
67
|
+
return invalid("Quantity precision must be between 0 and 6.");
|
|
68
|
+
if (
|
|
69
|
+
!fields.key.trim() ||
|
|
70
|
+
fields.key.length > 100 ||
|
|
71
|
+
!fields.version.trim() ||
|
|
72
|
+
fields.version.length > 100
|
|
73
|
+
)
|
|
74
|
+
return invalid(
|
|
75
|
+
"Enter a plan key and a new version, each at most 100 characters.",
|
|
76
|
+
);
|
|
77
|
+
if (!fields.unit.trim() || fields.unit.length > 100)
|
|
78
|
+
return invalid("Enter a billable unit name of at most 100 characters.");
|
|
79
|
+
if (
|
|
80
|
+
!/^[\w.-]{1,100}$/.test(fields.eventName) ||
|
|
81
|
+
!fields.displayName.trim() ||
|
|
82
|
+
fields.displayName.length > 100
|
|
83
|
+
)
|
|
84
|
+
return invalid(
|
|
85
|
+
"Enter a valid meter event name and display name, each at most 100 characters.",
|
|
86
|
+
);
|
|
87
|
+
const currency = fields.currency.toLowerCase();
|
|
88
|
+
const rate = minorRate(fields.price, currency);
|
|
89
|
+
try {
|
|
90
|
+
scaledUsageRate(rate, precision);
|
|
91
|
+
} catch {
|
|
92
|
+
return invalid(
|
|
93
|
+
"The rate exceeds native decimal precision or amount bounds. Change the rate or quantity precision.",
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
const common = {
|
|
97
|
+
key: fields.key,
|
|
98
|
+
version: fields.version,
|
|
99
|
+
unit: { name: fields.unit, precision },
|
|
100
|
+
rate: { currency, unitAmountDecimal: rate },
|
|
101
|
+
};
|
|
102
|
+
if (fields.model === "included_overage") {
|
|
103
|
+
try {
|
|
104
|
+
const included = BigInt(
|
|
105
|
+
parseUsageQuantity(fields.included, precision).scaledQuantity,
|
|
106
|
+
);
|
|
107
|
+
if (included < 1n || included > BigInt(Number.MAX_SAFE_INTEGER))
|
|
108
|
+
throw new Error();
|
|
109
|
+
} catch {
|
|
110
|
+
return invalid(
|
|
111
|
+
"Included units must be positive, exact at the selected precision and within native tier bounds.",
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const plan = defineUsagePlan(
|
|
116
|
+
fields.model === "included_overage"
|
|
117
|
+
? {
|
|
118
|
+
...common,
|
|
119
|
+
model: fields.model,
|
|
120
|
+
includedQuantity: fields.included,
|
|
121
|
+
reset: "billing_period",
|
|
122
|
+
}
|
|
123
|
+
: fields.model === "prepaid"
|
|
124
|
+
? { ...common, model: fields.model, insufficientBalance: "reject" }
|
|
125
|
+
: { ...common, model: fields.model },
|
|
126
|
+
);
|
|
127
|
+
let creditPackage: UsageConfigurationValue["creditPackage"];
|
|
128
|
+
if (plan.model === "prepaid") {
|
|
129
|
+
try {
|
|
130
|
+
const quantity = parseUsageQuantity(fields.packageQuantity, precision);
|
|
131
|
+
if (
|
|
132
|
+
chargeAmountToDecimal(
|
|
133
|
+
creditCost(plan, quantity.scaledQuantity),
|
|
134
|
+
currency,
|
|
135
|
+
) === null
|
|
136
|
+
)
|
|
137
|
+
throw new Error();
|
|
138
|
+
creditPackage = { quantity: quantity.quantity, topUp: "manual" };
|
|
139
|
+
} catch {
|
|
140
|
+
return invalid(
|
|
141
|
+
"Paid credits require a positive package quantity with whole minor currency backing and supported charge increments, within the native payment bound.",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
plan,
|
|
147
|
+
eventName: fields.eventName,
|
|
148
|
+
displayName: fields.displayName,
|
|
149
|
+
...(meterId ? { meterId } : {}),
|
|
150
|
+
...(creditPackage ? { creditPackage } : {}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { defineUsagePlan } from "../../client/usage/contracts.js";
|
|
4
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
5
|
+
import {
|
|
6
|
+
configurationFields,
|
|
7
|
+
validateConfiguration,
|
|
8
|
+
} from "./usage-configuration-validation.js";
|
|
9
|
+
import type {
|
|
10
|
+
UsageConfigurationProps,
|
|
11
|
+
UsageConfigurationState,
|
|
12
|
+
} from "./UsageConfiguration.types.js";
|
|
13
|
+
/** Shared form lifecycle for component and HTTP adapters. Server authorization is mandatory. */
|
|
14
|
+
export function useUsageConfiguration(
|
|
15
|
+
props: UsageConfigurationProps,
|
|
16
|
+
): UsageConfigurationState {
|
|
17
|
+
const key = JSON.stringify([props.scope, props.initial, props.meters]);
|
|
18
|
+
const initial = {
|
|
19
|
+
key,
|
|
20
|
+
meterId: props.initial.meterId ?? "",
|
|
21
|
+
fields: configurationFields(props.initial),
|
|
22
|
+
};
|
|
23
|
+
const [state, setState] = useState(initial);
|
|
24
|
+
if (state.key !== key) setState(initial);
|
|
25
|
+
const disabled =
|
|
26
|
+
!props.scope.canConfigure || props.scope.capability !== "issuer_native";
|
|
27
|
+
const immutable = state.meterId !== "";
|
|
28
|
+
const action = useWorkflowAction(async () => {
|
|
29
|
+
if (disabled) throw new Error("Configuration is read-only for this scope.");
|
|
30
|
+
let value = validateConfiguration(state.fields, state.meterId);
|
|
31
|
+
if (immutable) {
|
|
32
|
+
const meter = props.meters.find((item) => item.id === state.meterId);
|
|
33
|
+
if (
|
|
34
|
+
!meter?.active ||
|
|
35
|
+
JSON.stringify(
|
|
36
|
+
validateConfiguration(
|
|
37
|
+
configurationFields(meter.configuration),
|
|
38
|
+
meter.id,
|
|
39
|
+
),
|
|
40
|
+
) !== JSON.stringify(value)
|
|
41
|
+
)
|
|
42
|
+
throw new Error(
|
|
43
|
+
"Select an active managed meter with unchanged original terms.",
|
|
44
|
+
);
|
|
45
|
+
// Native intent fingerprints retain the original lexical decimal snapshot.
|
|
46
|
+
value = {
|
|
47
|
+
...value,
|
|
48
|
+
plan: defineUsagePlan(meter.configuration.plan),
|
|
49
|
+
...(value.creditPackage
|
|
50
|
+
? {
|
|
51
|
+
creditPackage: {
|
|
52
|
+
...value.creditPackage,
|
|
53
|
+
quantity:
|
|
54
|
+
meter.configuration.creditPackage?.quantity ??
|
|
55
|
+
value.creditPackage.quantity,
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
: {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
await props.onSave(value);
|
|
63
|
+
} catch {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"Configuration could not be saved. Retry the same terms or ask an administrator to reconcile the operation.",
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return "Configuration version saved. Existing subscriptions keep their original prices.";
|
|
69
|
+
}, key);
|
|
70
|
+
return {
|
|
71
|
+
fields: state.fields,
|
|
72
|
+
meterId: state.meterId,
|
|
73
|
+
immutable,
|
|
74
|
+
disabled,
|
|
75
|
+
isPending: action.isPending,
|
|
76
|
+
error: action.error,
|
|
77
|
+
message: action.result,
|
|
78
|
+
save: action.run,
|
|
79
|
+
setField: (field, value) => {
|
|
80
|
+
if (disabled || immutable || action.isPending) return;
|
|
81
|
+
action.reset();
|
|
82
|
+
setState((current) => ({
|
|
83
|
+
...current,
|
|
84
|
+
fields: { ...current.fields, [field]: value },
|
|
85
|
+
}));
|
|
86
|
+
},
|
|
87
|
+
selectMeter: (meterId) => {
|
|
88
|
+
if (disabled || action.isPending) return;
|
|
89
|
+
const meter = props.meters.find((item) => item.id === meterId);
|
|
90
|
+
if (meterId && !meter?.active) return;
|
|
91
|
+
action.reset();
|
|
92
|
+
setState({
|
|
93
|
+
key,
|
|
94
|
+
meterId,
|
|
95
|
+
fields: configurationFields(meter?.configuration ?? props.initial),
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|