Skip to content

Commit

Permalink
refactor(router): add amount conversion framework to netcetera (#6238)
Browse files Browse the repository at this point in the history
Co-authored-by: DEEPANSHU BANSAL <[email protected]>
Co-authored-by: Hrithikesh <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2024
1 parent 6829478 commit db6e7b1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
58 changes: 37 additions & 21 deletions crates/router/src/connector/netcetera.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
pub mod netcetera_types;
pub mod transformers;

use std::fmt::Debug;

use common_utils::{ext_traits::ByteSliceExt, request::RequestContent};
use common_utils::{
ext_traits::ByteSliceExt,
request::RequestContent,
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
};
use error_stack::ResultExt;
use hyperswitch_interfaces::authentication::ExternalAuthenticationPayload;
use transformers as netcetera;
use transformers::{self as netcetera, NetceteraRouterData};

use super::utils;
use crate::{
configs::settings,
core::errors::{self, CustomResult},
Expand All @@ -22,8 +25,18 @@ use crate::{
utils::BytesExt,
};

#[derive(Debug, Clone)]
pub struct Netcetera;
#[derive(Clone)]
pub struct Netcetera {
amount_convertor: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
}

impl Netcetera {
pub fn new() -> &'static Self {
&Self {
amount_convertor: &MinorUnitForConnector,
}
}
}

impl api::Payment for Netcetera {}
impl api::PaymentSession for Netcetera {}
Expand Down Expand Up @@ -267,7 +280,7 @@ impl
req: &types::authentication::PreAuthNRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = netcetera::NetceteraRouterData::try_from((0, req))?;
let connector_router_data = NetceteraRouterData::try_from((Some(MinorUnit::zero()), req))?;
let req_obj =
netcetera::NetceteraPreAuthenticationRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(req_obj)))
Expand Down Expand Up @@ -365,20 +378,23 @@ impl
req: &types::authentication::ConnectorAuthenticationRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = netcetera::NetceteraRouterData::try_from((
&self.get_currency_unit(),
req.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?,
req.request
.amount
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "amount",
})?,
req,
))?;
let amount = match req.request.amount {
Some(amount) => {
let currency =
req.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?;
Some(utils::convert_amount(
self.amount_convertor,
MinorUnit::new(amount),
currency,
)?)
}
None => None,
};
let connector_router_data = NetceteraRouterData::try_from((amount, req))?;
let req_obj = netcetera::NetceteraAuthenticationRequest::try_from(&connector_router_data);
Ok(RequestContent::Json(Box::new(req_obj?)))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/router/src/connector/netcetera/netcetera_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use common_utils::pii::Email;
use common_utils::{pii::Email, types::MinorUnit};
use masking::ExposeInterface;
use serde::{Deserialize, Serialize};
use unidecode::unidecode;
Expand Down Expand Up @@ -839,7 +839,7 @@ pub struct Purchase {
///
/// Starting from EMV 3DS 2.3.1:
/// Additionally this field is required for messageCategory = 02 (NPA) if threeRIInd = 01, 02, 06, 07, 08, 09, or 11.
pub purchase_amount: Option<i64>,
pub purchase_amount: Option<MinorUnit>,

/// Currency in which purchase amount is expressed. The value is limited to 3 numeric characters and is represented by
/// the ISO 4217 three-digit currency code, except 955-964 and 999.
Expand Down Expand Up @@ -906,7 +906,7 @@ pub struct Purchase {
/// purchaseAmount != recurringAmount AND recurringInd = 01.
///
/// Available for supporting EMV 3DS 2.3.1 and later versions.
pub recurring_amount: Option<i64>,
pub recurring_amount: Option<MinorUnit>,

/// Currency in which recurring amount is expressed. The value is limited to 3 numeric characters and is represented by
/// the ISO 4217 three-digit currency code, except 955-964 and 999.
Expand Down
29 changes: 5 additions & 24 deletions crates/router/src/connector/netcetera/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_utils::types::MinorUnit;
use error_stack::ResultExt;
use masking::Secret;
use serde::{Deserialize, Serialize};
Expand All @@ -12,33 +13,13 @@ use crate::{

//TODO: Fill the struct with respective fields
pub struct NetceteraRouterData<T> {
pub amount: i64, // The type of amount that a connector accepts, for example, String, i64, f64, etc.
pub amount: Option<MinorUnit>, // The type of amount that a connector accepts, for example, String, i64, f64, etc.
pub router_data: T,
}

impl<T> TryFrom<(&api::CurrencyUnit, types::storage::enums::Currency, i64, T)>
for NetceteraRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(_currency_unit, _currency, amount, item): (
&api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
//Todo : use utils to convert the amount to the type of amount that a connector accepts
Ok(Self {
amount,
router_data: item,
})
}
}

impl<T> TryFrom<(i64, T)> for NetceteraRouterData<T> {
impl<T> TryFrom<(Option<MinorUnit>, T)> for NetceteraRouterData<T> {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from((amount, router_data): (i64, T)) -> Result<Self, Self::Error> {
fn try_from((amount, router_data): (Option<MinorUnit>, T)) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data,
Expand Down Expand Up @@ -479,7 +460,7 @@ impl TryFrom<&NetceteraRouterData<&types::authentication::ConnectorAuthenticatio
let purchase = netcetera_types::Purchase {
purchase_instal_data: None,
merchant_risk_indicator: None,
purchase_amount: request.amount,
purchase_amount: item.amount,
purchase_currency: currency.iso_4217().to_string(),
purchase_exponent: currency.number_of_digits_after_decimal_point(),
purchase_date: Some(
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ impl ConnectorData {
Ok(ConnectorEnum::Old(Box::new(connector::Multisafepay::new())))
}
enums::Connector::Netcetera => {
Ok(ConnectorEnum::Old(Box::new(&connector::Netcetera)))
Ok(ConnectorEnum::Old(Box::new(connector::Netcetera::new())))
}
enums::Connector::Nexinets => {
Ok(ConnectorEnum::Old(Box::new(&connector::Nexinets)))
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/api/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl AuthenticationConnectorData {
Ok(ConnectorEnum::Old(Box::new(&connector::Threedsecureio)))
}
enums::AuthenticationConnectors::Netcetera => {
Ok(ConnectorEnum::Old(Box::new(&connector::Netcetera)))
Ok(ConnectorEnum::Old(Box::new(connector::Netcetera::new())))
}
enums::AuthenticationConnectors::Gpayments => {
Ok(ConnectorEnum::Old(Box::new(connector::Gpayments::new())))
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/netcetera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl utils::Connector for NetceteraTest {
fn get_data(&self) -> types::api::ConnectorData {
use router::connector::Netcetera;
utils::construct_connector_data_old(
Box::new(&Netcetera),
Box::new(Netcetera::new()),
types::Connector::Netcetera,
types::api::GetToken::Connector,
None,
Expand Down

0 comments on commit db6e7b1

Please sign in to comment.