Skip to content

Commit

Permalink
refactor(connector): added amount conversion framework for Mollie (#6280
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Sidharth-Singh10 authored Oct 18, 2024
1 parent fba4a02 commit 451376e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
41 changes: 26 additions & 15 deletions crates/hyperswitch_connectors/src/connectors/mollie.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
pub mod transformers;

use std::fmt::Debug;

use common_enums::enums;
use common_utils::{
errors::CustomResult,
ext_traits::BytesExt,
request::{Method, Request, RequestBuilder, RequestContent},
types::{AmountConvertor, StringMajorUnit, StringMajorUnitForConnector},
};
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::{
Expand Down Expand Up @@ -43,10 +42,20 @@ use masking::{Mask, PeekInterface};
use transformers as mollie;

// use self::mollie::{webhook_headers, VoltWebhookBodyEventType};
use crate::{constants::headers, types::ResponseRouterData};
use crate::{constants::headers, types::ResponseRouterData, utils::convert_amount};

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

#[derive(Debug, Clone)]
pub struct Mollie;
impl Mollie {
pub fn new() -> &'static Self {
&Self {
amount_converter: &StringMajorUnitForConnector,
}
}
}

impl api::Payment for Mollie {}
impl api::PaymentSession for Mollie {}
Expand Down Expand Up @@ -254,12 +263,13 @@ impl ConnectorIntegration<Authorize, PaymentsAuthorizeData, PaymentsResponseData
req: &PaymentsAuthorizeRouterData,
_connectors: &Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let router_obj = mollie::MollieRouterData::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 router_obj = mollie::MollieRouterData::from((amount, req));
let connector_req = mollie::MolliePaymentsRequest::try_from(&router_obj)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down Expand Up @@ -448,12 +458,13 @@ impl ConnectorIntegration<Execute, RefundsData, RefundsResponseData> for Mollie
req: &RefundsRouterData<Execute>,
_connectors: &Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let router_obj = mollie::MollieRouterData::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 router_obj = mollie::MollieRouterData::from((amount, req));
let connector_req = mollie::MollieRefundRequest::try_from(&router_obj)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use cards::CardNumber;
use common_enums::enums;
use common_utils::{pii::Email, request::Method};
use common_utils::{pii::Email, request::Method, types::StringMajorUnit};
use hyperswitch_domain_models::{
payment_method_data::{BankDebitData, BankRedirectData, PaymentMethodData, WalletData},
router_data::{ConnectorAuthType, PaymentMethodToken, RouterData},
router_request_types::ResponseId,
router_response_types::{PaymentsResponseData, RedirectForm, RefundsResponseData},
types,
};
use hyperswitch_interfaces::{api, errors};
use hyperswitch_interfaces::errors;
use masking::{ExposeInterface, Secret};
use serde::{Deserialize, Serialize};
use url::Url;
Expand All @@ -17,7 +17,7 @@ use crate::{
types::{RefundsResponseRouterData, ResponseRouterData},
unimplemented_payment_method,
utils::{
self, AddressDetailsData, BrowserInformationData, CardData as CardDataUtil,
AddressDetailsData, BrowserInformationData, CardData as CardDataUtil,
PaymentMethodTokenizationRequestData, PaymentsAuthorizeRequestData, RouterData as _,
},
};
Expand All @@ -26,26 +26,16 @@ type Error = error_stack::Report<errors::ConnectorError>;

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

impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for MollieRouterData<T> {
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(
(currency_unit, currency, amount, router_data): (
&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 MollieRouterData<T> {
fn from((amount, router_data): (StringMajorUnit, T)) -> Self {
Self {
amount,
router_data,
})
}
}
}

Expand All @@ -68,7 +58,7 @@ pub struct MolliePaymentsRequest {
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Amount {
currency: enums::Currency,
value: String,
value: StringMajorUnit,
}

#[derive(Debug, Serialize)]
Expand Down
4 changes: 3 additions & 1 deletion crates/router/src/types/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ impl ConnectorData {
Ok(ConnectorEnum::Old(Box::new(connector::Itaubank::new())))
}
enums::Connector::Klarna => Ok(ConnectorEnum::Old(Box::new(&connector::Klarna))),
enums::Connector::Mollie => Ok(ConnectorEnum::Old(Box::new(&connector::Mollie))),
enums::Connector::Mollie => {
Ok(ConnectorEnum::Old(Box::new(connector::Mollie::new())))
}
enums::Connector::Nexixpay => {
Ok(ConnectorEnum::Old(Box::new(connector::Nexixpay::new())))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/mollie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl utils::Connector for MollieTest {
fn get_data(&self) -> types::api::ConnectorData {
use router::connector::Mollie;
utils::construct_connector_data_old(
Box::new(&Mollie),
Box::new(Mollie::new()),
types::Connector::Mollie,
types::api::GetToken::Connector,
None,
Expand Down

0 comments on commit 451376e

Please sign in to comment.