Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: update invoice upcoming

Marc Seitz20d17574d9a1
diff --git a/ee/stripe/webhooks/invoice-upcoming.ts b/ee/stripe/webhooks/invoice-upcoming.ts
index 0a063959..8618d8b8 100644
--- a/ee/stripe/webhooks/invoice-upcoming.ts
+++ b/ee/stripe/webhooks/invoice-upcoming.ts
@@ -1,10 +1,15 @@
-import { tasks } from "@trigger.dev/sdk/v3";
+import { NextApiResponse } from "next";
+
+import { sendSubscriptionRenewalReminderEmail } from "@/ee/features/billing/renewal-reminder/lib/send-subscription-renewal-reminder";
import Stripe from "stripe";
-import { sendRenewalReminderEmailTask } from "@/lib/trigger/send-renewal-reminder";
import { log } from "@/lib/utils";
-export async function invoiceUpcoming(event: Stripe.Event) {
+export async function invoiceUpcoming(
+ event: Stripe.Event,
+ res: NextApiResponse,
+ isOldAccount: boolean = false,
+) {
const invoice = event.data.object as Stripe.Invoice;
// Only process invoices for yearly renewals
@@ -14,7 +19,7 @@ export async function invoiceUpcoming(event: Stripe.Event) {
message: "No line items found in invoice.upcoming event",
type: "info",
});
- return;
+ return res.status(200).json({ received: true });
}
// Check if this is a yearly subscription
@@ -52,47 +57,28 @@ export async function invoiceUpcoming(event: Stripe.Event) {
const renewalTimestamp = invoice.period_end;
const renewalDate = new Date(renewalTimestamp * 1000);
const formattedRenewalDate = renewalDate.toLocaleDateString("en-US", {
- year: "numeric",
month: "long",
day: "numeric",
});
- // Format amount (convert from cents to dollars/euros)
- const amount = (invoice.amount_due / 100).toFixed(2);
- const currency = invoice.currency;
-
- // Calculate delay: webhook arrives ~7 days before, we want to send 3 days before
- // So we delay by 4 days
- const delayInMs = 4 * 24 * 60 * 60 * 1000; // 4 days in milliseconds
- const scheduledTime = new Date(Date.now() + delayInMs);
-
try {
- // Schedule the task to run in 4 days
- const handle = await tasks.trigger(
- sendRenewalReminderEmailTask.id,
- {
- customerEmail,
- customerName,
- renewalDate: formattedRenewalDate,
- amount,
- currency,
- },
- {
- delay: delayInMs,
- },
- );
+ // send email immediately
+ await sendSubscriptionRenewalReminderEmail({
+ customerEmail,
+ customerName,
+ renewalDate: formattedRenewalDate,
+ isOldAccount,
+ });
await log({
- message: `Scheduled renewal reminder email for ${customerEmail} to be sent on ${scheduledTime.toISOString()}. Renewal date: ${formattedRenewalDate}`,
+ message: `Renewal reminder email sent for ${customerEmail}. Renewal date: ${formattedRenewalDate}`,
type: "info",
});
-
- return handle;
} catch (error) {
await log({
- message: `Failed to schedule renewal reminder email for ${customerEmail}: ${error}`,
+ message: `Failed to send renewal reminder email for ${customerEmail}: ${error}`,
type: "error",
});
- throw error;
+ return res.status(200).json({ received: true });
}
}
diff --git a/pages/api/stripe/webhook-old.ts b/pages/api/stripe/webhook-old.ts
index 47c4bd9d..f29aa47f 100644
--- a/pages/api/stripe/webhook-old.ts
+++ b/pages/api/stripe/webhook-old.ts
@@ -4,6 +4,7 @@ import { stripeInstance } from "@/ee/stripe";
import { checkoutSessionCompleted } from "@/ee/stripe/webhooks/checkout-session-completed";
import { customerSubscriptionDeleted } from "@/ee/stripe/webhooks/customer-subscription-deleted";
import { customerSubsciptionUpdated } from "@/ee/stripe/webhooks/customer-subscription-updated";
+import { invoiceUpcoming } from "@/ee/stripe/webhooks/invoice-upcoming";
import { Readable } from "node:stream";
import type Stripe from "stripe";
@@ -64,6 +65,9 @@ export default async function webhookHandler(
case "customer.subscription.deleted":
await customerSubscriptionDeleted(event, res);
break;
+ case "invoice.upcoming":
+ await invoiceUpcoming(event, res, true);
+ break;
}
} catch (error) {
await log({
diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.ts
index d3849373..d681fa7a 100644
--- a/pages/api/stripe/webhook.ts
+++ b/pages/api/stripe/webhook.ts
@@ -74,7 +74,7 @@ export default async function webhookHandler(
await processPaymentFailure(event);
break;
case "invoice.upcoming":
- await invoiceUpcoming(event);
+ await invoiceUpcoming(event, res);
break;
}
} catch (error) {