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.

fix: stripe webhook not processing incomplete status transitions (#2430)

dev-rbc377e36b2764
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index b8b1654b86..0b69b5653c 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -610,6 +610,7 @@ dependencies = [
"macro_user_id",
"macro_uuid",
"maud",
+ "miniserde",
"mockall",
"model",
"model-entity",
@@ -6400,9 +6401,9 @@ dependencies = [
[[package]]
name = "mini-internal"
-version = "0.1.43"
+version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "560f32b6891d8d9bade8942c45a27694f16d789d3b4b8e6b7135a5240de0a8af"
+checksum = "2a8df435db5df1dd82a74f77e3c3addf6ab7665079c31e222a64f34f7475d87e"
dependencies = [
"proc-macro2",
"quote",
@@ -6417,13 +6418,13 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniserde"
-version = "0.1.43"
+version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ac79f4123c070de643a7a93b9339abf18c30005c622bf4b1c29c2c0960f52d39"
+checksum = "8c48e83ec09ab8a51d4e6be46608bf2a1293a79e2f7ea60246a2ce50eaef44ba"
dependencies = [
"itoa",
"mini-internal",
- "ryu",
+ "zmij",
]
[[package]]
@@ -12474,6 +12475,12 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3be3d40e40a133f9c916ee3f9f4fa2d9d63435b5fbe1bfc6d9dae0aa0ada1513"
+[[package]]
+name = "zmij"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
+
[[package]]
name = "zopfli"
version = "0.8.1"
diff --git a/rust/cloud-storage/authentication_service/Cargo.toml b/rust/cloud-storage/authentication_service/Cargo.toml
index 3663156b44..ab837a3eb6 100644
--- a/rust/cloud-storage/authentication_service/Cargo.toml
+++ b/rust/cloud-storage/authentication_service/Cargo.toml
@@ -78,6 +78,7 @@ macro_tower_layers = { path = "../macro_tower_layers" }
macro_user_id = { path = "../macro_user_id" }
macro_uuid = { path = "../macro_uuid" }
maud = { version = "0.27.0" }
+miniserde = "0.1.45"
mockall = { workspace = true }
model = { path = "../model" }
model-entity = { path = "../model-entity" }
diff --git a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
index 50aa5b811a..717855f693 100644
--- a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
+++ b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs
@@ -12,11 +12,24 @@ use axum::{
};
use macro_user_id::cowlike::CowLike;
use macro_user_id::email::Email;
+use miniserde::json::Value as JsonValue;
use model::response::ErrorResponse;
use referral::domain::ports::ReferralService;
use roles_and_permissions::domain::{model::ProductTier, port::UserRolesAndPermissionsService};
use serde::Serialize;
use stripe_webhook::{EventObject, EventType};
+
+/// Extracts the previous status from the previous_attributes JSON value.
+fn extract_previous_status(previous_attributes: &Option<JsonValue>) -> Option<String> {
+ let attrs = previous_attributes.as_ref()?;
+ match attrs {
+ JsonValue::Object(map) => match map.get("status")? {
+ JsonValue::String(s) => Some(s.clone()),
+ _ => None,
+ },
+ _ => None,
+ }
+}
use teams::domain::team_repo::TeamService;
use tracing::Instrument;
@@ -78,12 +91,19 @@ pub async fn handler(
);
let event_type = event.type_.clone();
+ let previous_attributes = event.data.previous_attributes.clone();
match event.type_ {
EventType::CustomerSubscriptionCreated
| EventType::CustomerSubscriptionUpdated
| EventType::CustomerSubscriptionDeleted
| EventType::CustomerSubscriptionPaused => {
- handle_customer_subscription_event(&ctx, event.data.object, event_type).await
+ handle_customer_subscription_event(
+ &ctx,
+ event.data.object,
+ event_type,
+ previous_attributes,
+ )
+ .await
}
_ => {
tracing::error!(event_type=?event.type_, "unexpected event type");
@@ -98,11 +118,12 @@ pub async fn handler(
Ok(StatusCode::OK.into_response())
}
-#[tracing::instrument(skip(ctx, event_object), err, ret)]
+#[tracing::instrument(skip(ctx, event_object, previous_attributes), err, ret)]
async fn handle_customer_subscription_event(
ctx: &ApiContext,
event_object: EventObject,
event_type: EventType,
+ previous_attributes: Option<JsonValue>,
) -> anyhow::Result<()> {
let subscription = match event_object {
EventObject::CustomerSubscriptionCreated(subscription) => subscription,
@@ -114,10 +135,30 @@ async fn handle_customer_subscription_event(
}
};
- if subscription.status.as_str() == "incomplete" {
+ let current_status = subscription.status.as_str();
+
+ // Skip processing if the subscription is still incomplete
+ if current_status == "incomplete" {
+ tracing::debug!("skipping incomplete subscription");
return Ok(());
}
+ // For subscription.updated events, check if we're transitioning from incomplete
+ // to active/trialing. If so, this is effectively a "new" subscription activation.
+ let previous_status = extract_previous_status(&previous_attributes);
+ let is_transition_from_incomplete =
+ matches!(event_type, EventType::CustomerSubscriptionUpdated)
+ && previous_status.as_deref() == Some("incomplete")
+ && matches!(current_status, "active" | "trialing");
+
+ if is_transition_from_incomplete {
+ tracing::info!(
+ previous_status = ?previous_status,
+ current_status = current_status,
+ "subscription transitioned from incomplete to active state"
+ );
+ }
+
// Stripe CustomerId and subscription CustomerId are not the same type...
let customer_id = subscription
.customer
@@ -170,6 +211,12 @@ async fn handle_customer_subscription_event(
"processing stripe subscription"
);
+ // Determine if this should be treated as a new subscription for analytics purposes.
+ // A subscription is "new" if it's a CustomerSubscriptionCreated event OR if it
+ // transitioned from incomplete to active/trialing (meaning payment succeeded).
+ let is_new_subscription = matches!(event_type, EventType::CustomerSubscriptionCreated)
+ || is_transition_from_incomplete;
+
// Get subscription metadata, if this is a team subscription then we need to handle it
// separately.
if let Some(team_id) = subscription.metadata.get("team_id") {
@@ -186,7 +233,7 @@ async fn handle_customer_subscription_event(
value_cents: subscription_value,
currency: subscription_currency,
status: subscription_status.to_string(),
- is_new: matches!(event_type, EventType::CustomerSubscriptionCreated),
+ is_new: is_new_subscription,
},
)
.await;
@@ -313,7 +360,7 @@ async fn handle_customer_subscription_event(
value_cents: subscription_value,
currency: subscription_currency,
status: subscription_status.to_string(),
- is_new: matches!(event_type, EventType::CustomerSubscriptionCreated),
+ is_new: is_new_subscription,
},
);