Skip to content

Commit 2782923

Browse files
shivansh-bhatnagar18shivanshbhatnagar18deepanshu-iiitu
authored
refactor(connector): [Stax] Currency Unit Conversion (#2711)
Co-authored-by: Shivansh Bhatnagar <[email protected]> Co-authored-by: DEEPANSHU BANSAL <[email protected]>
1 parent 1b45a30 commit 2782923

File tree

2 files changed

+78
-29
lines changed

2 files changed

+78
-29
lines changed

crates/router/src/connector/stax.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ impl ConnectorCommon for Stax {
7070
"stax"
7171
}
7272

73+
fn get_currency_unit(&self) -> api::CurrencyUnit {
74+
api::CurrencyUnit::Base
75+
}
76+
7377
fn common_get_content_type(&self) -> &'static str {
7478
"application/json"
7579
}
@@ -347,7 +351,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
347351
&self,
348352
req: &types::PaymentsAuthorizeRouterData,
349353
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
350-
let req_obj = stax::StaxPaymentsRequest::try_from(req)?;
354+
let connector_router_data = stax::StaxRouterData::try_from((
355+
&self.get_currency_unit(),
356+
req.request.currency,
357+
req.request.amount,
358+
req,
359+
))?;
360+
let req_obj = stax::StaxPaymentsRequest::try_from(&connector_router_data)?;
351361

