mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
refactor(connector): add amount conversion framework to Globepay (#5470)
This commit is contained in:
@ -1,10 +1,9 @@
|
|||||||
pub mod transformers;
|
pub mod transformers;
|
||||||
|
|
||||||
use std::fmt::Debug;
|
|
||||||
|
|
||||||
use common_utils::{
|
use common_utils::{
|
||||||
crypto::{self, GenerateDigest},
|
crypto::{self, GenerateDigest},
|
||||||
request::RequestContent,
|
request::RequestContent,
|
||||||
|
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
|
||||||
};
|
};
|
||||||
use diesel_models::enums;
|
use diesel_models::enums;
|
||||||
use error_stack::{report, ResultExt};
|
use error_stack::{report, ResultExt};
|
||||||
@ -16,6 +15,7 @@ use transformers as globepay;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
configs::settings,
|
configs::settings,
|
||||||
|
connector::utils::convert_amount,
|
||||||
consts,
|
consts,
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
events::connector_api_logs::ConnectorEvent,
|
events::connector_api_logs::ConnectorEvent,
|
||||||
@ -28,9 +28,18 @@ use crate::{
|
|||||||
},
|
},
|
||||||
utils::BytesExt,
|
utils::BytesExt,
|
||||||
};
|
};
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Globepay {
|
||||||
|
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
impl Globepay {
|
||||||
pub struct Globepay;
|
pub fn new() -> &'static Self {
|
||||||
|
&Self {
|
||||||
|
amount_converter: &MinorUnitForConnector,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl api::Payment for Globepay {}
|
impl api::Payment for Globepay {}
|
||||||
impl api::PaymentSession for Globepay {}
|
impl api::PaymentSession for Globepay {}
|
||||||
@ -213,7 +222,14 @@ 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_req = globepay::GlobepayPaymentsRequest::try_from(req)?;
|
let amount = convert_amount(
|
||||||
|
self.amount_converter,
|
||||||
|
req.request.minor_amount,
|
||||||
|
req.request.currency,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let connector_router_data = globepay::GlobepayRouterData::from((amount, req));
|
||||||
|
let connector_req = globepay::GlobepayPaymentsRequest::try_from(&connector_router_data)?;
|
||||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -399,7 +415,14 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
|
|||||||
req: &types::RefundsRouterData<api::Execute>,
|
req: &types::RefundsRouterData<api::Execute>,
|
||||||
_connectors: &settings::Connectors,
|
_connectors: &settings::Connectors,
|
||||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||||
let connector_req = globepay::GlobepayRefundRequest::try_from(req)?;
|
let refund_amount = convert_amount(
|
||||||
|
self.amount_converter,
|
||||||
|
req.request.minor_refund_amount,
|
||||||
|
req.request.currency,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let connector_router_data = globepay::GlobepayRouterData::from((refund_amount, req));
|
||||||
|
let connector_req = globepay::GlobepayRefundRequest::try_from(&connector_router_data)?;
|
||||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use common_utils::ext_traits::Encode;
|
use common_utils::{ext_traits::Encode, types::MinorUnit};
|
||||||
use error_stack::ResultExt;
|
use error_stack::ResultExt;
|
||||||
use masking::Secret;
|
use masking::Secret;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -11,9 +11,24 @@ use crate::{
|
|||||||
};
|
};
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct GlobepayRouterData<T> {
|
||||||
|
pub amount: MinorUnit,
|
||||||
|
pub router_data: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<(MinorUnit, T)> for GlobepayRouterData<T> {
|
||||||
|
fn from((amount, router_data): (MinorUnit, T)) -> Self {
|
||||||
|
Self {
|
||||||
|
amount,
|
||||||
|
router_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct GlobepayPaymentsRequest {
|
pub struct GlobepayPaymentsRequest {
|
||||||
price: i64,
|
price: MinorUnit,
|
||||||
description: String,
|
description: String,
|
||||||
currency: enums::Currency,
|
currency: enums::Currency,
|
||||||
channel: GlobepayChannel,
|
channel: GlobepayChannel,
|
||||||
@ -25,9 +40,12 @@ pub enum GlobepayChannel {
|
|||||||
Wechat,
|
Wechat,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&types::PaymentsAuthorizeRouterData> for GlobepayPaymentsRequest {
|
impl TryFrom<&GlobepayRouterData<&types::PaymentsAuthorizeRouterData>> for GlobepayPaymentsRequest {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
|
item_data: &GlobepayRouterData<&types::PaymentsAuthorizeRouterData>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
let item = item_data.router_data.clone();
|
||||||
let channel: GlobepayChannel = match &item.request.payment_method_data {
|
let channel: GlobepayChannel = match &item.request.payment_method_data {
|
||||||
domain::PaymentMethodData::Wallet(ref wallet_data) => match wallet_data {
|
domain::PaymentMethodData::Wallet(ref wallet_data) => match wallet_data {
|
||||||
domain::WalletData::AliPayQr(_) => GlobepayChannel::Alipay,
|
domain::WalletData::AliPayQr(_) => GlobepayChannel::Alipay,
|
||||||
@ -83,7 +101,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for GlobepayPaymentsRequest {
|
|||||||
};
|
};
|
||||||
let description = item.get_description()?;
|
let description = item.get_description()?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
price: item.request.amount,
|
price: item_data.amount,
|
||||||
description,
|
description,
|
||||||
currency: item.request.currency,
|
currency: item.request.currency,
|
||||||
channel,
|
channel,
|
||||||
@ -308,15 +326,15 @@ fn get_error_response(
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct GlobepayRefundRequest {
|
pub struct GlobepayRefundRequest {
|
||||||
pub fee: i64,
|
pub fee: MinorUnit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for GlobepayRefundRequest {
|
impl<F> TryFrom<&GlobepayRouterData<&types::RefundsRouterData<F>>> for GlobepayRefundRequest {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
Ok(Self {
|
item: &GlobepayRouterData<&types::RefundsRouterData<F>>,
|
||||||
fee: item.request.refund_amount,
|
) -> Result<Self, Self::Error> {
|
||||||
})
|
Ok(Self { fee: item.amount })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -398,7 +398,7 @@ impl ConnectorData {
|
|||||||
Ok(ConnectorEnum::Old(Box::new(connector::Globalpay::new())))
|
Ok(ConnectorEnum::Old(Box::new(connector::Globalpay::new())))
|
||||||
}
|
}
|
||||||
enums::Connector::Globepay => {
|
enums::Connector::Globepay => {
|
||||||
Ok(ConnectorEnum::Old(Box::new(&connector::Globepay)))
|
Ok(ConnectorEnum::Old(Box::new(connector::Globepay::new())))
|
||||||
}
|
}
|
||||||
enums::Connector::Gocardless => {
|
enums::Connector::Gocardless => {
|
||||||
Ok(ConnectorEnum::Old(Box::new(&connector::Gocardless)))
|
Ok(ConnectorEnum::Old(Box::new(&connector::Gocardless)))
|
||||||
|
|||||||
@ -13,7 +13,7 @@ impl utils::Connector for GlobepayTest {
|
|||||||
fn get_data(&self) -> types::api::ConnectorData {
|
fn get_data(&self) -> types::api::ConnectorData {
|
||||||
use router::connector::Globepay;
|
use router::connector::Globepay;
|
||||||
utils::construct_connector_data_old(
|
utils::construct_connector_data_old(
|
||||||
Box::new(&Globepay),
|
Box::new(Globepay::new()),
|
||||||
types::Connector::Globepay,
|
types::Connector::Globepay,
|
||||||
types::api::GetToken::Connector,
|
types::api::GetToken::Connector,
|
||||||
None,
|
None,
|
||||||
|
|||||||
Reference in New Issue
Block a user