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.

2.2 KB
Raw Blame
import { stripe } from "@/lib/stripe";
import { currencyFormatter, log } from "@dub/utils";

export const createPaymentIntent = async ({
  stripeId,
  amount,
  invoiceId,
  description,
  statementDescriptor,
  idempotencyKey,
}: {
  stripeId: string;
  amount: number;
  invoiceId?: string; // used for transfer_group
  description: string;
  statementDescriptor: string;
  idempotencyKey?: string;
}) => {
  const [cards, links] = await Promise.all([
    stripe.paymentMethods.list({
      customer: stripeId,
      type: "card",
    }),

    stripe.paymentMethods.list({
      customer: stripeId,
      type: "link",
    }),
  ]);

  if (cards.data.length === 0 && links.data.length === 0) {
    console.error(`No valid payment methods found for customer ${stripeId}.`);
    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 { paymentIntent: null, paymentMethod: null };
  }

  try {
    const paymentIntent = await stripe.paymentIntents.create(
      {
        amount,
        customer: stripeId,
        ...(invoiceId ? { transfer_group: invoiceId } : {}),
        payment_method_types: ["card", "link"],
        payment_method: paymentMethod.id,
        currency: "usd",
        confirmation_method: "automatic",
        confirm: true,
        // Card PI creation rejects `statement_descriptor`; use suffix (max 22 chars on networks).
        statement_descriptor_suffix: statementDescriptor.slice(0, 22),
        description,
      },
      idempotencyKey ? { idempotencyKey } : undefined,
    );

    console.log(
      `Payment intent ${paymentIntent.id} created ${invoiceId ? `for invoice ${invoiceId} ` : ""}with amount ${currencyFormatter(paymentIntent.amount)}`,
    );

    return { paymentIntent, paymentMethod };
  } catch (error) {
    console.error(error);

    await log({
      message: `Failed to create payment intent${invoiceId ? ` for the invoice ${invoiceId}` : ""}. ${JSON.stringify(error, null, 2)}`,
      type: "errors",
      mention: true,
    });

    return { paymentIntent: null, paymentMethod: null };
  }
};