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.

Refactor payout email templates and confirm payouts action

Kiran K667a3444d3e6
diff --git a/apps/web/app/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
index 217f4e32f2..690f52553c 100644
--- a/apps/web/app/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
@@ -1,7 +1,6 @@
import { limiter } from "@/lib/cron/limiter";
import { stripe } from "@/lib/stripe";
import { prisma } from "@dub/prisma";
-import { formatDate } from "@dub/utils";
import { sendEmail } from "emails";
import PartnerPayoutSent from "emails/partner-payout-sent";
import Stripe from "stripe";
@@ -100,16 +99,8 @@ export async function chargeSucceeded(event: Stripe.Event) {
payout: {
id: payout.id,
amount: payout.amount,
- startDate: formatDate(payout.periodStart!, {
- year: "numeric",
- month: "short",
- day: "numeric",
- }),
- endDate: formatDate(payout.periodEnd!, {
- year: "numeric",
- month: "short",
- day: "numeric",
- }),
+ startDate: payout.periodStart!,
+ endDate: payout.periodEnd!,
},
}),
}),
diff --git a/apps/web/emails/partner-payout-confirmed.tsx b/apps/web/emails/partner-payout-confirmed.tsx
new file mode 100644
index 0000000000..83df507490
--- /dev/null
+++ b/apps/web/emails/partner-payout-confirmed.tsx
@@ -0,0 +1,108 @@
+import { currencyFormatter, DUB_WORDMARK, formatDate } from "@dub/utils";
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Img,
+ Link,
+ Preview,
+ Section,
+ Tailwind,
+ Text,
+} from "@react-email/components";
+import Footer from "./components/footer";
+
+// Send this email when the payout is confirmed when payment is send using ACH
+export default function PartnerPayoutConfirmed({
+ email = "[email protected]",
+ program = {
+ id: "prog_d8pl69xXCv4AoHNT281pHQdo",
+ name: "Acme",
+ logo: DUB_WORDMARK,
+ },
+ payout = {
+ id: "po_8VuCr2i7WnG65d4TNgZO19fT",
+ amount: 490,
+ startDate: new Date("2024-11-01"),
+ endDate: new Date("2024-11-30"),
+ },
+}: {
+ email: string;
+ program: {
+ id: string;
+ name: string;
+ logo: string | null;
+ };
+ payout: {
+ id: string;
+ amount: number;
+ startDate: Date;
+ endDate: Date;
+ };
+}) {
+ const saleAmountInDollars = currencyFormatter(payout.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+
+ const startDate = formatDate(payout.startDate, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ });
+
+ const endDate = formatDate(payout.endDate, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ });
+
+ return (
+ <Html>
+ <Head />
+ <Preview>Your payout is being processed</Preview>
+ <Tailwind>
+ <Body className="mx-auto my-auto bg-white font-sans">
+ <Container className="mx-auto my-10 max-w-[500px] rounded border border-solid border-gray-200 px-10 py-5">
+ <Section className="my-8">
+ <Img
+ src={program.logo || "https://assets.dub.co/logo.png"}
+ height="32"
+ alt={program.name}
+ />
+ </Section>
+
+ <Heading className="mx-0 p-0 text-lg font-medium text-black">
+ Your payout is being processed!
+ </Heading>
+
+ <Text className="text-sm leading-6 text-gray-600">
+ <strong className="text-black">{program.name}</strong> has
+ initiated a payout of{" "}
+ <strong className="text-black">{saleAmountInDollars}</strong> for
+ affiliate sales made from{" "}
+ <strong className="text-black">{startDate}</strong> to{" "}
+ <strong className="text-black">{endDate}</strong>.
+ </Text>
+ <Text className="text-sm leading-6 text-gray-600">
+ The payout is currently being processed and is expected to be
+ credited to your account within 4 business days.
+ </Text>
+
+ <Section className="mb-12 mt-8">
+ <Link
+ className="rounded-md bg-neutral-900 px-4 py-3 text-[12px] font-medium text-white no-underline"
+ href={`https://partners.dub.co/settings/payouts?payoutId=${payout.id}`}
+ >
+ View payout
+ </Link>
+ </Section>
+ <Footer email={email} />
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+}
diff --git a/apps/web/emails/partner-payout-sent.tsx b/apps/web/emails/partner-payout-sent.tsx
index 24e0a889bb..cf2d4af567 100644
--- a/apps/web/emails/partner-payout-sent.tsx
+++ b/apps/web/emails/partner-payout-sent.tsx
@@ -1,4 +1,4 @@
-import { currencyFormatter, DUB_WORDMARK } from "@dub/utils";
+import { currencyFormatter, DUB_WORDMARK, formatDate } from "@dub/utils";
import {
Body,
Container,
@@ -14,6 +14,7 @@ import {
} from "@react-email/components";
import Footer from "./components/footer";
+// Send this email when the payout completes
export default function PartnerPayoutSent({
email = "[email protected]",
program = {
@@ -24,8 +25,8 @@ export default function PartnerPayoutSent({
payout = {
id: "po_8VuCr2i7WnG65d4TNgZO19fT",
amount: 490,
- startDate: "Nov 1, 2024",
- endDate: "Nov 30, 2024",
+ startDate: new Date("2024-11-01"),
+ endDate: new Date("2024-11-30"),
},
}: {
email: string;
@@ -37,17 +38,27 @@ export default function PartnerPayoutSent({
payout: {
id: string;
amount: number;
- startDate: string;
- endDate: string;
+ startDate: Date;
+ endDate: Date;
};
}) {
- const linkToPayout = `https://partners.dub.co/settings/payouts?payoutId=${payout.id}`;
-
const saleAmountInDollars = currencyFormatter(payout.amount / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
+ const startDate = formatDate(payout.startDate, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ });
+
+ const endDate = formatDate(payout.endDate, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ });
+
return (
<Html>
<Head />
@@ -71,8 +82,8 @@ export default function PartnerPayoutSent({
<strong className="text-black">{program.name}</strong> has sent
you <strong className="text-black">{saleAmountInDollars}</strong>{" "}
for affiliate sales made from{" "}
- <strong className="text-black">{payout.startDate}</strong> to{" "}
- <strong className="text-black">{payout.endDate}</strong>.
+ <strong className="text-black">{startDate}</strong> to{" "}
+ <strong className="text-black">{endDate}</strong>.
</Text>
<Text className="text-sm leading-6 text-gray-600">
The funds are on their way to your account.
@@ -81,7 +92,7 @@ export default function PartnerPayoutSent({
<Section className="mb-12 mt-8">
<Link
className="rounded-md bg-neutral-900 px-4 py-3 text-[12px] font-medium text-white no-underline"
- href={linkToPayout}
+ href={`https://partners.dub.co/settings/payouts?payoutId=${payout.id}`}
>
View payout
</Link>
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 0db8a36c75..771ff40cfd 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -2,6 +2,7 @@
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { createId } from "@/lib/api/utils";
+import { limiter } from "@/lib/cron/limiter";
import {
DUB_PARTNERS_PAYOUT_FEE_ACH,
DUB_PARTNERS_PAYOUT_FEE_CARD,
@@ -9,6 +10,9 @@ import {
} from "@/lib/partners/constants";
import { stripe } from "@/lib/stripe";
import { prisma } from "@dub/prisma";
+import { waitUntil } from "@vercel/functions";
+import { sendEmail } from "emails";
+import PartnerPayoutConfirmed from "emails/partner-payout-confirmed";
import z from "zod";
import { authActionClient } from "../safe-action";
@@ -36,13 +40,13 @@ export const confirmPayoutsAction = authActionClient
}
// Check the payout method is valid
- const payoutMethod = await stripe.paymentMethods.retrieve(paymentMethodId);
+ const paymentMethod = await stripe.paymentMethods.retrieve(paymentMethodId);
- if (payoutMethod.customer !== workspace.stripeId) {
+ if (paymentMethod.customer !== workspace.stripeId) {
throw new Error("Invalid payout method.");
}
- if (!["card", "us_bank_account"].includes(payoutMethod.type)) {
+ if (!["card", "us_bank_account"].includes(paymentMethod.type)) {
throw new Error(
`We only support card and ACH for now. Please update your payout method to one of these.`,
);
@@ -69,6 +73,28 @@ export const confirmPayoutsAction = authActionClient
select: {
id: true,
amount: true,
+ periodStart: true,
+ periodEnd: true,
+ partner: {
+ select: {
+ users: {
+ select: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ },
+ },
+ },
+ program: {
+ select: {
+ id: true,
+ name: true,
+ logo: true,
+ },
+ },
},
});
@@ -77,14 +103,14 @@ export const confirmPayoutsAction = authActionClient
}
// Create the invoice for the payouts
- return await prisma.$transaction(async (tx) => {
+ const newInvoice = await prisma.$transaction(async (tx) => {
const amount = payouts.reduce(
(total, payout) => total + payout.amount,
0,
);
const fee =
- payoutMethod.type === "card"
+ paymentMethod.type === "card"
? amount * DUB_PARTNERS_PAYOUT_FEE_CARD
: amount * DUB_PARTNERS_PAYOUT_FEE_ACH;
@@ -130,8 +156,39 @@ export const confirmPayoutsAction = authActionClient
},
});
- return {
- invoice,
- };
+ return invoice;
});
+
+ waitUntil(
+ (async () => {
+ // Send emails to all the partners involved in the payouts if the payout method is ACH
+ // ACH takes 4 business days to process
+ if (newInvoice && paymentMethod.type === "us_bank_account") {
+ for (const payout of payouts) {
+ const { program, partner } = payout;
+ const partnerUsers = partner.users.map(({ user }) => user);
+
+ partnerUsers.map((user) =>
+ limiter.schedule(() =>
+ sendEmail({
+ subject: "Payout confirmed!",
+ email: user.email!,
+ from: "Dub Partners <[email protected]>",
+ react: PartnerPayoutConfirmed({
+ email: user.email!,
+ program,
+ payout: {
+ id: payout.id,
+ amount: payout.amount,
+ startDate: payout.periodStart!,
+ endDate: payout.periodEnd!,
+ },
+ }),
+ }),
+ ),
+ );
+ }
+ }
+ })(),
+ );
});
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index ec1fb1b8fd..3e0550c007 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -343,10 +343,10 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
getRowId: (originalRow: PayoutResponse) => originalRow.id,
onRowSelectionChange: (rows: Row<PayoutResponse>[]) =>
setSelectedPayouts(rows.map((row) => row.original)),
- selectedRows: (selectedPayouts || []).reduce((acc, payout) => {
- acc[payout.id] = true;
- return acc;
- }, {} as RowSelectionState),
+ // selectedRows: (selectedPayouts || []).reduce((acc, payout) => {
+ // acc[payout.id] = true;
+ // return acc;
+ // }, {} as RowSelectionState),
} as any);
return (