Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(api): associate Signed BAA subscriptions from creation events

Richie McIlroy458af3df12b0
diff --git a/apps/web/__tests__/unit/signed-baa-webhook.test.ts b/apps/web/__tests__/unit/signed-baa-webhook.test.ts
index 46684e0f3..3d4337823 100644
--- a/apps/web/__tests__/unit/signed-baa-webhook.test.ts
+++ b/apps/web/__tests__/unit/signed-baa-webhook.test.ts
@@ -145,6 +145,31 @@ describe("Stripe webhook — Signed BAA", () => {
expect(mockStripe.customers.retrieve).not.toHaveBeenCalled();
});
+ it("associates the BAA subscription from its creation event", async () => {
+ mockStripe.webhooks.constructEvent.mockReturnValue({
+ type: "customer.subscription.created",
+ data: {
+ object: {
+ id: "sub_baa_new",
+ status: "active",
+ metadata: {
+ type: "signed_baa",
+ organizationId: "org-1",
+ },
+ },
+ },
+ });
+
+ const res = await POST(makeWebhookRequest());
+
+ expect(res.status).toBe(200);
+ expect(mockDbChain.set).toHaveBeenCalledWith({
+ status: "active",
+ stripeSubscriptionId: "sub_baa_new",
+ });
+ expect(mockStripe.customers.retrieve).not.toHaveBeenCalled();
+ });
+
it("cancels Signed BAA when Pro becomes unpaid", async () => {
mockStripe.webhooks.constructEvent.mockReturnValue({
type: "customer.subscription.updated",
diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.ts
index 93ec7ddc5..afbeba3e0 100644
--- a/apps/web/app/api/webhooks/stripe/route.ts
+++ b/apps/web/app/api/webhooks/stripe/route.ts
@@ -19,6 +19,7 @@ import { trackServerEvent } from "@/lib/server-analytics";
const relevantEvents = new Set([
"checkout.session.completed",
"checkout.session.async_payment_succeeded",
+ "customer.subscription.created",
"customer.subscription.updated",
"customer.subscription.deleted",
"invoice.payment_failed",
@@ -444,6 +445,17 @@ export const POST = async (req: Request) => {
}
}
+ if (event.type === "customer.subscription.created") {
+ const subscription = event.data.object as Stripe.Subscription;
+ // Recovers purchases whose create response was lost: the purchase
+ // action may have reverted its record without a subscription ID, and
+ // no updated event fires until the next subscription change, so the
+ // creation event is the only reliable association signal.
+ if (isSignedBaaSubscription(subscription)) {
+ return await syncSignedBaaStatus(subscription);
+ }
+ }
+
if (event.type === "customer.subscription.updated") {
console.log("Processing customer.subscription.updated event");
const subscription = event.data.object as Stripe.Subscription;