mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
refactor(connector): add amount conversion framework to placetopay (#4988)
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
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 transformers as placetopay;
|
||||
|
||||
@ -25,8 +26,18 @@ use crate::{
|
||||
utils::BytesExt,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Placetopay;
|
||||
#[derive(Clone)]
|
||||
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::PaymentSession for Placetopay {}
|
||||
@ -190,12 +201,12 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = placetopay::PlacetopayRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = connector_utils::convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount,
|
||||
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)?;
|
||||
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 error_stack::ResultExt;
|
||||
use masking::{PeekInterface, Secret};
|
||||
@ -16,26 +16,16 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct PlacetopayRouterData<T> {
|
||||
pub amount: i64,
|
||||
pub amount: MinorUnit,
|
||||
pub router_data: T,
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(&api::CurrencyUnit, types::storage::enums::Currency, i64, T)>
|
||||
for PlacetopayRouterData<T>
|
||||
{
|
||||
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 {
|
||||
impl<T> From<(MinorUnit, T)> for PlacetopayRouterData<T> {
|
||||
fn from((amount, item): (MinorUnit, T)) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
router_data: item,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,7 +73,7 @@ pub struct PlacetopayPayment {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PlacetopayAmount {
|
||||
currency: storage_enums::Currency,
|
||||
total: i64,
|
||||
total: MinorUnit,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@ -302,7 +292,7 @@ pub struct PlacetopayRefundRequest {
|
||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for PlacetopayRefundRequest {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
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 internal_reference = item
|
||||
|
||||
@ -408,7 +408,7 @@ impl ConnectorData {
|
||||
}
|
||||
enums::Connector::Payu => Ok(ConnectorEnum::Old(Box::new(&connector::Payu))),
|
||||
enums::Connector::Placetopay => {
|
||||
Ok(ConnectorEnum::Old(Box::new(&connector::Placetopay)))
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Placetopay::new())))
|
||||
}
|
||||
enums::Connector::Powertranz => {
|
||||
Ok(ConnectorEnum::Old(Box::new(&connector::Powertranz)))
|
||||
|
||||
Reference in New Issue
Block a user