From cf7cd9945ddeaa26fa925f5fee1efaf22dd38651 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 19 Sep 2022 04:39:51 -0400 Subject: [PATCH] Handle grace period days --- .../calculate_subscription_end_date.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/component_subsystem/billing/src/stripe/webhook_event_handlers/customer_subscription/calculate_subscription_end_date.rs b/crates/component_subsystem/billing/src/stripe/webhook_event_handlers/customer_subscription/calculate_subscription_end_date.rs index dcb81e3947..10cc805371 100644 --- a/crates/component_subsystem/billing/src/stripe/webhook_event_handlers/customer_subscription/calculate_subscription_end_date.rs +++ b/crates/component_subsystem/billing/src/stripe/webhook_event_handlers/customer_subscription/calculate_subscription_end_date.rs @@ -1,9 +1,19 @@ use chrono::{Duration, NaiveDateTime}; use crate::stripe::webhook_event_handlers::customer_subscription::subscription_event_extractor::SubscriptionSummary; +use once_cell::sync::Lazy; use std::ops::Add; +const SUBSCRIPTION_GRACE_DAYS: Lazy = Lazy::new(|| { + Duration::days(2) +}); + #[inline] pub fn calculate_subscription_end_date(subscription_summary: &SubscriptionSummary) -> NaiveDateTime { - let SUBSCRIPTION_GRACE_DAYS = Duration::days(2); - subscription_summary.current_billing_period_end.add(SUBSCRIPTION_GRACE_DAYS) + let mut subscription_end = subscription_summary.current_billing_period_end; + + if !subscription_summary.cancel_at_period_end { + subscription_end = subscription_end.add(*SUBSCRIPTION_GRACE_DAYS); + } + + subscription_end }