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
fix(router): resolve webhook event delivery-success from the correct source (#13197)
Ayush Anand979febe60074
diff --git a/crates/router/src/core/webhooks/webhook_events.rs b/crates/router/src/core/webhooks/webhook_events.rsindex 27ad37b0d7..8ccc09f8c3 100644--- a/crates/router/src/core/webhooks/webhook_events.rs+++ b/crates/router/src/core/webhooks/webhook_events.rs@@ -241,7 +241,14 @@ pub async fn list_initial_delivery_attempts(let events = events.into_iter()- .map(api::webhook_events::EventListItemResponse::try_from)+ .map(|event| {+ api::webhook_events::EventListItemResponse::try_from(+ domain::EventWithDeliverySuccessSource {+ event,+ source: domain::DeliverySuccessSource::ListInitialEvents,+ },+ )+ }).collect::<Result<Vec<_>, _>>()?;Ok(ApplicationResponse::Json(@@ -288,7 +295,14 @@ pub async fn list_delivery_attempts(Ok(ApplicationResponse::Json(events.into_iter()- .map(api::webhook_events::EventRetrieveResponse::try_from)+ .map(|event| {+ api::webhook_events::EventRetrieveResponse::try_from(+ domain::EventWithDeliverySuccessSource {+ event,+ source: domain::DeliverySuccessSource::ListDeliveryAttempts,+ },+ )+ }).collect::<Result<Vec<_>, _>>()?,))}@@ -424,7 +438,12 @@ pub async fn retry_delivery_attempt(.to_not_found_response(errors::ApiErrorResponse::EventNotFound)?;Ok(ApplicationResponse::Json(- api::webhook_events::EventRetrieveResponse::try_from(updated_event)?,+ api::webhook_events::EventRetrieveResponse::try_from(+ domain::EventWithDeliverySuccessSource {+ event: updated_event,+ source: domain::DeliverySuccessSource::ListDeliveryAttempts,+ },+ )?,))}diff --git a/crates/router/src/types/domain/event.rs b/crates/router/src/types/domain/event.rsindex aea2461eae..cedcf7b94a 100644--- a/crates/router/src/types/domain/event.rs+++ b/crates/router/src/types/domain/event.rs@@ -83,6 +83,30 @@ pub struct Event {pub recipient: Option<EventRecipient>,}+/// The API that is asking for this event's delivery-success value.+#[derive(Clone, Copy, Debug)]+pub enum DeliverySuccessSource {+ ListInitialEvents,+ ListDeliveryAttempts,+}++/// Pairs an event with the API context needed to resolve its delivery-success value — see+/// `DeliverySuccessSource`.+pub struct EventWithDeliverySuccessSource {+ pub event: Event,+ pub source: DeliverySuccessSource,+}++impl Event {+ /// Resolves the correct delivery-success value for the given API context.+ pub fn resolve_delivery_success(&self, source: DeliverySuccessSource) -> Option<bool> {+ match source {+ DeliverySuccessSource::ListInitialEvents => self.is_overall_delivery_successful,+ DeliverySuccessSource::ListDeliveryAttempts => Some(self.is_webhook_notified),+ }+ }+}+#[derive(Debug)]pub enum EventUpdate {UpdateResponse {diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rsindex a3ba939293..d134ee16ec 100644--- a/crates/router/src/types/transformers.rs+++ b/crates/router/src/types/transformers.rs@@ -2059,12 +2059,17 @@ impl ForeignTryFrom<api_types::webhook_events::EventListConstraints>}#[cfg(feature = "olap")]-impl TryFrom<domain::Event> for api_models::webhook_events::EventListItemResponse {+impl TryFrom<domain::EventWithDeliverySuccessSource>+ for api_models::webhook_events::EventListItemResponse+{type Error = error_stack::Report<errors::ApiErrorResponse>;- fn try_from(item: domain::Event) -> Result<Self, Self::Error> {+ fn try_from(value: domain::EventWithDeliverySuccessSource) -> Result<Self, Self::Error> {use crate::utils::OptionExt;+ let item = value.event;+ let is_delivery_successful = item.resolve_delivery_success(value.source);+// We only allow retrieving events with merchant_id, business_profile_id// and initial_attempt_id populated.// We cannot retrieve events with only some of these fields populated.@@ -2088,7 +2093,7 @@ impl TryFrom<domain::Event> for api_models::webhook_events::EventListItemResponsobject_id: item.primary_object_id,event_type: item.event_type,event_class: item.event_class,- is_delivery_successful: item.is_overall_delivery_successful,+ is_delivery_successful,initial_attempt_id,processor_merchant_id: item.processor_merchant_id,created: item.created_at,@@ -2097,17 +2102,20 @@ impl TryFrom<domain::Event> for api_models::webhook_events::EventListItemRespons}#[cfg(feature = "olap")]-impl TryFrom<domain::Event> for api_models::webhook_events::EventRetrieveResponse {+impl TryFrom<domain::EventWithDeliverySuccessSource>+ for api_models::webhook_events::EventRetrieveResponse+{type Error = error_stack::Report<errors::ApiErrorResponse>;- fn try_from(item: domain::Event) -> Result<Self, Self::Error> {+ fn try_from(value: domain::EventWithDeliverySuccessSource) -> Result<Self, Self::Error> {use crate::utils::OptionExt;+ let item = value.event.clone();+// We only allow retrieving events with all required fields in `EventListItemResponse`, and// `request` and `response` populated.// We cannot retrieve events with only some of these fields populated.- let event_information =- api_models::webhook_events::EventListItemResponse::try_from(item.clone())?;+ let event_information = api_models::webhook_events::EventListItemResponse::try_from(value)?;let request = item.request