Bug: [REFACTOR] Refactor API Event Metric implementation for refunds v2 apis



Refactor API Event Metric implementation for refunds v2 apis for logging and analytics.
diff --git a/crates/api_models/src/events/refund.rs b/crates/api_models/src/events/refund.rs
index 08c6dd52c39..7a50af69284 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 f7a068b8e49..da7b9a873db 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 927965a5338..0defac38a45 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 922b9dce245..00e4a38cb8e 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 1ae4412807c..efc6fa2e93c 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 {

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [x] New feature
- [x] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
<!-- Describe your changes in detail -->
This PR adds support for API Event Metric logging, the events will be logged to clickhouse when a request/response is triggered.

In refunds v2, it will be for requests and responses for 4 apis
1. Refunds Create
2. Refunds Metadata Update
3. Refunds Retrieve
4. Refunds List

### Additional Changes

- [ ] This PR modifies the API contract
- [ ] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->
It will help us in keeping track of the requests/response and for analytical purposes and help in debugging.

## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
- This will be tested when kafka and clickhouse is enabled for v2.

## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code
- [ ] I added unit tests for my changes where possible
