Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Handle `charge.failed` webhook event from Stripe

Kiran K9d11aa8ebc4a
diff --git a/apps/web/app/api/stripe/webhook/charge-failed.ts b/apps/web/app/api/stripe/webhook/charge-failed.ts
new file mode 100644
index 0000000000..31363f5eaa
--- /dev/null
+++ b/apps/web/app/api/stripe/webhook/charge-failed.ts
@@ -0,0 +1,39 @@
+import { prisma } from "@dub/prisma";
+import Stripe from "stripe";
+
+export async function chargeFailed(event: Stripe.Event) {
+ const charge = event.data.object as Stripe.Charge;
+
+ const { transfer_group: invoiceId, failure_message: failedReason } = charge;
+
+ if (!invoiceId) {
+ console.log("No transfer group found, skipping...");
+ return;
+ }
+
+ // Mark the invoice as failed
+ await prisma.invoice.update({
+ where: {
+ id: invoiceId,
+ },
+ data: {
+ status: "failed",
+ failedReason,
+ },
+ });
+
+ // Mark the payouts as pending again
+ await prisma.payout.updateMany({
+ where: {
+ invoiceId,
+ },
+ data: {
+ status: "pending",
+ userId: null,
+ invoiceId: null,
+ },
+ });
+
+ // TODO:
+ // Send an email to the program owner about the failure
+}
diff --git a/apps/web/app/api/stripe/webhook/route.ts b/apps/web/app/api/stripe/webhook/route.ts
index e4fd4d2ca6..af4aa4c3ee 100644
--- a/apps/web/app/api/stripe/webhook/route.ts
+++ b/apps/web/app/api/stripe/webhook/route.ts
@@ -2,6 +2,7 @@ import { stripe } from "@/lib/stripe";
import { log } from "@dub/utils";
import { NextResponse } from "next/server";
import Stripe from "stripe";
+import { chargeFailed } from "./charge-failed";
import { chargeSucceeded } from "./charge-succeeded";
import { checkoutSessionCompleted } from "./checkout-session-completed";
import { customerSubscriptionDeleted } from "./customer-subscription-deleted";
@@ -10,6 +11,7 @@ import { invoicePaymentFailed } from "./invoice-payment-failed";
const relevantEvents = new Set([
"charge.succeeded",
+ "charge.failed",
"checkout.session.completed",
"customer.subscription.updated",
"customer.subscription.deleted",
@@ -38,11 +40,15 @@ export const POST = async (req: Request) => {
status: 200,
});
}
+
try {
switch (event.type) {
case "charge.succeeded":
await chargeSucceeded(event);
break;
+ case "charge.failed":
+ await chargeFailed(event);
+ break;
case "checkout.session.completed":
await checkoutSessionCompleted(event);
break;
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts
index 8bfcdcb736..2a86f9a794 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts
@@ -55,8 +55,8 @@ const payoutInvoices = async (workspaceId: string) => {
id: true,
total: true,
createdAt: true,
- receiptUrl: true,
status: true,
+ failedReason: true,
},
orderBy: {
createdAt: "desc",
@@ -67,7 +67,10 @@ const payoutInvoices = async (workspaceId: string) => {
return {
...invoice,
description: "Dub Partner payout",
- pdfUrl: `${APP_DOMAIN}/invoices/${invoice.id}`,
+ pdfUrl:
+ invoice.status === "completed"
+ ? `${APP_DOMAIN}/invoices/${invoice.id}`
+ : null,
};
});
};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/invoices/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/invoices/page-client.tsx
index 4b35dc59e4..52fdc3c8ef 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/invoices/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/invoices/page-client.tsx
@@ -144,7 +144,10 @@ const InvoiceCard = ({ invoice }: { invoice: InvoiceProps }) => {
variant="secondary"
text="View invoice"
disabled
- disabledTooltip="Invoice not available. Contact support if you need assistance."
+ disabledTooltip={
+ invoice.failedReason ||
+ "Invoice not available. Contact support if you need assistance."
+ }
/>
)}
</div>
diff --git a/apps/web/lib/zod/schemas/invoices.ts b/apps/web/lib/zod/schemas/invoices.ts
index 2e7993865d..91e8f63474 100644
--- a/apps/web/lib/zod/schemas/invoices.ts
+++ b/apps/web/lib/zod/schemas/invoices.ts
@@ -8,4 +8,5 @@ export const InvoiceSchema = z.object({
createdAt: z.date(),
description: z.string().default("Dub payout"),
pdfUrl: z.string().nullable(),
+ failedReason: z.string().nullish().default(null),
});
diff --git a/packages/prisma/schema/invoice.prisma b/packages/prisma/schema/invoice.prisma
index 52ac1987f7..868e876727 100644
--- a/packages/prisma/schema/invoice.prisma
+++ b/packages/prisma/schema/invoice.prisma
@@ -5,16 +5,17 @@ enum InvoiceStatus {
}
model Invoice {
- id String @id @default(cuid())
- programId String
- workspaceId String
- number String? @unique // This starts with the customer’s unique invoicePrefix
- status InvoiceStatus @default(processing)
- amount Int @default(0)
- fee Int @default(0)
- total Int @default(0)
- receiptUrl String? @db.Text
- createdAt DateTime @default(now())
+ id String @id @default(cuid())
+ programId String
+ workspaceId String
+ number String? @unique // This starts with the customer’s unique invoicePrefix
+ status InvoiceStatus @default(processing)
+ amount Int @default(0)
+ fee Int @default(0)
+ total Int @default(0)
+ receiptUrl String? @db.Text
+ failedReason String?
+ createdAt DateTime @default(now())
payouts Payout[]
program Program @relation(fields: [programId], references: [id])