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(router): Outgoing Webhooks (#13141)

Akshaya Shankar23e9d9f46aac
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 2c826a701b..7aa22e7d7f 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -21264,6 +21264,14 @@
"type": "boolean",
"description": "Filter all events by `is_overall_delivery_successful` field of the event.",
"nullable": true
+ },
+ "recipient": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/EventRecipient"
+ }
+ ],
+ "nullable": true
}
}
},
@@ -21336,6 +21344,13 @@
}
}
},
+ "EventRecipient": {
+ "type": "string",
+ "enum": [
+ "merchant",
+ "connector"
+ ]
+ },
"EventRetrieveResponse": {
"allOf": [
{
diff --git a/crates/api_models/src/webhook_events.rs b/crates/api_models/src/webhook_events.rs
index 538836df96..cf2f7c8a94 100644
--- a/crates/api_models/src/webhook_events.rs
+++ b/crates/api_models/src/webhook_events.rs
@@ -1,6 +1,6 @@
use std::collections::HashSet;
-use common_enums::{EventClass, EventType, WebhookDeliveryAttempt};
+use common_enums::{EventClass, EventRecipient, EventType, WebhookDeliveryAttempt};
use hyperswitch_masking::Secret;
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
@@ -41,6 +41,8 @@ pub struct EventListConstraints {
pub event_types: Option<HashSet<EventType>>,
/// Filter all events by `is_overall_delivery_successful` field of the event.
pub is_delivered: Option<bool>,
+ /// Filter all events by the recipient of the webhook.
+ pub recipient: Option<EventRecipient>,
}
#[derive(Debug)]
@@ -53,9 +55,11 @@ pub enum EventListConstraintsInternal {
event_classes: Option<HashSet<EventClass>>,
event_types: Option<HashSet<EventType>>,
is_delivered: Option<bool>,
+ recipient: Option<EventRecipient>,
},
ObjectIdFilter {
object_id: String,
+ recipient: Option<EventRecipient>,
},
EventIdFilter {
event_id: String,
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 8e4a0a7bbf..9ca71bcfb4 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -1760,6 +1760,7 @@ impl Currency {
serde::Serialize,
strum::Display,
strum::EnumString,
+ ToSchema,
)]
#[router_derive::diesel_enum(storage_type = "text")]
#[serde(rename_all = "snake_case")]
diff --git a/crates/diesel_models/src/query/events.rs b/crates/diesel_models/src/query/events.rs
index e0e288b26e..60ef9865c7 100644
--- a/crates/diesel_models/src/query/events.rs
+++ b/crates/diesel_models/src/query/events.rs
@@ -101,9 +101,7 @@ impl Event {
.order(dsl::created_at.desc())
.into_boxed();
- if let Some(recipient) = event_recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
- };
+ query = Self::apply_event_recipient(query, event_recipient);
logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
@@ -190,9 +188,7 @@ impl Event {
.order(dsl::created_at.desc())
.into_boxed();
- if let Some(recipient) = event_recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
- };
+ query = Self::apply_event_recipient(query, event_recipient);
logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
@@ -230,9 +226,7 @@ impl Event {
.order(dsl::created_at.desc())
.into_boxed();
- if let Some(recipient) = recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
- }
+ query = Self::apply_event_recipient(query, recipient);
if let Some(profile_id) = profile_id {
query = query.filter(dsl::business_profile_id.eq(profile_id));
@@ -323,9 +317,7 @@ impl Event {
.order(dsl::created_at.desc())
.into_boxed();
- if let Some(recipient) = event_recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
- };
+ query = Self::apply_event_recipient(query, event_recipient);
logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
@@ -352,9 +344,7 @@ impl Event {
.order(dsl::created_at.desc())
.into_boxed();
- if let Some(recipient) = event_recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
- };
+ query = Self::apply_event_recipient(query, event_recipient);
logger::debug!(query = %debug_query::<Pg, _>(&query).to_string());
@@ -520,6 +510,13 @@ impl Event {
diesel::dsl::Eq<dsl::recipient, String>,
Output = T,
>,
+ T: diesel::query_dsl::methods::FilterDsl<
+ diesel::dsl::Or<
+ diesel::dsl::Eq<dsl::recipient, String>,
+ diesel::dsl::IsNull<dsl::recipient>,
+ >,
+ Output = T,
+ >,
{
if let Some(profile_id) = profile_id {
query = query.filter(dsl::business_profile_id.eq(profile_id));
@@ -545,8 +542,43 @@ impl Event {
query = query.filter(dsl::is_overall_delivery_successful.eq(is_delivered));
}
- if let Some(recipient) = event_recipient {
- query = query.filter(dsl::recipient.eq(recipient.to_string()));
+ query = Self::apply_event_recipient(query, event_recipient);
+
+ query
+ }
+
+ fn apply_event_recipient<T>(
+ mut query: T,
+ event_recipient: Option<common_enums::EventRecipient>,
+ ) -> T
+ where
+ T: diesel::query_dsl::methods::FilterDsl<
+ diesel::dsl::Eq<dsl::recipient, String>,
+ Output = T,
+ >,
+ T: diesel::query_dsl::methods::FilterDsl<
+ diesel::dsl::Or<
+ diesel::dsl::Eq<dsl::recipient, String>,
+ diesel::dsl::IsNull<dsl::recipient>,
+ >,
+ Output = T,
+ >,
+ {
+ match event_recipient {
+ Some(common_enums::EventRecipient::Merchant) => {
+ query = query.filter(
+ dsl::recipient
+ .eq(common_enums::EventRecipient::Merchant.to_string())
+ .or(
+ dsl::recipient.is_null(), // Treat NULL recipient as Merchant for backward compatibility
+ ),
+ );
+ }
+ Some(common_enums::EventRecipient::Connector) => {
+ query = query
+ .filter(dsl::recipient.eq(common_enums::EventRecipient::Connector.to_string()));
+ }
+ None => {}
}
query
diff --git a/crates/openapi/src/openapi.rs b/crates/openapi/src/openapi.rs
index df4389ad85..77fb2ef777 100644
--- a/crates/openapi/src/openapi.rs
+++ b/crates/openapi/src/openapi.rs
@@ -900,6 +900,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::webhooks::OutgoingWebhookContent,
api_models::enums::EventClass,
api_models::enums::EventType,
+ api_models::enums::EventRecipient,
api_models::enums::DecoupledAuthenticationType,
api_models::enums::Tokenization,
api_models::enums::AuthenticationStatus,
diff --git a/crates/router/src/core/webhooks/outgoing.rs b/crates/router/src/core/webhooks/outgoing.rs
index d71a21ccae..8868629129 100644
--- a/crates/router/src/core/webhooks/outgoing.rs
+++ b/crates/router/src/core/webhooks/outgoing.rs
@@ -245,6 +245,7 @@ async fn insert_event_and_spawn_webhook_delivery(
"merchant webhook URL \
could not be obtained; skipping outgoing webhooks for event"
);
+ return Ok(());
}
};
diff --git a/crates/router/src/core/webhooks/webhook_events.rs b/crates/router/src/core/webhooks/webhook_events.rs
index 87f9991b26..27ad37b0d7 100644
--- a/crates/router/src/core/webhooks/webhook_events.rs
+++ b/crates/router/src/core/webhooks/webhook_events.rs
@@ -55,14 +55,17 @@ pub async fn list_initial_delivery_attempts(
(now.date() - time::Duration::days(INITIAL_DELIVERY_ATTEMPTS_LIST_MAX_DAYS)).midnight();
let (events, total_count) = match constraints {
- api_models::webhook_events::EventListConstraintsInternal::ObjectIdFilter { object_id } => {
+ api_models::webhook_events::EventListConstraintsInternal::ObjectIdFilter {
+ object_id,
+ recipient,
+ } => {
let events = store
.list_initial_events_by_initiator_merchant_id_primary_object_id(
&merchant_id,
object_id.as_str(),
profile_id.clone(),
&key_store,
- Some(common_enums::EventRecipient::Merchant),
+ recipient,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
@@ -113,6 +116,7 @@ pub async fn list_initial_delivery_attempts(
event_classes,
event_types,
is_delivered,
+ recipient,
} => {
let limit = match limit {
Some(limit) if limit <= INITIAL_DELIVERY_ATTEMPTS_LIST_MAX_LIMIT => Ok(Some(limit)),
@@ -178,7 +182,7 @@ pub async fn list_initial_delivery_attempts(
event_types.clone(),
is_delivered,
&key_store,
- Some(common_enums::EventRecipient::Merchant),
+ recipient,
)
.await
}
@@ -193,7 +197,7 @@ pub async fn list_initial_delivery_attempts(
event_types.clone(),
is_delivered,
&key_store,
- Some(common_enums::EventRecipient::Merchant),
+ recipient,
)
.await
}
@@ -210,7 +214,7 @@ pub async fn list_initial_delivery_attempts(
created_before,
event_types,
is_delivered,
- Some(common_enums::EventRecipient::Merchant),
+ recipient,
)
.await
}
@@ -223,7 +227,7 @@ pub async fn list_initial_delivery_attempts(
created_before,
event_types,
is_delivered,
- Some(common_enums::EventRecipient::Merchant),
+ recipient,
)
.await
}
@@ -269,7 +273,7 @@ pub async fn list_delivery_attempts(
&initial_attempt_id,
&merchant_id,
&key_store,
- Some(common_enums::EventRecipient::Merchant),
+ None,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs
index 2b904a3235..a3ba939293 100644
--- a/crates/router/src/types/transformers.rs
+++ b/crates/router/src/types/transformers.rs
@@ -2032,7 +2032,10 @@ impl ForeignTryFrom<api_types::webhook_events::EventListConstraints>
}
match (item.object_id.clone(), item.event_id.clone()) {
- (Some(object_id), None) => Ok(Self::ObjectIdFilter { object_id }),
+ (Some(object_id), None) => Ok(Self::ObjectIdFilter {
+ object_id,
+ recipient: item.recipient,
+ }),
(None, Some(event_id)) => Ok(Self::EventIdFilter { event_id }),
@@ -2044,6 +2047,7 @@ impl ForeignTryFrom<api_types::webhook_events::EventListConstraints>
event_classes: item.event_classes,
event_types: item.event_types,
is_delivered: item.is_delivered,
+ recipient: item.recipient,
}),
(Some(_), Some(_)) => Err(report!(errors::ApiErrorResponse::PreconditionFailed {