Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(teams): support stripe trials in the checkout process (#3391)

Will Hutchinsond79ec7a0144f
diff --git a/rust/cloud-storage/teams/src/domain/customer_repo.rs b/rust/cloud-storage/teams/src/domain/customer_repo.rs
index 0cdc47865f..7d8945a427 100644
--- a/rust/cloud-storage/teams/src/domain/customer_repo.rs
+++ b/rust/cloud-storage/teams/src/domain/customer_repo.rs
@@ -49,5 +49,6 @@ pub trait CustomerRepository: Clone + Send + Sync + 'static {
team_id: &uuid::Uuid,
customer_id: stripe::CustomerId,
req: &TeamCheckoutSessionRequest,
+ has_trialed: bool,
) -> impl Future<Output = Result<String, CustomerError>> + Send;
}
diff --git a/rust/cloud-storage/teams/src/domain/team_repo.rs b/rust/cloud-storage/teams/src/domain/team_repo.rs
index f47e76b95a..9e92536e52 100644
--- a/rust/cloud-storage/teams/src/domain/team_repo.rs
+++ b/rust/cloud-storage/teams/src/domain/team_repo.rs
@@ -40,6 +40,12 @@ pub trait TeamRepository: Clone + Send + Sync + 'static {
user_id: &MacroUserIdStr<'_>,
) -> impl Future<Output = Result<Option<stripe::CustomerId>, TeamError>> + Send;
+ /// Checks if a user has already used a trial.
+ fn has_user_trialed(
+ &self,
+ user_id: &MacroUserIdStr<'_>,
+ ) -> impl Future<Output = Result<bool, TeamError>> + Send;
+
/// Gets the subscription id for a team
fn get_team_subscription_id(
&self,
diff --git a/rust/cloud-storage/teams/src/domain/team_service.rs b/rust/cloud-storage/teams/src/domain/team_service.rs
index 5fb2381fd7..88e71bc292 100644
--- a/rust/cloud-storage/teams/src/domain/team_service.rs
+++ b/rust/cloud-storage/teams/src/domain/team_service.rs
@@ -692,6 +692,8 @@ where
.get_authenticated_user()
.map_err(TeamError::AccessError)?;
+ let has_trialed = self.team_repository.has_user_trialed(user_id).await?;
+
let stripe_customer_id: stripe::CustomerId =
match self.team_repository.get_stripe_customer_id(user_id).await? {
Some(customer_id) => customer_id,
@@ -710,7 +712,7 @@ where
let url = self
.customer_repository
- .create_team_checkout_session(&team_id, stripe_customer_id, req)
+ .create_team_checkout_session(&team_id, stripe_customer_id, req, has_trialed)
.await?;
Ok(url)
diff --git a/rust/cloud-storage/teams/src/domain/team_service/test.rs b/rust/cloud-storage/teams/src/domain/team_service/test.rs
index 2f942ae1ab..518c5234f0 100644
--- a/rust/cloud-storage/teams/src/domain/team_service/test.rs
+++ b/rust/cloud-storage/teams/src/domain/team_service/test.rs
@@ -109,6 +109,13 @@ impl TeamRepository for MockTeamRepository {
async move { Ok(subscription_id) }
}
+ fn has_user_trialed(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ ) -> impl Future<Output = Result<bool, TeamError>> + Send {
+ async { Ok(false) }
+ }
+
fn create_team(
&self,
_: &MacroUserIdStr<'_>,
@@ -422,6 +429,7 @@ impl CustomerRepository for MockCustomerRepository {
_team_id: &uuid::Uuid,
_customer_id: stripe::CustomerId,
_req: &TeamCheckoutSessionRequest,
+ _has_trialed: bool,
) -> impl Future<Output = Result<String, CustomerError>> + Send {
async { unimplemented!() }
}
diff --git a/rust/cloud-storage/teams/src/outbound/customer_repo.rs b/rust/cloud-storage/teams/src/outbound/customer_repo.rs
index c32f7c66fd..ff835509c7 100644
--- a/rust/cloud-storage/teams/src/outbound/customer_repo.rs
+++ b/rust/cloud-storage/teams/src/outbound/customer_repo.rs
@@ -239,6 +239,7 @@ impl CustomerRepository for CustomerRepositoryImpl {
team_id: &uuid::Uuid,
customer_id: stripe::CustomerId,
req: &TeamCheckoutSessionRequest,
+ has_trialed: bool,
) -> Result<String, CustomerError> {
let promo_code_id = if let Some(discount) = req.discount.as_ref() {
let mut list_params = stripe::ListPromotionCodes::new();
@@ -278,12 +279,16 @@ impl CustomerRepository for CustomerRepositoryImpl {
metadata.insert("fbc".to_string(), fbc.clone());
}
- // Only set subscription_data if we have metadata to include
- let subscription_data =
- (!metadata.is_empty()).then_some(stripe::CreateCheckoutSessionSubscriptionData {
- metadata: Some(metadata),
+ let subscription_metadata = (!metadata.is_empty()).then_some(metadata);
+
+ // Only set subscription_data if we have metadata or a trial to include.
+ let subscription_data = (subscription_metadata.is_some() || !has_trialed).then_some(
+ stripe::CreateCheckoutSessionSubscriptionData {
+ metadata: subscription_metadata,
+ trial_period_days: (!has_trialed).then_some(60),
..Default::default()
- });
+ },
+ );
let price_id = self
.team_plan_stripe_price_ids
diff --git a/rust/cloud-storage/teams/src/outbound/team_repo.rs b/rust/cloud-storage/teams/src/outbound/team_repo.rs
index 379afe19b5..7bf0826583 100644
--- a/rust/cloud-storage/teams/src/outbound/team_repo.rs
+++ b/rust/cloud-storage/teams/src/outbound/team_repo.rs
@@ -213,6 +213,23 @@ impl TeamRepository for TeamRepositoryImpl {
}
}
+ #[tracing::instrument(skip(self), err)]
+ async fn has_user_trialed(&self, user_id: &MacroUserIdStr<'_>) -> Result<bool, TeamError> {
+ let has_trialed = sqlx::query_scalar::<_, bool>(
+ r#"
+ SELECT mu.has_trialed
+ FROM "User" u
+ INNER JOIN macro_user mu ON mu.id = u.macro_user_id
+ WHERE u.id = $1
+ "#,
+ )
+ .bind(user_id.as_ref())
+ .fetch_one(&self.pool)
+ .await?;
+
+ Ok(has_trialed)
+ }
+
#[tracing::instrument(skip(self), err)]
async fn get_team_subscription_id(
&self,
diff --git a/rust/cloud-storage/teams/src/outbound/team_repo/test.rs b/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
index ea7a6ef311..e90b28758d 100644
--- a/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
+++ b/rust/cloud-storage/teams/src/outbound/team_repo/test.rs
@@ -29,6 +29,32 @@ async fn test_get_stripe_customer_id(pool: Pool<Postgres>) -> anyhow::Result<()>
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("teams"))
+)]
+async fn test_has_user_trialed(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ sqlx::query("UPDATE macro_user SET has_trialed = FALSE WHERE email = '[email protected]'")
+ .execute(&pool)
+ .await?;
+
+ let team_repo = TeamRepositoryImpl::new(pool);
+
+ let has_trialed = team_repo
+ .has_user_trialed(&MacroUserIdStr::parse_from_str("macro|[email protected]")?)
+ .await?;
+
+ assert!(has_trialed);
+
+ let has_trialed = team_repo
+ .has_user_trialed(&MacroUserIdStr::parse_from_str("macro|[email protected]")?)
+ .await?;
+
+ assert!(!has_trialed);
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("teams"))