Skip to content

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

fix(webhooks): delay outgoing webhook until recurrence flow completes (#13660)

Sayak Bhattacharya28fd8d21a791
diff --git a/crates/hyperswitch_connectors/src/connectors/santander.rs b/crates/hyperswitch_connectors/src/connectors/santander.rs
index f254c75b3b..e2ed8c5c17 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander.rs
@@ -2145,16 +2145,13 @@ static SANTANDER_SUPPORTED_WEBHOOK_FLOWS: [enums::EventClass; 0] = [];
impl ConnectorSpecifications for Santander {
fn is_payment_recurrence_operation_needed(
&self,
- payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ setup_future_usage: Option<common_enums::FutureUsage>,
+ current_flow: Option<CurrentFlowInfo>,
) -> Option<bool> {
- #[cfg(feature = "v1")]
- {
- Some(payment_intent.setup_future_usage == Some(common_enums::FutureUsage::OffSession))
- }
- #[cfg(feature = "v2")]
- {
- Some(payment_intent.setup_future_usage == common_enums::FutureUsage::OffSession)
- }
+ Some(
+ setup_future_usage == Some(common_enums::FutureUsage::OffSession)
+ && matches!(current_flow, Some(CurrentFlowInfo::Authorize { .. }) | None),
+ )
}
fn get_connector_about(&self) -> Option<&'static ConnectorInfo> {
diff --git a/crates/hyperswitch_interfaces/src/api.rs b/crates/hyperswitch_interfaces/src/api.rs
index 12d254b5fe..10e4990a23 100644
--- a/crates/hyperswitch_interfaces/src/api.rs
+++ b/crates/hyperswitch_interfaces/src/api.rs
@@ -546,7 +546,8 @@ pub trait ConnectorSpecifications {
/// Validate if another operation is required
fn is_payment_recurrence_operation_needed(
&self,
- _payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ _setup_future_usage: Option<common_enums::FutureUsage>,
+ _current_flow: Option<CurrentFlowInfo>,
) -> Option<bool> {
Some(false)
}
diff --git a/crates/hyperswitch_interfaces/src/connector_integration_interface.rs b/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
index 3efa926f90..0412208a1b 100644
--- a/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
+++ b/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
@@ -827,14 +827,15 @@ impl ConnectorSpecifications for ConnectorEnum {
fn is_payment_recurrence_operation_needed(
&self,
- payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ setup_future_usage: Option<common_enums::FutureUsage>,
+ current_flow: Option<CurrentFlowInfo>,
) -> Option<bool> {
match self {
Self::Old(connector) => {
- connector.is_payment_recurrence_operation_needed(payment_intent)
+ connector.is_payment_recurrence_operation_needed(setup_future_usage, current_flow)
}
Self::New(connector) => {
- connector.is_payment_recurrence_operation_needed(payment_intent)
+ connector.is_payment_recurrence_operation_needed(setup_future_usage, current_flow)
}
}
}
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 3f6abbb181..051e67cfc0 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -73,6 +73,7 @@ use hyperswitch_domain_models::{
payments::{self, payment_intent::CustomerData, ClickToPayMetaData},
router_data::{AccessToken, FeatureData},
};
+use hyperswitch_interfaces::api::ConnectorSpecifications;
use hyperswitch_masking::{ExposeInterface, PeekInterface, Secret};
#[cfg(feature = "v2")]
use operations::ValidateStatusForOperation;
@@ -859,6 +860,7 @@ where
let mut connector_http_status_code = None;
let mut external_latency = None;
+ let mut current_flow_info_for_recurrence_webhook = None;
if let Some(connector_details) = connector {
// Fetch and check FRM configs
#[cfg(feature = "frm")]
@@ -1117,6 +1119,7 @@ where
connector_http_status_code = router_data.connector_http_status_code;
external_latency = router_data.external_latency;
+ current_flow_info_for_recurrence_webhook = router_data.current_flow_info();
//add connector http status code metrics
add_connector_http_status_code_metrics(connector_http_status_code);
@@ -1342,6 +1345,7 @@ where
let operation = Box::new(PaymentResponse);
connector_http_status_code = router_data.connector_http_status_code;
external_latency = router_data.external_latency;
+ current_flow_info_for_recurrence_webhook = router_data.current_flow_info();
//add connector http status code metrics
add_connector_http_status_code_metrics(connector_http_status_code);
@@ -1544,16 +1548,42 @@ where
)
.await?;
- utils::trigger_payments_webhook(
- platform,
- business_profile,
- cloned_payment_data,
- state,
- operation,
- )
- .await
- .map_err(|error| logger::warn!(payments_outgoing_webhook_error=?error))
- .ok();
+ let should_delay_webhook_for_recurring_payment = current_flow_info_for_recurrence_webhook
+ .is_some()
+ && payment_data
+ .get_payment_attempt_connector()
+ .and_then(|connector_name| {
+ api::ConnectorData::get_connector_by_name(
+ &state.conf.connectors,
+ connector_name,
+ api::GetToken::Connector,
+ None,
+ )
+ .ok()
+ })
+ .and_then(|connector_data| {
+ let setup_future_usage = payment_data.get_payment_intent().setup_future_usage;
+ connector_data
+ .connector
+ .is_payment_recurrence_operation_needed(
+ setup_future_usage,
+ current_flow_info_for_recurrence_webhook,
+ )
+ })
+ .unwrap_or(false);
+
+ if !should_delay_webhook_for_recurring_payment {
+ utils::trigger_payments_webhook(
+ platform,
+ business_profile,
+ cloned_payment_data,
+ state,
+ operation,
+ )
+ .await
+ .map_err(|error| logger::warn!(payments_outgoing_webhook_error=?error))
+ .ok();
+ }
Ok((
payment_data,
diff --git a/crates/router/src/core/payments/flows.rs b/crates/router/src/core/payments/flows.rs
index 377758a69f..e023e2db80 100644
--- a/crates/router/src/core/payments/flows.rs
+++ b/crates/router/src/core/payments/flows.rs
@@ -27,7 +27,8 @@ use hyperswitch_domain_models::router_flow_types::{
BillingConnectorInvoiceSync, BillingConnectorPaymentsSync, InvoiceRecordBack,
};
use hyperswitch_domain_models::{
- payments as domain_payments, router_request_types::PaymentsCaptureData,
+ payments as domain_payments,
+ router_request_types::{CurrentFlowInfo, PaymentsCaptureData},
};
use crate::{
@@ -92,6 +93,10 @@ pub trait ConstructFlowSpecificData<F, Req, Res> {
#[allow(clippy::too_many_arguments)]
#[async_trait]
pub trait Feature<F, T> {
+ fn current_flow_info(&self) -> Option<CurrentFlowInfo> {
+ None
+ }
+
async fn decide_flows<'a>(
self,
state: &SessionState,
diff --git a/crates/router/src/core/payments/flows/authorize_flow.rs b/crates/router/src/core/payments/flows/authorize_flow.rs
index 55ae3df855..d6ee094ed7 100644
--- a/crates/router/src/core/payments/flows/authorize_flow.rs
+++ b/crates/router/src/core/payments/flows/authorize_flow.rs
@@ -208,6 +208,13 @@ impl
#[async_trait]
impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAuthorizeRouterData {
+ fn current_flow_info(&self) -> Option<api_interface::CurrentFlowInfo> {
+ Some(api_interface::CurrentFlowInfo::Authorize {
+ auth_type: self.auth_type,
+ request_data: Box::new(self.request.clone()),
+ })
+ }
+
async fn decide_flows<'a>(
mut self,
state: &SessionState,
diff --git a/crates/router/src/core/payments/flows/setup_mandate_flow.rs b/crates/router/src/core/payments/flows/setup_mandate_flow.rs
index 294a894d93..9ddd2755b2 100644
--- a/crates/router/src/core/payments/flows/setup_mandate_flow.rs
+++ b/crates/router/src/core/payments/flows/setup_mandate_flow.rs
@@ -104,6 +104,13 @@ impl
#[async_trait]
impl Feature<api::SetupMandate, types::SetupMandateRequestData> for types::SetupMandateRouterData {
+ fn current_flow_info(&self) -> Option<api_interface::CurrentFlowInfo> {
+ Some(api_interface::CurrentFlowInfo::SetupMandate {
+ auth_type: self.auth_type,
+ request_data: Box::new(self.request.clone()),
+ })
+ }
+
async fn decide_flows<'a>(
mut self,
state: &SessionState,
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index 421ee42371..a9f07b07bc 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -2752,11 +2752,10 @@ where
GetToken::Connector,
None,
)?;
+ let setup_future_usage = payment_data.payment_intent.setup_future_usage;
let should_continue_further = connector_data
.connector
- .is_payment_recurrence_operation_needed(
- &payment_data.payment_intent.clone(),
- )
+ .is_payment_recurrence_operation_needed(setup_future_usage, None)
.unwrap_or(false);
if should_continue_further {
logger::info!(