Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(connector): [Stax] Currency Unit Conversion #2711

Merged
merged 35 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b96ec3b
refactor(connector): [Rapyd]added the get_currency_unit modification
shivanshbhatnagar18 Oct 22, 2023
514c18c
Merge branch 'main' of https://github.com/shivansh-bhatnagar18/hypers…
shivanshbhatnagar18 Oct 22, 2023
ea100cf
Some Minor Fixes on Formatting and Typos
shivanshbhatnagar18 Oct 23, 2023
ccc2a89
Merge branch 'main' into main
shivansh-bhatnagar18 Oct 23, 2023
5c805ab
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 25, 2023
e77ac09
Fixed some errors
shivanshbhatnagar18 Oct 25, 2023
916e988
Merge branch 'main' of https://github.com/shivansh-bhatnagar18/hypers…
shivanshbhatnagar18 Oct 25, 2023
df2df61
replaced items.amount after changes in rapyd.rs
shivanshbhatnagar18 Oct 25, 2023
4f2d4ae
Done with the Capture Tryfrom changes
shivanshbhatnagar18 Oct 25, 2023
61453c4
Some minor fixes
shivanshbhatnagar18 Oct 25, 2023
4dd1ce6
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 25, 2023
622094d
Changed the Rapyd Currency Handling Format
shivanshbhatnagar18 Oct 25, 2023
0a6dd88
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 26, 2023
de692d2
Minor Fixes
shivanshbhatnagar18 Oct 26, 2023
0836b06
Merge branch 'main' of https://github.com/shivansh-bhatnagar18/hypers…
shivanshbhatnagar18 Oct 26, 2023
495af26
Fixes
shivanshbhatnagar18 Oct 26, 2023
1bacffd
Fixes the errors
shivanshbhatnagar18 Oct 26, 2023
c147eff
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 27, 2023
18b741c
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 27, 2023
0cf9ad6
Added Currency Conversion Unit for Stax
shivanshbhatnagar18 Oct 27, 2023
5252a54
Minor changes
shivanshbhatnagar18 Oct 27, 2023
fdb3e0a
Minor Fixes
shivanshbhatnagar18 Oct 27, 2023
299be23
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 29, 2023
83b8d87
Some typos fixed
shivanshbhatnagar18 Oct 29, 2023
169f906
Merge branch 'main' of https://github.com/shivansh-bhatnagar18/hypers…
shivanshbhatnagar18 Oct 29, 2023
77466e2
converted amount to f64
shivanshbhatnagar18 Oct 29, 2023
f3ea8b0
Merge branch 'main' into main
deepanshu-iiitu Oct 31, 2023
c533c86
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Oct 31, 2023
16de99d
Corrected the errors
shivansh-bhatnagar18 Oct 31, 2023
51e6aab
Minor fixes
shivansh-bhatnagar18 Oct 31, 2023
9d0eee2
Formatting done
shivansh-bhatnagar18 Oct 31, 2023
93c6912
Merge branch 'juspay:main' into main
shivansh-bhatnagar18 Nov 1, 2023
7b12db3
Fixed some errors
shivansh-bhatnagar18 Nov 1, 2023
392f492
Merge branch 'main' of https://github.com/shivansh-bhatnagar18/hypers…
shivansh-bhatnagar18 Nov 1, 2023
7412ea1
Merge branch 'main' into main
shivansh-bhatnagar18 Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions crates/router/src/connector/stax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -347,7 +351,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, 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,
Expand Down Expand Up @@ -503,7 +513,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
&self,
req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<types::RequestBody>, 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::<stax::StaxCaptureRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -657,7 +673,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, 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::<stax::StaxRefundRequest>::encode_to_string_of_json,
Expand Down
79 changes: 53 additions & 26 deletions crates/router/src/connector/stax/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ use crate::{
types::{self, api, storage::enums},
};

#[derive(Debug, Serialize)]
pub struct StaxRouterData<T> {
pub amount: f64,
pub router_data: T,
}

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for StaxRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(currency_unit, currency, amount, item): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
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,
Expand All @@ -26,21 +57,23 @@ pub struct StaxPaymentsRequest {
idempotency_id: Option<String>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
impl TryFrom<&StaxRouterData<&types::PaymentsAuthorizeRouterData>> for StaxPaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
if item.request.currency != enums::Currency::USD {
fn try_from(
item: &StaxRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
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,
Expand All @@ -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,
Expand All @@ -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(_)
Expand Down Expand Up @@ -347,13 +380,12 @@ pub struct StaxCaptureRequest {
total: Option<f64>,
}

impl TryFrom<&types::PaymentsCaptureRouterData> for StaxCaptureRequest {
impl TryFrom<&StaxRouterData<&types::PaymentsCaptureRouterData>> for StaxCaptureRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
let total = utils::to_currency_base_unit_asf64(
item.request.amount_to_capture,
item.request.currency,
)?;
fn try_from(
item: &StaxRouterData<&types::PaymentsCaptureRouterData>,
) -> Result<Self, Self::Error> {
let total = item.amount;
Ok(Self { total: Some(total) })
}
}
Expand All @@ -365,15 +397,10 @@ pub struct StaxRefundRequest {
pub total: f64,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for StaxRefundRequest {
impl<F> TryFrom<&StaxRouterData<&types::RefundsRouterData<F>>> for StaxRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
Ok(Self {
total: utils::to_currency_base_unit_asf64(
item.request.refund_amount,
item.request.currency,
)?,
})
fn try_from(item: &StaxRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
Ok(Self { total: item.amount })
}
}

Expand Down
Loading