The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
handle processPayoutInvoiceFailure for paymentIntentRequiresAction webhook as well
Steven Teyf23f86b1d6a0
diff --git a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts b/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.tsindex 791ce50394..fd2e11aa11 100644--- a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts@@ -1,9 +1,10 @@import { prisma } from "@dub/prisma";import Stripe from "stripe";import { processDomainRenewalFailure } from "./utils/process-domain-renewal-failure";+import { processPayoutInvoiceFailure } from "./utils/process-payout-invoice-failure";export async function paymentIntentRequiresAction(event: Stripe.Event) {- const { transfer_group: invoiceId } = event.data+ const { transfer_group: invoiceId, latest_charge: charge } = event.data.object as Stripe.PaymentIntent;if (!invoiceId) {@@ -22,13 +23,6 @@ export async function paymentIntentRequiresAction(event: Stripe.Event) {return;}- if (invoice.type !== "domainRenewal") {- console.log(- `Invoice with transfer group ${invoiceId} is not a domain renewal.`,- );- return;- }-invoice = await prisma.invoice.update({where: {id: invoiceId,@@ -43,5 +37,9 @@ export async function paymentIntentRequiresAction(event: Stripe.Event) {},});- return await processDomainRenewalFailure({ invoice });+ if (invoice.type === "partnerPayout") {+ return await processPayoutInvoiceFailure({ invoice });+ } else if (invoice.type === "domainRenewal") {+ return await processDomainRenewalFailure({ invoice });+ }}diff --git a/apps/web/app/(ee)/api/stripe/webhook/utils/process-payout-invoice-failure.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/process-payout-invoice-failure.tsindex be94710f8f..a502077ce5 100644--- a/apps/web/app/(ee)/api/stripe/webhook/utils/process-payout-invoice-failure.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/process-payout-invoice-failure.ts@@ -16,7 +16,7 @@ export async function processPayoutInvoiceFailure({charge,}: {invoice: Invoice;- charge: Stripe.Charge;+ charge?: Stripe.Charge;}) {await log({message: `Partner payout failed for invoice ${invoice.id}.`,@@ -69,16 +69,20 @@ export async function processPayoutInvoiceFailure({return;}- let cardLast4: string | undefined;- let chargedFailureFee = false;-- // Charge failure fee for direct debit payment failures- if (+ const paymentMethod =+ charge &&charge.payment_method_details &&DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(charge.payment_method_details.type as Stripe.PaymentMethod.Type,)- ) {+ ? "direct_debit"+ : "card";++ let chargedFailureFee = false;+ let cardLast4: string | undefined;++ // Charge failure fee for direct debit payment failures+ if (paymentMethod === "direct_debit") {const { paymentIntent, paymentMethod } = await createPaymentIntent({stripeId: workspace.stripeId,amount: PAYOUT_FAILURE_FEE_CENTS,@@ -112,7 +116,9 @@ export async function processPayoutInvoiceFailure({name: workspace.programs[0].name,},payout: {- amount: charge.amount,+ amount: invoice.total,+ method: paymentMethod as "card" | "direct_debit",+ failedReason: invoice.failedReason,...(chargedFailureFee && {failureFee: PAYOUT_FAILURE_FEE_CENTS,cardLast4,diff --git a/apps/web/scripts/perplexity/backfill-tenantids.ts b/apps/web/scripts/perplexity/backfill-tenantids.tsnew file mode 100644index 0000000000..a17e05721e--- /dev/null+++ b/apps/web/scripts/perplexity/backfill-tenantids.ts@@ -0,0 +1,94 @@+import { includeTags } from "@/lib/api/links/include-tags";+import { prisma } from "@dub/prisma";+import "dotenv-flow/config";+import * as fs from "fs";+import * as Papa from "papaparse";+import { recordLink } from "../../lib/tinybird/record-link";++const programId = "prog_xxx";++let partnersToBackfill: {+ partnerId: string;+ tenantId: string;+}[] = [];++async function main() {+ Papa.parse(fs.createReadStream("pplx_partnerId_tenantId.csv", "utf-8"), {+ header: true,+ skipEmptyLines: true,+ step: (result: {+ data: {+ PARTNERID: string;+ TENANTID: string;+ };+ }) => {+ if (result.data.PARTNERID && result.data.TENANTID) {+ partnersToBackfill.push({+ partnerId: result.data.PARTNERID,+ tenantId: result.data.TENANTID,+ });+ }+ },+ complete: async () => {+ //filter out partners that are already backfilled+ const alreadyBackfilledPartners = await prisma.programEnrollment.findMany(+ {+ where: {+ programId,+ tenantId: {+ not: null,+ },+ },+ },+ );+ const filteredPartnersToBackfill = partnersToBackfill.filter(+ (partner) =>+ !alreadyBackfilledPartners.some(+ (p) => p.partnerId === partner.partnerId,+ ),+ );++ console.log(+ `Found ${filteredPartnersToBackfill.length} partners to backfill`,+ );++ for (const partner of filteredPartnersToBackfill) {+ const where = {+ programId,+ partnerId: partner.partnerId,+ };++ const programEnrollment = await prisma.$transaction(async (tx) => {+ await tx.link.updateMany({+ where,+ data: {+ tenantId: partner.tenantId,+ },+ });+ return await tx.programEnrollment.update({+ where: {+ partnerId_programId: where,+ },+ data: {+ tenantId: partner.tenantId,+ },+ include: {+ links: {+ include: includeTags,+ },+ },+ });+ });++ console.log(+ `Updated ${partner.partnerId} and their ${programEnrollment.links.length} links with tenantId ${partner.tenantId}`,+ );++ const tbRes = await recordLink(programEnrollment.links);+ console.log("tbRes", tbRes);+ }+ },+ });+}++main();diff --git a/packages/email/src/templates/partner-payout-failed.tsx b/packages/email/src/templates/partner-payout-failed.tsxindex 8d674dea7c..dc5a993fba 100644--- a/packages/email/src/templates/partner-payout-failed.tsx+++ b/packages/email/src/templates/partner-payout-failed.tsx@@ -23,7 +23,10 @@ export default function PartnerPayoutFailed({},payout = {amount: 530000,- failureFee: 1000,+ method: "card",+ // failureFee: 1000,+ failureReason:+ "Your payment requires additional authentication to complete.",cardLast4: "1234",},email = "[email protected]",@@ -36,6 +39,8 @@ export default function PartnerPayoutFailed({};payout: {amount: number; // in cents+ method: "card" | "direct_debit";+ failureReason?: string | null;failureFee?: number; // in centscardLast4?: string;};@@ -67,11 +72,20 @@ export default function PartnerPayoutFailed({<span className="font-semibold text-purple-600">{program.name}</span>{" "}- failed. It's been reverted back to{" "}+ failed. The payouts have been reverted back to{" "}<span className="font-semibold text-neutral-800">Pending</span>{" "}and will need to be confirmed again.</Text>+ {payout.failureReason && (+ <Text className="text-sm leading-6 text-neutral-600">+ Reason:{" "}+ <span className="font-semibold italic text-neutral-800">+ {payout.failureReason}+ </span>+ </Text>+ )}+{payout.failureFee && (<Text className="text-sm leading-6 text-neutral-600">To cover the cost of the failed payout, we've charged a{" "}@@ -92,8 +106,9 @@ export default function PartnerPayoutFailed({)}<Text className="text-sm leading-6 text-neutral-600">- Please update your direct debit details at your earliest- convenience to ensure that your partners are paid on time.+ Please update your {payout.method.replace("_", " ")} details at+ your earliest convenience to ensure that your partners are paid on+ time.</Text><Section className="my-8">