From 17703310ae651d06b8c6f775cab1b3466e8b1e6c Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 28 Apr 2025 18:04:05 -0400 Subject: [PATCH] collab: Avoid creating duplicate Stripe customers (#29566) This PR makes it so we check for an existing Stripe customer by email address before attempting to create a new one. This should avoid the case where we end up creating multiple Stripe customers for the same user. Release Notes: - N/A --- crates/collab/src/api/billing.rs | 37 ++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/collab/src/api/billing.rs b/crates/collab/src/api/billing.rs index 87645b746b..b635f3daa1 100644 --- a/crates/collab/src/api/billing.rs +++ b/crates/collab/src/api/billing.rs @@ -322,16 +322,35 @@ async fn create_billing_subscription( CustomerId::from_str(&existing_customer.stripe_customer_id) .context("failed to parse customer ID")? } else { - let customer = Customer::create( - &stripe_client, - CreateCustomer { - email: user.email_address.as_deref(), - ..Default::default() - }, - ) - .await?; + let existing_customer = if let Some(email) = user.email_address.as_deref() { + let customers = Customer::list( + &stripe_client, + &stripe::ListCustomers { + email: Some(email), + ..Default::default() + }, + ) + .await?; - customer.id + customers.data.first().cloned() + } else { + None + }; + + if let Some(existing_customer) = existing_customer { + existing_customer.id + } else { + let customer = Customer::create( + &stripe_client, + CreateCustomer { + email: user.email_address.as_deref(), + ..Default::default() + }, + ) + .await?; + + customer.id + } }; let success_url = format!(