From 3578db7640d8eda8f063e11b8bb64452fb987eef Mon Sep 17 00:00:00 2001 From: Seemebadnekai <51400137+SagarDevAchar@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:25:35 +0530 Subject: [PATCH] feat(connector): [Mollie] Currency Unit Conversion (#2671) Co-authored-by: SamraatBansal <55536657+SamraatBansal@users.noreply.github.com> --- crates/router/src/connector/mollie.rs | 20 ++- .../src/connector/mollie/transformers.rs | 123 ++++++++++++------ 2 files changed, 99 insertions(+), 44 deletions(-) diff --git a/crates/router/src/connector/mollie.rs b/crates/router/src/connector/mollie.rs index 38abed9dea6a..337e1b78c9a9 100644 --- a/crates/router/src/connector/mollie.rs +++ b/crates/router/src/connector/mollie.rs @@ -62,6 +62,10 @@ impl ConnectorCommon for Mollie { "mollie" } + fn get_currency_unit(&self) -> api::CurrencyUnit { + api::CurrencyUnit::Base + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.mollie.base_url.as_ref() } @@ -229,7 +233,13 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { - let req_obj = mollie::MolliePaymentsRequest::try_from(req)?; + let router_obj = mollie::MollieRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.amount, + req, + ))?; + let req_obj = mollie::MolliePaymentsRequest::try_from(&router_obj)?; let mollie_req = types::RequestBody::log_and_get_request_body( &req_obj, utils::Encode::::encode_to_string_of_json, @@ -417,7 +427,13 @@ impl ConnectorIntegration, ) -> CustomResult, errors::ConnectorError> { - let req_obj = mollie::MollieRefundRequest::try_from(req)?; + let router_obj = mollie::MollieRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.refund_amount, + req, + ))?; + let req_obj = mollie::MollieRefundRequest::try_from(&router_obj)?; let mollie_req = types::RequestBody::log_and_get_request_body( &req_obj, utils::Encode::::encode_to_string_of_json, diff --git a/crates/router/src/connector/mollie/transformers.rs b/crates/router/src/connector/mollie/transformers.rs index d0036d3c2f55..3c23c9f1d39b 100644 --- a/crates/router/src/connector/mollie/transformers.rs +++ b/crates/router/src/connector/mollie/transformers.rs @@ -19,6 +19,38 @@ use crate::{ type Error = error_stack::Report; +#[derive(Debug, Serialize)] +pub struct MollieRouterData { + pub amount: String, + pub router_data: T, +} + +impl + TryFrom<( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + )> for MollieRouterData +{ + type Error = error_stack::Report; + + fn try_from( + (currency_unit, currency, amount, router_data): ( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + ), + ) -> Result { + let amount = utils::get_amount_as_string(currency_unit, amount, currency)?; + Ok(Self { + amount, + router_data, + }) + } +} + #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct MolliePaymentsRequest { @@ -120,50 +152,55 @@ pub struct MollieBrowserInfo { language: String, } -impl TryFrom<&types::PaymentsAuthorizeRouterData> for MolliePaymentsRequest { +impl TryFrom<&MollieRouterData<&types::PaymentsAuthorizeRouterData>> for MolliePaymentsRequest { type Error = Error; - fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result { + fn try_from( + item: &MollieRouterData<&types::PaymentsAuthorizeRouterData>, + ) -> Result { let amount = Amount { - currency: item.request.currency, - value: utils::to_currency_base_unit(item.request.amount, item.request.currency)?, + currency: item.router_data.request.currency, + value: item.amount.clone(), }; - let description = item.get_description()?; - let redirect_url = item.request.get_return_url()?; - let payment_method_data = match item.request.capture_method.unwrap_or_default() { - enums::CaptureMethod::Automatic => match &item.request.payment_method_data { - api_models::payments::PaymentMethodData::Card(_) => { - let pm_token = item.get_payment_method_token()?; - Ok(PaymentMethodData::CreditCard(Box::new( - CreditCardMethodData { - billing_address: get_billing_details(item)?, - shipping_address: get_shipping_details(item)?, - card_token: Some(Secret::new(match pm_token { - types::PaymentMethodToken::Token(token) => token, - types::PaymentMethodToken::ApplePayDecrypt(_) => { - Err(errors::ConnectorError::InvalidWalletToken)? - } - })), - }, - ))) - } - api_models::payments::PaymentMethodData::BankRedirect(ref redirect_data) => { - PaymentMethodData::try_from(redirect_data) + let description = item.router_data.get_description()?; + let redirect_url = item.router_data.request.get_return_url()?; + let payment_method_data = match item.router_data.request.capture_method.unwrap_or_default() + { + enums::CaptureMethod::Automatic => { + match &item.router_data.request.payment_method_data { + api_models::payments::PaymentMethodData::Card(_) => { + let pm_token = item.router_data.get_payment_method_token()?; + Ok(PaymentMethodData::CreditCard(Box::new( + CreditCardMethodData { + billing_address: get_billing_details(item.router_data)?, + shipping_address: get_shipping_details(item.router_data)?, + card_token: Some(Secret::new(match pm_token { + types::PaymentMethodToken::Token(token) => token, + types::PaymentMethodToken::ApplePayDecrypt(_) => { + Err(errors::ConnectorError::InvalidWalletToken)? + } + })), + }, + ))) + } + api_models::payments::PaymentMethodData::BankRedirect(ref redirect_data) => { + PaymentMethodData::try_from(redirect_data) + } + api_models::payments::PaymentMethodData::Wallet(ref wallet_data) => { + get_payment_method_for_wallet(item.router_data, wallet_data) + } + api_models::payments::PaymentMethodData::BankDebit(ref directdebit_data) => { + PaymentMethodData::try_from(directdebit_data) + } + _ => Err(errors::ConnectorError::NotImplemented( + "Payment Method".to_string(), + )) + .into_report(), } - api_models::payments::PaymentMethodData::Wallet(ref wallet_data) => { - get_payment_method_for_wallet(item, wallet_data) - } - api_models::payments::PaymentMethodData::BankDebit(ref directdebit_data) => { - PaymentMethodData::try_from(directdebit_data) - } - _ => Err(errors::ConnectorError::NotImplemented( - "Payment Method".to_string(), - )) - .into_report(), - }, + } _ => Err(errors::ConnectorError::FlowNotSupported { flow: format!( "{} capture", - item.request.capture_method.unwrap_or_default() + item.router_data.request.capture_method.unwrap_or_default() ), connector: "Mollie".to_string(), }) @@ -526,16 +563,18 @@ pub struct MollieRefundRequest { description: Option, } -impl TryFrom<&types::RefundsRouterData> for MollieRefundRequest { +impl TryFrom<&MollieRouterData<&types::RefundsRouterData>> for MollieRefundRequest { type Error = Error; - fn try_from(item: &types::RefundsRouterData) -> Result { + fn try_from( + item: &MollieRouterData<&types::RefundsRouterData>, + ) -> Result { let amount = Amount { - currency: item.request.currency, - value: utils::to_currency_base_unit(item.request.refund_amount, item.request.currency)?, + currency: item.router_data.request.currency, + value: item.amount.clone(), }; Ok(Self { amount, - description: item.request.reason.to_owned(), + description: item.router_data.request.reason.to_owned(), }) } }