From d7844996b8fcd828afe43477c7b872b6d40643f7 Mon Sep 17 00:00:00 2001 From: Sk Sakil Mostak Date: Thu, 18 Jan 2024 17:10:34 +0530 Subject: [PATCH 1/2] feat: add outgoing webhooks for requires_capture and partial_capture event --- crates/router/src/utils.rs | 77 ++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs index aaa145099e4..b4818d8f29f 100644 --- a/crates/router/src/utils.rs +++ b/crates/router/src/utils.rs @@ -51,7 +51,7 @@ use crate::{ types::{encrypt_optional, AsyncLift}, }, storage, - transformers::{ForeignTryFrom, ForeignTryInto}, + transformers::ForeignFrom, }, }; @@ -681,23 +681,6 @@ pub fn add_apple_pay_payment_status_metrics( } } -impl ForeignTryFrom for enums::EventType { - type Error = errors::ValidationError; - - fn foreign_try_from(value: enums::IntentStatus) -> Result { - match value { - enums::IntentStatus::Succeeded => Ok(Self::PaymentSucceeded), - enums::IntentStatus::Failed => Ok(Self::PaymentFailed), - enums::IntentStatus::Processing => Ok(Self::PaymentProcessing), - enums::IntentStatus::RequiresMerchantAction - | enums::IntentStatus::RequiresCustomerAction => Ok(Self::ActionRequired), - _ => Err(errors::ValidationError::IncorrectValueProvided { - field_name: "intent_status", - }), - } - } -} - pub async fn trigger_payments_webhook( merchant_account: domain::MerchantAccount, business_profile: diesel_models::business_profile::BusinessProfile, @@ -726,7 +709,10 @@ where if matches!( status, - enums::IntentStatus::Succeeded | enums::IntentStatus::Failed + enums::IntentStatus::Succeeded + | enums::IntentStatus::Failed + | enums::IntentStatus::PartiallyCaptured + | enums::IntentStatus::RequiresCapture ) { let payments_response = crate::core::payments::transformers::payments_to_payments_response( req, @@ -742,11 +728,7 @@ where None, )?; - let event_type: enums::EventType = status - .foreign_try_into() - .into_report() - .change_context(errors::ApiErrorResponse::WebhookProcessingFailure) - .attach_printable("payment event type mapping failed")?; + let event_type = ForeignFrom::foreign_from(status); if let services::ApplicationResponse::JsonWithHeaders((payments_response_json, _)) = payments_response @@ -755,27 +737,34 @@ where // This spawns this futures in a background thread, the exception inside this future won't affect // the current thread and the lifecycle of spawn thread is not handled by runtime. // So when server shutdown won't wait for this thread's completion. - tokio::spawn( - async move { - Box::pin( - webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook( - m_state, - merchant_account, - business_profile, - event_type, - diesel_models::enums::EventClass::Payments, - None, - payment_id, - diesel_models::enums::EventObjectType::PaymentDetails, - webhooks::OutgoingWebhookContent::PaymentDetails( - payments_response_json, + + if let Some(event_type) = event_type { + tokio::spawn( + async move { + Box::pin( + webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook( + m_state, + merchant_account, + business_profile, + event_type, + diesel_models::enums::EventClass::Payments, + None, + payment_id, + diesel_models::enums::EventObjectType::PaymentDetails, + webhooks::OutgoingWebhookContent::PaymentDetails( + payments_response_json, + ), ), - ), - ) - .await - } - .in_current_span(), - ); + ) + .await + } + .in_current_span(), + ); + } else { + logger::warn!( + "Outgoing webhook not sent because of missing event type status mapping" + ); + } } } From a001b6cb9f421e287b2fe4409ecb898d7e2bc981 Mon Sep 17 00:00:00 2001 From: Sk Sakil Mostak Date: Fri, 19 Jan 2024 17:16:32 +0530 Subject: [PATCH 2/2] refactor: remove outgoing webhook for requires capture --- crates/router/src/utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs index b4818d8f29f..68909d1127c 100644 --- a/crates/router/src/utils.rs +++ b/crates/router/src/utils.rs @@ -712,7 +712,6 @@ where enums::IntentStatus::Succeeded | enums::IntentStatus::Failed | enums::IntentStatus::PartiallyCaptured - | enums::IntentStatus::RequiresCapture ) { let payments_response = crate::core::payments::transformers::payments_to_payments_response( req,