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): add amount conversion framework to payone #4981

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 22 additions & 11 deletions crates/router/src/connector/payone.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod transformers;

use std::fmt::Debug;

use base64::Engine;
#[cfg(feature = "payouts")]
use common_utils::request::RequestContent;
#[cfg(feature = "payouts")]
use common_utils::types::{AmountConvertor, MinorUnit, MinorUnitForConnector};
use error_stack::{report, ResultExt};
use masking::{ExposeInterface, PeekInterface};
use ring::hmac;
Expand All @@ -13,6 +13,8 @@ use router_env::{instrument, tracing};

use self::transformers as payone;
#[cfg(feature = "payouts")]
use crate::connector::utils::convert_amount;
#[cfg(feature = "payouts")]
use crate::get_formatted_date_time;
#[cfg(feature = "payouts")]
use crate::services;
Expand All @@ -37,10 +39,19 @@ use crate::{
},
utils::BytesExt,
};
#[derive(Debug, Clone)]
pub struct Payone;
#[derive(Clone)]
pub struct Payone {
#[cfg(feature = "payouts")]
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
}

impl Payone {
pub fn new() -> &'static Self {
&Self {
#[cfg(feature = "payouts")]
amount_converter: &MinorUnitForConnector,
}
}
pub fn generate_signature(
&self,
auth: payone::PayoneAuthType,
Expand Down Expand Up @@ -102,7 +113,7 @@ where
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let auth = payone::PayoneAuthType::try_from(&req.connector_auth_type)?;
let http_method = Self.get_http_method().to_string();
let http_method = self.get_http_method().to_string();
let content_type = Self::get_content_type(self);
let base_url = self.base_url(connectors);
let url = Self::get_url(self, req, connectors)?;
Expand Down Expand Up @@ -173,7 +184,7 @@ impl ConnectorCommon for Payone {
let errors_list = response.errors.clone().unwrap_or_default();
let option_error_code_message =
connector_utils::get_error_code_error_message_based_on_priority(
Self.clone(),
self.clone(),
errors_list
.into_iter()
.map(|errors| errors.into())
Expand Down Expand Up @@ -291,12 +302,12 @@ impl ConnectorIntegration<api::PoFulfill, types::PayoutsData, types::PayoutsResp
req: &types::PayoutsRouterData<api::PoFulfill>,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = payone::PayoneRouterData::try_from((
&self.get_currency_unit(),
let amount = convert_amount(
self.amount_converter,
req.request.minor_amount,
req.request.destination_currency,
req.request.amount,
req,
))?;
)?;
let connector_router_data = payone::PayoneRouterData::from((amount, req));
let connector_req = payone::PayonePayoutFulfillRequest::try_from(connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down
26 changes: 8 additions & 18 deletions crates/router/src/connector/payone/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use api_models::payouts::PayoutMethodData;
#[cfg(feature = "payouts")]
use cards::CardNumber;
use common_utils::types::MinorUnit;
#[cfg(feature = "payouts")]
use error_stack::ResultExt;
use masking::Secret;
Expand All @@ -24,30 +25,19 @@ use crate::{
utils::OptionExt,
};
#[cfg(not(feature = "payouts"))]
use crate::{
core::errors,
types::{self, api, storage::enums as storage_enums},
};
use crate::{core::errors, types};

pub struct PayoneRouterData<T> {
pub amount: i64,
pub amount: MinorUnit,
pub router_data: T,
}

impl<T> TryFrom<(&api::CurrencyUnit, storage_enums::Currency, i64, T)> for PayoneRouterData<T> {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(_currency_unit, _currency, amount, item): (
&api::CurrencyUnit,
storage_enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
impl<T> From<(MinorUnit, T)> for PayoneRouterData<T> {
fn from((amount, item): (MinorUnit, T)) -> Self {
Self {
amount,
router_data: item,
})
}
}
}

Expand Down Expand Up @@ -109,7 +99,7 @@ pub struct PayonePayoutFulfillRequest {
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AmountOfMoney {
amount: i64,
amount: MinorUnit,
currency_code: String,
}

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 @@ -453,7 +453,9 @@ impl ConnectorData {
enums::Connector::Payme => {
Ok(ConnectorEnum::Old(Box::new(connector::Payme::new())))
}
enums::Connector::Payone => Ok(ConnectorEnum::Old(Box::new(&connector::Payone))),
enums::Connector::Payone => {
Ok(ConnectorEnum::Old(Box::new(connector::Payone::new())))
}
enums::Connector::Payu => Ok(ConnectorEnum::Old(Box::new(&connector::Payu))),
enums::Connector::Placetopay => {
Ok(ConnectorEnum::Old(Box::new(&connector::Placetopay)))
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/payone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl utils::Connector for PayoneTest {
fn get_data(&self) -> types::api::ConnectorData {
use router::connector::Payone;
utils::construct_connector_data_old(
Box::new(&Payone),
Box::new(Payone::new()),
types::Connector::Payone,
types::api::GetToken::Connector,
None,
Expand Down
Loading