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

8.2 KB
Raw Blame
//! Interactions with external systems.

#![warn(missing_docs, missing_debug_implementations)]

#[cfg(feature = "aws_kms")]
pub mod aws_kms;
/// crm module
pub mod crm;
#[cfg(feature = "email")]
pub mod email;
pub mod file_storage;
#[cfg(feature = "gcp_kms")]
pub mod gcp_kms;
/// Building grpc clients to communicate with the server
pub mod grpc_client;
#[cfg(feature = "hashicorp-vault")]
pub mod hashicorp_vault;
/// http_client module
pub mod http_client;
/// hubspot_proxy module
pub mod hubspot_proxy;
pub mod managers;
pub mod no_encryption;
#[cfg(feature = "oci_kms")]
pub mod oci_kms;
#[cfg(feature = "superposition")]
pub mod superposition;
/// deserializers module_path
pub mod utils;

/// Builds a report from a shared [`cloud_services`] client error, attaching the error's cause as
/// text: `error-stack` prints only a context's own message, so the cause would otherwise be
/// missing from logged reports.
#[cfg(any(feature = "aws_kms", feature = "gcp_kms", feature = "email"))]
fn report_with_cause<E>(error: E) -> error_stack::Report<E>
where
    E: std::error::Error + Send + Sync + 'static,
{
    let cause = error.source().map(ToString::to_string);
    let report = error_stack::Report::new(error);
    match cause {
        Some(cause) => report.attach_printable(cause),
        None => report,
    }
}

#[cfg(feature = "revenue_recovery")]
/// date_time module
pub mod date_time {
    use error_stack::ResultExt;

    /// Errors in time conversion
    #[derive(Debug, thiserror::Error)]
    pub enum DateTimeConversionError {
        #[error("Invalid timestamp value from prost Timestamp: out of representable range")]
        /// Error for out of range
        TimestampOutOfRange,
    }

    /// Converts a `time::PrimitiveDateTime` to a `prost_types::Timestamp`.
    pub fn convert_to_prost_timestamp(dt: time::PrimitiveDateTime) -> prost_types::Timestamp {
        let odt = dt.assume_utc();
        prost_types::Timestamp {
            seconds: odt.unix_timestamp(),
            // This conversion is safe as nanoseconds (0..999_999_999) always fit within an i32.
            #[allow(clippy::as_conversions)]
            nanos: odt.nanosecond() as i32,
        }
    }

    /// Converts a `prost_types::Timestamp` to an `time::PrimitiveDateTime`.
    pub fn convert_from_prost_timestamp(
        ts: &prost_types::Timestamp,
    ) -> error_stack::Result<time::PrimitiveDateTime, DateTimeConversionError> {
        let timestamp_nanos = i128::from(ts.seconds) * 1_000_000_000 + i128::from(ts.nanos);

        time::OffsetDateTime::from_unix_timestamp_nanos(timestamp_nanos)
            .map(|offset_dt| time::PrimitiveDateTime::new(offset_dt.date(), offset_dt.time()))
            .change_context(DateTimeConversionError::TimestampOutOfRange)
    }
}

/// Crate specific constants
pub mod consts {
    /// Default per-RPC timeout (seconds) for unified connector service calls.
    pub(crate) const DEFAULT_UCS_REQUEST_TIMEOUT_SECS: u64 = 35;

    /// Default HTTP/2 PING keepalive interval (seconds) on the shared channel to the unified
    /// connector service. PINGs are sent while idle so a dead connection is detected and
    /// re-established in the background instead of by the next payment RPC.
    pub(crate) const DEFAULT_UCS_KEEP_ALIVE_INTERVAL_SECS: u64 = 30;

    /// Default time (seconds) to wait for a keepalive PING acknowledgement before the
    /// connection to the unified connector service is treated as dead. Matches the gRPC-Go
    /// client default; a tighter value drops connections during ordinary load spikes.
    pub(crate) const DEFAULT_UCS_KEEP_ALIVE_TIMEOUT_SECS: u64 = 20;

    /// Default TCP keepalive idle time (seconds) on the shared channel to the unified connector
    /// service. OS-level backstop for the HTTP/2 PING keepalive.
    pub(crate) const DEFAULT_UCS_TCP_KEEPALIVE_SECS: u64 = 60;

    /// Header key used to specify the connector name in UCS requests.
    pub(crate) const UCS_HEADER_CONNECTOR: &str = "x-connector";

    /// Provider identity for UCS webhook event parsing, independent of the resource family.
    pub(crate) const UCS_HEADER_WEBHOOK_CONNECTOR: &str = "x-webhook-connector";

    /// Header key used to specify the payout connector name in UCS requests.
    pub(crate) const UCS_HEADER_PAYOUT_CONNECTOR: &str = "x-payout-connector";

    /// Header key used to specify the surcharge connector name in UCS requests.
    pub(crate) const UCS_HEADER_SURCHARGE_CONNECTOR: &str = "x-surcharge-connector";
    pub(crate) const UCS_HEADER_FRM_CONNECTOR: &str = "x-frm-connector";

