The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
use createPaymentIntent in more places
Steven Teye89ffc781760
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.tsindex 02ee7fa2be..669bf9d3dc 100644--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts@@ -4,7 +4,7 @@ import {DIRECT_DEBIT_PAYMENT_METHOD_TYPES,PAYOUT_FAILURE_FEE_CENTS,} from "@/lib/partners/constants";-import { stripe } from "@/lib/stripe";+import { createPaymentIntent } from "@/lib/stripe/create-payment-intent";import { sendEmail } from "@dub/email";import { resend } from "@dub/email/resend";import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";@@ -126,45 +126,24 @@ async function processPayoutInvoice({charge.payment_method_details.type as Stripe.PaymentMethod.Type,)) {- 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}`,- });+ const { paymentIntent, paymentMethod } = await createPaymentIntent({+ stripeId: workspace.stripeId,+ amount: PAYOUT_FAILURE_FEE_CENTS,+ invoiceId: invoice.id,+ description: `Dub Partners payout failure fee for invoice ${invoice.id}`,+ statementDescriptor: "Dub Partners",+ });+ if (paymentIntent) {chargedFailureFee = true;-console.log(`Charged a failure fee of $${PAYOUT_FAILURE_FEE_CENTS / 100} to ${workspace.slug}.`,);}++ if (paymentMethod?.card) {+ cardLast4 = paymentMethod.card.last4;+ }}waitUntil(diff --git a/apps/web/lib/stripe/create-payment-intent.ts b/apps/web/lib/stripe/create-payment-intent.tsindex e2bb901497..31266c098c 100644--- a/apps/web/lib/stripe/create-payment-intent.ts+++ b/apps/web/lib/stripe/create-payment-intent.ts@@ -30,14 +30,14 @@ export const createPaymentIntent = async ({if (cards.data.length === 0 && links.data.length === 0) {console.error(`No valid payment methods found for customer ${stripeId}.`);- return;+ return { paymentIntent: null, paymentMethod: null };}const paymentMethod = cards.data[0] || links.data[0];if (!paymentMethod) {console.error(`No valid payment method found for customer ${stripeId}.`);- return;+ return { paymentIntent: null, paymentMethod: null };}try {@@ -54,20 +54,14 @@ export const createPaymentIntent = async ({statement_descriptor: statementDescriptor,description,},- {- ...(idempotencyKey- ? {- idempotencyKey,- }- : {}),- },+ idempotencyKey ? { idempotencyKey } : {},);console.log(`Payment intent ${paymentIntent.id} created for invoice ${invoiceId} with amount ${currencyFormatter(paymentIntent.amount / 100)}`,);- return paymentIntent;+ return { paymentIntent, paymentMethod };} catch (error) {console.error(error);@@ -76,5 +70,7 @@ export const createPaymentIntent = async ({type: "errors",mention: true,});++ return { paymentIntent: null, paymentMethod: null };}};