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

3.5 KB
Raw Blame
pub mod payment_connector_required_fields;
pub mod settings;

use hyperswitch_masking::Secret;
use serde::Deserialize;
use url::Url;

/// Microservice configuration for payment method flows.
#[derive(Debug, Deserialize, Clone, Default)]
#[serde(default)]
pub struct MicroServicesConfig {
    /// Base URL for the modular payment methods service.
    pub payment_methods_base_url: ModularPaymentMethodServiceUrl,
    pub payment_methods_prefix: ModularPaymentMethodServicePrefix,
    pub use_legacy_locker: bool,
    pub authentication_service: Option<AuthenticationServiceConfig>,
}

impl MicroServicesConfig {
    pub async fn decrypt_credentials(
        self,
        client: &dyn hyperswitch_interfaces::secrets_interface::SecretManagementInterface,
    ) -> common_utils::errors::CustomResult<
        Self,
        hyperswitch_interfaces::secrets_interface::SecretsManagementError,
    > {
        let authentication_service = match self.authentication_service {
            Some(service) => Some(AuthenticationServiceConfig {
                api_key: client.get_secret(service.api_key).await.map_err(|error| {
                    error.attach_printable("micro_services.authentication_service.api_key")
                })?,
                ..service
            }),
            None => None,
        };
        Ok(Self {
            authentication_service,
            ..self
        })
    }
}

/// Configuration for the authentication microservice.
#[derive(Debug, Deserialize, Clone, Default)]
#[serde(default)]
pub struct AuthenticationServiceConfig {
    pub base_url: AuthenticationServiceUrl,
    pub api_key: Secret<String>,
}

/// Base URL wrapper for the modular payment methods service.
#[derive(Debug, Deserialize, Clone)]
#[serde(transparent)]
pub struct AuthenticationServiceUrl(pub Url);

impl Default for AuthenticationServiceUrl {
    fn default() -> Self {
        Self(
            #[allow(clippy::expect_used)]
            Url::parse("http://localhost:8080")
                .expect("Failed to parse default payment_methods_base_url"),
        )
    }
}

impl std::ops::Deref for AuthenticationServiceUrl {
    type Target = Url;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<Url> for AuthenticationServiceUrl {
    fn as_ref(&self) -> &Url {
        &self.0
    }
}

/// Prefix wrapper for the modular payment methods service.
#[derive(Debug, Deserialize, Clone)]
#[serde(transparent)]
pub struct ModularPaymentMethodServicePrefix(pub String);

impl Default for ModularPaymentMethodServicePrefix {
    fn default() -> Self {
        Self("v2".to_string())
    }
}

impl std::ops::Deref for ModularPaymentMethodServicePrefix {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for ModularPaymentMethodServicePrefix {
    fn as_ref(&self) -> &str {
        &self.0
    }
}
/// Base URL wrapper for the modular payment methods service.
#[derive(Debug, Deserialize, Clone)]
#[serde(transparent)]
pub struct ModularPaymentMethodServiceUrl(pub Url);

impl Default for ModularPaymentMethodServiceUrl {
    fn default() -> Self {
        Self(
            #[allow(clippy::expect_used)]
            Url::parse("http://localhost:8080")
                .expect("Failed to parse default payment_methods_base_url"),
        )
    }
}

impl std::ops::Deref for ModularPaymentMethodServiceUrl {
    type Target = Url;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<Url> for ModularPaymentMethodServiceUrl {
    fn as_ref(&self) -> &Url {
        &self.0
    }
}