Skip to content

Commit

Permalink
feat(core): Add outgoing webhook for manual partial_capture events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakilmostak authored Jan 25, 2024
1 parent c2946cf commit d5e9866
Showing 1 changed file with 32 additions and 44 deletions.
76 changes: 32 additions & 44 deletions crates/router/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::{
types::{encrypt_optional, AsyncLift},
},
storage,
transformers::{ForeignTryFrom, ForeignTryInto},
transformers::ForeignFrom,
},
};

Expand Down Expand Up @@ -681,23 +681,6 @@ pub fn add_apple_pay_payment_status_metrics(
}
}

impl ForeignTryFrom<enums::IntentStatus> for enums::EventType {
type Error = errors::ValidationError;

fn foreign_try_from(value: enums::IntentStatus) -> Result<Self, Self::Error> {
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<F, Req, Op>(
merchant_account: domain::MerchantAccount,
business_profile: diesel_models::business_profile::BusinessProfile,
Expand Down Expand Up @@ -726,7 +709,9 @@ where

if matches!(
status,
enums::IntentStatus::Succeeded | enums::IntentStatus::Failed
enums::IntentStatus::Succeeded
| enums::IntentStatus::Failed
| enums::IntentStatus::PartiallyCaptured
) {
let payments_response = crate::core::payments::transformers::payments_to_payments_response(
req,
Expand All @@ -742,11 +727,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
Expand All @@ -755,27 +736,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"
);
}
}
}

Expand Down

0 comments on commit d5e9866

Please sign in to comment.