@fantastic.dev/better-stripe 0.77.0 → 0.79.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 +12 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/core/accountLinks.d.ts +17 -1
- package/dist/client/core/accountLinks.d.ts.map +1 -1
- package/dist/client/core/accountLinks.js +72 -11
- package/dist/client/core/accountLinks.js.map +1 -1
- package/dist/client/core/billingPortalClient.d.ts.map +1 -1
- package/dist/client/core/billingPortalClient.js +1 -1
- package/dist/client/core/billingPortalClient.js.map +1 -1
- package/dist/client/core/billingPortalFlow.d.ts +69 -0
- package/dist/client/core/billingPortalFlow.d.ts.map +1 -0
- package/dist/client/core/billingPortalFlow.js +81 -0
- package/dist/client/core/billingPortalFlow.js.map +1 -0
- package/dist/client/core/idempotency.d.ts.map +1 -1
- package/dist/client/core/idempotency.js +4 -1
- package/dist/client/core/idempotency.js.map +1 -1
- package/dist/client/helpers.d.ts +3 -1
- package/dist/client/helpers.d.ts.map +1 -1
- package/dist/client/helpers.js +13 -2
- package/dist/client/helpers.js.map +1 -1
- package/dist/client/index.d.ts +5 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/invoiceReads.d.ts +3 -1
- package/dist/client/invoiceReads.d.ts.map +1 -1
- package/dist/client/singleAttemptHttpClient.d.ts +11 -0
- package/dist/client/singleAttemptHttpClient.d.ts.map +1 -0
- package/dist/client/singleAttemptHttpClient.js +51 -0
- package/dist/client/singleAttemptHttpClient.js.map +1 -0
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/react/workflows/Billing.d.ts.map +1 -1
- package/dist/react/workflows/Billing.js +6 -4
- package/dist/react/workflows/Billing.js.map +1 -1
- package/docs/client-api.md +123 -0
- package/package.json +1 -1
- package/src/client/core/accountLinks.test.ts +4 -4
- package/src/client/core/accountLinks.ts +120 -13
- package/src/client/core/billingPortal.test.ts +8 -6
- package/src/client/core/billingPortalClient.ts +5 -1
- package/src/client/core/billingPortalFlow.test.ts +349 -0
- package/src/client/core/billingPortalFlow.ts +98 -0
- package/src/client/core/billingPortalIdentity.test.ts +418 -0
- package/src/client/core/idempotency.ts +6 -1
- package/src/client/customer-preview.test.ts +188 -7
- package/src/client/helpers.ts +15 -1
- package/src/client/index.ts +5 -2
- package/src/client/invoiceReads.ts +1 -1
- package/src/client/singleAttemptHttpClient.test.ts +70 -0
- package/src/client/singleAttemptHttpClient.ts +58 -0
- package/src/react/workflows/Billing.tsx +15 -8
- package/src/react/workflows/BillingPortal.test.tsx +101 -0
package/src/client/helpers.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
OptionalRestArgs,
|
|
5
5
|
} from "convex/server";
|
|
6
6
|
import Stripe from "stripe";
|
|
7
|
+
import { SingleAttemptHttpClient } from "./singleAttemptHttpClient.js";
|
|
7
8
|
|
|
8
9
|
import type { ComponentApi } from "../component/_generated/component.js";
|
|
9
10
|
import { STRIPE_API_VERSION } from "./constants.js";
|
|
@@ -42,13 +43,26 @@ const _clients = new Map<string, Stripe>();
|
|
|
42
43
|
export function getStripeClient(
|
|
43
44
|
secretKey: string,
|
|
44
45
|
apiVersion?: string,
|
|
46
|
+
policy?: { singleAttempt?: boolean },
|
|
45
47
|
): Stripe {
|
|
46
48
|
const version = (apiVersion as StripeApiVersion) || STRIPE_API_VERSION;
|
|
47
|
-
const cacheKey =
|
|
49
|
+
const cacheKey = JSON.stringify([
|
|
50
|
+
secretKey,
|
|
51
|
+
version,
|
|
52
|
+
policy?.singleAttempt === true,
|
|
53
|
+
]);
|
|
48
54
|
let client = _clients.get(cacheKey);
|
|
49
55
|
if (!client) {
|
|
50
56
|
client = new Stripe(secretKey, {
|
|
51
57
|
apiVersion: version,
|
|
58
|
+
...(policy?.singleAttempt
|
|
59
|
+
? {
|
|
60
|
+
maxNetworkRetries: 0,
|
|
61
|
+
httpClient: new SingleAttemptHttpClient(
|
|
62
|
+
Stripe.createFetchHttpClient(),
|
|
63
|
+
),
|
|
64
|
+
}
|
|
65
|
+
: {}),
|
|
52
66
|
});
|
|
53
67
|
_clients.set(cacheKey, client);
|
|
54
68
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -73,7 +73,10 @@ export type {
|
|
|
73
73
|
RetrieveBillingPortalConfigurationOptions,
|
|
74
74
|
UpdateBillingPortalConfigurationOptions,
|
|
75
75
|
CreateBillingPortalSessionOptions,
|
|
76
|
+
BillingPortalSubscriptionUpdateFlow,
|
|
77
|
+
BillingPortalSessionRequestIdentity,
|
|
76
78
|
} from "./core/accountLinks.js";
|
|
79
|
+
export { prepareBillingPortalSessionRequestIdentity } from "./core/accountLinks.js";
|
|
77
80
|
export type {
|
|
78
81
|
AdoptProductOptions,
|
|
79
82
|
EnsurePriceOptions,
|
|
@@ -341,8 +344,8 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
341
344
|
return registerClientRoutes(this, http, config);
|
|
342
345
|
}
|
|
343
346
|
|
|
344
|
-
protected stripe(): Stripe {
|
|
345
|
-
return getStripeClient(this.apiKey, this.stripeApiVersion);
|
|
347
|
+
protected stripe(policy?: { singleAttempt?: boolean }): Stripe {
|
|
348
|
+
return getStripeClient(this.apiKey, this.stripeApiVersion, policy);
|
|
346
349
|
}
|
|
347
350
|
|
|
348
351
|
// Static defaults (preserved for backwards compatibility)
|
|
@@ -10,7 +10,7 @@ import * as checkoutPages from "./billing/checkoutPages.js";
|
|
|
10
10
|
|
|
11
11
|
/** Provider billing history readers inherited by BetterStripe; hosts authorize their caller. */
|
|
12
12
|
export abstract class InvoiceReadClient extends ReadContractClient {
|
|
13
|
-
protected abstract stripe(): Stripe;
|
|
13
|
+
protected abstract stripe(policy?: { singleAttempt?: boolean }): Stripe;
|
|
14
14
|
|
|
15
15
|
/** Required-actor observation only; the host authorizes community scope and revalidates creation. */
|
|
16
16
|
async getRefundContext(ctx: RunCtx, opts: RefundContextOptions) {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { expect, it, vi } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
SingleAttemptHttpClient,
|
|
5
|
+
singleAttemptPortalError,
|
|
6
|
+
} from "./singleAttemptHttpClient.js";
|
|
7
|
+
|
|
8
|
+
const args: Parameters<Stripe.HttpClient["makeRequest"]> = [
|
|
9
|
+
"api.stripe.test",
|
|
10
|
+
"443",
|
|
11
|
+
"/v1/billing_portal/sessions",
|
|
12
|
+
"POST",
|
|
13
|
+
{ "Idempotency-Key": "portal:retained", "Stripe-Account": "acct_original" },
|
|
14
|
+
"customer_account=acct_customer",
|
|
15
|
+
"https",
|
|
16
|
+
1000,
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
it.each(Stripe.HttpClient.CONNECTION_CLOSED_ERROR_CODES)(
|
|
20
|
+
"preserves %s as the cause without giving Stripe a retry signal",
|
|
21
|
+
async (code) => {
|
|
22
|
+
const original = Object.assign(new Error("controlled failure"), { code });
|
|
23
|
+
const makeRequest = vi.fn().mockRejectedValue(original);
|
|
24
|
+
const adapter = new SingleAttemptHttpClient({
|
|
25
|
+
getClientName: () => "controlled",
|
|
26
|
+
makeRequest,
|
|
27
|
+
});
|
|
28
|
+
const failure = await adapter
|
|
29
|
+
.makeRequest(...args)
|
|
30
|
+
.catch((error: unknown) => error);
|
|
31
|
+
expect(failure).toBeInstanceOf(Error);
|
|
32
|
+
expect(failure).toMatchObject({ cause: original, transportCode: code });
|
|
33
|
+
expect(failure).not.toHaveProperty("code");
|
|
34
|
+
expect(original.code).toBe(code);
|
|
35
|
+
expect(makeRequest).toHaveBeenCalledExactlyOnceWith(...args);
|
|
36
|
+
if (!(failure instanceof Error))
|
|
37
|
+
throw new Error("Expected a transport error");
|
|
38
|
+
const stripeError = new Stripe.errors.StripeConnectionError({
|
|
39
|
+
message: "Connection failed",
|
|
40
|
+
detail: failure,
|
|
41
|
+
});
|
|
42
|
+
expect(singleAttemptPortalError(stripeError)).toEqual({
|
|
43
|
+
type: "StripeConnectionError",
|
|
44
|
+
code,
|
|
45
|
+
message: expect.stringContaining("unknown outcome"),
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
it("leaves timeouts, provider responses and unbranded error details unchanged", async () => {
|
|
51
|
+
const timeout = Object.assign(new Error("timeout"), { code: "ETIMEDOUT" });
|
|
52
|
+
const response = { getStatusCode: () => 409 } as Stripe.HttpClientResponse;
|
|
53
|
+
const makeRequest = vi
|
|
54
|
+
.fn()
|
|
55
|
+
.mockRejectedValueOnce(timeout)
|
|
56
|
+
.mockResolvedValueOnce(response);
|
|
57
|
+
const adapter = new SingleAttemptHttpClient({
|
|
58
|
+
getClientName: () => "controlled",
|
|
59
|
+
makeRequest,
|
|
60
|
+
});
|
|
61
|
+
await expect(adapter.makeRequest(...args)).rejects.toBe(timeout);
|
|
62
|
+
expect(await adapter.makeRequest(...args)).toBe(response);
|
|
63
|
+
const spoof = {
|
|
64
|
+
type: "StripeConnectionError",
|
|
65
|
+
detail: { transportCode: "ECONNRESET" },
|
|
66
|
+
};
|
|
67
|
+
expect(singleAttemptPortalError(spoof)).toBe(spoof);
|
|
68
|
+
expect(singleAttemptPortalError(null)).toBeNull();
|
|
69
|
+
expect(adapter.getClientName()).toBe("single-attempt:controlled");
|
|
70
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
|
|
3
|
+
/** A transport failure with an unknown remote outcome, never a retry signal. */
|
|
4
|
+
class SingleAttemptConnectionClosed extends Error {
|
|
5
|
+
constructor(
|
|
6
|
+
readonly transportCode: string,
|
|
7
|
+
readonly cause: unknown,
|
|
8
|
+
) {
|
|
9
|
+
super(
|
|
10
|
+
"The connection closed with an unknown outcome. Automatic replay was prevented; retain the original request identity.",
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Portal-only adapter for Stripe's closed-connection retry exception. */
|
|
16
|
+
export class SingleAttemptHttpClient implements Stripe.HttpClient {
|
|
17
|
+
constructor(private readonly delegate: Stripe.HttpClient) {}
|
|
18
|
+
|
|
19
|
+
getClientName(): string {
|
|
20
|
+
return `single-attempt:${this.delegate.getClientName()}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async makeRequest(
|
|
24
|
+
...args: Parameters<Stripe.HttpClient["makeRequest"]>
|
|
25
|
+
): ReturnType<Stripe.HttpClient["makeRequest"]> {
|
|
26
|
+
try {
|
|
27
|
+
return await this.delegate.makeRequest(...args);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (
|
|
30
|
+
typeof error === "object" &&
|
|
31
|
+
error !== null &&
|
|
32
|
+
"code" in error &&
|
|
33
|
+
typeof error.code === "string" &&
|
|
34
|
+
Stripe.HttpClient.CONNECTION_CLOSED_ERROR_CODES.includes(error.code)
|
|
35
|
+
)
|
|
36
|
+
// Stripe retries these codes once even with maxNetworkRetries=0.
|
|
37
|
+
// Keep the original as cause, without exposing .code to its retry loop.
|
|
38
|
+
throw new SingleAttemptConnectionClosed(error.code, error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Recover the safe error code only after Stripe's request loop has finished. */
|
|
45
|
+
export function singleAttemptPortalError(error: unknown): unknown {
|
|
46
|
+
if (
|
|
47
|
+
typeof error === "object" &&
|
|
48
|
+
error !== null &&
|
|
49
|
+
"detail" in error &&
|
|
50
|
+
error.detail instanceof SingleAttemptConnectionClosed
|
|
51
|
+
)
|
|
52
|
+
return {
|
|
53
|
+
type: "StripeConnectionError",
|
|
54
|
+
code: error.detail.transportCode,
|
|
55
|
+
message: error.detail.message,
|
|
56
|
+
};
|
|
57
|
+
return error;
|
|
58
|
+
}
|
|
@@ -183,15 +183,22 @@ export type BillingPortalProps = {
|
|
|
183
183
|
}) => ReactNode;
|
|
184
184
|
};
|
|
185
185
|
export function BillingPortal(props: BillingPortalProps) {
|
|
186
|
-
const action = useWorkflowAction(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
186
|
+
const action = useWorkflowAction(
|
|
187
|
+
async () => {
|
|
188
|
+
const result = await props.createSession();
|
|
189
|
+
const url = typeof result === "string" ? result : result.url;
|
|
190
|
+
if (!url) throw new Error("Stripe did not return a portal URL.");
|
|
191
|
+
return url;
|
|
192
|
+
},
|
|
193
|
+
props.scopeKey,
|
|
194
|
+
(url) => {
|
|
195
|
+
(props.onNavigate ?? ((nextUrl) => window.location.assign(nextUrl)))(url);
|
|
196
|
+
},
|
|
197
|
+
);
|
|
198
|
+
const open = async (): Promise<true | undefined> =>
|
|
199
|
+
(await action.run()) === undefined ? undefined : true;
|
|
193
200
|
const state = {
|
|
194
|
-
open
|
|
201
|
+
open,
|
|
195
202
|
isPending: action.isPending,
|
|
196
203
|
error: action.error,
|
|
197
204
|
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
5
|
+
import { BillingPortal, type BillingPortalProps } from "./Billing.js";
|
|
6
|
+
afterEach(cleanup);
|
|
7
|
+
type PortalState = Parameters<NonNullable<BillingPortalProps["children"]>>[0];
|
|
8
|
+
|
|
9
|
+
it.each(["scope", "unmount"])(
|
|
10
|
+
"does not navigate after a pending portal's %s changes",
|
|
11
|
+
async (change) => {
|
|
12
|
+
let release!: (url: string) => void;
|
|
13
|
+
const createSession = vi.fn(
|
|
14
|
+
() =>
|
|
15
|
+
new Promise<string>((resolve) => {
|
|
16
|
+
release = resolve;
|
|
17
|
+
}),
|
|
18
|
+
);
|
|
19
|
+
const onNavigate = vi.fn();
|
|
20
|
+
let state!: PortalState;
|
|
21
|
+
const child = (value: PortalState) => {
|
|
22
|
+
state = value;
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
const view = render(
|
|
26
|
+
<BillingPortal
|
|
27
|
+
scopeKey="actor-a:sub-a:target-a"
|
|
28
|
+
createSession={createSession}
|
|
29
|
+
onNavigate={onNavigate}
|
|
30
|
+
>
|
|
31
|
+
{child}
|
|
32
|
+
</BillingPortal>,
|
|
33
|
+
);
|
|
34
|
+
let pending!: Promise<true | undefined>;
|
|
35
|
+
act(() => {
|
|
36
|
+
pending = state.open();
|
|
37
|
+
});
|
|
38
|
+
expect(createSession).toHaveBeenCalledOnce();
|
|
39
|
+
if (change === "scope")
|
|
40
|
+
view.rerender(
|
|
41
|
+
<BillingPortal
|
|
42
|
+
scopeKey="actor-b:sub-b:target-a"
|
|
43
|
+
createSession={createSession}
|
|
44
|
+
onNavigate={onNavigate}
|
|
45
|
+
>
|
|
46
|
+
{child}
|
|
47
|
+
</BillingPortal>,
|
|
48
|
+
);
|
|
49
|
+
else view.unmount();
|
|
50
|
+
await act(async () => {
|
|
51
|
+
release("https://billing.stripe.test/old");
|
|
52
|
+
expect(await pending).toBeUndefined();
|
|
53
|
+
});
|
|
54
|
+
expect(onNavigate).not.toHaveBeenCalled();
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
it("navigates once on current success and preserves the true return contract", async () => {
|
|
59
|
+
const onNavigate = vi.fn();
|
|
60
|
+
let state!: PortalState;
|
|
61
|
+
render(
|
|
62
|
+
<BillingPortal
|
|
63
|
+
scopeKey="actor:original:target"
|
|
64
|
+
createSession={async () => ({
|
|
65
|
+
url: "https://billing.stripe.test/current",
|
|
66
|
+
})}
|
|
67
|
+
onNavigate={onNavigate}
|
|
68
|
+
>
|
|
69
|
+
{(value) => {
|
|
70
|
+
state = value;
|
|
71
|
+
return null;
|
|
72
|
+
}}
|
|
73
|
+
</BillingPortal>,
|
|
74
|
+
);
|
|
75
|
+
await act(async () => {
|
|
76
|
+
expect(await state.open()).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
expect(onNavigate).toHaveBeenCalledExactlyOnceWith(
|
|
79
|
+
"https://billing.stripe.test/current",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("keeps provider failure visible without navigating or fabricating success", async () => {
|
|
84
|
+
const onNavigate = vi.fn();
|
|
85
|
+
render(
|
|
86
|
+
<BillingPortal
|
|
87
|
+
scopeKey="actor:original:target"
|
|
88
|
+
createSession={async () => {
|
|
89
|
+
throw new Error("Confirmation unavailable");
|
|
90
|
+
}}
|
|
91
|
+
onNavigate={onNavigate}
|
|
92
|
+
/>,
|
|
93
|
+
);
|
|
94
|
+
await userEvent.click(
|
|
95
|
+
screen.getByRole("button", { name: "Open Stripe Billing Portal" }),
|
|
96
|
+
);
|
|
97
|
+
expect((await screen.findByRole("alert")).textContent).toContain(
|
|
98
|
+
"Confirmation unavailable",
|
|
99
|
+
);
|
|
100
|
+
expect(onNavigate).not.toHaveBeenCalled();
|
|
101
|
+
});
|