mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
refactor(connector): add amount conversion framework to placetopay (#4988)
This commit is contained in:
@ -1,8 +1,9 @@
|
|||||||
pub mod transformers;
|
pub mod transformers;
|
||||||
|
|
||||||
use std::fmt::Debug;
|
use common_utils::{
|
||||||
|
request::RequestContent,
|
||||||
use common_utils::request::RequestContent;
|
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
|
||||||
|
};
|
||||||
use error_stack::{report, ResultExt};
|
use error_stack::{report, ResultExt};
|
||||||
use transformers as placetopay;
|
use transformers as placetopay;
|
||||||
|
|
||||||
@ -25,8 +26,18 @@ use crate::{
|
|||||||
utils::BytesExt,
|
utils::BytesExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Placetopay;
|
pub struct Placetopay {
|
||||||
|
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Placetopay {
|
||||||
|
pub fn new() -> &'static Self {
|
||||||
|
&Self {
|
||||||
|
amount_converter: &MinorUnitForConnector,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl api::Payment for Placetopay {}
|
impl api::Payment for Placetopay {}
|
||||||
impl api::PaymentSession for Placetopay {}
|
impl api::PaymentSession for Placetopay {}
|
||||||
@ -190,12 +201,12 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
|||||||
req: &types::PaymentsAuthorizeRouterData,
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
_connectors: &settings::Connectors,
|
_connectors: &settings::Connectors,
|
||||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||||
let connector_router_data = placetopay::PlacetopayRouterData::try_from((
|
let amount = connector_utils::convert_amount(
|
||||||
&self.get_currency_unit(),
|
self.amount_converter,
|
||||||
|
req.request.minor_amount,
|
||||||
req.request.currency,
|
req.request.currency,
|
||||||
req.request.amount,
|
)?;
|
||||||
req,
|
let connector_router_data = placetopay::PlacetopayRouterData::from((amount, req));
|
||||||
))?;
|
|
||||||
let req_obj = placetopay::PlacetopayPaymentsRequest::try_from(&connector_router_data)?;
|
let req_obj = placetopay::PlacetopayPaymentsRequest::try_from(&connector_router_data)?;
|
||||||
Ok(RequestContent::Json(Box::new(req_obj)))
|
Ok(RequestContent::Json(Box::new(req_obj)))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use common_utils::date_time;
|
use common_utils::{date_time, types::MinorUnit};
|
||||||
use diesel_models::enums;
|
use diesel_models::enums;
|
||||||
use error_stack::ResultExt;
|
use error_stack::ResultExt;
|
||||||
use masking::{PeekInterface, Secret};
|
use masking::{PeekInterface, Secret};
|
||||||
@ -16,26 +16,16 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct PlacetopayRouterData<T> {
|
pub struct PlacetopayRouterData<T> {
|
||||||
pub amount: i64,
|
pub amount: MinorUnit,
|
||||||
pub router_data: T,
|
pub router_data: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> TryFrom<(&api::CurrencyUnit, types::storage::enums::Currency, i64, T)>
|
impl<T> From<(MinorUnit, T)> for PlacetopayRouterData<T> {
|
||||||
for PlacetopayRouterData<T>
|
fn from((amount, item): (MinorUnit, T)) -> Self {
|
||||||
{
|
Self {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
|
||||||
fn try_from(
|
|
||||||
(_currency_unit, _currency, amount, item): (
|
|
||||||
&api::CurrencyUnit,
|
|
||||||
types::storage::enums::Currency,
|
|
||||||
i64,
|
|
||||||
T,
|
|
||||||
),
|
|
||||||
) -> Result<Self, Self::Error> {
|
|
||||||
Ok(Self {
|
|
||||||
amount,
|
amount,
|
||||||
router_data: item,
|
router_data: item,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +73,7 @@ pub struct PlacetopayPayment {
|
|||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PlacetopayAmount {
|
pub struct PlacetopayAmount {
|
||||||
currency: storage_enums::Currency,
|
currency: storage_enums::Currency,
|
||||||
total: i64,
|
total: MinorUnit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -302,7 +292,7 @@ pub struct PlacetopayRefundRequest {
|
|||||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for PlacetopayRefundRequest {
|
impl<F> TryFrom<&types::RefundsRouterData<F>> for PlacetopayRefundRequest {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
||||||
if item.request.refund_amount == item.request.payment_amount {
|
if item.request.minor_refund_amount == item.request.minor_payment_amount {
|
||||||
let auth = PlacetopayAuth::try_from(&item.connector_auth_type)?;
|
let auth = PlacetopayAuth::try_from(&item.connector_auth_type)?;
|
||||||
|
|
||||||
let internal_reference = item
|
let internal_reference = item
|
||||||
|
|||||||
@ -408,7 +408,7 @@ impl ConnectorData {
|
|||||||
}
|
}
|
||||||
enums::Connector::Payu => Ok(ConnectorEnum::Old(Box::new(&connector::Payu))),
|
enums::Connector::Payu => Ok(ConnectorEnum::Old(Box::new(&connector::Payu))),
|
||||||
enums::Connector::Placetopay => {
|
enums::Connector::Placetopay => {
|
||||||
Ok(ConnectorEnum::Old(Box::new(&connector::Placetopay)))
|
Ok(ConnectorEnum::Old(Box::new(connector::Placetopay::new())))
|
||||||
}
|
}
|
||||||
enums::Connector::Powertranz => {
|
enums::Connector::Powertranz => {
|
||||||
Ok(ConnectorEnum::Old(Box::new(&connector::Powertranz)))
|
Ok(ConnectorEnum::Old(Box::new(&connector::Powertranz)))
|
||||||
|
|||||||
@ -11,7 +11,7 @@ impl utils::Connector for PlacetopayTest {
|
|||||||
fn get_data(&self) -> types::api::ConnectorData {
|
fn get_data(&self) -> types::api::ConnectorData {
|
||||||
use router::connector::Placetopay;
|
use router::connector::Placetopay;
|
||||||
utils::construct_connector_data_old(
|
utils::construct_connector_data_old(
|
||||||
Box::new(&Placetopay),
|
Box::new(Placetopay::new()),
|
||||||
types::Connector::Placetopay,
|
types::Connector::Placetopay,
|
||||||
types::api::GetToken::Connector,
|
types::api::GetToken::Connector,
|
||||||
None,
|
None,
|
||||||
|
|||||||
Reference in New Issue
Block a user