From f6962cd6176ce5eace89028a8c4678682a266eae Mon Sep 17 00:00:00 2001 From: Sampras lopes Date: Wed, 8 Nov 2023 15:55:05 +0530 Subject: [PATCH] refactor(events): refactor apievent traits --- Cargo.lock | 2 +- crates/api_models/src/admin.rs | 2 + crates/api_models/src/api_keys.rs | 6 + crates/api_models/src/events.rs | 73 ++++++ crates/api_models/src/events/customer.rs | 41 ++++ crates/api_models/src/events/payment.rs | 151 +++++++++++++ crates/api_models/src/events/payouts.rs | 29 +++ crates/api_models/src/events/refund.rs | 63 ++++++ crates/api_models/src/events/routing.rs | 58 +++++ crates/api_models/src/lib.rs | 1 + crates/api_models/src/payment_methods.rs | 6 +- crates/api_models/src/refunds.rs | 2 + crates/common_enums/Cargo.toml | 1 - crates/common_utils/Cargo.toml | 1 + crates/common_utils/src/events.rs | 91 ++++++++ crates/common_utils/src/lib.rs | 2 + crates/diesel_models/src/ephemeral_key.rs | 6 + crates/diesel_models/src/refund.rs | 9 + .../src/compatibility/stripe/refunds.rs | 8 +- .../src/compatibility/stripe/refunds/types.rs | 3 + crates/router/src/core/api_keys.rs | 8 +- crates/router/src/core/refunds.rs | 7 +- crates/router/src/events/api_logs.rs | 213 ++---------------- crates/router/src/routes/admin.rs | 8 +- crates/router/src/routes/api_keys.rs | 10 +- crates/router/src/routes/refunds.rs | 9 +- crates/router/src/types/api/customers.rs | 6 + 27 files changed, 590 insertions(+), 226 deletions(-) create mode 100644 crates/api_models/src/events.rs create mode 100644 crates/api_models/src/events/customer.rs create mode 100644 crates/api_models/src/events/payment.rs create mode 100644 crates/api_models/src/events/payouts.rs create mode 100644 crates/api_models/src/events/refund.rs create mode 100644 crates/api_models/src/events/routing.rs create mode 100644 crates/common_utils/src/events.rs diff --git a/Cargo.lock b/Cargo.lock index 886a8b50acc8..ac7fde55d8e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1503,7 +1503,6 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" name = "common_enums" version = "0.1.0" dependencies = [ - "common_utils", "diesel", "router_derive", "serde", @@ -1519,6 +1518,7 @@ version = "0.1.0" dependencies = [ "async-trait", "bytes", + "common_enums", "diesel", "error-stack", "fake", diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rs index 037d223754a0..e844d1900a1a 100644 --- a/crates/api_models/src/admin.rs +++ b/crates/api_models/src/admin.rs @@ -893,6 +893,8 @@ pub struct ToggleKVResponse { #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct ToggleKVRequest { + #[serde(skip_deserializing)] + pub merchant_id: String, /// Status of KV for the specific merchant #[schema(example = true)] pub kv_enabled: bool, diff --git a/crates/api_models/src/api_keys.rs b/crates/api_models/src/api_keys.rs index f0ab403d9c65..805c5616c2a0 100644 --- a/crates/api_models/src/api_keys.rs +++ b/crates/api_models/src/api_keys.rs @@ -129,6 +129,12 @@ pub struct UpdateApiKeyRequest { /// rotating your keys once every 6 months. #[schema(example = "2022-09-10T10:11:12Z")] pub expiration: Option, + + #[serde(skip_deserializing)] + pub key_id: String, + + #[serde(skip_deserializing)] + pub merchant_id: String, } /// The response body for revoking an API Key. diff --git a/crates/api_models/src/events.rs b/crates/api_models/src/events.rs new file mode 100644 index 000000000000..1d72f54eb151 --- /dev/null +++ b/crates/api_models/src/events.rs @@ -0,0 +1,73 @@ +pub mod customer; +pub mod payment; +pub mod payouts; +pub mod refund; +pub mod routing; + +use common_utils::{ + events::{ApiEventMetric, ApiEventsType}, + impl_misc_api_event_type, +}; + +use crate::{ + admin::*, api_keys::*, cards_info::*, disputes::*, files::*, mandates::*, payment_methods::*, + payments::*, verifications::*, +}; + +impl ApiEventMetric for TimeRange {} + +impl_misc_api_event_type!( + PaymentMethodId, + PaymentsSessionResponse, + PaymentMethodListResponse, + PaymentMethodCreate, + PaymentLinkInitiateRequest, + RetrievePaymentLinkResponse, + MandateListConstraints, + CreateFileResponse, + DisputeResponse, + SubmitEvidenceRequest, + MerchantConnectorResponse, + MerchantConnectorId, + MandateResponse, + MandateRevokedResponse, + RetrievePaymentLinkRequest, + MandateId, + DisputeListConstraints, + RetrieveApiKeyResponse, + BusinessProfileResponse, + BusinessProfileUpdate, + BusinessProfileCreate, + RevokeApiKeyResponse, + ToggleKVResponse, + ToggleKVRequest, + MerchantAccountDeleteResponse, + MerchantAccountUpdate, + CardInfoResponse, + CreateApiKeyResponse, + CreateApiKeyRequest, + MerchantConnectorDeleteResponse, + MerchantConnectorUpdate, + MerchantConnectorCreate, + MerchantId, + CardsInfoRequest, + MerchantAccountResponse, + MerchantAccountListRequest, + MerchantAccountCreate, + PaymentsSessionRequest, + ApplepayMerchantVerificationRequest, + ApplepayMerchantResponse, + ApplepayVerifiedDomainsResponse, + UpdateApiKeyRequest +); + +#[cfg(feature = "stripe")] +impl_misc_api_event_type!( + StripeSetupIntentResponse, + StripeRefundResponse, + StripePaymentIntentListResponse, + StripePaymentIntentResponse, + CustomerDeleteResponse, + CustomerPaymentMethodListResponse, + CreateCustomerResponse +); diff --git a/crates/api_models/src/events/customer.rs b/crates/api_models/src/events/customer.rs new file mode 100644 index 000000000000..c40619871248 --- /dev/null +++ b/crates/api_models/src/events/customer.rs @@ -0,0 +1,41 @@ +use common_utils::events::{ApiEventMetric, ApiEventsType}; + +use crate::customers::{CustomerDeleteResponse, CustomerId, CustomerRequest, CustomerResponse}; + +impl ApiEventMetric for CustomerDeleteResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Customer { + customer_id: self.customer_id.clone(), + }) + } +} + +impl ApiEventMetric for CustomerRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Customer { + customer_id: self.customer_id.clone(), + }) + } +} + +impl ApiEventMetric for CustomerResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Customer { + customer_id: self.customer_id.clone(), + }) + } +} + +impl ApiEventMetric for CustomerId { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Customer { + customer_id: self.customer_id.clone(), + }) + } +} + +// impl ApiEventMetric for (PaymentMethodListRequest, CustomerId) { +// fn get_api_event_type(&self) -> Option { +// self.1.get_api_event_type() +// } +// } diff --git a/crates/api_models/src/events/payment.rs b/crates/api_models/src/events/payment.rs new file mode 100644 index 000000000000..2f3336fc2777 --- /dev/null +++ b/crates/api_models/src/events/payment.rs @@ -0,0 +1,151 @@ +use common_utils::events::{ApiEventMetric, ApiEventsType}; + +use crate::{ + payment_methods::{ + CustomerPaymentMethodsListResponse, PaymentMethodDeleteResponse, PaymentMethodListRequest, + PaymentMethodResponse, PaymentMethodUpdate, + }, + payments::{ + PaymentIdType, PaymentListConstraints, PaymentListFilterConstraints, PaymentListFilters, + PaymentListResponse, PaymentListResponseV2, PaymentsApproveRequest, PaymentsCancelRequest, + PaymentsCaptureRequest, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse, + PaymentsRetrieveRequest, PaymentsStartRequest, RedirectionResponse, + }, +}; +impl ApiEventMetric for PaymentsRetrieveRequest { + fn get_api_event_type(&self) -> Option { + match self.resource_id { + PaymentIdType::PaymentIntentId(ref id) => Some(ApiEventsType::Payment { + payment_id: id.clone(), + }), + _ => None, + } + } +} + +impl ApiEventMetric for PaymentsStartRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.clone(), + }) + } +} + +impl ApiEventMetric for PaymentsCaptureRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.to_owned(), + }) + } +} + +impl ApiEventMetric for PaymentsCancelRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.clone(), + }) + } +} + +impl ApiEventMetric for PaymentsApproveRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.clone(), + }) + } +} + +impl ApiEventMetric for PaymentsRejectRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payment { + payment_id: self.payment_id.clone(), + }) + } +} + +impl ApiEventMetric for PaymentsRequest { + fn get_api_event_type(&self) -> Option { + match self.payment_id { + Some(PaymentIdType::PaymentIntentId(ref id)) => Some(ApiEventsType::Payment { + payment_id: id.clone(), + }), + _ => None, + } + } +} + +impl ApiEventMetric for PaymentsResponse { + fn get_api_event_type(&self) -> Option { + self.payment_id + .clone() + .map(|payment_id| ApiEventsType::Payment { payment_id }) + } +} + +impl ApiEventMetric for PaymentMethodResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::PaymentMethod { + payment_method_id: self.payment_method_id.clone(), + payment_method: Some(self.payment_method), + payment_method_type: self.payment_method_type, + }) + } +} + +impl ApiEventMetric for PaymentMethodUpdate {} + +impl ApiEventMetric for PaymentMethodDeleteResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::PaymentMethod { + payment_method_id: self.payment_method_id.clone(), + payment_method: None, + payment_method_type: None, + }) + } +} + +impl ApiEventMetric for CustomerPaymentMethodsListResponse {} + +impl ApiEventMetric for PaymentMethodListRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::PaymentMethodList { + payment_id: self + .client_secret + .as_ref() + .and_then(|cs| cs.rsplit_once("_secret_")) + .map(|(pid, _)| pid.to_string()), + }) + } +} + +impl ApiEventMetric for PaymentListFilterConstraints { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for PaymentListFilters { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for PaymentListConstraints { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for PaymentListResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for PaymentListResponseV2 { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for RedirectionResponse {} diff --git a/crates/api_models/src/events/payouts.rs b/crates/api_models/src/events/payouts.rs new file mode 100644 index 000000000000..303709acc476 --- /dev/null +++ b/crates/api_models/src/events/payouts.rs @@ -0,0 +1,29 @@ +use common_utils::events::{ApiEventMetric, ApiEventsType}; + +use crate::payouts::{ + PayoutActionRequest, PayoutCreateRequest, PayoutCreateResponse, PayoutRetrieveRequest, +}; + +impl ApiEventMetric for PayoutRetrieveRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payout) + } +} + +impl ApiEventMetric for PayoutCreateRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payout) + } +} + +impl ApiEventMetric for PayoutCreateResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payout) + } +} + +impl ApiEventMetric for PayoutActionRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Payout) + } +} diff --git a/crates/api_models/src/events/refund.rs b/crates/api_models/src/events/refund.rs new file mode 100644 index 000000000000..424a3191db66 --- /dev/null +++ b/crates/api_models/src/events/refund.rs @@ -0,0 +1,63 @@ +use common_utils::events::{ApiEventMetric, ApiEventsType}; + +use crate::refunds::{ + RefundListMetaData, RefundListRequest, RefundListResponse, RefundRequest, RefundResponse, + RefundUpdateRequest, RefundsRetrieveRequest, +}; + +impl ApiEventMetric for RefundRequest { + fn get_api_event_type(&self) -> Option { + let payment_id = self.payment_id.clone(); + self.refund_id + .clone() + .map(|refund_id| ApiEventsType::Refund { + payment_id: Some(payment_id), + refund_id, + }) + } +} + +impl ApiEventMetric for RefundResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Refund { + payment_id: Some(self.payment_id.clone()), + refund_id: self.refund_id.clone(), + }) + } +} + +impl ApiEventMetric for RefundsRetrieveRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Refund { + payment_id: None, + refund_id: self.refund_id.clone(), + }) + } +} + +impl ApiEventMetric for RefundUpdateRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Refund { + payment_id: None, + refund_id: self.refund_id.clone(), + }) + } +} + +impl ApiEventMetric for RefundListRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for RefundListResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} + +impl ApiEventMetric for RefundListMetaData { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::ResourceListAPI) + } +} diff --git a/crates/api_models/src/events/routing.rs b/crates/api_models/src/events/routing.rs new file mode 100644 index 000000000000..5eca01acc6fb --- /dev/null +++ b/crates/api_models/src/events/routing.rs @@ -0,0 +1,58 @@ +use common_utils::events::{ApiEventMetric, ApiEventsType}; + +use crate::routing::{ + LinkedRoutingConfigRetrieveResponse, MerchantRoutingAlgorithm, RoutingAlgorithmId, + RoutingConfigRequest, RoutingDictionaryRecord, RoutingKind, +}; +#[cfg(feature = "business_profile_routing")] +use crate::routing::{RoutingRetrieveLinkQuery, RoutingRetrieveQuery}; + +impl ApiEventMetric for RoutingKind { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +impl ApiEventMetric for MerchantRoutingAlgorithm { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +impl ApiEventMetric for RoutingAlgorithmId { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +impl ApiEventMetric for RoutingDictionaryRecord { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +impl ApiEventMetric for LinkedRoutingConfigRetrieveResponse { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +#[cfg(feature = "business_profile_routing")] +impl ApiEventMetric for RoutingRetrieveQuery { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +impl ApiEventMetric for RoutingConfigRequest { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} + +#[cfg(feature = "business_profile_routing")] +impl ApiEventMetric for RoutingRetrieveLinkQuery { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Routing) + } +} diff --git a/crates/api_models/src/lib.rs b/crates/api_models/src/lib.rs index ec272514e38a..9fff344b9ff7 100644 --- a/crates/api_models/src/lib.rs +++ b/crates/api_models/src/lib.rs @@ -9,6 +9,7 @@ pub mod enums; pub mod ephemeral_key; #[cfg(feature = "errors")] pub mod errors; +pub mod events; pub mod files; pub mod mandates; pub mod organization; diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs index b30590bfd6f2..3d74e51a58ce 100644 --- a/crates/api_models/src/payment_methods.rs +++ b/crates/api_models/src/payment_methods.rs @@ -12,7 +12,9 @@ use utoipa::ToSchema; #[cfg(feature = "payouts")] use crate::payouts; use crate::{ - admin, enums as api_enums, + admin, + customers::CustomerId, + enums as api_enums, payments::{self, BankCodeResponse}, }; @@ -474,6 +476,8 @@ pub struct RequestPaymentMethodTypes { #[derive(Debug, Clone, serde::Serialize, Default, ToSchema)] #[serde(deny_unknown_fields)] pub struct PaymentMethodListRequest { + #[serde(skip_deserializing)] + pub customer_id: Option, /// This is a 15 minute expiry token which shall be used from the client to authenticate and perform sessions from the SDK #[schema(max_length = 30, min_length = 30, example = "secret_k2uj3he2893ein2d")] pub client_secret: Option, diff --git a/crates/api_models/src/refunds.rs b/crates/api_models/src/refunds.rs index 271179546bb0..6fe8be8b5291 100644 --- a/crates/api_models/src/refunds.rs +++ b/crates/api_models/src/refunds.rs @@ -76,6 +76,8 @@ pub struct RefundsRetrieveRequest { #[derive(Default, Debug, ToSchema, Clone, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct RefundUpdateRequest { + #[serde(skip)] + pub refund_id: String, /// An arbitrary string attached to the object. Often useful for displaying to users and your customer support executive #[schema(max_length = 255, example = "Customer returned the product")] pub reason: Option, diff --git a/crates/common_enums/Cargo.toml b/crates/common_enums/Cargo.toml index 10b4fb509e88..e9f2dffcc050 100644 --- a/crates/common_enums/Cargo.toml +++ b/crates/common_enums/Cargo.toml @@ -19,7 +19,6 @@ time = { version = "0.3.21", features = ["serde", "serde-well-known", "std"] } utoipa = { version = "3.3.0", features = ["preserve_order"] } # First party crates -common_utils = { version = "0.1.0", path = "../common_utils" } router_derive = { version = "0.1.0", path = "../router_derive" } [dev-dependencies] diff --git a/crates/common_utils/Cargo.toml b/crates/common_utils/Cargo.toml index c1fd91a351c7..62bd747da1b0 100644 --- a/crates/common_utils/Cargo.toml +++ b/crates/common_utils/Cargo.toml @@ -42,6 +42,7 @@ phonenumber = "0.3.3" # First party crates masking = { version = "0.1.0", path = "../masking" } router_env = { version = "0.1.0", path = "../router_env", features = ["log_extra_implicit_fields", "log_custom_entries_to_extra"], optional = true } +common_enums = { version = "0.1.0", path = "../common_enums" } [target.'cfg(not(target_os = "windows"))'.dependencies] signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"], optional = true } diff --git a/crates/common_utils/src/events.rs b/crates/common_utils/src/events.rs new file mode 100644 index 000000000000..1d487364031d --- /dev/null +++ b/crates/common_utils/src/events.rs @@ -0,0 +1,91 @@ +use common_enums::{PaymentMethod, PaymentMethodType}; +use serde::Serialize; + +pub trait ApiEventMetric { + fn get_api_event_type(&self) -> Option { + None + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +#[serde(tag = "flow_type")] +pub enum ApiEventsType { + Payout, + Payment { + payment_id: String, + }, + Refund { + payment_id: Option, + refund_id: String, + }, + PaymentMethod { + payment_method_id: String, + payment_method: Option, + payment_method_type: Option, + }, + Customer { + customer_id: String, + }, + User { + //specified merchant_id will overridden on global defined + merchant_id: String, + user_id: String, + }, + PaymentMethodList { + payment_id: Option, + }, + Webhooks { + connector: String, + payment_id: Option, + }, + Routing, + ResourceListAPI, + PaymentRedirectionResponse, + // TODO: This has to be removed once the corresponding apiEventTypes are created + Miscellaneous, +} + +impl ApiEventMetric for serde_json::Value {} +impl ApiEventMetric for () {} + +impl ApiEventMetric for Result { + fn get_api_event_type(&self) -> Option { + match self { + Ok(q) => q.get_api_event_type(), + Err(_) => None, + } + } +} + +// TODO: Ideally all these types should be replaced by newtype responses +impl ApiEventMetric for Vec { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Miscellaneous) + } +} + +#[macro_export] +macro_rules! impl_misc_api_event_type { + ($($type:ty),+) => { + $( + impl ApiEventMetric for $type { + fn get_api_event_type(&self) -> Option { + Some(ApiEventsType::Miscellaneous) + } + } + )+ + }; +} + +impl_misc_api_event_type!( + String, + (&String, &String), + (Option, Option, String), + bool +); + +impl ApiEventMetric for &T { + fn get_api_event_type(&self) -> Option { + T::get_api_event_type(self) + } +} diff --git a/crates/common_utils/src/lib.rs b/crates/common_utils/src/lib.rs index 724c3bca0a27..62428dccfb6a 100644 --- a/crates/common_utils/src/lib.rs +++ b/crates/common_utils/src/lib.rs @@ -6,6 +6,8 @@ pub mod consts; pub mod crypto; pub mod custom_serde; pub mod errors; +#[allow(missing_docs)] // Todo: add docs +pub mod events; pub mod ext_traits; pub mod fp_utils; pub mod pii; diff --git a/crates/diesel_models/src/ephemeral_key.rs b/crates/diesel_models/src/ephemeral_key.rs index 96bd6e497c33..77b9c647e43b 100644 --- a/crates/diesel_models/src/ephemeral_key.rs +++ b/crates/diesel_models/src/ephemeral_key.rs @@ -14,3 +14,9 @@ pub struct EphemeralKey { pub expires: i64, pub secret: String, } + +impl common_utils::events::ApiEventMetric for EphemeralKey { + fn get_api_event_type(&self) -> Option { + Some(common_utils::events::ApiEventsType::Miscellaneous) + } +} diff --git a/crates/diesel_models/src/refund.rs b/crates/diesel_models/src/refund.rs index 73ff34030f81..62aec3fb27d8 100644 --- a/crates/diesel_models/src/refund.rs +++ b/crates/diesel_models/src/refund.rs @@ -227,3 +227,12 @@ pub struct RefundCoreWorkflow { pub merchant_id: String, pub payment_id: String, } + +impl common_utils::events::ApiEventMetric for Refund { + fn get_api_event_type(&self) -> Option { + Some(common_utils::events::ApiEventsType::Refund { + payment_id: Some(self.payment_id.clone()), + refund_id: self.refund_id.clone(), + }) + } +} diff --git a/crates/router/src/compatibility/stripe/refunds.rs b/crates/router/src/compatibility/stripe/refunds.rs index dc147443828c..ad4accf6ca74 100644 --- a/crates/router/src/compatibility/stripe/refunds.rs +++ b/crates/router/src/compatibility/stripe/refunds.rs @@ -149,8 +149,8 @@ pub async fn refund_update( path: web::Path, form_payload: web::Form, ) -> HttpResponse { - let refund_id = path.into_inner(); - let payload = form_payload.into_inner(); + let mut payload = form_payload.into_inner(); + payload.refund_id = path.into_inner(); let create_refund_update_req: refund_types::RefundUpdateRequest = payload.into(); let flow = Flow::RefundsUpdate; @@ -169,9 +169,7 @@ pub async fn refund_update( state.into_inner(), &req, create_refund_update_req, - |state, auth, req| { - refunds::refund_update_core(state, auth.merchant_account, &refund_id, req) - }, + |state, auth, req| refunds::refund_update_core(state, auth.merchant_account, req), &auth::ApiKeyAuth, api_locking::LockAction::NotApplicable, )) diff --git a/crates/router/src/compatibility/stripe/refunds/types.rs b/crates/router/src/compatibility/stripe/refunds/types.rs index e1486186491a..8d65a09187d3 100644 --- a/crates/router/src/compatibility/stripe/refunds/types.rs +++ b/crates/router/src/compatibility/stripe/refunds/types.rs @@ -17,6 +17,8 @@ pub struct StripeCreateRefundRequest { #[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq)] pub struct StripeUpdateRefundRequest { + #[serde(skip)] + pub refund_id: String, pub metadata: Option, } @@ -58,6 +60,7 @@ impl From for refunds::RefundRequest { impl From for refunds::RefundUpdateRequest { fn from(req: StripeUpdateRefundRequest) -> Self { Self { + refund_id: req.refund_id, metadata: req.metadata, reason: None, } diff --git a/crates/router/src/core/api_keys.rs b/crates/router/src/core/api_keys.rs index 7bda894826a1..c1ddc43cd65d 100644 --- a/crates/router/src/core/api_keys.rs +++ b/crates/router/src/core/api_keys.rs @@ -294,10 +294,10 @@ pub async fn retrieve_api_key( #[instrument(skip_all)] pub async fn update_api_key( state: AppState, - merchant_id: &str, - key_id: &str, api_key: api::UpdateApiKeyRequest, ) -> RouterResponse { + let merchant_id = api_key.merchant_id.clone(); + let key_id = api_key.key_id.clone(); let store = state.store.as_ref(); let api_key = store @@ -313,7 +313,7 @@ pub async fn update_api_key( { let expiry_reminder_days = state.conf.api_keys.expiry_reminder_days.clone(); - let task_id = generate_task_id_for_api_key_expiry_workflow(key_id); + let task_id = generate_task_id_for_api_key_expiry_workflow(&key_id); // In order to determine how to update the existing process in the process_tracker table, // we need access to the current entry in the table. let existing_process_tracker_task = store @@ -339,7 +339,7 @@ pub async fn update_api_key( // If an expiry is set to 'never' else { // Process exist in process, revoke it - revoke_api_key_expiry_task(store, key_id) + revoke_api_key_expiry_task(store, &key_id) .await .into_report() .change_context(errors::ApiErrorResponse::InternalServerError) diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs index e582695ca2aa..a42e46ca62d5 100644 --- a/crates/router/src/core/refunds.rs +++ b/crates/router/src/core/refunds.rs @@ -476,14 +476,13 @@ pub async fn sync_refund_with_gateway( pub async fn refund_update_core( state: AppState, merchant_account: domain::MerchantAccount, - refund_id: &str, req: refunds::RefundUpdateRequest, ) -> RouterResponse { let db = state.store.as_ref(); let refund = db .find_refund_by_merchant_id_refund_id( &merchant_account.merchant_id, - refund_id, + &req.refund_id, merchant_account.storage_scheme, ) .await @@ -501,7 +500,9 @@ pub async fn refund_update_core( ) .await .change_context(errors::ApiErrorResponse::InternalServerError) - .attach_printable_lazy(|| format!("Unable to update refund with refund_id: {refund_id}"))?; + .attach_printable_lazy(|| { + format!("Unable to update refund with refund_id: {}", req.refund_id) + })?; Ok(services::ApplicationResponse::Json(response.foreign_into())) } diff --git a/crates/router/src/events/api_logs.rs b/crates/router/src/events/api_logs.rs index 961d127bd9ff..1e3d5f203943 100644 --- a/crates/router/src/events/api_logs.rs +++ b/crates/router/src/events/api_logs.rs @@ -1,53 +1,15 @@ -mod customer; -mod payment; -mod payouts; -mod refund; -mod routing; -use api_models::{ - admin::{ - BusinessProfileCreate, BusinessProfileResponse, BusinessProfileUpdate, - MerchantAccountCreate, MerchantAccountDeleteResponse, MerchantAccountListRequest, - MerchantAccountResponse, MerchantAccountUpdate, MerchantConnectorCreate, - MerchantConnectorDeleteResponse, MerchantConnectorId, MerchantConnectorResponse, - MerchantConnectorUpdate, MerchantId, ToggleKVRequest, ToggleKVResponse, - }, - api_keys::{ - CreateApiKeyRequest, CreateApiKeyResponse, RetrieveApiKeyResponse, RevokeApiKeyResponse, - UpdateApiKeyRequest, - }, - cards_info::{CardInfoResponse, CardsInfoRequest}, - disputes::{ - DisputeEvidenceBlock, DisputeListConstraints, DisputeResponse, SubmitEvidenceRequest, - }, - enums as api_enums, - files::CreateFileResponse, - mandates::{MandateId, MandateListConstraints, MandateResponse, MandateRevokedResponse}, - payment_methods::{PaymentMethodCreate, PaymentMethodId, PaymentMethodListResponse}, - payments::{ - PaymentLinkInitiateRequest, PaymentsSessionRequest, PaymentsSessionResponse, - RetrievePaymentLinkRequest, RetrievePaymentLinkResponse, - }, - refunds::RefundUpdateRequest, - verifications::{ - ApplepayMerchantResponse, ApplepayMerchantVerificationRequest, - ApplepayVerifiedDomainsResponse, - }, -}; -use diesel_models::EphemeralKey; +// mod customer; +// mod payment; +// mod payouts; +// mod refund; +// mod routing; +pub use common_utils::events::{ApiEventMetric, ApiEventsType}; +use common_utils::impl_misc_api_event_type; use router_env::{tracing_actix_web::RequestId, types::FlowMetric}; use serde::Serialize; use time::OffsetDateTime; use super::{EventType, RawEvent}; -#[cfg(feature = "stripe")] -use crate::compatibility::stripe::{ - customers::types::{ - CreateCustomerResponse, CustomerDeleteResponse, CustomerPaymentMethodListResponse, - }, - payment_intents::types::{StripePaymentIntentListResponse, StripePaymentIntentResponse}, - refunds::types::StripeRefundResponse, - setup_intents::types::StripeSetupIntentResponse, -}; #[cfg(feature = "dummy_connector")] use crate::routes::dummy_connector::types::{ DummyConnectorPaymentCompleteRequest, DummyConnectorPaymentConfirmRequest, @@ -56,10 +18,10 @@ use crate::routes::dummy_connector::types::{ DummyConnectorRefundResponse, DummyConnectorRefundRetrieveRequest, }; use crate::{ + core::payments::PaymentsRedirectResponseData, services::{authentication::AuthenticationType, ApplicationResponse, PaymentLinkFormData}, types::api::{ - AttachEvidenceRequest, Config, ConfigUpdate, CreateFileRequest, CustomerResponse, - DisputeId, FileId, + AttachEvidenceRequest, Config, ConfigUpdate, CreateFileRequest, DisputeId, FileId, }, }; @@ -116,50 +78,6 @@ impl TryFrom for RawEvent { } } -pub trait ApiEventMetric { - fn get_api_event_type(&self) -> Option { - None - } -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize)] -#[serde(tag = "flow_type")] -pub enum ApiEventsType { - Payout, - Payment { - payment_id: String, - }, - Refund { - payment_id: Option, - refund_id: String, - }, - PaymentMethod { - payment_method_id: String, - payment_method: Option, - payment_method_type: Option, - }, - Customer { - customer_id: String, - }, - User { - //specified merchant_id will overridden on global defined - merchant_id: String, - user_id: String, - }, - PaymentMethodList { - payment_id: Option, - }, - Webhooks { - connector: String, - payment_id: Option, - }, - Routing, - ResourceListAPI, - PaymentRedirectionResponse, - // TODO: This has to be removed once the corresponding apiEventTypes are created - Miscellaneous, -} - impl ApiEventMetric for ApplicationResponse { fn get_api_event_type(&self) -> Option { match self { @@ -169,108 +87,15 @@ impl ApiEventMetric for ApplicationResponse { } } } - -impl ApiEventMetric for serde_json::Value {} -impl ApiEventMetric for () {} -impl ApiEventMetric for api_models::payments::TimeRange {} - -impl ApiEventMetric for Result { - fn get_api_event_type(&self) -> Option { - match self { - Ok(q) => q.get_api_event_type(), - Err(_) => None, - } - } -} - -macro_rules! impl_misc_api_event_type { - ($($type:ty),+) => { - $( - impl ApiEventMetric for $type { - fn get_api_event_type(&self) -> Option { - Some(ApiEventsType::Miscellaneous) - } - } - )+ - }; -} - impl_misc_api_event_type!( - PaymentMethodId, - PaymentsSessionResponse, - PaymentMethodListResponse, - PaymentMethodCreate, - PaymentLinkFormData, - PaymentLinkInitiateRequest, - RetrievePaymentLinkResponse, - MandateListConstraints, - Vec, - DisputeId, - CreateFileResponse, - AttachEvidenceRequest, - DisputeResponse, - SubmitEvidenceRequest, - bool, - MerchantConnectorResponse, - MerchantConnectorId, - MandateResponse, - FileId, - MandateRevokedResponse, - RetrievePaymentLinkRequest, - MandateId, - EphemeralKey, - String, - DisputeListConstraints, - RetrieveApiKeyResponse, - Vec, - BusinessProfileResponse, - BusinessProfileUpdate, - BusinessProfileCreate, - CreateFileRequest, - CustomerResponse, Config, - ConfigUpdate, - RevokeApiKeyResponse, - ToggleKVResponse, - ToggleKVRequest, - Vec, - MerchantAccountDeleteResponse, - MerchantAccountUpdate, - Vec, - CardInfoResponse, - Vec, - (Option, Option, String), - (&String, &String), - (&String, &String, UpdateApiKeyRequest), - CreateApiKeyResponse, - CreateApiKeyRequest, - (String, ToggleKVRequest), - MerchantConnectorDeleteResponse, - MerchantConnectorUpdate, - Vec, - MerchantConnectorCreate, - MerchantId, - CardsInfoRequest, - MerchantAccountResponse, - Vec, - MerchantAccountListRequest, - MerchantAccountCreate, - PaymentsSessionRequest, - ApplepayMerchantVerificationRequest, - ApplepayMerchantResponse, - ApplepayVerifiedDomainsResponse, - RefundUpdateRequest -); - -#[cfg(feature = "stripe")] -impl_misc_api_event_type!( - StripeSetupIntentResponse, - StripeRefundResponse, - StripePaymentIntentListResponse, - StripePaymentIntentResponse, - CustomerDeleteResponse, - CustomerPaymentMethodListResponse, - CreateCustomerResponse + CreateFileRequest, + FileId, + AttachEvidenceRequest, + DisputeId, + PaymentLinkFormData, + PaymentsRedirectResponseData, + ConfigUpdate ); #[cfg(feature = "dummy_connector")] @@ -284,9 +109,3 @@ impl_misc_api_event_type!( DummyConnectorRefundResponse, DummyConnectorRefundRequest ); - -impl ApiEventMetric for &T { - fn get_api_event_type(&self) -> Option { - T::get_api_event_type(self) - } -} diff --git a/crates/router/src/routes/admin.rs b/crates/router/src/routes/admin.rs index a93556202aab..9153e9e747f6 100644 --- a/crates/router/src/routes/admin.rs +++ b/crates/router/src/routes/admin.rs @@ -388,15 +388,15 @@ pub async fn merchant_account_toggle_kv( json_payload: web::Json, ) -> HttpResponse { let flow = Flow::ConfigKeyUpdate; - let payload = json_payload.into_inner(); - let merchant_id = path.into_inner(); + let mut payload = json_payload.into_inner(); + payload.merchant_id = path.into_inner(); api::server_wrap( flow, state, &req, - (merchant_id, payload), - |state, _, (merchant_id, payload)| kv_for_merchant(state, merchant_id, payload.kv_enabled), + payload, + |state, _, payload| kv_for_merchant(state, payload.merchant_id, payload.kv_enabled), &auth::AdminApiAuth, api_locking::LockAction::NotApplicable, ) diff --git a/crates/router/src/routes/api_keys.rs b/crates/router/src/routes/api_keys.rs index 6057b4c5db24..c2e289cd0f7e 100644 --- a/crates/router/src/routes/api_keys.rs +++ b/crates/router/src/routes/api_keys.rs @@ -124,16 +124,16 @@ pub async fn api_key_update( ) -> impl Responder { let flow = Flow::ApiKeyUpdate; let (merchant_id, key_id) = path.into_inner(); - let payload = json_payload.into_inner(); + let mut payload = json_payload.into_inner(); + payload.key_id = key_id; + payload.merchant_id = merchant_id; api::server_wrap( flow, state, &req, - (&merchant_id, &key_id, payload), - |state, _, (merchant_id, key_id, payload)| { - api_keys::update_api_key(state, merchant_id, key_id, payload) - }, + payload, + |state, _, payload| api_keys::update_api_key(state, payload), &auth::AdminApiAuth, api_locking::LockAction::NotApplicable, ) diff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rs index fcb36214a1c0..c20f3fbf975d 100644 --- a/crates/router/src/routes/refunds.rs +++ b/crates/router/src/routes/refunds.rs @@ -161,15 +161,14 @@ pub async fn refunds_update( path: web::Path, ) -> HttpResponse { let flow = Flow::RefundsUpdate; - let refund_id = path.into_inner(); + let mut refund_update_req = json_payload.into_inner(); + refund_update_req.refund_id = path.into_inner(); api::server_wrap( flow, state, &req, - (&refund_id, json_payload.into_inner()), - |state, auth, (refund_id, req)| { - refund_update_core(state, auth.merchant_account, refund_id, req) - }, + refund_update_req, + |state, auth, req| refund_update_core(state, auth.merchant_account, req), &auth::ApiKeyAuth, api_locking::LockAction::NotApplicable, ) diff --git a/crates/router/src/types/api/customers.rs b/crates/router/src/types/api/customers.rs index 2050b4149ef8..32430c0918a2 100644 --- a/crates/router/src/types/api/customers.rs +++ b/crates/router/src/types/api/customers.rs @@ -10,6 +10,12 @@ newtype!( derives = (Debug, Clone, Serialize) ); +impl common_utils::events::ApiEventMetric for CustomerResponse { + fn get_api_event_type(&self) -> Option { + self.0.get_api_event_type() + } +} + pub(crate) trait CustomerRequestExt: Sized { fn validate(self) -> RouterResult; }