From 780ee74b95dff6317b2b3f9df6a2edd1d0f09d4a Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 16 Sep 2025 22:30:13 -0400 Subject: [PATCH] user->customer link on checkout and portal --- ...ft_create_credits_pack_checkout_handler.rs | 2 +- ...ft_create_subscription_checkout_handler.rs | 37 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_credits_pack_checkout_handler.rs b/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_credits_pack_checkout_handler.rs index 00e4c5cb4c..2d0f5db368 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_credits_pack_checkout_handler.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_credits_pack_checkout_handler.rs @@ -12,6 +12,7 @@ use component_traits::traits::internal_user_lookup::InternalUserLookup; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; use enums::common::payments_namespace::PaymentsNamespace; use log::{error, info, warn}; +use mysql_queries::queries::users::user_stripe_customer_links::find_user_stripe_customer_link::find_user_stripe_customer_link_using_connection; use mysql_queries::queries::users::user_subscriptions::find_subscription_for_owner_user::find_subscription_for_owner_user_using_connection; use reusable_types::server_environment::ServerEnvironment; use sqlx::MySqlPool; @@ -22,7 +23,6 @@ use stripe_checkout::checkout_session::{CreateCheckoutSession, CreateCheckoutSes use stripe_checkout::CheckoutSessionMode; use stripe_core::CustomerId; use stripe_shared::CheckoutSessionCustomerCreation; -use mysql_queries::queries::users::user_stripe_customer_links::find_user_stripe_customer_link::find_user_stripe_customer_link_using_connection; use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; //use utoipa::ToSchema; diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_subscription_checkout_handler.rs b/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_subscription_checkout_handler.rs index 1ab83c5e05..175ac01020 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_subscription_checkout_handler.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/checkout/stripe_artcraft_create_subscription_checkout_handler.rs @@ -19,6 +19,7 @@ use std::sync::Arc; use stripe_checkout::checkout_session::{CreateCheckoutSession, CreateCheckoutSessionAutomaticTax, CreateCheckoutSessionLineItems, CreateCheckoutSessionSavedPaymentMethodOptions, CreateCheckoutSessionSavedPaymentMethodOptionsAllowRedisplayFilters, CreateCheckoutSessionSavedPaymentMethodOptionsPaymentMethodSave, CreateCheckoutSessionSubscriptionData}; use stripe_checkout::CheckoutSessionMode; use stripe_core::CustomerId; +use mysql_queries::queries::users::user_stripe_customer_links::find_user_stripe_customer_link::find_user_stripe_customer_link_using_connection; use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; //use utoipa::ToSchema; @@ -117,6 +118,21 @@ pub async fn stripe_artcraft_create_subscription_session_handler( // let maybe_existing_inactive_stripe_customer_id = maybe_inactive_subscription.as_ref() // .map(|sub| sub.stripe_customer_id.as_str()); + let mut maybe_existing_stripe_customer_id = None; + + let result = find_user_stripe_customer_link_using_connection( + &user_metadata.user_token_typed, + PaymentsNamespace::Artcraft, + &mut mysql_connection + ).await; + + // NB: Fail silently. + if let Ok(Some(link)) = result { + maybe_existing_stripe_customer_id = CustomerId::from_str(&link.stripe_customer_id).ok(); + } else if let Err(err) = result { + warn!("Error looking up user's ({}) existing stripe customer link: {:?}", &user_metadata.user_token_typed, err); + } + let success_url = stripe_config.checkout_success_url.clone(); let cancel_url = stripe_config.checkout_cancel_url.clone(); @@ -191,23 +207,10 @@ pub async fn stripe_artcraft_create_subscription_session_handler( }) ; - // NB: This works, but it feels really weird to have the customer's old email shown in a - // completely immutable state in the stripe checkout form. - // - // if let Some(existing_inactive_stripe_customer_id) = maybe_existing_inactive_stripe_customer_id{ - // info!("Adding existing stripe customer id to checkout session: {}", existing_inactive_stripe_customer_id); - // match CustomerId::from_str(existing_inactive_stripe_customer_id) { - // Ok(customer_id) => { - // checkout_builder = checkout_builder.customer(customer_id); - // } - // Err(err) => { - // // NB: Don't block checkout. - // warn!("Error parsing user's ({}) supposed existing stripe customer id: {:?}", - // &user_metadata.user_token, - // err); - // } - // } - // } + if let Some(customer_id) = maybe_existing_stripe_customer_id { + info!("Adding existing stripe customer id to checkout session: {}", customer_id.as_str()); + checkout_builder = checkout_builder.customer(customer_id); + } let checkout_session = checkout_builder .send(&stripe_config.client)