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(refunds): Trigger refund outgoing webhooks in create and retrieve refund flows #6635

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 22 additions & 2 deletions crates/router/src/core/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ pub async fn trigger_refund_to_gateway(
refund.refund_id
)
})?;
utils::trigger_refund_outgoing_webhook(
state,
merchant_account,
&response,
payment_attempt.profile_id.clone(),
key_store,
)
.await
.map_err(|error| logger::warn!(refunds_outgoing_webhook_error=?error))
.ok();
Ok(response)
}

Expand Down Expand Up @@ -467,7 +477,7 @@ pub async fn refund_retrieve_core(
.transpose()?;

let response = if should_call_refund(&refund, request.force_sync.unwrap_or(false)) {
sync_refund_with_gateway(
Box::pin(sync_refund_with_gateway(
&state,
&merchant_account,
&key_store,
Expand All @@ -476,7 +486,7 @@ pub async fn refund_retrieve_core(
&refund,
creds_identifier,
charges_req,
)
))
.await
} else {
Ok(refund)
Expand Down Expand Up @@ -669,6 +679,16 @@ pub async fn sync_refund_with_gateway(
refund.refund_id
)
})?;
utils::trigger_refund_outgoing_webhook(
state,
merchant_account,
&response,
payment_attempt.profile_id.clone(),
key_store,
)
.await
.map_err(|error| logger::warn!(refunds_outgoing_webhook_error=?error))
.ok();
Ok(response)
}

Expand Down
72 changes: 71 additions & 1 deletion crates/router/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ use crate::{
logger,
routes::{metrics, SessionState},
services,
types::{self, domain, transformers::ForeignFrom},
types::{
self, domain,
transformers::{ForeignFrom, ForeignInto},
},
};

pub mod error_parser {
Expand Down Expand Up @@ -1270,3 +1273,70 @@ pub async fn flatten_join_error<T>(handle: Handle<T>) -> RouterResult<T> {
.attach_printable("Join Error"),
}
}

#[cfg(feature = "v1")]
pub async fn trigger_refund_outgoing_webhook(
state: &SessionState,
merchant_account: &domain::MerchantAccount,
refund: &diesel_models::Refund,
profile_id: id_type::ProfileId,
key_store: &domain::MerchantKeyStore,
) -> RouterResult<()> {
let refund_status = refund.refund_status;
if matches!(
refund_status,
enums::RefundStatus::Success
| enums::RefundStatus::Failure
| enums::RefundStatus::TransactionFailure
) {
let event_type = ForeignFrom::foreign_from(refund_status);
let refund_response: api_models::refunds::RefundResponse = refund.clone().foreign_into();
let key_manager_state = &(state).into();
let refund_id = refund_response.refund_id.clone();
let business_profile = state
.store
.find_business_profile_by_profile_id(key_manager_state, key_store, &profile_id)
.await
.to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
id: profile_id.get_string_repr().to_owned(),
})?;
let cloned_state = state.clone();
let cloned_key_store = key_store.clone();
let cloned_merchant_account = merchant_account.clone();
let primary_object_created_at = refund_response.created_at;
if let Some(outgoing_event_type) = event_type {
tokio::spawn(
async move {
Box::pin(webhooks_core::create_event_and_trigger_outgoing_webhook(
cloned_state,
cloned_merchant_account,
business_profile,
&cloned_key_store,
outgoing_event_type,
diesel_models::enums::EventClass::Refunds,
refund_id.to_string(),
diesel_models::enums::EventObjectType::RefundDetails,
webhooks::OutgoingWebhookContent::RefundDetails(Box::new(refund_response)),
primary_object_created_at,
))
.await
}
.in_current_span(),
);
} else {
logger::warn!("Outgoing webhook not sent because of missing event type status mapping");
};
}
Ok(())
}

#[cfg(feature = "v2")]
pub async fn trigger_refund_outgoing_webhook(
state: &SessionState,
merchant_account: &domain::MerchantAccount,
refund: &diesel_models::Refund,
profile_id: id_type::ProfileId,
key_store: &domain::MerchantKeyStore,
) -> RouterResult<()> {
todo!()
}
Loading