Skip to content

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

Refactor(refunds_v2): Refactor api event metric for refunds v2 apis (#8041)

Amey Walebc4bab935fa6
diff --git a/crates/api_models/src/events/refund.rs b/crates/api_models/src/events/refund.rs
index 08c6dd52c3..7a50af6928 100644
--- a/crates/api_models/src/events/refund.rs
+++ b/crates/api_models/src/events/refund.rs
@@ -22,13 +22,6 @@ impl ApiEventMetric for RefundRequest {
}
}
-#[cfg(feature = "v2")]
-impl ApiEventMetric for refunds::RefundsCreateRequest {
- fn get_api_event_type(&self) -> Option<ApiEventsType> {
- None
- }
-}
-
#[cfg(feature = "v1")]
impl ApiEventMetric for refunds::RefundResponse {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
@@ -43,7 +36,7 @@ impl ApiEventMetric for refunds::RefundResponse {
impl ApiEventMetric for refunds::RefundResponse {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Refund {
- payment_id: self.payment_id.clone(),
+ payment_id: Some(self.payment_id.clone()),
refund_id: self.id.clone(),
})
}
@@ -62,7 +55,10 @@ impl ApiEventMetric for RefundsRetrieveRequest {
#[cfg(feature = "v2")]
impl ApiEventMetric for refunds::RefundsRetrieveRequest {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
- None
+ Some(ApiEventsType::Refund {
+ payment_id: None,
+ refund_id: self.refund_id.clone(),
+ })
}
}
diff --git a/crates/common_utils/src/events.rs b/crates/common_utils/src/events.rs
index 5ba16ee2c4..31ae3e28c3 100644
--- a/crates/common_utils/src/events.rs
+++ b/crates/common_utils/src/events.rs
@@ -29,7 +29,7 @@ pub enum ApiEventsType {
},
#[cfg(feature = "v2")]
Refund {
- payment_id: id_type::GlobalPaymentId,
+ payment_id: Option<id_type::GlobalPaymentId>,
refund_id: id_type::GlobalRefundId,
},
#[cfg(feature = "v1")]
diff --git a/crates/diesel_models/src/refund.rs b/crates/diesel_models/src/refund.rs
index 927965a533..0defac38a4 100644
--- a/crates/diesel_models/src/refund.rs
+++ b/crates/diesel_models/src/refund.rs
@@ -811,7 +811,7 @@ impl common_utils::events::ApiEventMetric for Refund {
impl common_utils::events::ApiEventMetric for Refund {
fn get_api_event_type(&self) -> Option<common_utils::events::ApiEventsType> {
Some(common_utils::events::ApiEventsType::Refund {
- payment_id: self.payment_id.clone(),
+ payment_id: Some(self.payment_id.clone()),
refund_id: self.id.clone(),
})
}
diff --git a/crates/router/src/core/refunds_v2.rs b/crates/router/src/core/refunds_v2.rs
index 922b9dce24..00e4a38cb8 100644
--- a/crates/router/src/core/refunds_v2.rs
+++ b/crates/router/src/core/refunds_v2.rs
@@ -40,6 +40,7 @@ pub async fn refund_create_core(
state: SessionState,
merchant_context: domain::MerchantContext,
req: refunds::RefundsCreateRequest,
+ global_refund_id: id_type::GlobalRefundId,
) -> errors::RouterResponse<refunds::RefundResponse> {
let db = &*state.store;
let (payment_intent, payment_attempt, amount);
@@ -94,8 +95,6 @@ pub async fn refund_create_core(
.await
.to_not_found_response(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?;
- let global_refund_id = id_type::GlobalRefundId::generate(&state.conf.cell_information.id);
-
tracing::Span::current().record("global_refund_id", global_refund_id.get_string_repr());
Box::pin(validate_and_create_refund(
diff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rs
index 1ae4412807..efc6fa2e93 100644
--- a/crates/router/src/routes/refunds.rs
+++ b/crates/router/src/routes/refunds.rs
@@ -35,12 +35,11 @@ mod internal_payload_types {
{
fn get_api_event_type(&self) -> Option<common_utils::events::ApiEventsType> {
let refund_id = self.global_refund_id.clone();
- self.payment_id
- .clone()
- .map(|payment_id| common_utils::events::ApiEventsType::Refund {
- payment_id,
- refund_id,
- })
+ let payment_id = self.payment_id.clone();
+ Some(common_utils::events::ApiEventsType::Refund {
+ payment_id,
+ refund_id,
+ })
}
}
}
@@ -105,16 +104,32 @@ pub async fn refunds_create(
) -> HttpResponse {
let flow = Flow::RefundsCreate;
+ let global_refund_id =
+ common_utils::id_type::GlobalRefundId::generate(&state.conf.cell_information.id);
+ let payload = json_payload.into_inner();
+
+ let internal_refund_create_payload =
+ internal_payload_types::RefundsGenericRequestWithResourceId {
+ global_refund_id: global_refund_id.clone(),
+ payment_id: Some(payload.payment_id.clone()),
+ payload,
+ };
+
Box::pin(api::server_wrap(
flow,
state,
&req,
- json_payload.into_inner(),
+ internal_refund_create_payload,
|state, auth: auth::AuthenticationData, req, _| {
let merchant_context = domain::MerchantContext::NormalMerchant(Box::new(
domain::Context(auth.merchant_account, auth.key_store),
));
- refund_create_core(state, merchant_context, req)
+ refund_create_core(
+ state,
+ merchant_context,
+ req.payload,
+ global_refund_id.clone(),
+ )
},
auth::auth_type(
&auth::V2ApiKeyAuth {