Bug: Audit trail - Add dispute and refund ID to connector calls




diff --git a/crates/analytics/docs/clickhouse/scripts/connector_events.sql b/crates/analytics/docs/clickhouse/scripts/connector_events.sql
index 5821cd03556..4a53f9edb0b 100644
--- a/crates/analytics/docs/clickhouse/scripts/connector_events.sql
+++ b/crates/analytics/docs/clickhouse/scripts/connector_events.sql
@@ -10,7 +10,9 @@ CREATE TABLE connector_events_queue (
     `status_code` UInt32,
     `created_at` DateTime64(3),
     `latency` UInt128,
-    `method` LowCardinality(String)
+    `method` LowCardinality(String),
+    `refund_id` Nullable(String),
+    `dispute_id` Nullable(String)
 ) ENGINE = Kafka SETTINGS kafka_broker_list = 'kafka0:29092',
 kafka_topic_list = 'hyperswitch-connector-api-events',
 kafka_group_name = 'hyper-c1',
@@ -32,6 +34,8 @@ CREATE TABLE connector_events_dist (
     `inserted_at` DateTime64(3),
     `latency` UInt128,
     `method` LowCardinality(String),
+    `refund_id` Nullable(String),
+    `dispute_id` Nullable(String),
     INDEX flowIndex flowTYPE bloom_filter GRANULARITY 1,
     INDEX connectorIndex connector_name TYPE bloom_filter GRANULARITY 1,
     INDEX statusIndex status_code TYPE bloom_filter GRANULARITY 1
@@ -54,7 +58,9 @@ CREATE MATERIALIZED VIEW connector_events_mv TO connector_events_dist (
     `status_code` UInt32,
     `created_at` DateTime64(3),
     `latency` UInt128,
-    `method` LowCardinality(String)
+    `method` LowCardinality(String),
+    `refund_id` Nullable(String),
+    `dispute_id` Nullable(String)
 ) AS
 SELECT
     merchant_id,
@@ -70,6 +76,8 @@ SELECT
     now() as inserted_at,
     latency,
     method,
+    refund_id,
+    dispute_id
 FROM
     connector_events_queue
 where length(_error) = 0;
diff --git a/crates/analytics/src/connector_events/events.rs b/crates/analytics/src/connector_events/events.rs
index 096520777ee..47044811a8b 100644
--- a/crates/analytics/src/connector_events/events.rs
+++ b/crates/analytics/src/connector_events/events.rs
@@ -1,7 +1,4 @@
-use api_models::analytics::{
-    connector_events::{ConnectorEventsRequest, QueryType},
-    Granularity,
-};
+use api_models::analytics::{connector_events::ConnectorEventsRequest, Granularity};
 use common_utils::errors::ReportSwitchExt;
 use error_stack::ResultExt;
 use time::PrimitiveDateTime;
@@ -32,11 +29,23 @@ where
     query_builder
         .add_filter_clause("merchant_id", merchant_id)
         .switch()?;
-    match query_param.query_param {
-        QueryType::Payment { payment_id } => query_builder
-            .add_filter_clause("payment_id", payment_id)
-            .switch()?,
+
+    query_builder
+        .add_filter_clause("payment_id", query_param.payment_id)
+        .switch()?;
+
+    if let Some(refund_id) = query_param.refund_id {
+        query_builder
+            .add_filter_clause("refund_id", &refund_id)
+            .switch()?;
+    }
+
+    if let Some(dispute_id) = query_param.dispute_id {
+        query_builder
+            .add_filter_clause("dispute_id", &dispute_id)
+            .switch()?;
     }
+
     //TODO!: update the execute_query function to return reports instead of plain errors...
     query_builder
         .execute_query::<ConnectorEventsResult, _>(pool)
diff --git a/crates/api_models/src/analytics/connector_events.rs b/crates/api_models/src/analytics/connector_events.rs
index b2974b0a339..7d7e4ae2a8b 100644
--- a/crates/api_models/src/analytics/connector_events.rs
+++ b/crates/api_models/src/analytics/connector_events.rs
@@ -1,11 +1,6 @@
-#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
-#[serde(tag = "type")]
-pub enum QueryType {
-    Payment { payment_id: String },
-}
-
 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
 pub struct ConnectorEventsRequest {
-    #[serde(flatten)]
-    pub query_param: QueryType,
+    pub payment_id: String,
+    pub refund_id: Option<String>,
+    pub dispute_id: Option<String>,
 }
diff --git a/crates/router/src/core/fraud_check/flows/checkout_flow.rs b/crates/router/src/core/fraud_check/flows/checkout_flow.rs
index 7f8993af527..8b3eed45f9e 100644
--- a/crates/router/src/core/fraud_check/flows/checkout_flow.rs
+++ b/crates/router/src/core/fraud_check/flows/checkout_flow.rs
@@ -119,6 +119,8 @@ impl ConstructFlowSpecificData<frm_api::Checkout, FraudCheckCheckoutData, FraudC
             connector_api_version: None,
             apple_pay_flow: None,
             frm_metadata: self.frm_metadata.clone(),
+            refund_id: None,
+            dispute_id: None,
         };
 
         Ok(router_data)
diff --git a/crates/router/src/core/fraud_check/flows/fulfillment_flow.rs b/crates/router/src/core/fraud_check/flows/fulfillment_flow.rs
index 79b6811a6e9..706a0f9142e 100644
--- a/crates/router/src/core/fraud_check/flows/fulfillment_flow.rs
+++ b/crates/router/src/core/fraud_check/flows/fulfillment_flow.rs
@@ -106,6 +106,8 @@ pub async fn construct_fulfillment_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
     Ok(router_data)
 }
