The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add `isRecipientPendingIdVerification` check
Steven Teya7e6dbfac8e0
diff --git a/apps/web/lib/partners/create-stablecoin-payout.ts b/apps/web/lib/partners/create-stablecoin-payout.tsindex 1199d67bcf..94f2c595e7 100644--- a/apps/web/lib/partners/create-stablecoin-payout.ts+++ b/apps/web/lib/partners/create-stablecoin-payout.ts@@ -28,6 +28,7 @@ import { createStripeOutboundPayment } from "../stripe/create-stripe-outbound-paimport { fundFinancialAccount } from "../stripe/fund-financial-account";import { getStripeRecipientAccount } from "../stripe/get-stripe-recipient-account";import { getStripeRecipientPayoutMethod } from "../stripe/get-stripe-recipient-payout-method";+import { isRecipientPendingIdVerification } from "../stripe/is-recipient-pending-id-verification";interface CreateStablecoinPayoutParams {partnerId: string;@@ -202,11 +203,33 @@ export const createStablecoinPayout = async ({return;}- // Stripe recipient account does not have crypto wallet capabilities- if (+ const cryptoWalletsActive =stripeRecipientAccount.configuration?.recipient?.capabilities- ?.crypto_wallets?.status !== "active"+ ?.crypto_wallets?.status === "active";++ // Identity verification restricts crypto_wallets until Stripe finishes review.+ // Keep payouts enabled and defer the transfer so it can be sent once the+ // capability is active again.+ if (+ !cryptoWalletsActive &&+ isRecipientPendingIdVerification(stripeRecipientAccount)) {+ await markPayoutsAsProcessed(currentInvoicePayouts);++ const message = `Stripe recipient account for partner ${partner.email} is restricted while identity verification is in progress. Skipping payout until verification is complete.`;++ if (forceWithdrawal) {+ throw new Error(+ "Your payout account is temporarily restricted while Stripe verifies your identity. Please try again once verification is complete.",+ );+ }++ console.warn(message);+ return;+ }++ // Stripe recipient account does not have crypto wallet capabilities+ if (!cryptoWalletsActive) {await prisma.partner.update({where: {id: partner.id,diff --git a/apps/web/lib/payouts/recompute-partner-payout-state.ts b/apps/web/lib/payouts/recompute-partner-payout-state.tsindex ce6c651ff1..1af08a24f8 100644--- a/apps/web/lib/payouts/recompute-partner-payout-state.ts+++ b/apps/web/lib/payouts/recompute-partner-payout-state.ts@@ -1,5 +1,6 @@import { stripe } from "@/lib/stripe";import { getStripeRecipientAccount } from "@/lib/stripe/get-stripe-recipient-account";+import { isRecipientPendingIdVerification } from "@/lib/stripe/is-recipient-pending-id-verification";import { prettyPrint } from "@dub/utils";import { Partner, PartnerPayoutMethod } from "@prisma/client";import { getStripeRecipientPayoutMethod } from "../stripe/get-stripe-recipient-payout-method";@@ -25,7 +26,9 @@ export async function recomputePartnerPayoutState(| "payoutsEnabledAt"| "defaultPayoutMethod"| "tremendousEmail"- >,+ > & {+ cryptoWalletAddress?: string | null;+ },) {const [connectAccount, stablecoinAccount] = await Promise.all([partner.stripeConnectId@@ -37,18 +40,44 @@ export async function recomputePartnerPayoutState(: Promise.resolve(null),]);- const hasCryptoWalletCapabilities = Boolean(- stablecoinAccount?.configuration?.recipient?.capabilities?.crypto_wallets- ?.status === "active",+ const pendingIdVerification = Boolean(+ stablecoinAccount && isRecipientPendingIdVerification(stablecoinAccount),);- const stablecoinPayoutMethod =- partner.stripeRecipientId && hasCryptoWalletCapabilities- ? await getStripeRecipientPayoutMethod(partner.stripeRecipientId)- : null;+ const cryptoWalletsActive =+ stablecoinAccount?.configuration?.recipient?.capabilities?.crypto_wallets+ ?.status === "active";++ // Verification hides crypto_wallets until Stripe finishes ID review. Keep+ // looking up the wallet so we don't clear payouts for a temporary restriction.+ const hasCryptoWalletCapabilities =+ cryptoWalletsActive || pendingIdVerification;++ let stablecoinPayoutMethod: Awaited<+ ReturnType<typeof getStripeRecipientPayoutMethod>+ > | null = null;++ if (partner.stripeRecipientId && hasCryptoWalletCapabilities) {+ try {+ stablecoinPayoutMethod = await getStripeRecipientPayoutMethod(+ partner.stripeRecipientId,+ );+ } catch (error) {+ if (!pendingIdVerification) {+ throw error;+ }++ console.warn(+ `Failed to load crypto wallet payout method during identity verification: ${+ error instanceof Error ? error.message : String(error)+ }`,+ );+ }+ }const cryptoWalletAddress =- stablecoinPayoutMethod?.crypto_wallet?.address ?? null;+ stablecoinPayoutMethod?.crypto_wallet?.address ??+ (pendingIdVerification ? (partner.cryptoWalletAddress ?? null) : null);const cryptoWalletNetwork =stablecoinPayoutMethod?.crypto_wallet?.network ?? null;@@ -57,9 +86,14 @@ export async function recomputePartnerPayoutState(connectAccount?.capabilities?.transfers === "active",);- const stablecoinActive = Boolean(- hasCryptoWalletCapabilities && cryptoWalletAddress && cryptoWalletNetwork,- );+ const stablecoinActive =+ Boolean(+ cryptoWalletAddress &&+ cryptoWalletNetwork &&+ (cryptoWalletsActive || pendingIdVerification),+ ) ||+ (pendingIdVerification &&+ partner.defaultPayoutMethod === PartnerPayoutMethod.stablecoin);const paypalActive = Boolean(partner.paypalEmail);@@ -107,6 +141,7 @@ export async function recomputePartnerPayoutState(prettyPrint({connectActive,stablecoinActive,+ pendingIdVerification,paypalActive,tremendousActive,payoutsEnabledAt,diff --git a/apps/web/lib/stripe/get-stripe-recipient-account.ts b/apps/web/lib/stripe/get-stripe-recipient-account.tsindex b230fed6e2..d9811af611 100644--- a/apps/web/lib/stripe/get-stripe-recipient-account.ts+++ b/apps/web/lib/stripe/get-stripe-recipient-account.ts@@ -7,6 +7,7 @@ export async function getStripeRecipientAccount(stripeRecipientId: string) {},query: {"include[0]": "configuration.recipient",+ "include[1]": "requirements",},});diff --git a/apps/web/lib/stripe/is-recipient-pending-id-verification.ts b/apps/web/lib/stripe/is-recipient-pending-id-verification.tsnew file mode 100644index 0000000000..0245622eeb--- /dev/null+++ b/apps/web/lib/stripe/is-recipient-pending-id-verification.ts@@ -0,0 +1,68 @@+type CryptoWalletCapability = {+ status?: string | null;+ status_details?: {+ code?: string | null;+ } | null;+};++type RequirementEntry = {+ awaiting_action_from?: string | null;+ impact?: {+ restricts_capabilities?:+ | {+ capability?: string | null;+ configuration?: string | null;+ }[]+ | null;+ } | null;+};++function restrictsCryptoWallets(entry: RequirementEntry) {+ return entry.impact?.restricts_capabilities?.some(+ (restriction) =>+ restriction?.capability === "crypto_wallets" &&+ restriction?.configuration === "recipient",+ );+}++// Accounts v2: `crypto_wallets.status` of `pending` means Stripe is verifying+// that capability's requirements. The capability can also be omitted while an+// open requirement restricts it and `awaiting_action_from` is `stripe`.+export function isRecipientPendingIdVerification(+ account: {+ configuration?: {+ recipient?: {+ capabilities?: Record<string, CryptoWalletCapability | null> | null;+ } | null;+ } | null;+ requirements?: {+ entries?: unknown[] | null;+ } | null;+ } | null,+) {+ const cryptoWallets =+ account?.configuration?.recipient?.capabilities?.crypto_wallets;++ if (cryptoWallets?.status === "pending") {+ return true;+ }++ const entries = account?.requirements?.entries;++ if (!entries?.length) {+ return false;+ }++ return entries.some((entry) => {+ if (!entry || typeof entry !== "object") {+ return false;+ }++ const requirement = entry as RequirementEntry;++ return (+ requirement.awaiting_action_from === "stripe" &&+ Boolean(restrictsCryptoWallets(requirement))+ );+ });+}diff --git a/apps/web/lib/stripe/stripe-v2-schemas.ts b/apps/web/lib/stripe/stripe-v2-schemas.tsindex 6ee889a7f9..4ec2a3e71d 100644--- a/apps/web/lib/stripe/stripe-v2-schemas.ts+++ b/apps/web/lib/stripe/stripe-v2-schemas.ts@@ -157,6 +157,7 @@ export const listPayoutMethodsOutputSchema = z.object({export const retrieveAccountQuerySchema = z.object({"include[0]": z.string(),+ "include[1]": z.string().optional(),});export const retrieveAccountOutputSchema = z.object({@@ -172,6 +173,13 @@ export const retrieveAccountOutputSchema = z.object({.nullable(),}).nullable(),+ requirements: z+ .object({+ entries: z.array(z.any()).nullable().optional(),+ })+ .passthrough()+ .nullable()+ .optional(),});export const createPayoutInputSchema = z.object({diff --git a/apps/web/tests/stripe/is-recipient-pending-id-verification.test.ts b/apps/web/tests/stripe/is-recipient-pending-id-verification.test.tsnew file mode 100644index 0000000000..f47908b013--- /dev/null+++ b/apps/web/tests/stripe/is-recipient-pending-id-verification.test.ts@@ -0,0 +1,132 @@+import { isRecipientPendingIdVerification } from "@/lib/stripe/is-recipient-pending-id-verification";+import { retrieveAccountOutputSchema } from "@/lib/stripe/stripe-v2-schemas";+import { describe, expect, test } from "vitest";++const recipientVerificationAccount = {+ id: "acct_test",+ closed: false,+ configuration: {+ recipient: {+ applied: true,+ capabilities: {+ stripe_balance: null,+ },+ },+ },+ requirements: {+ entries: [+ {+ awaiting_action_from: "stripe",+ description: "recipient.recipient_verification",+ errors: [],+ impact: {+ restricts_capabilities: [+ {+ capability: "crypto_wallets",+ configuration: "recipient",+ deadline: {+ status: "past_due",+ },+ },+ ],+ restricts_features: [+ {+ configuration: "recipient",+ deadline: {+ status: "past_due",+ },+ feature: "crypto_wallets",+ },+ ],+ restricts_payouts: null,+ },+ minimum_deadline: {+ status: "past_due",+ },+ reference: null,+ requested_reasons: [+ {+ code: "routine_verification",+ },+ ],+ },+ ],+ summary: {+ minimum_deadline: {+ status: "past_due",+ time: null,+ },+ },+ },+};++describe("isRecipientPendingIdVerification", () => {+ test("detects crypto_wallets restricted while Stripe still owns the requirement", () => {+ const account = retrieveAccountOutputSchema.parse(+ recipientVerificationAccount,+ );++ expect(isRecipientPendingIdVerification(account)).toBe(true);+ });++ test("detects crypto_wallets status pending", () => {+ const account = retrieveAccountOutputSchema.parse({+ id: "acct_test",+ closed: false,+ configuration: {+ recipient: {+ applied: true,+ capabilities: {+ crypto_wallets: {+ status: "pending",+ status_details: {+ code: "requirements_pending_verification",+ },+ },+ },+ },+ },+ });++ expect(isRecipientPendingIdVerification(account)).toBe(true);+ });++ test("ignores requirements the user still has to resolve", () => {+ const account = retrieveAccountOutputSchema.parse({+ ...recipientVerificationAccount,+ requirements: {+ ...recipientVerificationAccount.requirements,+ entries: [+ {+ ...recipientVerificationAccount.requirements.entries[0],+ awaiting_action_from: "user",+ },+ ],+ },+ });++ expect(isRecipientPendingIdVerification(account)).toBe(false);+ });++ test("ignores accounts that are not waiting on identity verification", () => {+ const account = retrieveAccountOutputSchema.parse({+ id: "acct_test",+ closed: false,+ configuration: {+ recipient: {+ applied: true,+ capabilities: {+ crypto_wallets: {+ status: "active",+ },+ },+ },+ },+ requirements: {+ entries: [],+ },+ });++ expect(isRecipientPendingIdVerification(account)).toBe(false);+ });+});