Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(revenue_recovery): move grace period and partial authorization to billing connector MCA level (#14803)
CHALLA NISHANTH BABUc585b808c90d
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.jsonindex b73981d5b1..b1a9d80ffd 100644--- a/api-reference/v2/openapi_spec_v2.json+++ b/api-reference/v2/openapi_spec_v2.json@@ -32404,6 +32404,20 @@"example": "5","minimum": 0},+ "grace_period_days": {+ "type": "integer",+ "format": "int32",+ "description": "Grace period, in days from the invoice's billing start, within which retries are allowed\nfor invoices of this `billing connector`. Defaults to 30 days when not provided.",+ "default": 30,+ "example": "30",+ "minimum": 0+ },+ "enable_partial_authorization": {+ "type": "boolean",+ "description": "Allow partial authorization for payments recovered through this `billing connector`.\nA value passed in the recovery payments API request takes priority over this. Defaults to\n`false` when not provided.",+ "default": false,+ "example": false+ },"billing_account_reference": {"type": "integer","format": "int32",diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rsindex 0069c653d9..f4c989d47a 100644--- a/crates/api_models/src/admin.rs+++ b/crates/api_models/src/admin.rs@@ -1368,6 +1368,10 @@ pub struct MerchantConnectorAccountFeatureMetadata {pub revenue_recovery: Option<RevenueRecoveryMetadata>,}+fn default_grace_period_days() -> u16 {+ 30+}+#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, ToSchema)]/// Revenue recovery metadata for merchant connector accountpub struct RevenueRecoveryMetadata {@@ -1382,6 +1386,17 @@ pub struct RevenueRecoveryMetadata {#[serde(default)]#[schema(value_type = u16, example = "5")]pub max_hybrid_cascading_retry_count: u16,+ /// Grace period, in days from the invoice's billing start, within which retries are allowed+ /// for invoices of this `billing connector`. Defaults to 30 days when not provided.+ #[serde(default = "default_grace_period_days")]+ #[schema(value_type = u16, default = 30, example = "30")]+ pub grace_period_days: u16,+ /// Allow partial authorization for payments recovered through this `billing connector`.+ /// A value passed in the recovery payments API request takes priority over this. Defaults to+ /// `false` when not provided.+ #[serde(default)]+ #[schema(default = false, example = false)]+ pub enable_partial_authorization: bool,/// Billing account reference id is payment gateway id at billing connector end./// Merchants need to provide a mapping between these merchant connector account and the corresponding account reference IDs for each `billing connector`.#[schema(value_type = u16, example = r#"{ "mca_vDSg5z6AxnisHq5dbJ6g": "stripe_123", "mca_vDSg5z6AumisHqh4x5m1": "adyen_123" }"#)]diff --git a/crates/diesel_models/src/merchant_connector_account.rs b/crates/diesel_models/src/merchant_connector_account.rsindex 5a57745f53..2e1d885ad6 100644--- a/crates/diesel_models/src/merchant_connector_account.rs+++ b/crates/diesel_models/src/merchant_connector_account.rs@@ -324,6 +324,11 @@ pub struct MerchantConnectorAccountFeatureMetadata {#[cfg(feature = "v2")]common_utils::impl_to_sql_from_sql_json!(MerchantConnectorAccountFeatureMetadata);+#[cfg(feature = "v2")]+fn default_grace_period_days() -> u16 {+ 30+}+#[cfg(feature = "v2")]#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]pub struct RevenueRecoveryMetadata {@@ -333,6 +338,13 @@ pub struct RevenueRecoveryMetadata {pub billing_connector_retry_threshold: u16,#[serde(default)]pub max_hybrid_cascading_retry_count: u16,+ /// Grace period, in days from the invoice's billing start, within which retries are allowed.+ /// Defaults to 30 days when not set.+ #[serde(default = "default_grace_period_days")]+ pub grace_period_days: u16,+ /// Whether payments recovered through this billing connector allow partial authorization.+ #[serde(default)]+ pub enable_partial_authorization: bool,/// Billing account reference id is payment gateway id at billing connector end./// Merchants need to provide a mapping between these merchant connector account and the corresponding/// account reference IDs for each `billing connector`.diff --git a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs b/crates/hyperswitch_domain_models/src/merchant_connector_account.rsindex 10d590e9f0..416fa19662 100644--- a/crates/hyperswitch_domain_models/src/merchant_connector_account.rs+++ b/crates/hyperswitch_domain_models/src/merchant_connector_account.rs@@ -278,6 +278,23 @@ impl MerchantConnectorAccount {.map(|recovery| recovery.max_retry_count)}+ /// Grace period, in days, configured on the billing connector account.+ pub fn get_grace_period_days(&self) -> Option<u16> {+ self.feature_metadata+ .as_ref()+ .and_then(|metadata| metadata.revenue_recovery.as_ref())+ .map(|recovery| recovery.grace_period_days)+ }++ /// Whether partial authorization is enabled on the billing connector account. Defaults to+ /// `false` when the account has no revenue recovery metadata.+ pub fn is_partial_authorization_enabled(&self) -> bool {+ self.feature_metadata+ .as_ref()+ .and_then(|metadata| metadata.revenue_recovery.as_ref())+ .is_some_and(|recovery| recovery.enable_partial_authorization)+ }+/// Positions on the cascading ladder available to an invoice under the hybrid scheme.pub fn get_max_hybrid_cascading_retry_count(&self) -> Option<u16> {self.feature_metadata@@ -374,6 +391,11 @@ pub struct MerchantConnectorAccountFeatureMetadata {pub revenue_recovery: Option<RevenueRecoveryMetadata>,}+#[cfg(feature = "v2")]+fn default_grace_period_days() -> u16 {+ 30+}+#[cfg(feature = "v2")]#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]pub struct RevenueRecoveryMetadata {@@ -383,6 +405,10 @@ pub struct RevenueRecoveryMetadata {/// hybrid static + adaptive scheme.#[serde(default)]pub max_hybrid_cascading_retry_count: u16,+ #[serde(default = "default_grace_period_days")]+ pub grace_period_days: u16,+ #[serde(default)]+ pub enable_partial_authorization: bool,pub mca_reference: AccountReferenceMap,}@@ -836,6 +862,8 @@ impl From<MerchantConnectorAccountFeatureMetadata>.billing_connector_retry_threshold,max_hybrid_cascading_retry_count: recovery_metadata.max_hybrid_cascading_retry_count,+ grace_period_days: recovery_metadata.grace_period_days,+ enable_partial_authorization: recovery_metadata.enable_partial_authorization,billing_account_reference: DieselBillingAccountReference(recovery_metadata.mca_reference.recovery_to_billing,),@@ -861,6 +889,8 @@ impl From<DieselMerchantConnectorAccountFeatureMetadata>.billing_connector_retry_threshold,max_hybrid_cascading_retry_count: recovery_metadata.max_hybrid_cascading_retry_count,+ grace_period_days: recovery_metadata.grace_period_days,+ enable_partial_authorization: recovery_metadata.enable_partial_authorization,mca_reference: AccountReferenceMap {recovery_to_billing: recovery_metadata.billing_account_reference.0,billing_to_recovery,diff --git a/crates/router/src/core/revenue_recovery.rs b/crates/router/src/core/revenue_recovery.rsindex 61b5e00c94..bec1da0774 100644--- a/crates/router/src/core/revenue_recovery.rs+++ b/crates/router/src/core/revenue_recovery.rs@@ -737,22 +737,11 @@ fn record_grace_window_unresolved(reason: &'static str) {/// When the invoice's recovery grace window closes: its billing start plus the configured grace/// period.-async fn get_recovery_grace_window_end(- state: &SessionState,+fn get_recovery_grace_window_end(payment_intent: &PaymentIntent,- billing_connector: common_enums::connector_enums::Connector,+ grace_period_days: u16,) -> CustomResult<time::PrimitiveDateTime, errors::RecoveryError> {- let dimensions = crate::core::configs::dimension_state::Dimensions::new()- .with_processor_merchant_id(payment_intent.merchant_id.clone().into())- .with_connector(billing_connector);-- let grace_period_days = dimensions- .get_recovery_grace_period_days(- state.store.as_ref(),- state.superposition_service.as_ref(),- None,- )- .await;+ let grace_period_days = i64::from(grace_period_days);let grace_window_start = payment_intent.get_revenue_recovery_metadata()@@ -883,14 +872,17 @@ pub async fn perform_calculate_workflow(let grace_window_end = if retry_budget_exhausted {None} else {- Some(- get_recovery_grace_window_end(- state,- payment_intent,- revenue_recovery_payment_data.billing_mca.connector_name,- )- .await?,- )+ let grace_period_days = revenue_recovery_payment_data+ .billing_mca+ .get_grace_period_days()+ .ok_or(errors::RecoveryError::ValueNotFound)+ .attach_printable(+ "Failed to get grace period days from billing merchant connector account",+ )?;+ Some(get_recovery_grace_window_end(+ payment_intent,+ grace_period_days,+ )?)};let grace_window_elapsed = grace_window_enddiff --git a/crates/router/src/core/webhooks/recovery_incoming.rs b/crates/router/src/core/webhooks/recovery_incoming.rsindex a62f760782..4d6967b978 100644--- a/crates/router/src/core/webhooks/recovery_incoming.rs+++ b/crates/router/src/core/webhooks/recovery_incoming.rs@@ -122,7 +122,10 @@ pub async fn recovery_incoming_webhook_flow(connector_enum,request_details,billing_connector_invoice_details.as_ref(),- )?;+ )?+ .with_default_partial_authorization(+ billing_connector_account.is_partial_authorization_enabled(),+ );let is_event_recovery_transaction_event = event_type.is_recovery_transaction_event();@@ -347,6 +350,16 @@ impl RevenueRecoveryInvoice {}).await}+ /// Fills in partial authorization from the billing connector account when the invoice+ /// itself does not carry a value.+ fn with_default_partial_authorization(mut self, default: bool) -> Self {+ self.0.enable_partial_authorization = self+ .0+ .enable_partial_authorization+ .or_else(|| Some(default.into()));+ self+ }+fn get_recovery_invoice_details(connector_enum: &connector_integration_interface::ConnectorEnum,request_details: &hyperswitch_interfaces::webhooks::IncomingWebhookRequestDetails<'_>,diff --git a/crates/router/src/types.rs b/crates/router/src/types.rsindex b406c19821..941881286a 100644--- a/crates/router/src/types.rs+++ b/crates/router/src/types.rs@@ -1621,6 +1621,9 @@ impl ForeignFrom<&domain::MerchantConnectorAccountFeatureMetadata>.billing_connector_retry_threshold,max_hybrid_cascading_retry_count: revenue_recovery_metadata.max_hybrid_cascading_retry_count,+ grace_period_days: revenue_recovery_metadata.grace_period_days,+ enable_partial_authorization: revenue_recovery_metadata+ .enable_partial_authorization,billing_account_reference: revenue_recovery_metadata.mca_reference.recovery_to_billing@@ -1652,6 +1655,9 @@ impl ForeignTryFrom<&api_models::admin::MerchantConnectorAccountFeatureMetadata>.billing_connector_retry_threshold,max_hybrid_cascading_retry_count: revenue_recovery_metadata.max_hybrid_cascading_retry_count,+ grace_period_days: revenue_recovery_metadata.grace_period_days,+ enable_partial_authorization: revenue_recovery_metadata+ .enable_partial_authorization,mca_reference,})})