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
This commit is contained in:
Marshall Bowers 2025-04-28 18:04:05 -04:00 committed by GitHub
parent bbe8d6a654
commit 17703310ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -321,6 +321,24 @@ async fn create_billing_subscription(
let customer_id = if let Some(existing_customer) = &existing_billing_customer {
CustomerId::from_str(&existing_customer.stripe_customer_id)
.context("failed to parse customer ID")?
} else {
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?;
customers.data.first().cloned()
} else {
None
};
if let Some(existing_customer) = existing_customer {
existing_customer.id
} else {
let customer = Customer::create(
&stripe_client,
@ -332,6 +350,7 @@ async fn create_billing_subscription(
.await?;
customer.id
}
};
let success_url = format!(