mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 09:07:09 +08:00
refactor(required_fields): move pm required fields to pm crate (#7539)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
2
crates/payment_methods/src/configs.rs
Normal file
2
crates/payment_methods/src/configs.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod payment_connector_required_fields;
|
||||
pub mod settings;
|
||||
File diff suppressed because it is too large
Load Diff
144
crates/payment_methods/src/configs/settings.rs
Normal file
144
crates/payment_methods/src/configs/settings.rs
Normal file
@ -0,0 +1,144 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use api_models::{enums, payment_methods::RequiredFieldInfo};
|
||||
use common_utils::errors::CustomResult;
|
||||
use hyperswitch_interfaces::secrets_interface::{
|
||||
secret_handler::SecretsHandler,
|
||||
secret_state::{RawSecret, SecretStateContainer, SecuredSecret},
|
||||
SecretManagementInterface, SecretsManagementError,
|
||||
};
|
||||
use masking::Secret;
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[cfg_attr(feature = "v2", derive(Default))] // Configs are read from the config file in config/payment_required_fields.toml
|
||||
pub struct RequiredFields(pub HashMap<enums::PaymentMethod, PaymentMethodType>);
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct PaymentMethodType(pub HashMap<enums::PaymentMethodType, ConnectorFields>);
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ConnectorFields {
|
||||
pub fields: HashMap<enums::Connector, RequiredFieldFinal>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "v1")]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct RequiredFieldFinal {
|
||||
pub mandate: HashMap<String, RequiredFieldInfo>,
|
||||
pub non_mandate: HashMap<String, RequiredFieldInfo>,
|
||||
pub common: HashMap<String, RequiredFieldInfo>,
|
||||
}
|
||||
#[cfg(feature = "v2")]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct RequiredFieldFinal {
|
||||
pub mandate: Option<Vec<RequiredFieldInfo>>,
|
||||
pub non_mandate: Option<Vec<RequiredFieldInfo>>,
|
||||
pub common: Option<Vec<RequiredFieldInfo>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default)]
|
||||
pub struct PaymentMethodAuth {
|
||||
pub redis_expiry: i64,
|
||||
pub pm_auth_key: Secret<String>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl SecretsHandler for PaymentMethodAuth {
|
||||
async fn convert_to_raw_secret(
|
||||
value: SecretStateContainer<Self, SecuredSecret>,
|
||||
secret_management_client: &dyn SecretManagementInterface,
|
||||
) -> CustomResult<SecretStateContainer<Self, RawSecret>, SecretsManagementError> {
|
||||
let payment_method_auth = value.get_inner();
|
||||
|
||||
let pm_auth_key = secret_management_client
|
||||
.get_secret(payment_method_auth.pm_auth_key.clone())
|
||||
.await?;
|
||||
|
||||
Ok(value.transition_state(|payment_method_auth| Self {
|
||||
pm_auth_key,
|
||||
..payment_method_auth
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default)]
|
||||
#[serde(default)]
|
||||
pub struct EligiblePaymentMethods {
|
||||
#[serde(deserialize_with = "deserialize_hashset")]
|
||||
pub sdk_eligible_payment_methods: HashSet<String>,
|
||||
}
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct SupportedPaymentMethodsForMandate(
|
||||
pub HashMap<enums::PaymentMethod, SupportedPaymentMethodTypesForMandate>,
|
||||
);
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct SupportedPaymentMethodTypesForMandate(
|
||||
pub HashMap<enums::PaymentMethodType, SupportedConnectorsForMandate>,
|
||||
);
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct SupportedConnectorsForMandate {
|
||||
#[serde(deserialize_with = "deserialize_hashset")]
|
||||
pub connector_list: HashSet<enums::Connector>,
|
||||
}
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Mandates {
|
||||
pub supported_payment_methods: SupportedPaymentMethodsForMandate,
|
||||
pub update_mandate_supported: SupportedPaymentMethodsForMandate,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ZeroMandates {
|
||||
pub supported_payment_methods: SupportedPaymentMethodsForMandate,
|
||||
}
|
||||
|
||||
fn deserialize_hashset_inner<T>(value: impl AsRef<str>) -> Result<HashSet<T>, String>
|
||||
where
|
||||
T: Eq + std::str::FromStr + std::hash::Hash,
|
||||
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
let (values, errors) = value
|
||||
.as_ref()
|
||||
.trim()
|
||||
.split(',')
|
||||
.map(|s| {
|
||||
T::from_str(s.trim()).map_err(|error| {
|
||||
format!(
|
||||
"Unable to deserialize `{}` as `{}`: {error}",
|
||||
s.trim(),
|
||||
std::any::type_name::<T>()
|
||||
)
|
||||
})
|
||||
})
|
||||
.fold(
|
||||
(HashSet::new(), Vec::new()),
|
||||
|(mut values, mut errors), result| match result {
|
||||
Ok(t) => {
|
||||
values.insert(t);
|
||||
(values, errors)
|
||||
}
|
||||
Err(error) => {
|
||||
errors.push(error);
|
||||
(values, errors)
|
||||
}
|
||||
},
|
||||
);
|
||||
if !errors.is_empty() {
|
||||
Err(format!("Some errors occurred:\n{}", errors.join("\n")))
|
||||
} else {
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_hashset<'a, D, T>(deserializer: D) -> Result<HashSet<T>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'a>,
|
||||
T: Eq + std::str::FromStr + std::hash::Hash,
|
||||
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
use serde::de::Error;
|
||||
|
||||
deserialize_hashset_inner(<String>::deserialize(deserializer)?).map_err(D::Error::custom)
|
||||
}
|
||||
@ -1 +1,2 @@
|
||||
pub mod configs;
|
||||
pub mod state;
|
||||
|
||||
Reference in New Issue
Block a user