Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add outgoing webhook for manual partial_capture events #3388

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading