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.

checkPaymentMethodMandate (#3405)

Steven Tey2b6d094e2fc9
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 6a1ebf3bc3..ccfc96d975 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -7,6 +7,7 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import {
CUTOFF_PERIOD_MAX_PAYOUTS,
+ DIRECT_DEBIT_PAYMENT_METHOD_TYPES,
INVOICE_MIN_PAYOUT_AMOUNT_CENTS,
PAYMENT_METHOD_TYPES,
STRIPE_PAYMENT_METHOD_NORMALIZATION,
@@ -15,6 +16,7 @@ import { qstash } from "@/lib/cron";
import { exceededLimitError } from "@/lib/exceeded-limit-error";
import { CUTOFF_PERIOD_ENUM } from "@/lib/partners/cutoff-period";
import { stripe } from "@/lib/stripe";
+import { checkPaymentMethodMandate } from "@/lib/stripe/check-payment-method-mandate";
import { getWebhooks } from "@/lib/webhook/get-webhooks";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
@@ -161,6 +163,20 @@ export const confirmPayoutsAction = authActionClient
throw new Error("Fast settlement is only supported for ACH payment.");
}
+ if (DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(paymentMethod.type)) {
+ // check if mandate is valid
+ const mandate = await checkPaymentMethodMandate({
+ paymentMethodId,
+ });
+ if (!mandate) {
+ // remove the payment method
+ await stripe.paymentMethods.detach(paymentMethodId);
+ throw new Error(
+ "No active mandate found for this bank account. Please set up a new bank account for payouts under your billing settings page.",
+ );
+ }
+ }
+
const invoice = await prisma.$transaction(async (tx) => {
// Generate the next invoice number by counting the number of invoices for the workspace
const totalInvoices = await tx.invoice.count({
diff --git a/apps/web/lib/stripe/check-payment-method-mandate.ts b/apps/web/lib/stripe/check-payment-method-mandate.ts
new file mode 100644
index 0000000000..012b6b4e69
--- /dev/null
+++ b/apps/web/lib/stripe/check-payment-method-mandate.ts
@@ -0,0 +1,30 @@
+export const checkPaymentMethodMandate = async ({
+ paymentMethodId,
+}: {
+ paymentMethodId: string;
+}) => {
+ // Check mandate via REST API (mandates require Stripe-Version 2025-12-15.preview)
+ // TODO: Update this to use the Stripe SDK when we upgrade to the new API version
+ const mandatesResponse = await fetch(
+ `https://api.stripe.com/v1/mandates?payment_method=${encodeURIComponent(paymentMethodId)}&status=active`,
+ {
+ method: "GET",
+ headers: {
+ "Stripe-Version": "2025-12-15.preview",
+ Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
+ },
+ },
+ );
+
+ if (!mandatesResponse.ok) {
+ const errText = await mandatesResponse.text();
+ throw new Error(`Failed to verify mandate: ${errText}`);
+ }
+
+ const { data: mandatesData } = await mandatesResponse.json();
+ if (mandatesData.length === 0) {
+ return null;
+ }
+
+ return mandatesData[0];
+};