-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from giselles-ai/feat/charge-last-month-usage…
…-on-subscription-deletion feat(billing): Implement last month usage charging on subscription cancellation
- Loading branch information
Showing
2 changed files
with
24 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { stripe } from "@/services/external/stripe"; | ||
import type Stripe from "stripe"; | ||
|
||
export async function handleInvoiceCreation(invoice: Stripe.Invoice) { | ||
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") { | ||
return; | ||
} | ||
|
||
// When a subscription is canceled, we should charge for usage-based billing from the previous billing cycle. The final invoice, which includes these charges, will be automatically created but will not be processed for payment. Therefore, we need to handle this case manually. | ||
await stripe.invoices.pay(invoice.id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters