From 278292322c7c06f4239dd73861469e436bd941fa Mon Sep 17 00:00:00 2001 From: Shivansh Bhatnagar Date: Sun, 5 Nov 2023 17:15:56 +0530 Subject: [PATCH] refactor(connector): [Stax] Currency Unit Conversion (#2711) Co-authored-by: Shivansh Bhatnagar Co-authored-by: DEEPANSHU BANSAL <41580413+deepanshu-iiitu@users.noreply.github.com> --- crates/router/src/connector/stax.rs | 28 ++++++- .../router/src/connector/stax/transformers.rs | 79 +++++++++++++------ 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/crates/router/src/connector/stax.rs b/crates/router/src/connector/stax.rs index 82a4c7ff3233..7f5fde719389 100644 --- a/crates/router/src/connector/stax.rs +++ b/crates/router/src/connector/stax.rs @@ -70,6 +70,10 @@ impl ConnectorCommon for Stax { "stax" } + fn get_currency_unit(&self) -> api::CurrencyUnit { + api::CurrencyUnit::Base + } + fn common_get_content_type(&self) -> &'static str { "application/json" } @@ -347,7 +351,13 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { - let req_obj = stax::StaxPaymentsRequest::try_from(req)?; + let connector_router_data = stax::StaxRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.amount, + req, + ))?; + let req_obj = stax::StaxPaymentsRequest::try_from(&connector_router_data)?; let stax_req = types::RequestBody::log_and_get_request_body( &req_obj, @@ -503,7 +513,13 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { - let connector_req = stax::StaxCaptureRequest::try_from(req)?; + let connector_router_data = stax::StaxRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.amount_to_capture, + req, + ))?; + let connector_req = stax::StaxCaptureRequest::try_from(&connector_router_data)?; let stax_req = types::RequestBody::log_and_get_request_body( &connector_req, utils::Encode::::encode_to_string_of_json, @@ -657,7 +673,13 @@ impl ConnectorIntegration, ) -> CustomResult, errors::ConnectorError> { - let req_obj = stax::StaxRefundRequest::try_from(req)?; + let connector_router_data = stax::StaxRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.refund_amount, + req, + ))?; + let req_obj = stax::StaxRefundRequest::try_from(&connector_router_data)?; let stax_req = types::RequestBody::log_and_get_request_body( &req_obj, utils::Encode::::encode_to_string_of_json, diff --git a/crates/router/src/connector/stax/transformers.rs b/crates/router/src/connector/stax/transformers.rs index 4ee28be19375..f2aae442ddd6 100644 --- a/crates/router/src/connector/stax/transformers.rs +++ b/crates/router/src/connector/stax/transformers.rs @@ -11,6 +11,37 @@ use crate::{ types::{self, api, storage::enums}, }; +#[derive(Debug, Serialize)] +pub struct StaxRouterData { + pub amount: f64, + pub router_data: T, +} + +impl + TryFrom<( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + )> for StaxRouterData +{ + type Error = error_stack::Report; + fn try_from( + (currency_unit, currency, amount, item): ( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + ), + ) -> Result { + let amount = utils::get_amount_as_f64(currency_unit, amount, currency)?; + Ok(Self { + amount, + router_data: item, + }) + } +} + #[derive(Debug, Serialize)] pub struct StaxPaymentsRequestMetaData { tax: i64, @@ -26,21 +57,23 @@ pub struct StaxPaymentsRequest { idempotency_id: Option, } -impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest { +impl TryFrom<&StaxRouterData<&types::PaymentsAuthorizeRouterData>> for StaxPaymentsRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result { - if item.request.currency != enums::Currency::USD { + fn try_from( + item: &StaxRouterData<&types::PaymentsAuthorizeRouterData>, + ) -> Result { + if item.router_data.request.currency != enums::Currency::USD { Err(errors::ConnectorError::NotSupported { - message: item.request.currency.to_string(), + message: item.router_data.request.currency.to_string(), connector: "Stax", })? } - let total = utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?; + let total = item.amount; - match item.request.payment_method_data.clone() { + match item.router_data.request.payment_method_data.clone() { api::PaymentMethodData::Card(_) => { - let pm_token = item.get_payment_method_token()?; - let pre_auth = !item.request.is_auto_capture()?; + let pm_token = item.router_data.get_payment_method_token()?; + let pre_auth = !item.router_data.request.is_auto_capture()?; Ok(Self { meta: StaxPaymentsRequestMetaData { tax: 0 }, total, @@ -52,14 +85,14 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest { Err(errors::ConnectorError::InvalidWalletToken)? } }), - idempotency_id: Some(item.connector_request_reference_id.clone()), + idempotency_id: Some(item.router_data.connector_request_reference_id.clone()), }) } api::PaymentMethodData::BankDebit( api_models::payments::BankDebitData::AchBankDebit { .. }, ) => { - let pm_token = item.get_payment_method_token()?; - let pre_auth = !item.request.is_auto_capture()?; + let pm_token = item.router_data.get_payment_method_token()?; + let pre_auth = !item.router_data.request.is_auto_capture()?; Ok(Self { meta: StaxPaymentsRequestMetaData { tax: 0 }, total, @@ -71,7 +104,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest { Err(errors::ConnectorError::InvalidWalletToken)? } }), - idempotency_id: Some(item.connector_request_reference_id.clone()), + idempotency_id: Some(item.router_data.connector_request_reference_id.clone()), }) } api::PaymentMethodData::BankDebit(_) @@ -347,13 +380,12 @@ pub struct StaxCaptureRequest { total: Option, } -impl TryFrom<&types::PaymentsCaptureRouterData> for StaxCaptureRequest { +impl TryFrom<&StaxRouterData<&types::PaymentsCaptureRouterData>> for StaxCaptureRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsCaptureRouterData) -> Result { - let total = utils::to_currency_base_unit_asf64( - item.request.amount_to_capture, - item.request.currency, - )?; + fn try_from( + item: &StaxRouterData<&types::PaymentsCaptureRouterData>, + ) -> Result { + let total = item.amount; Ok(Self { total: Some(total) }) } } @@ -365,15 +397,10 @@ pub struct StaxRefundRequest { pub total: f64, } -impl TryFrom<&types::RefundsRouterData> for StaxRefundRequest { +impl TryFrom<&StaxRouterData<&types::RefundsRouterData>> for StaxRefundRequest { type Error = error_stack::Report; - fn try_from(item: &types::RefundsRouterData) -> Result { - Ok(Self { - total: utils::to_currency_base_unit_asf64( - item.request.refund_amount, - item.request.currency, - )?, - }) + fn try_from(item: &StaxRouterData<&types::RefundsRouterData>) -> Result { + Ok(Self { total: item.amount }) } }