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.

Update Stripe webhook to use earnings table for payout tracking

Kiran K84aae16c0726
diff --git a/apps/web/app/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
index 47be45c5ea..70d310ba1d 100644
--- a/apps/web/app/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
@@ -83,7 +83,7 @@ export async function chargeSucceeded(event: Stripe.Event) {
},
});
- await tx.sale.updateMany({
+ await tx.earnings.updateMany({
where: {
payoutId: payout.id,
},
diff --git a/apps/web/lib/partners/create-sales-payout.ts b/apps/web/lib/partners/create-sales-payout.ts
deleted file mode 100644
index fb9ccd2426..0000000000
--- a/apps/web/lib/partners/create-sales-payout.ts
+++ /dev/null
@@ -1,135 +0,0 @@
-import { createId } from "@/lib/api/utils";
-import { prisma } from "@dub/prisma";
-import { Payout } from "@dub/prisma/client";
-
-// TODO:
-// Remove this since we no longer use this
-
-// Calculate the commission earned for the partner for the given program
-export const createSalesPayout = async ({
- programId,
- partnerId,
- periodStart,
- periodEnd,
- description,
-}: {
- programId: string;
- partnerId: string;
- periodStart?: Date | null;
- periodEnd?: Date | null;
- description?: string;
-}) => {
- return await prisma.$transaction(async (tx) => {
- const sales = await tx.sale.findMany({
- where: {
- programId,
- partnerId,
- payoutId: null,
- status: "pending", // We only want to pay out sales that are pending (not refunded / fraud / duplicate)
- // Referral commissions are held for 30 days before becoming available.
- // createdAt: {
- // lte: subDays(new Date(), 30),
- // },
- },
- select: {
- id: true,
- earnings: true,
- createdAt: true,
- },
- });
-
- if (!sales.length) {
- return;
- }
-
- const quantity = sales.length;
- const amount = sales.reduce((total, sale) => total + sale.earnings, 0);
-
- if (!periodStart) {
- // get the earliest sale date
- periodStart = sales.reduce(
- (min, sale) => (sale.createdAt < min ? sale.createdAt : min),
- sales[0].createdAt,
- );
- }
-
- if (!periodEnd) {
- // get the end of the month of the latest sale
- // e.g. if the latest sale is 2024-12-16, the periodEnd should be 2024-12-31
- const latestSale = sales.reduce(
- (max, sale) => (sale.createdAt > max ? sale.createdAt : max),
- sales[0].createdAt,
- );
- periodEnd = new Date(
- latestSale.getFullYear(),
- latestSale.getMonth() + 1,
- 0,
- );
- }
-
- let payout: Payout | null = null;
-
- // only create a payout if the total sale amount is greater than 0
- if (amount > 0) {
- // Check if the partner has another pending payout
- const existingPayout = await tx.payout.findFirst({
- where: {
- programId,
- partnerId,
- status: "pending",
- type: "sales",
- },
- });
-
- // Update the existing payout
- if (existingPayout) {
- payout = await tx.payout.update({
- where: {
- id: existingPayout.id,
- },
- data: {
- amount: {
- increment: amount,
- },
- quantity: {
- increment: quantity,
- },
- periodEnd,
- description: existingPayout.description ?? "Dub Partners payout",
- },
- });
-
- console.info("Payout updated", payout);
- }
-
- // Create the payout
- else {
- payout = await tx.payout.create({
- data: {
- id: createId({ prefix: "po_" }),
- programId,
- partnerId,
- amount,
- periodStart,
- periodEnd,
- quantity,
- description: description ?? "Dub Partners payout",
- },
- });
-
- console.info("Payout created", payout);
- }
- }
-
- // Update the sales records
- await tx.sale.updateMany({
- where: { id: { in: sales.map((sale) => sale.id) } },
- data: {
- status: "processed",
- ...(payout ? { payoutId: payout.id } : {}),
- },
- });
-
- return payout;
- });
-};