@fantastic.dev/better-stripe 0.78.0 → 0.79.1
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/core/accountLinks.d.ts +13 -0
- package/dist/client/core/accountLinks.d.ts.map +1 -1
- package/dist/client/core/accountLinks.js +65 -22
- 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 +23 -0
- package/dist/client/core/billingPortalFlow.d.ts.map +1 -1
- package/dist/client/core/billingPortalFlow.js +5 -2
- package/dist/client/core/billingPortalFlow.js.map +1 -1
- 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/docs/client-api.md +72 -0
- package/package.json +1 -1
- package/src/client/core/accountLinks.test.ts +4 -4
- package/src/client/core/accountLinks.ts +102 -24
- package/src/client/core/billingPortal.test.ts +8 -6
- package/src/client/core/billingPortalClient.ts +5 -1
- package/src/client/core/billingPortalFlow.test.ts +26 -8
- package/src/client/core/billingPortalFlow.ts +5 -2
- 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 +4 -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/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
|
@@ -74,7 +74,9 @@ export type {
|
|
|
74
74
|
UpdateBillingPortalConfigurationOptions,
|
|
75
75
|
CreateBillingPortalSessionOptions,
|
|
76
76
|
BillingPortalSubscriptionUpdateFlow,
|
|
77
|
+
BillingPortalSessionRequestIdentity,
|
|
77
78
|
} from "./core/accountLinks.js";
|
|
79
|
+
export { prepareBillingPortalSessionRequestIdentity } from "./core/accountLinks.js";
|
|
78
80
|
export type {
|
|
79
81
|
AdoptProductOptions,
|
|
80
82
|
EnsurePriceOptions,
|
|
@@ -342,8 +344,8 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
342
344
|
return registerClientRoutes(this, http, config);
|
|
343
345
|
}
|
|
344
346
|
|
|
345
|
-
protected stripe(): Stripe {
|
|
346
|
-
return getStripeClient(this.apiKey, this.stripeApiVersion);
|
|
347
|
+
protected stripe(policy?: { singleAttempt?: boolean }): Stripe {
|
|
348
|
+
return getStripeClient(this.apiKey, this.stripeApiVersion, policy);
|
|
347
349
|
}
|
|
348
350
|
|
|
349
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
|
+
}
|