Skip to content

Commit

Permalink
refactor(webhook): Simplify invoice creation handling for canceled su…
Browse files Browse the repository at this point in the history
…bscriptions

Remove redundant comments and consolidate invoice handling logic:
- Remove TODO comment and old conditional structure
- Replace with cleaner invoice validation check
- Prepare for implementing proper subscription cancellation processing

The changes streamline the webhook handler code while maintaining
functionality for canceled subscription scenarios.
  • Loading branch information
gentamura committed Jan 7, 2025
1 parent 0047639 commit 1bcb884
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions app/webhooks/stripe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,43 @@ export async function POST(req: Request) {
await handleSubscriptionCancellation(event.data.object);
break;

case "invoice.created":
case "invoice.created": {
console.log(`🔔 Invoice created: ${event.data.object.id}`);

// TODO: Skip for now - will be handled when implementing subscription cancellation invoice processing
if (
event.data.object.subscription &&
typeof event.data.object.subscription === "string"
) {
const subscriptionId = event.data.object.subscription;
const subscription =
await stripe.subscriptions.retrieve(subscriptionId);

if (subscription.status === "canceled") {
console.log(
"Skipping processing for canceled subscription invoice: ",
subscriptionId,
);
break;
const invoice = event.data.object;

if (!invoice.subscription || typeof invoice.subscription !== "string") {
throw new Error(
"Invoice is missing a subscription ID. Please check the invoice data.",
);
}

const subscription = await stripe.subscriptions.retrieve(
invoice.subscription,
);

if (subscription.status === "canceled") {
try {
await stripe.invoices.finalizeInvoice(invoice.id);
} catch (error) {
console.error(`Error finalizing invoice ${invoice.id}:`, error);
throw new Error("Failed to finalize invoice.");
}

try {
await stripe.invoices.pay(invoice.id);
} catch (error) {
console.error(`Error paying invoice ${invoice.id}:`, error);
throw new Error("Failed to pay invoice.");
}
}

// TODO: This block will be removed in the other issue.
if (event.data.object.billing_reason === "subscription_cycle") {
await handleSubscriptionCycleInvoice(event.data.object);
}
break;
}

default:
throw new Error("Unhandled relevant event!");
Expand Down

0 comments on commit 1bcb884

Please sign in to comment.