From 0a18d9f5a29b8d2b7d6336acf1a6e863553930d4 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 11 May 2024 17:13:27 -0700 Subject: [PATCH] pass tolt params to stripe checkout sessions --- .../src/stripe/helpers/common_metadata_keys.rs | 2 ++ .../stripe_create_checkout_session_json_handler.rs | 5 +++++ ...tripe_create_checkout_session_redirect_handler.rs | 5 +++++ .../create/stripe_create_checkout_session_shared.rs | 12 +++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/service/plugins/billing/src/stripe/helpers/common_metadata_keys.rs b/crates/service/plugins/billing/src/stripe/helpers/common_metadata_keys.rs index ee51efb128..db78d24f93 100644 --- a/crates/service/plugins/billing/src/stripe/helpers/common_metadata_keys.rs +++ b/crates/service/plugins/billing/src/stripe/helpers/common_metadata_keys.rs @@ -3,3 +3,5 @@ pub const METADATA_USER_TOKEN : &str = "user_token"; pub const METADATA_USERNAME : &str = "username"; pub const METADATA_EMAIL : &str = "email"; + +pub const METADATA_TOLT_REFERRAL: &str = "tolt_referral"; diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs index 15d1d4cd76..236caa126d 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs @@ -18,6 +18,10 @@ pub struct CreateCheckoutSessionRequest { /// The (non-Stripe) internal identifier for the product or subscription. /// This will be translated into a Stripe identifier. internal_plan_key: Option, + + /// Optional Tolt referral code + /// See: https://help.tolt.io/en/articles/6843411-how-to-set-up-stripe-with-tolt + maybe_tolt_referral: Option, } // =============== Success Response =============== @@ -51,6 +55,7 @@ pub async fn stripe_create_checkout_session_json_handler( url_redirector: &url_redirector, internal_product_to_stripe_lookup: internal_product_to_stripe_lookup.get_ref(), internal_user_lookup: internal_user_lookup.get_ref(), + maybe_tolt_referral: request.maybe_tolt_referral.as_deref() }).await?; // Best effort to delete Redis session cache diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs index 05c6d983bb..a984942080 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs @@ -17,6 +17,10 @@ use crate::stripe::traits::internal_user_lookup::InternalUserLookup; #[derive(Deserialize)] pub struct CreateCheckoutSessionRequest { product: Option, + + /// Optional Tolt referral code + /// See: https://help.tolt.io/en/articles/6843411-how-to-set-up-stripe-with-tolt + maybe_tolt_referral: Option, } pub async fn stripe_create_checkout_session_redirect_handler( @@ -42,6 +46,7 @@ pub async fn stripe_create_checkout_session_redirect_handler( url_redirector: &url_redirector, internal_product_to_stripe_lookup: internal_product_to_stripe_lookup.get_ref(), internal_user_lookup: internal_user_lookup.get_ref(), + maybe_tolt_referral: request.maybe_tolt_referral.as_deref() }).await?; // Best effort to delete Redis session cache diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs index ba01be1602..bf21e9ac10 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs @@ -8,7 +8,7 @@ use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, Create use reusable_types::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; -use crate::stripe::helpers::common_metadata_keys::{METADATA_EMAIL, METADATA_USER_TOKEN, METADATA_USERNAME}; +use crate::stripe::helpers::common_metadata_keys::{METADATA_EMAIL, METADATA_TOLT_REFERRAL, METADATA_USER_TOKEN, METADATA_USERNAME}; use crate::stripe::http_endpoints::checkout::create::stripe_create_checkout_session_error::CreateCheckoutSessionError; use crate::stripe::stripe_config::{FullUrlOrPath, StripeConfig}; use crate::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; @@ -23,6 +23,10 @@ pub struct CreateStripeCheckoutSessionArgs<'a> { pub url_redirector: &'a ThirdPartyUrlRedirector, pub internal_product_to_stripe_lookup: &'a dyn InternalProductToStripeLookup, pub internal_user_lookup: &'a dyn InternalUserLookup, + + /// Optional Tolt referral code + /// See: https://help.tolt.io/en/articles/6843411-how-to-set-up-stripe-with-tolt + pub maybe_tolt_referral: Option<&'a str>, } /// Create a checkout session and return the URL @@ -115,10 +119,15 @@ pub async fn stripe_create_checkout_session_shared( if let Some(username) = user_metadata.username.as_deref() { metadata.insert(METADATA_USERNAME.to_string(), username.to_string()); } + if let Some(user_email) = user_metadata.user_email.as_deref() { metadata.insert(METADATA_EMAIL.to_string(), user_email.to_string()); } + if let Some(tolt_referral) = args.maybe_tolt_referral.as_deref() { + metadata.insert(METADATA_TOLT_REFERRAL.to_string(), tolt_referral.to_string()); + } + // NB: This metadata attaches to Stripe's Checkout Session object. // This does not attach to the subscription or payment intent, which have their own metadata // objects. (TODO: Confirm this.) @@ -260,6 +269,7 @@ mod tests { url_redirector: &url_redirector, internal_product_to_stripe_lookup: &internal_product_to_stripe_lookup_mock, internal_user_lookup: &internal_user_lookup_mock, + maybe_tolt_referral: None, }).await; // TODO: Sort of throwing my hands up over testing this.