mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 13:30:39 +08:00
refactor(connector): add amount conversion framework to Wellsfargo (#6298)
This commit is contained in:
@ -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};
|
||||
@ -12,6 +13,7 @@ use time::OffsetDateTime;
|
||||
use transformers as wellsfargo;
|
||||
use url::Url;
|
||||
|
||||
use super::utils::convert_amount;
|
||||
use crate::{
|
||||
configs::settings,
|
||||
connector::{
|
||||
@ -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)
|
||||
@ -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)))
|
||||
@ -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)))
|
||||
@ -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)))
|
||||
@ -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)))
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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),
|
||||
@ -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,
|
||||
}
|
||||
|
||||
|
||||
@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user