Force environmental plans

This commit is contained in:
Brandon Thomas committed 2022-11-16 05:42:18 -05:00
1 parent 567f3143d4
commit f690c73ebf
6 files changed
+23 -7

No files matched your search

@@ -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<CreateCheckoutSessionRequest>,
stripe_config: web::Data<StripeConfig>,
stripe_client: web::Data<stripe::Client>,
server_environment: web::Data<ServerEnvironment>,
url_redirector: web::Data<ThirdPartyUrlRedirector>,
internal_product_to_stripe_lookup: web::Data<dyn InternalProductToStripeLookup>,
internal_user_lookup: web::Data<dyn InternalUserLookup>,
@@ -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(),
@@ -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<CreateCheckoutSessionRequest>,
stripe_config: web::Data<StripeConfig>,
stripe_client: web::Data<stripe::Client>,
server_environment: web::Data<ServerEnvironment>,
url_redirector: web::Data<ThirdPartyUrlRedirector>,
internal_product_to_stripe_lookup: web::Data<dyn InternalProductToStripeLookup>,
internal_user_lookup: web::Data<dyn InternalUserLookup>,
@@ -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(),
@@ -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,
@@ -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<Option<StripeProduct>, StripeProductLookupError>;
}
@@ -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<Option<StripeProduct>, 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<Option<StripeProduct>, 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,
@@ -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<T>) for dynamic dispatch
.app_data(web::Data::from(stripe_lookup)) // NB: Data::from(Arc<T>) for dynamic dispatch
.app_data(web::Data::from(user_lookup)) // NB: Data::from(Arc<T>) for dynamic dispatch