mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
refactor(connector): add amount conversion framework for ACI (#5456)
This commit is contained in:
@ -1,13 +1,15 @@
|
||||
mod result_codes;
|
||||
pub mod transformers;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use common_utils::request::RequestContent;
|
||||
use common_utils::{
|
||||
request::RequestContent,
|
||||
types::{AmountConvertor, StringMajorUnit, StringMajorUnitForConnector},
|
||||
};
|
||||
use error_stack::{report, ResultExt};
|
||||
use masking::PeekInterface;
|
||||
use transformers as aci;
|
||||
|
||||
use super::utils::{is_mandate_supported, PaymentsAuthorizeRequestData};
|
||||
use super::utils::{convert_amount, is_mandate_supported, PaymentsAuthorizeRequestData};
|
||||
use crate::{
|
||||
configs::settings,
|
||||
connector::utils::PaymentMethodDataType,
|
||||
@ -26,8 +28,18 @@ use crate::{
|
||||
utils::BytesExt,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Aci;
|
||||
#[derive(Clone)]
|
||||
pub struct Aci {
|
||||
amount_converter: &'static (dyn AmountConvertor<Output = StringMajorUnit> + Sync),
|
||||
}
|
||||
|
||||
impl Aci {
|
||||
pub fn new() -> &'static Self {
|
||||
&Self {
|
||||
amount_converter: &StringMajorUnitForConnector,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConnectorCommon for Aci {
|
||||
fn id(&self) -> &'static str {
|
||||
@ -312,12 +324,14 @@ impl
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
// encode only for for urlencoded things.
|
||||
let connector_router_data = aci::AciRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount,
|
||||
req.request.currency,
|
||||
req.request.amount,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
|
||||
let connector_router_data = aci::AciRouterData::from((amount, req));
|
||||
let connector_req = aci::AciPaymentsRequest::try_from(&connector_router_data)?;
|
||||
|
||||
Ok(RequestContent::FormUrlEncoded(Box::new(connector_req)))
|
||||
@ -514,12 +528,13 @@ impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::Ref
|
||||
req: &types::RefundsRouterData<api::Execute>,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = aci::AciRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_refund_amount,
|
||||
req.request.currency,
|
||||
req.request.refund_amount,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
|
||||
let connector_router_data = aci::AciRouterData::from((amount, req));
|
||||
let connector_req = aci::AciRefundRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::FormUrlEncoded(Box::new(connector_req)))
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use common_utils::{id_type, pii::Email};
|
||||
use common_utils::{id_type, pii::Email, types::StringMajorUnit};
|
||||
use error_stack::report;
|
||||
use masking::{ExposeInterface, Secret};
|
||||
use reqwest::Url;
|
||||
@ -18,26 +18,16 @@ type Error = error_stack::Report<errors::ConnectorError>;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct AciRouterData<T> {
|
||||
amount: String,
|
||||
amount: StringMajorUnit,
|
||||
router_data: T,
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(&types::api::CurrencyUnit, enums::Currency, i64, T)> for AciRouterData<T> {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
|
||||
fn try_from(
|
||||
(currency_unit, currency, amount, item): (
|
||||
&types::api::CurrencyUnit,
|
||||
enums::Currency,
|
||||
i64,
|
||||
T,
|
||||
),
|
||||
) -> Result<Self, Self::Error> {
|
||||
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
|
||||
Ok(Self {
|
||||
impl<T> From<(StringMajorUnit, T)> for AciRouterData<T> {
|
||||
fn from((amount, item): (StringMajorUnit, T)) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
router_data: item,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,7 +66,7 @@ pub struct AciPaymentsRequest {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TransactionDetails {
|
||||
pub entity_id: Secret<String>,
|
||||
pub amount: String,
|
||||
pub amount: StringMajorUnit,
|
||||
pub currency: String,
|
||||
pub payment_type: AciPaymentType,
|
||||
}
|
||||
@ -786,7 +776,7 @@ impl<F, T>
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AciRefundRequest {
|
||||
pub amount: String,
|
||||
pub amount: StringMajorUnit,
|
||||
pub currency: String,
|
||||
pub payment_type: AciPaymentType,
|
||||
pub entity_id: Secret<String>,
|
||||
|
||||
@ -306,7 +306,7 @@ impl ConnectorData {
|
||||
) -> CustomResult<ConnectorEnum, errors::ApiErrorResponse> {
|
||||
match enums::Connector::from_str(connector_name) {
|
||||
Ok(name) => match name {
|
||||
enums::Connector::Aci => Ok(ConnectorEnum::Old(Box::new(&connector::Aci))),
|
||||
enums::Connector::Aci => Ok(ConnectorEnum::Old(Box::new(connector::Aci::new()))),
|
||||
enums::Connector::Adyen => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Adyen::new())))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user