352362
let stax_req = types::RequestBody::log_and_get_request_body(
353363
&req_obj,
@@ -503,7 +513,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
503513
&self,
504514
req: &types::PaymentsCaptureRouterData,
505515
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
506-
let connector_req = stax::StaxCaptureRequest::try_from(req)?;
516+
let connector_router_data = stax::StaxRouterData::try_from((
517+
&self.get_currency_unit(),
518+
req.request.currency,
519+
req.request.amount_to_capture,
520+
req,
521+
))?;
522+
let connector_req = stax::StaxCaptureRequest::try_from(&connector_router_data)?;
507523
let stax_req = types::RequestBody::log_and_get_request_body(
508524
&connector_req,
509525
utils::Encode::<stax::StaxCaptureRequest>::encode_to_string_of_json,
@@ -657,7 +673,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
657673
&self,
658674
req: &types::RefundsRouterData<api::Execute>,
659675
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
660-
let req_obj = stax::StaxRefundRequest::try_from(req)?;
676+
let connector_router_data = stax::StaxRouterData::try_from((
677+
&self.get_currency_unit(),
678+
req.request.currency,
679+
req.request.refund_amount,
680+
req,
681+
))?;
682+
let req_obj = stax::StaxRefundRequest::try_from(&connector_router_data)?;
661683
let stax_req = types::RequestBody::log_and_get_request_body(
662684
&req_obj,
663685
utils::Encode::<stax::StaxRefundRequest>::encode_to_string_of_json,

crates/router/src/connector/stax/transformers.rs

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ use crate::{
1111
types::{self, api, storage::enums},
1212
};
1313

14+
#[derive(Debug, Serialize)]
15+
pub struct StaxRouterData<T> {
16+
pub amount: f64,
17+
pub router_data: T,
18+
}
19+
20+
impl<T>
21+
TryFrom<(
22+
&types::api::CurrencyUnit,
23+
types::storage::enums::Currency,
24+
i64,
25+
T,
26+
)> for StaxRouterData<T>
27+
{
28+
type Error = error_stack::Report<errors::ConnectorError>;
29+
fn try_from(
30+
(currency_unit, currency, amount, item): (
31+
&types::api::CurrencyUnit,
32+
types::storage::enums::Currency,
33+
i64,
34+
T,
35+
),
36+
) -> Result<Self, Self::Error> {
37+
let amount = utils::get_amount_as_f64(currency_unit, amount, currency)?;
38+
Ok(Self {
39+
amount,
40+
router_data: item,
41+
})
42+
}
43+
}
44+
1445
#[derive(Debug, Serialize)]
1546
pub struct StaxPaymentsRequestMetaData {
1647
tax: i64,
@@ -26,21 +57,23 @@ pub struct StaxPaymentsRequest {
2657
idempotency_id: Option<String>,
2758
}
2859

29-
impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
60+
impl TryFrom<&StaxRouterData<&types::PaymentsAuthorizeRouterData>> for StaxPaymentsRequest {
3061
type Error = error_stack::Report<errors::ConnectorError>;
31-
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
32-
if item.request.currency != enums::Currency::USD {
62+
fn try_from(
63+
item: &StaxRouterData<&types::PaymentsAuthorizeRouterData>,
64+
) -> Result<Self, Self::Error> {
65+
if item.router_data.request.currency != enums::Currency::USD {
3366
Err(errors::ConnectorError::NotSupported {
34-
message: item.request.currency.to_string(),
67+
message: item.router_data.request.currency.to_string(),
3568
connector: "Stax",
3669
})?
3770
}
38-
let total = utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?;
71+
let total = item.amount;
3972

40-
match item.request.payment_method_data.clone() {
73+
match item.router_data.request.payment_method_data.clone() {
4174
api::PaymentMethodData::Card(_) => {
42-
let pm_token = item.get_payment_method_token()?;
43-
let pre_auth = !item.request.is_auto_capture()?;
75+
let pm_token = item.router_data.get_payment_method_token()?;
76+
let pre_auth = !item.router_data.request.is_auto_capture()?;
4477
Ok(Self {
4578
meta: StaxPaymentsRequestMetaData { tax: 0 },
4679
total,
@@ -52,14 +85,14 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
5285
Err(errors::ConnectorError::InvalidWalletToken)?
5386
}
5487
}),
55-
idempotency_id: Some(item.connector_request_reference_id.clone()),
88+
idempotency_id: Some(item.router_data.connector_request_reference_id.clone()),
5689
})
5790
}
5891
api::PaymentMethodData::BankDebit(
5992
api_models::payments::BankDebitData::AchBankDebit { .. },
6093
) => {
61-
let pm_token = item.get_payment_method_token()?;
62-
let pre_auth = !item.request.is_auto_capture()?;
94+
let pm_token = item.router_data.get_payment_method_token()?;
95+
let pre_auth = !item.router_data.request.is_auto_capture()?;
6396
Ok(Self {
6497
meta: StaxPaymentsRequestMetaData { tax: 0 },
6598
total,
@@ -71,7 +104,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
71104
Err(errors::ConnectorError::InvalidWalletToken)?
72105
}
73106
}),
74-
idempotency_id: Some(item.connector_request_reference_id.clone()),
107+
idempotency_id: Some(item.router_data.connector_request_reference_id.clone()),
75108
})
76109
}
77110
api::PaymentMethodData::BankDebit(_)
@@ -347,13 +380,12 @@ pub struct StaxCaptureRequest {
347380
total: Option<f64>,
348381
}
349382

350-
impl TryFrom<&types::PaymentsCaptureRouterData> for StaxCaptureRequest {
383+
impl TryFrom<&StaxRouterData<&types::PaymentsCaptureRouterData>> for StaxCaptureRequest {
351384
type Error = error_stack::Report<errors::ConnectorError>;
352-
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
353-
let total = utils::to_currency_base_unit_asf64(
354-
item.request.amount_to_capture,
355-
item.request.currency,
356-
)?;
385+
fn try_from(
386+
item: &StaxRouterData<&types::PaymentsCaptureRouterData>,
387+
) -> Result<Self, Self::Error> {
388+
let total = item.amount;
357389
Ok(Self { total: Some(total) })
358390
}
359391
}
@@ -365,15 +397,10 @@ pub struct StaxRefundRequest {
365397
pub total: f64,
366398
}
367399

368-
impl<F> TryFrom<&types::RefundsRouterData<F>> for StaxRefundRequest {
400+
impl<F> TryFrom<&StaxRouterData<&types::RefundsRouterData<F>>> for StaxRefundRequest {
369401
type Error = error_stack::Report<errors::ConnectorError>;
370-
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
371-
Ok(Self {
372-
total: utils::to_currency_base_unit_asf64(
373-
item.request.refund_amount,
374-
item.request.currency,
375-
)?,
376-
})
402+
fn try_from(item: &StaxRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
403+
Ok(Self { total: item.amount })
377404
}
378405
}
379406

0 commit comments

Comments
 (0)