    /// Header key used to indicate the authentication type being used.
    pub(crate) const UCS_HEADER_AUTH_TYPE: &str = "x-auth";

    /// Header key for sending the API key used for authentication.
    pub(crate) const UCS_HEADER_API_KEY: &str = "x-api-key";

    /// Header key for sending an additional secret key used in some auth types.
    pub(crate) const UCS_HEADER_KEY1: &str = "x-key1";

    /// Header key for sending the API secret in signature-based authentication.
    pub(crate) const UCS_HEADER_API_SECRET: &str = "x-api-secret";

    /// Header key for sending a second additional key used in multi-auth authentication.
    pub(crate) const UCS_HEADER_KEY2: &str = "x-key2";

    /// Header key for sending the AUTH KEY MAP in currency-based authentication.
    pub(crate) const UCS_HEADER_AUTH_KEY_MAP: &str = "x-auth-key-map";

    /// Header key for sending the EXTERNAL VAULT METADATA in proxy payments
    pub(crate) const UCS_HEADER_EXTERNAL_VAULT_METADATA: &str = "x-external-vault-metadata";

    /// Header key for sending the list of lineage ids
    pub(crate) const UCS_LINEAGE_IDS: &str = "x-lineage-ids";

    /// Header key for sending the merchant reference id to UCS
    pub(crate) const UCS_HEADER_REFERENCE_ID: &str = "x-reference-id";

    /// Header key for sending the resource id to UCS
    pub(crate) const UCS_HEADER_RESOURCE_ID: &str = "x-resource-id";

    /// Header key for sending connector-specific configuration to UCS
    pub(crate) const UCS_HEADER_CONNECTOR_CONFIG: &str = "x-connector-config";
}

/// Metrics for interactions with external systems.
#[cfg(any(feature = "aws_kms", feature = "gcp_kms", feature = "oci_kms"))]
pub mod metrics {
    use router_env::{counter_metric, global_meter, histogram_metric_f64};

    global_meter!(GLOBAL_METER, "EXTERNAL_SERVICES");

    #[cfg(feature = "aws_kms")]
    counter_metric!(AWS_KMS_DECRYPTION_FAILURES, GLOBAL_METER); // No. of AWS KMS Decryption failures
    #[cfg(feature = "aws_kms")]
    counter_metric!(AWS_KMS_ENCRYPTION_FAILURES, GLOBAL_METER); // No. of AWS KMS Encryption failures

    #[cfg(feature = "aws_kms")]
    histogram_metric_f64!(AWS_KMS_DECRYPT_TIME, GLOBAL_METER); // Histogram for AWS KMS decryption time (in sec)
    #[cfg(feature = "aws_kms")]
    histogram_metric_f64!(AWS_KMS_ENCRYPT_TIME, GLOBAL_METER); // Histogram for AWS KMS encryption time (in sec)

    #[cfg(feature = "gcp_kms")]
    counter_metric!(GCP_KMS_DECRYPTION_FAILURES, GLOBAL_METER); // No. of GCP KMS Decryption failures
    #[cfg(feature = "gcp_kms")]
    counter_metric!(GCP_KMS_ENCRYPTION_FAILURES, GLOBAL_METER); // No. of GCP KMS Encryption failures

    #[cfg(feature = "gcp_kms")]
    histogram_metric_f64!(GCP_KMS_DECRYPT_TIME, GLOBAL_METER); // Histogram for GCP KMS decryption time (in sec)
    #[cfg(feature = "gcp_kms")]
    histogram_metric_f64!(GCP_KMS_ENCRYPT_TIME, GLOBAL_METER); // Histogram for GCP KMS encryption time (in sec)

    #[cfg(feature = "oci_kms")]
    counter_metric!(OCI_KMS_DECRYPTION_FAILURES, GLOBAL_METER); // No. of OCI KMS Decryption failures
    #[cfg(feature = "oci_kms")]
    counter_metric!(OCI_KMS_ENCRYPTION_FAILURES, GLOBAL_METER); // No. of OCI KMS Encryption failures

    #[cfg(feature = "oci_kms")]
    histogram_metric_f64!(OCI_KMS_DECRYPT_TIME, GLOBAL_METER); // Histogram for OCI KMS decryption time (in sec)
    #[cfg(feature = "oci_kms")]
    histogram_metric_f64!(OCI_KMS_ENCRYPT_TIME, GLOBAL_METER); // Histogram for OCI KMS encryption time (in sec)
}

/// Metrics for config-related operations
#[cfg(feature = "superposition")]
pub mod config_metrics {
    use router_env::{counter_metric, global_meter};

    global_meter!(GLOBAL_METER, "EXTERNAL_SERVICES");

    counter_metric!(CONFIG_SUPERPOSITION_FETCH, GLOBAL_METER); // No. of configs fetched from Superposition
}