mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
refactor(connector): add amount conversion framework to rapyd (#6414)
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
pub mod transformers;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use base64::Engine;
|
||||
use common_utils::{
|
||||
date_time,
|
||||
ext_traits::{Encode, StringExt},
|
||||
request::RequestContent,
|
||||
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
|
||||
};
|
||||
use diesel_models::enums;
|
||||
use error_stack::{Report, ResultExt};
|
||||
@ -17,6 +17,7 @@ use transformers as rapyd;
|
||||
use super::utils as connector_utils;
|
||||
use crate::{
|
||||
configs::settings,
|
||||
connector::utils::convert_amount,
|
||||
consts,
|
||||
core::errors::{self, CustomResult},
|
||||
events::connector_api_logs::ConnectorEvent,
|
||||
@ -34,9 +35,17 @@ use crate::{
|
||||
utils::{self, crypto, ByteSliceExt, BytesExt},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Rapyd;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Rapyd {
|
||||
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
|
||||
}
|
||||
impl Rapyd {
|
||||
pub fn new() -> &'static Self {
|
||||
&Self {
|
||||
amount_converter: &MinorUnitForConnector,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Rapyd {
|
||||
pub fn generate_signature(
|
||||
&self,
|
||||
@ -198,12 +207,12 @@ impl
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = rapyd::RapydRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount,
|
||||
req.request.currency,
|
||||
req.request.amount,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
let connector_router_data = rapyd::RapydRouterData::from((amount, req));
|
||||
let connector_req = rapyd::RapydPaymentsRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
@ -528,12 +537,12 @@ impl
|
||||
req: &types::PaymentsCaptureRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = rapyd::RapydRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount_to_capture,
|
||||
req.request.currency,
|
||||
req.request.amount_to_capture,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
let connector_router_data = rapyd::RapydRouterData::from((amount, req));
|
||||
let connector_req = rapyd::CaptureRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
@ -668,12 +677,12 @@ impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::Ref
|
||||
req: &types::RefundsRouterData<api::Execute>,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = rapyd::RapydRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_refund_amount,
|
||||
req.request.currency,
|
||||
req.request.refund_amount,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
let connector_router_data = rapyd::RapydRouterData::from((amount, req));
|
||||
let connector_req = rapyd::RapydRefundRequest::try_from(&connector_router_data)?;
|
||||
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::types::MinorUnit;
|
||||
use error_stack::ResultExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
@ -15,25 +16,22 @@ use crate::{
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RapydRouterData<T> {
|
||||
pub amount: i64,
|
||||
pub amount: MinorUnit,
|
||||
pub router_data: T,
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for RapydRouterData<T> {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
fn try_from(
|
||||
(_currency_unit, _currency, amount, item): (&api::CurrencyUnit, enums::Currency, i64, T),
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
impl<T> From<(MinorUnit, T)> for RapydRouterData<T> {
|
||||
fn from((amount, router_data): (MinorUnit, T)) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
router_data: item,
|
||||
})
|
||||
router_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct RapydPaymentsRequest {
|
||||
pub amount: i64,
|
||||
pub amount: MinorUnit,
|
||||
pub currency: enums::Currency,
|
||||
pub payment_method: PaymentMethod,
|
||||
pub payment_method_options: Option<PaymentMethodOptions>,
|
||||
@ -302,7 +300,7 @@ pub struct DisputeResponseData {
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct RapydRefundRequest {
|
||||
pub payment: String,
|
||||
pub amount: Option<i64>,
|
||||
pub amount: Option<MinorUnit>,
|
||||
pub currency: Option<enums::Currency>,
|
||||
}
|
||||
|
||||
@ -409,7 +407,7 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, RefundResponse>>
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CaptureRequest {
|
||||
amount: Option<i64>,
|
||||
amount: Option<MinorUnit>,
|
||||
receipt_email: Option<Secret<String>>,
|
||||
statement_descriptor: Option<String>,
|
||||
}
|
||||
|
||||
@ -471,7 +471,9 @@ impl ConnectorData {
|
||||
enums::Connector::Razorpay => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Razorpay::new())))
|
||||
}
|
||||
enums::Connector::Rapyd => Ok(ConnectorEnum::Old(Box::new(&connector::Rapyd))),
|
||||
enums::Connector::Rapyd => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Rapyd::new())))
|
||||
}
|
||||
enums::Connector::Shift4 => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Shift4::new())))
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ impl utils::Connector for Rapyd {
|
||||
fn get_data(&self) -> types::api::ConnectorData {
|
||||
use router::connector::Rapyd;
|
||||
utils::construct_connector_data_old(
|
||||
Box::new(&Rapyd),
|
||||
Box::new(Rapyd::new()),
|
||||
types::Connector::Rapyd,
|
||||
types::api::GetToken::Connector,
|
||||
None,
|
||||
|
||||
Reference in New Issue
Block a user