diff --git a/crates/router/src/core/fraud_check/flows/record_return.rs b/crates/router/src/core/fraud_check/flows/record_return.rs
index bd0ba3e4f7f..2de6aaab7c7 100644
--- a/crates/router/src/core/fraud_check/flows/record_return.rs
+++ b/crates/router/src/core/fraud_check/flows/record_return.rs
@@ -97,6 +97,8 @@ impl ConstructFlowSpecificData<RecordReturn, FraudCheckRecordReturnData, FraudCh
             connector_api_version: None,
             apple_pay_flow: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         };
 
         Ok(router_data)
diff --git a/crates/router/src/core/fraud_check/flows/sale_flow.rs b/crates/router/src/core/fraud_check/flows/sale_flow.rs
index 9c2708b28b3..dfe70570047 100644
--- a/crates/router/src/core/fraud_check/flows/sale_flow.rs
+++ b/crates/router/src/core/fraud_check/flows/sale_flow.rs
@@ -93,6 +93,8 @@ impl ConstructFlowSpecificData<frm_api::Sale, FraudCheckSaleData, FraudCheckResp
             connector_api_version: None,
             apple_pay_flow: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         };
 
         Ok(router_data)
diff --git a/crates/router/src/core/fraud_check/flows/transaction_flow.rs b/crates/router/src/core/fraud_check/flows/transaction_flow.rs
index 5eaa17d3298..a84d9a771b4 100644
--- a/crates/router/src/core/fraud_check/flows/transaction_flow.rs
+++ b/crates/router/src/core/fraud_check/flows/transaction_flow.rs
@@ -109,6 +109,8 @@ impl
             connector_api_version: None,
             apple_pay_flow: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         };
 
         Ok(router_data)
diff --git a/crates/router/src/core/mandate/utils.rs b/crates/router/src/core/mandate/utils.rs
index 25267db1a96..4863edd6fcb 100644
--- a/crates/router/src/core/mandate/utils.rs
+++ b/crates/router/src/core/mandate/utils.rs
@@ -70,6 +70,8 @@ pub async fn construct_mandate_revoke_router_data(
         payout_method_data: None,
         #[cfg(feature = "payouts")]
         quote_id: None,
+        refund_id: None,
+        dispute_id: None,
     };
 
     Ok(router_data)
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 520582eb22f..fe4a7757ecc 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -2997,6 +2997,8 @@ pub fn router_data_type_conversion<F1, F2, Req1, Req2, Res1, Res2>(
         external_latency: router_data.external_latency,
         apple_pay_flow: router_data.apple_pay_flow,
         frm_metadata: router_data.frm_metadata,
+        refund_id: router_data.refund_id,
+        dispute_id: router_data.dispute_id,
     }
 }
 
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 61917fdcd2e..1c1b11c47b1 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -167,6 +167,8 @@ where
         external_latency: None,
         apple_pay_flow,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
 
     Ok(router_data)
diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs
index 016d5ec955d..ef8dddde955 100644
--- a/crates/router/src/core/utils.rs
+++ b/crates/router/src/core/utils.rs
@@ -176,6 +176,8 @@ pub async fn construct_payout_router_data<'a, F>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
 
     Ok(router_data)
@@ -328,6 +330,8 @@ pub async fn construct_refund_router_data<'a, F>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: Some(refund.refund_id.clone()),
+        dispute_id: None,
     };
 
     Ok(router_data)
@@ -558,6 +562,8 @@ pub async fn construct_accept_dispute_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        dispute_id: Some(dispute.dispute_id.clone()),
+        refund_id: None,
     };
     Ok(router_data)
 }
@@ -646,6 +652,8 @@ pub async fn construct_submit_evidence_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: Some(dispute.dispute_id.clone()),
     };
     Ok(router_data)
 }
@@ -740,6 +748,8 @@ pub async fn construct_upload_file_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
     Ok(router_data)
 }
@@ -831,6 +841,8 @@ pub async fn construct_defend_dispute_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: Some(dispute.dispute_id.clone()),
     };
     Ok(router_data)
 }
@@ -915,6 +927,8 @@ pub async fn construct_retrieve_file_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
     Ok(router_data)
 }
