mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
refactor(connector): add amount conversion framework to opayo (#6342)
Co-authored-by: DEEPANSHU BANSAL <41580413+deepanshu-iiitu@users.noreply.github.com>
This commit is contained in:
@ -1,8 +1,9 @@
|
|||||||
mod transformers;
|
mod transformers;
|
||||||
|
|
||||||
use std::fmt::Debug;
|
use common_utils::{
|
||||||
|
request::RequestContent,
|
||||||
use common_utils::request::RequestContent;
|
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
|
||||||
|
};
|
||||||
use diesel_models::enums;
|
use diesel_models::enums;
|
||||||
use error_stack::{report, ResultExt};
|
use error_stack::{report, ResultExt};
|
||||||
use masking::ExposeInterface;
|
use masking::ExposeInterface;
|
||||||
@ -10,7 +11,7 @@ use transformers as opayo;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
configs::settings,
|
configs::settings,
|
||||||
connector::utils as connector_utils,
|
connector::{utils as connector_utils, utils::convert_amount},
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
events::connector_api_logs::ConnectorEvent,
|
events::connector_api_logs::ConnectorEvent,
|
||||||
headers,
|
headers,
|
||||||
@ -27,8 +28,18 @@ use crate::{
|
|||||||
utils::BytesExt,
|
utils::BytesExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Opayo;
|
pub struct Opayo {
|
||||||
|
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Opayo {
|
||||||
|
pub fn new() -> &'static Self {
|
||||||
|
&Self {
|
||||||
|
amount_converter: &MinorUnitForConnector,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl api::Payment for Opayo {}
|
impl api::Payment for Opayo {}
|
||||||
impl api::PaymentSession for Opayo {}
|
impl api::PaymentSession for Opayo {}
|
||||||
@ -197,7 +208,13 @@ 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 = opayo::OpayoPaymentsRequest::try_from(req)?;
|
let amount = convert_amount(
|
||||||
|
self.amount_converter,
|
||||||
|
req.request.minor_amount,
|
||||||
|
req.request.currency,
|
||||||
|
)?;
|
||||||
|
let connector_router_data = opayo::OpayoRouterData::from((amount, req));
|
||||||
|
let connector_req = opayo::OpayoPaymentsRequest::try_from(&connector_router_data)?;
|
||||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -433,7 +450,13 @@ 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 = opayo::OpayoRefundRequest::try_from(req)?;
|
let refund_amount = convert_amount(
|
||||||
|
self.amount_converter,
|
||||||
|
req.request.minor_refund_amount,
|
||||||
|
req.request.currency,
|
||||||
|
)?;
|
||||||
|
let connector_router_data = opayo::OpayoRouterData::from((refund_amount, req));
|
||||||
|
let connector_req = opayo::OpayoRefundRequest::try_from(&connector_router_data)?;
|
||||||
|
|
||||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use common_utils::types::MinorUnit;
|
||||||
use masking::Secret;
|
use masking::Secret;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@ -6,10 +7,24 @@ use crate::{
|
|||||||
core::errors,
|
core::errors,
|
||||||
types::{self, api, domain, storage::enums},
|
types::{self, api, domain, storage::enums},
|
||||||
};
|
};
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct OpayoRouterData<T> {
|
||||||
|
pub amount: MinorUnit,
|
||||||
|
pub router_data: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<(MinorUnit, T)> for OpayoRouterData<T> {
|
||||||
|
fn from((amount, router_data): (MinorUnit, T)) -> Self {
|
||||||
|
Self {
|
||||||
|
amount,
|
||||||
|
router_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||||
pub struct OpayoPaymentsRequest {
|
pub struct OpayoPaymentsRequest {
|
||||||
amount: i64,
|
amount: MinorUnit,
|
||||||
card: OpayoCard,
|
card: OpayoCard,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,9 +38,12 @@ pub struct OpayoCard {
|
|||||||
complete: bool,
|
complete: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&types::PaymentsAuthorizeRouterData> for OpayoPaymentsRequest {
|
impl TryFrom<&OpayoRouterData<&types::PaymentsAuthorizeRouterData>> for OpayoPaymentsRequest {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
|
item_data: &OpayoRouterData<&types::PaymentsAuthorizeRouterData>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
let item = item_data.router_data.clone();
|
||||||
match item.request.payment_method_data.clone() {
|
match item.request.payment_method_data.clone() {
|
||||||
domain::PaymentMethodData::Card(req_card) => {
|
domain::PaymentMethodData::Card(req_card) => {
|
||||||
let card = OpayoCard {
|
let card = OpayoCard {
|
||||||
@ -39,7 +57,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for OpayoPaymentsRequest {
|
|||||||
complete: item.request.is_auto_capture()?,
|
complete: item.request.is_auto_capture()?,
|
||||||
};
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
amount: item.request.amount,
|
amount: item_data.amount,
|
||||||
card,
|
card,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -143,14 +161,14 @@ impl<F, T>
|
|||||||
// Type definition for RefundRequest
|
// Type definition for RefundRequest
|
||||||
#[derive(Default, Debug, Serialize)]
|
#[derive(Default, Debug, Serialize)]
|
||||||
pub struct OpayoRefundRequest {
|
pub struct OpayoRefundRequest {
|
||||||
pub amount: i64,
|
pub amount: MinorUnit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for OpayoRefundRequest {
|
impl<F> TryFrom<&OpayoRouterData<&types::RefundsRouterData<F>>> for OpayoRefundRequest {
|
||||||
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: &OpayoRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
amount: item.request.refund_amount,
|
amount: item.amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ impl utils::Connector for OpayoTest {
|
|||||||
fn get_data(&self) -> types::api::ConnectorData {
|
fn get_data(&self) -> types::api::ConnectorData {
|
||||||
use router::connector::Opayo;
|
use router::connector::Opayo;
|
||||||
utils::construct_connector_data_old(
|
utils::construct_connector_data_old(
|
||||||
Box::new(&Opayo),
|
Box::new(Opayo::new()),
|
||||||
// Remove `dummy_connector` feature gate from module in `main.rs` when updating this to use actual connector variant
|
// Remove `dummy_connector` feature gate from module in `main.rs` when updating this to use actual connector variant
|
||||||
types::Connector::DummyConnector1,
|
types::Connector::DummyConnector1,
|
||||||
types::api::GetToken::Connector,
|
types::api::GetToken::Connector,
|
||||||
|
|||||||
Reference in New Issue
Block a user