[web] purchase: use consistent idempotency params when creating Stripe customer + add network retries

(cherry picked from commit 2953fa0a4205c11c3fc5dd3de67976ad16407dc4)
This commit is contained in:
Yangshun 2025-06-05 09:40:59 +08:00
parent 6e6dbc8040
commit bf02fdef1f
3 changed files with 9 additions and 2 deletions

View File

@ -65,6 +65,7 @@ export async function POST(req: NextRequest) {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: '2023-10-16',
maxNetworkRetries: 2,
});
// This is needed because sometimes this hook is called after the
@ -94,9 +95,10 @@ export async function POST(req: NextRequest) {
}
const customer = await stripe.customers.create(
// Keep parameters across customer creation synced
// because they use the same idempotency key
{
email: user.email,
name: user.user_metadata.name,
},
{
idempotencyKey: user.id,

View File

@ -111,9 +111,12 @@ export default async function handler(req: NextRequest) {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: '2023-10-16',
maxNetworkRetries: 2,
});
const customer = await stripe.customers.create(
// Keep parameters across customer creation synced
// because they use the same idempotency key
{
email: user.email,
},

View File

@ -395,6 +395,7 @@ export const promotionsRouter = router({
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
apiVersion: '2023-10-16',
maxNetworkRetries: 2,
});
let stripeCustomer = profile?.stripeCustomer;
@ -412,9 +413,10 @@ export const promotionsRouter = router({
}
const newCustomer = await stripe.customers.create(
// Keep parameters across customer creation synced
// because they use the same idempotency key
{
email: user.email,
name: user.user_metadata.name,
},
{
idempotencyKey: user.id,