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.

add idempotencyKey

Kiran K4d1a71366b72
diff --git a/apps/web/app/(ee)/api/cron/domains/renewal-payments/route.ts b/apps/web/app/(ee)/api/cron/domains/renewal-payments/route.ts
index 238898776a..a5ff2933c8 100644
--- a/apps/web/app/(ee)/api/cron/domains/renewal-payments/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/renewal-payments/route.ts
@@ -106,7 +106,7 @@ export async function GET(req: Request) {
type: "domainRenewal",
amount: totalAmount,
total: totalAmount,
- registeredDomains: domains.map(({ slug }) => slug), // array of domain slugs
+ registeredDomains: domains.map(({ slug }) => slug), // array of domain slugs,
},
});
@@ -132,6 +132,7 @@ export async function GET(req: Request) {
invoiceId: invoice.id,
statementDescriptor: "Dub",
description: `Domain renewal invoice (${invoice.id})`,
+ idempotencyKey: `${invoice.id}-${invoice.failedAttempts}`,
});
}
diff --git a/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts b/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
index 998251de02..6df7761818 100644
--- a/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
+++ b/apps/web/app/(ee)/api/cron/invoices/retry-failed/route.ts
@@ -69,6 +69,7 @@ export async function POST(req: Request) {
invoiceId: invoice.id,
statementDescriptor: "Dub",
description: `Domain renewal invoice (${invoice.id})`,
+ idempotencyKey: `${invoice.id}-${invoice.failedAttempts}`,
});
return new Response(`Retrying invoice charge ${invoice.id}...`);
diff --git a/apps/web/lib/stripe/create-payment-intent.ts b/apps/web/lib/stripe/create-payment-intent.ts
index 4365fc8365..e2bb901497 100644
--- a/apps/web/lib/stripe/create-payment-intent.ts
+++ b/apps/web/lib/stripe/create-payment-intent.ts
@@ -7,12 +7,14 @@ export const createPaymentIntent = async ({
invoiceId,
description,
statementDescriptor,
+ idempotencyKey,
}: {
stripeId: string;
amount: number;
invoiceId: string;
description: string;
statementDescriptor: string;
+ idempotencyKey?: string;
}) => {
const [cards, links] = await Promise.all([
stripe.paymentMethods.list({
@@ -39,18 +41,27 @@ export const createPaymentIntent = async ({
}
try {
- const paymentIntent = await stripe.paymentIntents.create({
- amount,
- customer: stripeId,
- transfer_group: invoiceId,
- payment_method_types: ["card", "link"],
- payment_method: paymentMethod.id,
- currency: "usd",
- confirmation_method: "automatic",
- confirm: true,
- statement_descriptor: statementDescriptor,
- description,
- });
+ const paymentIntent = await stripe.paymentIntents.create(
+ {
+ amount,
+ customer: stripeId,
+ transfer_group: invoiceId,
+ payment_method_types: ["card", "link"],
+ payment_method: paymentMethod.id,
+ currency: "usd",
+ confirmation_method: "automatic",
+ confirm: true,
+ statement_descriptor: statementDescriptor,
+ description,
+ },
+ {
+ ...(idempotencyKey
+ ? {
+ idempotencyKey,
+ }
+ : {}),
+ },
+ );
console.log(
`Payment intent ${paymentIntent.id} created for invoice ${invoiceId} with amount ${currencyFormatter(paymentIntent.amount / 100)}`,