mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-27 11:24:45 +08:00
refactor(payment_id): add payment id domain type (#5738)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -16,10 +16,10 @@ pub enum ApiEventsType {
|
||||
payout_id: String,
|
||||
},
|
||||
Payment {
|
||||
payment_id: String,
|
||||
payment_id: id_type::PaymentId,
|
||||
},
|
||||
Refund {
|
||||
payment_id: Option<String>,
|
||||
payment_id: Option<id_type::PaymentId>,
|
||||
refund_id: String,
|
||||
},
|
||||
PaymentMethod {
|
||||
@ -46,13 +46,13 @@ pub enum ApiEventsType {
|
||||
},
|
||||
Webhooks {
|
||||
connector: String,
|
||||
payment_id: Option<String>,
|
||||
payment_id: Option<id_type::PaymentId>,
|
||||
},
|
||||
Routing,
|
||||
ResourceListAPI,
|
||||
PaymentRedirectionResponse {
|
||||
connector: Option<String>,
|
||||
payment_id: Option<String>,
|
||||
payment_id: Option<id_type::PaymentId>,
|
||||
},
|
||||
Gsm,
|
||||
// TODO: This has to be removed once the corresponding apiEventTypes are created
|
||||
@ -80,6 +80,14 @@ pub enum ApiEventsType {
|
||||
impl ApiEventMetric for serde_json::Value {}
|
||||
impl ApiEventMetric for () {}
|
||||
|
||||
impl ApiEventMetric for id_type::PaymentId {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
Some(ApiEventsType::Payment {
|
||||
payment_id: self.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q: ApiEventMetric, E> ApiEventMetric for Result<Q, E> {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
match self {
|
||||
|
||||
@ -7,6 +7,7 @@ mod customer;
|
||||
mod merchant;
|
||||
mod merchant_connector_account;
|
||||
mod organization;
|
||||
mod payment;
|
||||
mod profile;
|
||||
mod routing;
|
||||
|
||||
@ -23,6 +24,7 @@ use diesel::{
|
||||
pub use merchant::MerchantId;
|
||||
pub use merchant_connector_account::MerchantConnectorAccountId;
|
||||
pub use organization::OrganizationId;
|
||||
pub use payment::PaymentId;
|
||||
pub use profile::ProfileId;
|
||||
pub use routing::RoutingId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
86
crates/common_utils/src/id_type/payment.rs
Normal file
86
crates/common_utils/src/id_type/payment.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use crate::{
|
||||
errors::{CustomResult, ValidationError},
|
||||
generate_id_with_default_len,
|
||||
id_type::{AlphaNumericId, LengthId},
|
||||
};
|
||||
|
||||
crate::id_type!(
|
||||
PaymentId,
|
||||
"A type for payment_id that can be used for payment ids"
|
||||
);
|
||||
crate::impl_id_type_methods!(PaymentId, "payment_id");
|
||||
|
||||
// This is to display the `PaymentId` as PaymentId(abcd)
|
||||
crate::impl_debug_id_type!(PaymentId);
|
||||
crate::impl_default_id_type!(PaymentId, "pay");
|
||||
crate::impl_try_from_cow_str_id_type!(PaymentId, "payment_id");
|
||||
|
||||
crate::impl_queryable_id_type!(PaymentId);
|
||||
crate::impl_to_sql_from_sql_id_type!(PaymentId);
|
||||
|
||||
impl PaymentId {
|
||||
/// Get the hash key to be stored in redis
|
||||
pub fn get_hash_key_for_kv_store(&self) -> String {
|
||||
format!("pi_{}", self.0 .0 .0)
|
||||
}
|
||||
|
||||
// This function should be removed once we have a better way to handle mandatory payment id in other flows
|
||||
/// Get payment id in the format of irrelevant_payment_id_in_{flow}
|
||||
pub fn get_irrelevant_id(flow: &str) -> Self {
|
||||
let alphanumeric_id =
|
||||
AlphaNumericId::new_unchecked(format!("irrelevant_payment_id_in_{flow}"));
|
||||
let id = LengthId::new_unchecked(alphanumeric_id);
|
||||
Self(id)
|
||||
}
|
||||
|
||||
/// Get the attempt id for the payment id based on the attempt count
|
||||
pub fn get_attempt_id(&self, attempt_count: i16) -> String {
|
||||
format!("{}_{attempt_count}", self.get_string_repr())
|
||||
}
|
||||
|
||||
/// Generate a client id for the payment id
|
||||
pub fn generate_client_secret(&self) -> String {
|
||||
generate_id_with_default_len(&format!("{}_secret", self.get_string_repr()))
|
||||
}
|
||||
|
||||
/// Generate a key for pm_auth
|
||||
pub fn get_pm_auth_key(&self) -> String {
|
||||
format!("pm_auth_{}", self.get_string_repr())
|
||||
}
|
||||
|
||||
/// Get external authentication request poll id
|
||||
pub fn get_external_authentication_request_poll_id(&self) -> String {
|
||||
format!("external_authentication_{}", self.get_string_repr())
|
||||
}
|
||||
|
||||
/// Generate a test payment id with prefix test_
|
||||
pub fn generate_test_payment_id_for_sample_data() -> Self {
|
||||
let id = generate_id_with_default_len("test");
|
||||
let alphanumeric_id = AlphaNumericId::new_unchecked(id);
|
||||
let id = LengthId::new_unchecked(alphanumeric_id);
|
||||
Self(id)
|
||||
}
|
||||
|
||||
/// Wrap a string inside PaymentId
|
||||
pub fn wrap(payment_id_string: String) -> CustomResult<Self, ValidationError> {
|
||||
Self::try_from(std::borrow::Cow::from(payment_id_string))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
/// This is implemented so that we can use payment id directly as attribute in metrics
|
||||
impl From<PaymentId> for router_env::opentelemetry::Value {
|
||||
fn from(val: PaymentId) -> Self {
|
||||
let string_value = val.0 .0 .0;
|
||||
Self::String(router_env::opentelemetry::StringValue::from(string_value))
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(feature = "metrics")]
|
||||
// /// This is implemented so that we can use payment id directly as attribute in metrics
|
||||
// impl router_env::tracing::Value for PaymentId {
|
||||
// fn record(&self, key: &router_env::types::Field, visitor: &mut dyn router_env::types::Visit) {
|
||||
// let string_value = self.get_string_repr();
|
||||
// visitor.record_str(key, &string_value);
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user