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.

finalize implementation

Steven Tey974ddd3d740b
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
index a382102346..7b13f4a0ba 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
@@ -77,37 +77,50 @@ export async function chargeFailed(event: Stripe.Event) {
return;
}
- const paymentMethods = await stripe.paymentMethods.list({
- customer: workspace.stripeId,
- type: "card",
- });
-
- if (paymentMethods.data.length === 0) {
- console.log("No valid payment methods found for workspace, skipping...");
- return;
- }
-
- const paymentMethod = paymentMethods.data[0];
- const shouldChargeFailureFee =
- charge.payment_method_details?.type === "us_bank_account";
+ let cardLast4: string | undefined;
+ let chargedFailureFee = false;
// Charge failure fee for ACH payment failures
- if (shouldChargeFailureFee) {
- await stripe.paymentIntents.create({
- amount: PAYOUT_FAILURE_FEE_CENTS,
- customer: workspace.stripeId,
- payment_method_types: ["card"],
- payment_method: paymentMethod.id,
- currency: "usd",
- confirmation_method: "automatic",
- confirm: true,
- statement_descriptor: "Dub Partners",
- description: `Dub Partners payout failure fee for invoice ${invoice.id}`,
- });
-
- console.log(
- `Charged a failure fee of $${PAYOUT_FAILURE_FEE_CENTS / 100} to ${workspace.slug}.`,
- );
+ if (charge.payment_method_details?.type === "us_bank_account") {
+ const [cards, links] = await Promise.all([
+ stripe.paymentMethods.list({
+ customer: workspace.stripeId,
+ type: "card",
+ }),
+ stripe.paymentMethods.list({
+ customer: workspace.stripeId,
+ type: "link",
+ }),
+ ]);
+
+ if (cards.data.length === 0 && links.data.length === 0) {
+ console.log("No valid payment methods found for workspace, skipping...");
+ return;
+ }
+
+ const paymentMethod = cards.data[0] || links.data[0];
+
+ if (paymentMethod) {
+ cardLast4 = paymentMethod.card?.last4;
+
+ await stripe.paymentIntents.create({
+ amount: PAYOUT_FAILURE_FEE_CENTS,
+ customer: workspace.stripeId,
+ payment_method_types: ["card", "link"],
+ payment_method: paymentMethod.id,
+ currency: "usd",
+ confirmation_method: "automatic",
+ confirm: true,
+ statement_descriptor: "Dub Partners",
+ description: `Dub Partners payout failure fee for invoice ${invoice.id}`,
+ });
+
+ chargedFailureFee = true;
+
+ console.log(
+ `Charged a failure fee of $${PAYOUT_FAILURE_FEE_CENTS / 100} to ${workspace.slug}.`,
+ );
+ }
}
waitUntil(
@@ -123,9 +136,9 @@ export async function chargeFailed(event: Stripe.Event) {
},
payout: {
amount,
- ...(shouldChargeFailureFee && {
+ ...(chargedFailureFee && {
failureFee: PAYOUT_FAILURE_FEE_CENTS,
- cardLast4: paymentMethod.card?.last4 || "",
+ cardLast4,
}),
},
}));
diff --git a/packages/email/src/templates/partner-payout-failed.tsx b/packages/email/src/templates/partner-payout-failed.tsx
index 92c5dd7c72..0b03d16798 100644
--- a/packages/email/src/templates/partner-payout-failed.tsx
+++ b/packages/email/src/templates/partner-payout-failed.tsx
@@ -75,17 +75,18 @@ export default function PartnerPayoutFailed({
and will need to be confirmed again.
</Text>
- {payout.failureFee && payout.cardLast4 && (
+ {payout.failureFee && (
<Text className="text-sm leading-6 text-neutral-600">
To cover the cost of the failed payout, we've charged a{" "}
<span className="font-semibold text-neutral-800">
${payout.failureFee / 100} payment failure fee
- </span>{" "}
- to your card ending in{" "}
- <span className="font-semibold text-purple-600">
- {payout.cardLast4}
</span>
- .
+ {payout.cardLast4 &&
+ ` to your card ending in ${(
+ <span className="font-semibold text-purple-600">
+ {payout.cardLast4}
+ </span>
+ )}.`}
</Text>
)}