diff --git a/crates/router/src/core/webhooks/utils.rs b/crates/router/src/core/webhooks/utils.rs
index 08b49048043..75d37f94279 100644
--- a/crates/router/src/core/webhooks/utils.rs
+++ b/crates/router/src/core/webhooks/utils.rs
@@ -114,6 +114,8 @@ pub async fn construct_webhook_router_data<'a>(
         external_latency: None,
         apple_pay_flow: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     };
     Ok(router_data)
 }
diff --git a/crates/router/src/events/connector_api_logs.rs b/crates/router/src/events/connector_api_logs.rs
index 45c05a3077f..4d3aadc3c45 100644
--- a/crates/router/src/events/connector_api_logs.rs
+++ b/crates/router/src/events/connector_api_logs.rs
@@ -18,6 +18,8 @@ pub struct ConnectorEvent {
     created_at: i128,
     request_id: String,
     latency: u128,
+    refund_id: Option<String>,
+    dispute_id: Option<String>,
     status_code: u16,
 }
 
@@ -34,6 +36,8 @@ impl ConnectorEvent {
         merchant_id: String,
         request_id: Option<&RequestId>,
         latency: u128,
+        refund_id: Option<String>,
+        dispute_id: Option<String>,
         status_code: u16,
     ) -> Self {
         Self {
@@ -54,6 +58,8 @@ impl ConnectorEvent {
                 .map(|i| i.as_hyphenated().to_string())
                 .unwrap_or("NO_REQUEST_ID".to_string()),
             latency,
+            refund_id,
+            dispute_id,
             status_code,
         }
     }
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index e0c6a086257..cd672405d24 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -400,6 +400,8 @@ where
                         req.merchant_id.clone(),
                         state.request_id.as_ref(),
                         external_latency,
+                        req.refund_id.clone(),
+                        req.dispute_id.clone(),
                         status_code,
                     );
 
diff --git a/crates/router/src/types.rs b/crates/router/src/types.rs
index 0809ca17820..741a233ae9f 100644
--- a/crates/router/src/types.rs
+++ b/crates/router/src/types.rs
@@ -319,6 +319,9 @@ pub struct RouterData<Flow, Request, Response> {
     pub apple_pay_flow: Option<storage_enums::ApplePayFlow>,
 
     pub frm_metadata: Option<serde_json::Value>,
+
+    pub dispute_id: Option<String>,
+    pub refund_id: Option<String>,
 }
 
 #[derive(Debug, Clone, serde::Deserialize)]
@@ -1466,6 +1469,8 @@ impl<F1, F2, T1, T2> From<(&RouterData<F1, T1, PaymentsResponseData>, T2)>
             external_latency: data.external_latency,
             apple_pay_flow: data.apple_pay_flow.clone(),
             frm_metadata: data.frm_metadata.clone(),
+            dispute_id: data.dispute_id.clone(),
+            refund_id: data.refund_id.clone(),
         }
     }
 }
@@ -1522,6 +1527,8 @@ impl<F1, F2>
             external_latency: data.external_latency,
             apple_pay_flow: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         }
     }
 }
diff --git a/crates/router/src/types/api/verify_connector.rs b/crates/router/src/types/api/verify_connector.rs
index fbd94230584..c03f492e8b8 100644
--- a/crates/router/src/types/api/verify_connector.rs
+++ b/crates/router/src/types/api/verify_connector.rs
@@ -103,6 +103,8 @@ impl VerifyConnectorData {
             external_latency: None,
             apple_pay_flow: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         }
     }
 }
diff --git a/crates/router/tests/connectors/aci.rs b/crates/router/tests/connectors/aci.rs
index c820b7acd6e..d3f8147fb26 100644
--- a/crates/router/tests/connectors/aci.rs
+++ b/crates/router/tests/connectors/aci.rs
@@ -98,6 +98,8 @@ fn construct_payment_router_data() -> types::PaymentsAuthorizeRouterData {
         apple_pay_flow: None,
         external_latency: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     }
 }
 
@@ -157,6 +159,8 @@ fn construct_refund_router_data<F>() -> types::RefundsRouterData<F> {
         apple_pay_flow: None,
         external_latency: None,
         frm_metadata: None,
+        refund_id: None,
+        dispute_id: None,
     }
 }
 
diff --git a/crates/router/tests/connectors/utils.rs b/crates/router/tests/connectors/utils.rs
index ed3cdbe31b5..844777e2dca 100644
--- a/crates/router/tests/connectors/utils.rs
+++ b/crates/router/tests/connectors/utils.rs
@@ -524,6 +524,8 @@ pub trait ConnectorActions: Connector {
             apple_pay_flow: None,
             external_latency: None,
             frm_metadata: None,
+            refund_id: None,
+            dispute_id: None,
         }
     }
 

added refund_id, dispute_id to connector events

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

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

## Description
<!-- Describe your changes in detail -->


### 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
added refund_id, dispute_id for audit trail


## How did you test it?
created payment -> created refund for the payment -> checked ConnectorApiLogs for the refund_id


## 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
- [ ] I added a [CHANGELOG](/CHANGELOG.md) entry if applicable
