From f690c73ebf74b1617b30bc7df168a34a8751bef7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 16 Nov 2022 05:42:18 -0500 Subject: [PATCH] Force environmental plans --- .../stripe_create_checkout_session_json_handler.rs | 3 +++ ...stripe_create_checkout_session_redirect_handler.rs | 3 +++ .../create/stripe_create_checkout_session_shared.rs | 9 ++++++--- .../traits/internal_product_to_stripe_lookup.rs | 3 ++- .../billing/internal_product_to_stripe_lookup_impl.rs | 11 ++++++++--- crates/service/storyteller_web/src/main.rs | 1 + 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs index 146d064223..cedab33f53 100644 --- a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs +++ b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs @@ -16,6 +16,7 @@ use sqlx::MySqlPool; use std::collections::HashMap; use std::fmt; use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, CreateCheckoutSessionLineItems}; +use url_config::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; // =============== Request =============== @@ -40,6 +41,7 @@ pub async fn stripe_create_checkout_session_json_handler( request: Json, stripe_config: web::Data, stripe_client: web::Data, + server_environment: web::Data, url_redirector: web::Data, internal_product_to_stripe_lookup: web::Data, internal_user_lookup: web::Data, @@ -51,6 +53,7 @@ pub async fn stripe_create_checkout_session_json_handler( maybe_internal_product_key, &http_request, &stripe_config, + server_environment.as_ref().clone(), &stripe_client, &url_redirector, internal_product_to_stripe_lookup.get_ref(), diff --git a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs index 909f2c511f..dd9a622a56 100644 --- a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs +++ b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs @@ -15,6 +15,7 @@ use sqlx::MySqlPool; use std::collections::HashMap; use std::fmt; use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, CreateCheckoutSessionLineItems}; +use url_config::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; // =============== Request =============== @@ -29,6 +30,7 @@ pub async fn stripe_create_checkout_session_redirect_handler( request: Query, stripe_config: web::Data, stripe_client: web::Data, + server_environment: web::Data, url_redirector: web::Data, internal_product_to_stripe_lookup: web::Data, internal_user_lookup: web::Data, @@ -40,6 +42,7 @@ pub async fn stripe_create_checkout_session_redirect_handler( maybe_internal_product_key, &http_request, &stripe_config, + server_environment.get_ref().clone(), &stripe_client, &url_redirector, internal_product_to_stripe_lookup.get_ref(), diff --git a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs index 484f9ab047..30e2b98ae8 100644 --- a/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs +++ b/crates/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_shared.rs @@ -11,6 +11,7 @@ use sqlx::MySqlPool; use std::collections::HashMap; use std::str::FromStr; use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, CreateCheckoutSessionAutomaticTax, CreateCheckoutSessionLineItems, CreateCheckoutSessionPaymentIntentData, CreateCheckoutSessionSubscriptionData, CustomerId, ParseIdError}; +use url_config::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; /// Create a checkout session and return the URL @@ -19,6 +20,7 @@ pub async fn stripe_create_checkout_session_shared( maybe_internal_product_key: Option<&str>, http_request: &HttpRequest, stripe_config: &StripeConfig, + server_environment: ServerEnvironment, stripe_client: &stripe::Client, url_redirector: &ThirdPartyUrlRedirector, internal_product_to_stripe_lookup: &dyn InternalProductToStripeLookup, @@ -31,7 +33,7 @@ pub async fn stripe_create_checkout_session_shared( }; let stripe_product = internal_product_to_stripe_lookup - .lookup_stripe_product_from_internal_product_key(internal_product_key) + .lookup_stripe_product_from_internal_product_key(server_environment, internal_product_key) .map_err(|err| { error!("Error looking up product: {:?}", err); CreateCheckoutSessionError::ServerError // NB: This was probably *our* fault. @@ -224,8 +226,8 @@ mod tests { let mut internal_product_to_stripe_lookup_mock = MockInternalProductToStripeLookup::new(); internal_product_to_stripe_lookup_mock.expect_lookup_stripe_product_from_internal_product_key() - .with(eq("TEST_FAKEYOU_PRODUCT")) - .returning(|_| Ok(Some(StripeProduct { + .with(eq(ServerEnvironment::Development), eq("TEST_FAKEYOU_PRODUCT")) + .returning(|_, _| Ok(Some(StripeProduct { stripe_product_id: "TEST_PRODUCT_ID".to_string(), stripe_price_id: "TEST_PRICE_ID".to_string(), is_subscription_product: true, @@ -247,6 +249,7 @@ mod tests { maybe_internal_product_key, &http_request, &stripe_config, + ServerEnvironment::Development, &stripe_client, &url_redirector, &internal_product_to_stripe_lookup_mock, diff --git a/crates/plugins/billing/src/stripe/traits/internal_product_to_stripe_lookup.rs b/crates/plugins/billing/src/stripe/traits/internal_product_to_stripe_lookup.rs index a332580d58..31731532f3 100644 --- a/crates/plugins/billing/src/stripe/traits/internal_product_to_stripe_lookup.rs +++ b/crates/plugins/billing/src/stripe/traits/internal_product_to_stripe_lookup.rs @@ -3,6 +3,7 @@ use mockall::{automock, mock, predicate::*}; use std::error::Error; use std::fmt::{Display, Formatter}; +use url_config::server_environment::ServerEnvironment; /// Errors for this component are not strongly typed. #[derive(Debug)] @@ -37,6 +38,6 @@ pub struct StripeProduct { #[cfg_attr(test, automock)] pub trait InternalProductToStripeLookup { /// Look up stripe product information from an internal system identifier. - fn lookup_stripe_product_from_internal_product_key(&self, internal_product_key: &str) + fn lookup_stripe_product_from_internal_product_key(&self, server_environment: ServerEnvironment, internal_product_key: &str) -> Result, StripeProductLookupError>; } diff --git a/crates/service/storyteller_web/src/billing/internal_product_to_stripe_lookup_impl.rs b/crates/service/storyteller_web/src/billing/internal_product_to_stripe_lookup_impl.rs index f6146ed62f..52a0cbc54d 100644 --- a/crates/service/storyteller_web/src/billing/internal_product_to_stripe_lookup_impl.rs +++ b/crates/service/storyteller_web/src/billing/internal_product_to_stripe_lookup_impl.rs @@ -1,13 +1,18 @@ use billing_component::stripe::traits::internal_product_to_stripe_lookup::{InternalProductToStripeLookup, StripeProduct, StripeProductLookupError}; -use crate::configs::plans::plan_list::ALL_PLANS_BY_SLUG; +use crate::configs::plans::plan_list::{ALL_PLANS_BY_SLUG, DEVELOPMENT_PREMIUM_PLANS_BY_SLUG, PRODUCTION_PREMIUM_PLANS_BY_SLUG}; +use url_config::server_environment::ServerEnvironment; /// A simple Actix injectable action #[derive(Clone, Copy)] pub struct InternalProductToStripeLookupImpl {} impl InternalProductToStripeLookup for InternalProductToStripeLookupImpl { - fn lookup_stripe_product_from_internal_product_key(&self, internal_product_key: &str) -> Result, StripeProductLookupError> { - Ok(ALL_PLANS_BY_SLUG.get(internal_product_key) + fn lookup_stripe_product_from_internal_product_key(&self, server_environment: ServerEnvironment, internal_product_key: &str) -> Result, StripeProductLookupError> { + let plans_by_slug = match server_environment { + ServerEnvironment::Development => &DEVELOPMENT_PREMIUM_PLANS_BY_SLUG, + ServerEnvironment::Production => &PRODUCTION_PREMIUM_PLANS_BY_SLUG, + }; + Ok(plans_by_slug.get(internal_product_key) .and_then(|plan| { let stripe_product_id = match plan.stripe_product_id() { None => return None, diff --git a/crates/service/storyteller_web/src/main.rs b/crates/service/storyteller_web/src/main.rs index 5f8550cfbb..0efdd977e9 100644 --- a/crates/service/storyteller_web/src/main.rs +++ b/crates/service/storyteller_web/src/main.rs @@ -433,6 +433,7 @@ pub async fn serve(server_state: ServerState) -> AnyhowResult<()> .app_data(web::Data::new(server_state_arc.stripe.clone().config.clone())) .app_data(web::Data::new(server_state_arc.stripe.clone().client.clone())) .app_data(web::Data::new(server_state_arc.third_party_url_redirector.clone())) + .app_data(web::Data::new(server_state_arc.server_environment.clone())) .app_data(web::Data::from(product_lookup)) // NB: Data::from(Arc) for dynamic dispatch .app_data(web::Data::from(stripe_lookup)) // NB: Data::from(Arc) for dynamic dispatch .app_data(web::Data::from(user_lookup)) // NB: Data::from(Arc) for dynamic dispatch