|
1 | 1 | import { stripe } from "@/services/external/stripe";
|
2 | 2 | import { upsertSubscription } from "@/services/external/stripe/actions/upsert-subscription";
|
3 | 3 | import type Stripe from "stripe";
|
| 4 | +import { handleInvoiceCreation } from "./handle-invoice-creation"; |
4 | 5 | import { handleSubscriptionCancellation } from "./handle-subscription-cancellation";
|
5 | 6 | import { handleSubscriptionCycleInvoice } from "./handle-subscription-cycle-invoice";
|
6 | 7 |
|
@@ -85,35 +86,7 @@ export async function POST(req: Request) {
|
85 | 86 | break;
|
86 | 87 |
|
87 | 88 | case "invoice.created": {
|
88 |
| - console.log(`🔔 Invoice created: ${event.data.object.id}`); |
89 |
| - |
90 |
| - const invoice = event.data.object; |
91 |
| - |
92 |
| - if (!invoice.subscription || typeof invoice.subscription !== "string") { |
93 |
| - throw new Error( |
94 |
| - "Invoice is missing a subscription ID. Please check the invoice data.", |
95 |
| - ); |
96 |
| - } |
97 |
| - |
98 |
| - const subscription = await stripe.subscriptions.retrieve( |
99 |
| - invoice.subscription, |
100 |
| - ); |
101 |
| - |
102 |
| - if (subscription.status === "canceled") { |
103 |
| - try { |
104 |
| - await stripe.invoices.finalizeInvoice(invoice.id); |
105 |
| - } catch (error) { |
106 |
| - console.error(`Error finalizing invoice ${invoice.id}:`, error); |
107 |
| - throw new Error("Failed to finalize invoice."); |
108 |
| - } |
109 |
| - |
110 |
| - try { |
111 |
| - await stripe.invoices.pay(invoice.id); |
112 |
| - } catch (error) { |
113 |
| - console.error(`Error paying invoice ${invoice.id}:`, error); |
114 |
| - throw new Error("Failed to pay invoice."); |
115 |
| - } |
116 |
| - } |
| 89 | + await handleInvoiceCreation(event.data.object); |
117 | 90 |
|
118 | 91 | // TODO: This block will be removed in the other issue.
|
119 | 92 | if (event.data.object.billing_reason === "subscription_cycle") {
|
|
0 commit comments