Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cloud/src/server/billing-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
classifyCheckout,
complimentaryAfterSubscription,
instanceTransition,
isOwnCheckout,
isReleasableReservation,
isProcessedCheckout,
isSubscriptionLive,
Expand Down Expand Up @@ -147,3 +148,16 @@ describe("complimentary instances", () => {
expect(isReleasableReservation({ status: "running", complimentary: false, createdAt: old }, now, ttl)).toBe(false);
});
});

describe("isOwnCheckout (shared Stripe account)", () => {
it("accepts checkouts created by AutoSEO Cloud", () => {
expect(isOwnCheckout({ metadata: { instanceId: "i1", userId: "u1", slug: "acme" } })).toBe(true);
});
it.each([
["another product's checkout", { metadata: { plan: "monthly", license_key: "GM-XXXX" } }],
["no metadata", { metadata: null }],
["instance without user", { metadata: { instanceId: "i1" } }],
])("ignores %s", (_, session) => {
expect(isOwnCheckout(session)).toBe(false);
});
});
8 changes: 8 additions & 0 deletions cloud/src/server/billing-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ export function isReleasableReservation(
): boolean {
return instance.status === "pending_payment" && !instance.complimentary && now - instance.createdAt.getTime() > ttlMs;
}

/**
* The Stripe account is shared with other Codext products. Only checkouts created here carry an instanceId and
* userId; any other checkout belongs to another product and must be left alone (never canceled as an orphan).
*/
export function isOwnCheckout(session: { metadata?: Record<string, string> | null }): boolean {
return Boolean(session.metadata?.instanceId && session.metadata?.userId);
}
5 changes: 5 additions & 0 deletions cloud/src/server/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
classifyCheckout,
complimentaryAfterSubscription,
instanceTransition,
isOwnCheckout,
isProcessedCheckout,
isSubscriptionLive,
shouldApplySubscription,
Expand Down Expand Up @@ -801,6 +802,10 @@ async function recreateReservation(session: Stripe.Checkout.Session): Promise<In
*/
export async function processCheckoutSession(session: Stripe.Checkout.Session, source: string): Promise<Instance | null> {
if (session.mode !== "subscription" || session.status !== "complete") return null;
if (!isOwnCheckout(session)) {
await logEvent("stripe.checkout_foreign", { data: { sessionId: session.id, source } });
return null;
}
const instanceId = session.metadata?.instanceId;
const existing = instanceId ? await getInstance(instanceId) : null;
const instance = existing && existing.status !== "deleted" ? existing : await recreateReservation(session);
Expand Down
Loading