Skip to content

Commit

Permalink
refactor(connector): add amount conversion framework to Wellsfargo (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidharth-Singh10 authored Oct 24, 2024
1 parent fbe3951 commit c3b0f7c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
4 changes: 4 additions & 0 deletions crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ impl StringMajorUnit {
.ok_or(ParsingError::DecimalToI64ConversionFailure)?;
Ok(MinorUnit::new(amount_i64))
}
/// forms a new StringMajorUnit default unit i.e zero
pub fn zero() -> Self {
Self("0".to_string())
}

/// Get string amount from struct to be removed in future
pub fn get_amount_as_string(&self) -> String {
Expand Down
84 changes: 50 additions & 34 deletions crates/router/src/connector/wellsfargo.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod transformers;

use std::fmt::Debug;

use base64::Engine;
use common_utils::request::RequestContent;
use common_utils::{
request::RequestContent,
types::{AmountConvertor, MinorUnit, StringMajorUnit, StringMajorUnitForConnector},
};
use diesel_models::enums;
use error_stack::{report, Report, ResultExt};
use masking::{ExposeInterface, PeekInterface};
Expand All @@ -12,6 +13,7 @@ use time::OffsetDateTime;
use transformers as wellsfargo;
use url::Url;

use super::utils::convert_amount;
use crate::{
configs::settings,
connector::{
Expand All @@ -35,10 +37,18 @@ use crate::{
utils::BytesExt,
};

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

impl Wellsfargo {
pub fn new() -> &'static Self {
&Self {
amount_converter: &StringMajorUnitForConnector,
}
}

pub fn generate_digest(&self, payload: &[u8]) -> String {
let payload_digest = digest::digest(&digest::SHA256, payload);
consts::BASE64_ENGINE.encode(payload_digest)
Expand Down Expand Up @@ -600,12 +610,14 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
req: &types::PaymentsCaptureRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = wellsfargo::WellsfargoRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.amount_to_capture),
req.request.currency,
req.request.amount_to_capture,
req,
))?;
)?;

let connector_router_data = wellsfargo::WellsfargoRouterData::from((amount, req));

let connector_req =
wellsfargo::WellsfargoPaymentsCaptureRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
Expand Down Expand Up @@ -792,12 +804,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
req: &types::PaymentsAuthorizeRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = wellsfargo::WellsfargoRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.amount),
req.request.currency,
req.request.amount,
req,
))?;
)?;

let connector_router_data = wellsfargo::WellsfargoRouterData::from((amount, req));
let connector_req =
wellsfargo::WellsfargoPaymentsRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
Expand Down Expand Up @@ -915,20 +928,21 @@ impl ConnectorIntegration<api::Void, types::PaymentsCancelData, types::PaymentsR
req: &types::PaymentsCancelRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = wellsfargo::WellsfargoRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.amount.ok_or(
errors::ConnectorError::MissingRequiredField {
field_name: "Amount",
},
)?),
req.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "Currency",
})?,
req.request
.amount
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "Amount",
})?,
req,
))?;
)?;

let connector_router_data = wellsfargo::WellsfargoRouterData::from((amount, req));
let connector_req = wellsfargo::WellsfargoVoidRequest::try_from(&connector_router_data)?;

Ok(RequestContent::Json(Box::new(connector_req)))
Expand Down Expand Up @@ -1040,12 +1054,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
req: &types::RefundExecuteRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = wellsfargo::WellsfargoRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.refund_amount),
req.request.currency,
req.request.refund_amount,
req,
))?;
)?;

let connector_router_data = wellsfargo::WellsfargoRouterData::from((amount, req));
let connector_req = wellsfargo::WellsfargoRefundRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down Expand Up @@ -1204,12 +1219,13 @@ impl
req: &types::PaymentsIncrementalAuthorizationRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = wellsfargo::WellsfargoRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.additional_amount),
req.request.currency,
req.request.additional_amount,
req,
))?;
)?;

let connector_router_data = wellsfargo::WellsfargoRouterData::from((amount, req));
let connector_request =
wellsfargo::WellsfargoPaymentsIncrementalAuthorizationRequest::try_from(
&connector_router_data,
Expand Down
28 changes: 13 additions & 15 deletions crates/router/src/connector/wellsfargo/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use api_models::payments;
use base64::Engine;
use common_enums::FutureUsage;
use common_utils::{pii, types::SemanticVersion};
use common_utils::{
pii,
types::{SemanticVersion, StringMajorUnit},
};
use masking::{ExposeInterface, PeekInterface, Secret};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -26,21 +29,16 @@ use crate::{

#[derive(Debug, Serialize)]
pub struct WellsfargoRouterData<T> {
pub amount: String,
pub amount: StringMajorUnit,
pub router_data: T,
}

impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for WellsfargoRouterData<T> {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(currency_unit, currency, amount, item): (&api::CurrencyUnit, enums::Currency, i64, T),
) -> Result<Self, Self::Error> {
// This conversion function is used at different places in the file, if updating this, keep a check for those
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
Ok(Self {
impl<T> From<(StringMajorUnit, T)> for WellsfargoRouterData<T> {
fn from((amount, router_data): (StringMajorUnit, T)) -> Self {
Self {
amount,
router_data: item,
})
router_data,
}
}
}

Expand All @@ -61,7 +59,7 @@ impl TryFrom<&types::SetupMandateRouterData> for WellsfargoZeroMandateRequest {

let order_information = OrderInformationWithBill {
amount_details: Amount {
total_amount: "0".to_string(),
total_amount: StringMajorUnit::zero(),
currency: item.request.currency,
},
bill_to: Some(bill_to),
Expand Down Expand Up @@ -472,14 +470,14 @@ pub struct OrderInformation {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Amount {
total_amount: String,
total_amount: StringMajorUnit,
currency: api_models::enums::Currency,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AdditionalAmount {
additional_amount: String,
additional_amount: StringMajorUnit,
currency: String,
}

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 {

enums::Connector::Volt => Ok(ConnectorEnum::Old(Box::new(connector::Volt::new()))),
enums::Connector::Wellsfargo => {
Ok(ConnectorEnum::Old(Box::new(&connector::Wellsfargo)))
Ok(ConnectorEnum::Old(Box::new(connector::Wellsfargo::new())))
}

// enums::Connector::Wellsfargopayout => {
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/wellsfargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl utils::Connector for WellsfargoTest {
fn get_data(&self) -> api::ConnectorData {
use router::connector::Wellsfargo;
utils::construct_connector_data_old(
Box::new(&Wellsfargo),
Box::new(Wellsfargo::new()),
types::Connector::Wellsfargo,
api::GetToken::Connector,
None,
Expand Down

0 comments on commit c3b0f7c

Please sign in to comment.