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

5.1 KB
Raw Blame
use error_stack::report;
use router_env::instrument;

use crate::{
    connection,
    core::errors::{self, CustomResult},
    db::MockDb,
    services::Store,
    types::storage::{self, PaymentLinkDbExt},
};

#[router_derive::track_async_interface]
#[async_trait::async_trait]
pub trait PaymentLinkInterface {
    async fn find_payment_link_by_payment_link_id(
        &self,
        payment_link_id: &str,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError>;

    async fn insert_payment_link(
        &self,
        _payment_link: storage::PaymentLinkNew,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError>;

    async fn list_payment_link_by_processor_merchant_id(
        &self,
        processor_merchant_id: &common_utils::id_type::MerchantId,
        payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<Vec<storage::PaymentLink>, errors::StorageError>;

    async fn get_total_count_of_payment_links(
        &self,
        processor_merchant_id: &common_utils::id_type::MerchantId,
        payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<i64, errors::StorageError>;
}

#[async_trait::async_trait]
impl PaymentLinkInterface for Store {
    #[instrument(skip_all)]
    async fn find_payment_link_by_payment_link_id(
        &self,
        payment_link_id: &str,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError> {
        let conn = connection::pg_connection_read(self).await?;
        storage::PaymentLink::find_link_by_payment_link_id(&conn, payment_link_id)
            .await
            .map_err(|error| report!(errors::StorageError::from(error)))
    }

    #[instrument(skip_all)]
    async fn insert_payment_link(
        &self,
        payment_link_config: storage::PaymentLinkNew,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError> {
        let conn = connection::pg_connection_write(self).await?;
        payment_link_config
            .insert(&conn)
            .await
            .map_err(|error| report!(errors::StorageError::from(error)))
    }

    #[instrument(skip_all)]
    async fn list_payment_link_by_processor_merchant_id(
        &self,
        processor_merchant_id: &common_utils::id_type::MerchantId,
        payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<Vec<storage::PaymentLink>, errors::StorageError> {
        let conn = connection::pg_connection_read(self).await?;
        storage::PaymentLink::filter_by_constraints(
            &conn,
            processor_merchant_id,
            payment_link_constraints,
            profile_id,
        )
        .await
        .map_err(|error| report!(errors::StorageError::from(error)))
    }

    #[instrument(skip_all)]
    async fn get_total_count_of_payment_links(
        &self,
        processor_merchant_id: &common_utils::id_type::MerchantId,
        payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<i64, errors::StorageError> {
        let conn = connection::pg_connection_read(self).await?;
        storage::PaymentLink::get_total_count_of_payment_links(
            &conn,
            processor_merchant_id,
            payment_link_constraints,
            profile_id,
        )
        .await
        .map_err(|error| report!(errors::StorageError::from(error)))
    }
}

#[async_trait::async_trait]
impl PaymentLinkInterface for MockDb {
    async fn insert_payment_link(
        &self,
        _payment_link: storage::PaymentLinkNew,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError> {
        // TODO: Implement function for `MockDb`
        Err(errors::StorageError::MockDbError)?
    }

    async fn find_payment_link_by_payment_link_id(
        &self,
        _payment_link_id: &str,
    ) -> CustomResult<storage::PaymentLink, errors::StorageError> {
        // TODO: Implement function for `MockDb`
        Err(errors::StorageError::MockDbError)?
    }

    async fn list_payment_link_by_processor_merchant_id(
        &self,
        _processor_merchant_id: &common_utils::id_type::MerchantId,
        _payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        _profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<Vec<storage::PaymentLink>, errors::StorageError> {
        // TODO: Implement function for `MockDb`
        Err(errors::StorageError::MockDbError)?
    }

    async fn get_total_count_of_payment_links(
        &self,
        _processor_merchant_id: &common_utils::id_type::MerchantId,
        _payment_link_constraints: &api_models::payments::PaymentLinkListConstraints,
        _profile_id: Option<common_utils::id_type::ProfileId>,
    ) -> CustomResult<i64, errors::StorageError> {
        // TODO: Implement function for `MockDb`
        Err(errors::StorageError::MockDbError)?
    }
}