Skip to content

Commit

Permalink
refactor(connector): add amount conversion framework to opayo (juspay…
Browse files Browse the repository at this point in the history
…#6342)

Co-authored-by: DEEPANSHU BANSAL <[email protected]>
  • Loading branch information
gauravghodinde and deepanshu-iiitu authored Oct 18, 2024
1 parent 368e6b5 commit 91146de
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
39 changes: 31 additions & 8 deletions crates/router/src/connector/opayo.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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 error_stack::{report, ResultExt};
use masking::ExposeInterface;
use transformers as opayo;

use crate::{
configs::settings,
connector::utils as connector_utils,
connector::{utils as connector_utils, utils::convert_amount},
core::errors::{self, CustomResult},
events::connector_api_logs::ConnectorEvent,
headers,
Expand All @@ -27,8 +28,18 @@ use crate::{
utils::BytesExt,
};

#[derive(Debug, Clone)]
pub struct Opayo;
#[derive(Clone)]
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::PaymentSession for Opayo {}
Expand Down Expand Up @@ -197,7 +208,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
req: &types::PaymentsAuthorizeRouterData,
_connectors: &settings::Connectors,
) -> 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)))
}

Expand Down Expand Up @@ -433,7 +450,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
req: &types::RefundsRouterData<api::Execute>,
_connectors: &settings::Connectors,
) -> 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)))
}
Expand Down
34 changes: 26 additions & 8 deletions crates/router/src/connector/opayo/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_utils::types::MinorUnit;
use masking::Secret;
use serde::{Deserialize, Serialize};

Expand All @@ -6,10 +7,24 @@ use crate::{
core::errors,
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)]
pub struct OpayoPaymentsRequest {
amount: i64,
amount: MinorUnit,
card: OpayoCard,
}

Expand All @@ -23,9 +38,12 @@ pub struct OpayoCard {
complete: bool,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for OpayoPaymentsRequest {
impl TryFrom<&OpayoRouterData<&types::PaymentsAuthorizeRouterData>> for OpayoPaymentsRequest {
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() {
domain::PaymentMethodData::Card(req_card) => {
let card = OpayoCard {
Expand All @@ -39,7 +57,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for OpayoPaymentsRequest {
complete: item.request.is_auto_capture()?,
};
Ok(Self {
amount: item.request.amount,
amount: item_data.amount,
card,
})
}
Expand Down Expand Up @@ -143,14 +161,14 @@ impl<F, T>
// Type definition for RefundRequest
#[derive(Default, Debug, Serialize)]
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>;
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 {
amount: item.request.refund_amount,
amount: item.amount,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/tests/connectors/opayo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl utils::Connector for OpayoTest {
fn get_data(&self) -> types::api::ConnectorData {
use router::connector::Opayo;
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
types::Connector::DummyConnector1,
types::api::GetToken::Connector,
Expand Down

0 comments on commit 91146de

Please sign in to comment.