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