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:
parent
bbe8d6a654
commit
17703310ae
1 changed files with 28 additions and 9 deletions
|
@ -322,16 +322,35 @@ async fn create_billing_subscription(
|
||||||
CustomerId::from_str(&existing_customer.stripe_customer_id)
|
CustomerId::from_str(&existing_customer.stripe_customer_id)
|
||||||
.context("failed to parse customer ID")?
|
.context("failed to parse customer ID")?
|
||||||
} else {
|
} else {
|
||||||
let customer = Customer::create(
|
let existing_customer = if let Some(email) = user.email_address.as_deref() {
|
||||||
&stripe_client,
|
let customers = Customer::list(
|
||||||
CreateCustomer {
|
&stripe_client,
|
||||||
email: user.email_address.as_deref(),
|
&stripe::ListCustomers {
|
||||||
..Default::default()
|
email: Some(email),
|
||||||
},
|
..Default::default()
|
||||||
)
|
},
|
||||||
.await?;
|
)
|
||||||
|
.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!(
|
let success_url = format!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue