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

feat(analytics): add payment method audit table for API events and tag internal service traffic (#13692)

Prasunna Soppa27565a50e8c8
diff --git a/crates/analytics/docs/clickhouse/scripts/api_events.sql b/crates/analytics/docs/clickhouse/scripts/api_events.sql
index 40a50ce1ae..60a95fc8dd 100644
--- a/crates/analytics/docs/clickhouse/scripts/api_events.sql
+++ b/crates/analytics/docs/clickhouse/scripts/api_events.sql
@@ -359,4 +359,99 @@ SELECT
FROM
api_events_queue
WHERE
- length(_error) = 0;
\ No newline at end of file
+ length(_error) = 0;
+
+CREATE TABLE api_events_payment_method_audit (
+ `merchant_id` LowCardinality(String),
+ `payment_method_id` String,
+ `payment_id` Nullable(String),
+ `payment_method` Nullable(String),
+ `payment_method_type` Nullable(String),
+ `customer_id` Nullable(String),
+ `user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
+ `connector` Nullable(String),
+ `request_id` String,
+ `flow_type` LowCardinality(String),
+ `api_flow` LowCardinality(String),
+ `api_auth_type` LowCardinality(String),
+ `request` String,
+ `response` Nullable(String),
+ `error` Nullable(String),
+ `authentication_data` Nullable(String),
+ `status_code` UInt32,
+ `created_at` DateTime64(3),
+ `inserted_at` DateTime DEFAULT now() CODEC(T64, LZ4),
+ `latency` UInt128,
+ `user_agent` String,
+ `ip_addr` String,
+ `hs_latency` Nullable(UInt128),
+ `http_method` LowCardinality(Nullable(String)),
+ `url_path` Nullable(String),
+ `masked_response` Nullable(String)
+) ENGINE = MergeTree PARTITION BY merchant_id
+ORDER BY
+ (merchant_id, payment_method_id) TTL inserted_at + toIntervalMonth(18) SETTINGS index_granularity = 8192;
+
+CREATE MATERIALIZED VIEW api_events_payment_method_audit_mv TO api_events_payment_method_audit (
+ `merchant_id` String,
+ `payment_method_id` String,
+ `payment_id` Nullable(String),
+ `payment_method` Nullable(String),
+ `payment_method_type` Nullable(String),
+ `customer_id` Nullable(String),
+ `user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
+ `connector` Nullable(String),
+ `request_id` String,
+ `flow_type` LowCardinality(String),
+ `api_flow` LowCardinality(String),
+ `api_auth_type` LowCardinality(String),
+ `request` String,
+ `response` Nullable(String),
+ `error` Nullable(String),
+ `authentication_data` Nullable(String),
+ `status_code` UInt32,
+ `created_at` DateTime64(3),
+ `inserted_at` DateTime DEFAULT now() CODEC(T64, LZ4),
+ `latency` UInt128,
+ `user_agent` String,
+ `ip_addr` String,
+ `hs_latency` Nullable(UInt128),
+ `http_method` LowCardinality(Nullable(String)),
+ `url_path` Nullable(String),
+ `masked_response` Nullable(String)
+) AS
+SELECT
+ merchant_id,
+ payment_method_id,
+ payment_id,
+ payment_method,
+ payment_method_type,
+ customer_id,
+ user_id,
+ auth_user_id,
+ connector,
+ request_id,
+ flow_type,
+ api_flow,
+ api_auth_type,
+ request,
+ response,
+ error,
+ authentication_data,
+ status_code,
+ created_at_timestamp AS created_at,
+ now() AS inserted_at,
+ latency,
+ user_agent,
+ ip_addr,
+ hs_latency,
+ http_method,
+ url_path,
+ response AS masked_response
+FROM
+ api_events_queue
+WHERE
+ (length(_error) = 0)
+ AND (payment_method_id IS NOT NULL);
diff --git a/crates/api_models/src/events.rs b/crates/api_models/src/events.rs
index b465bcf377..fd227f6ab6 100644
--- a/crates/api_models/src/events.rs
+++ b/crates/api_models/src/events.rs
@@ -238,13 +238,37 @@ impl ApiEventMetric for PaymentMethodSessionResponse {
}
}
#[cfg(feature = "tokenization_v2")]
-impl ApiEventMetric for tokenization::GenericTokenizationRequest {}
+impl ApiEventMetric for tokenization::GenericTokenizationRequest {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::Customer {
+ customer_id: Some(self.customer_id.clone()),
+ })
+ }
+}
#[cfg(feature = "tokenization_v2")]
-impl ApiEventMetric for tokenization::GenericTokenizationResponse {}
+impl ApiEventMetric for tokenization::GenericTokenizationResponse {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::Token {
+ token_id: Some(self.id.clone()),
+ })
+ }
+}
#[cfg(feature = "tokenization_v2")]
-impl ApiEventMetric for tokenization::DeleteTokenDataResponse {}
+impl ApiEventMetric for tokenization::DeleteTokenDataResponse {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::Token {
+ token_id: Some(self.id.clone()),
+ })
+ }
+}
#[cfg(feature = "tokenization_v2")]
-impl ApiEventMetric for tokenization::DeleteTokenDataRequest {}
+impl ApiEventMetric for tokenization::DeleteTokenDataRequest {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::PaymentMethodSession {
+ payment_method_session_id: self.session_id.clone(),
+ })
+ }
+}
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index cb13b61474..0b355c8b7c 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -3305,7 +3305,15 @@ pub struct TokenDataResponse {
}
#[cfg(feature = "v2")]
-impl common_utils::events::ApiEventMetric for TokenDataResponse {}
+impl common_utils::events::ApiEventMetric for TokenDataResponse {
+ fn get_api_event_type(&self) -> Option<common_utils::events::ApiEventsType> {
+ Some(common_utils::events::ApiEventsType::PaymentMethod {
+ payment_method_id: self.payment_method_id.clone(),
+ payment_method_type: None,
+ payment_method_subtype: None,
+ })
+ }
+}
#[cfg(feature = "v2")]
#[derive(Debug, serde::Serialize, ToSchema)]
@@ -4737,7 +4745,21 @@ pub struct NetworkTokenStatusCheckSuccessResponse {
}
#[cfg(feature = "v2")]
-impl common_utils::events::ApiEventMetric for NetworkTokenStatusCheckResponse {}
+impl common_utils::events::ApiEventMetric for NetworkTokenStatusCheckResponse {
+ fn get_api_event_type(&self) -> Option<common_utils::events::ApiEventsType> {
+ match self {
+ Self::SuccessResponse(success) => {
+ Some(common_utils::events::ApiEventsType::PaymentMethod {
+ payment_method_id: success.payment_method_id.clone(),
+ payment_method_type: None,
+ payment_method_subtype: None,
+ })
+ }
+ // No payment method id in the failure shape; fall back to the request-side event.
+ Self::FailureResponse(_) => None,
+ }
+ }
+}
#[cfg(feature = "v2")]
#[derive(Debug, serde::Serialize, ToSchema)]