Skip to content

Commit

Permalink
refactor(connector): [Rapyd] add and implement the get_currency_unit …
Browse files Browse the repository at this point in the history
…function (#2664)

Co-authored-by: Shivansh Bhatnagar <[email protected]>
  • Loading branch information
1 parent af90089 commit 78e5cd0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
28 changes: 25 additions & 3 deletions crates/router/src/connector/rapyd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl ConnectorCommon for Rapyd {
"rapyd"
}

fn get_currency_unit(&self) -> api::CurrencyUnit {
api::CurrencyUnit::Minor
}

fn common_get_content_type(&self) -> &'static str {
"application/json"
}
Expand Down Expand Up @@ -179,7 +183,13 @@ impl
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = rapyd::RapydPaymentsRequest::try_from(req)?;
let connector_router_data = rapyd::RapydRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let req_obj = rapyd::RapydPaymentsRequest::try_from(&connector_router_data)?;
let rapyd_req = types::RequestBody::log_and_get_request_body(
&req_obj,
utils::Encode::<rapyd::RapydPaymentsRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -483,7 +493,13 @@ impl
&self,
req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = rapyd::CaptureRequest::try_from(req)?;
let connector_router_data = rapyd::RapydRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount_to_capture,
req,
))?;
let req_obj = rapyd::CaptureRequest::try_from(&connector_router_data)?;
let rapyd_req = types::RequestBody::log_and_get_request_body(
&req_obj,
utils::Encode::<rapyd::CaptureRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -615,7 +631,13 @@ impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::Ref
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = rapyd::RapydRefundRequest::try_from(req)?;
let connector_router_data = rapyd::RapydRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let req_obj = rapyd::RapydRefundRequest::try_from(&connector_router_data)?;
let rapyd_req = types::RequestBody::log_and_get_request_body(
&req_obj,
utils::Encode::<rapyd::RapydRefundRequest>::encode_to_string_of_json,
Expand Down
75 changes: 58 additions & 17 deletions crates/router/src/connector/rapyd/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ use crate::{
utils::OptionExt,
};

#[derive(Debug, Serialize)]
pub struct RapydRouterData<T> {
pub amount: i64,
pub router_data: T,
}

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for RapydRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(_currency_unit, _currency, amount, item): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data: item,
})
}
}

#[derive(Default, Debug, Serialize)]
pub struct RapydPaymentsRequest {
pub amount: i64,
Expand Down Expand Up @@ -69,26 +99,31 @@ pub struct RapydWallet {
token: Option<String>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for RapydPaymentsRequest {
impl TryFrom<&RapydRouterData<&types::PaymentsAuthorizeRouterData>> for RapydPaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
let (capture, payment_method_options) = match item.payment_method {
fn try_from(
item: &RapydRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let (capture, payment_method_options) = match item.router_data.payment_method {
diesel_models::enums::PaymentMethod::Card => {
let three_ds_enabled = matches!(item.auth_type, enums::AuthenticationType::ThreeDs);
let three_ds_enabled = matches!(
item.router_data.auth_type,
enums::AuthenticationType::ThreeDs
);
let payment_method_options = PaymentMethodOptions {
three_ds: three_ds_enabled,
};
(
Some(matches!(
item.request.capture_method,
item.router_data.request.capture_method,
Some(enums::CaptureMethod::Automatic) | None
)),
Some(payment_method_options),
)
}
_ => (None, None),
};
let payment_method = match item.request.payment_method_data {
let payment_method = match item.router_data.request.payment_method_data {
api_models::payments::PaymentMethodData::Card(ref ccard) => {
Some(PaymentMethod {
pm_type: "in_amex_card".to_owned(), //[#369] Map payment method type based on country
Expand Down Expand Up @@ -128,10 +163,10 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for RapydPaymentsRequest {
.change_context(errors::ConnectorError::NotImplemented(
"payment_method".to_owned(),
))?;
let return_url = item.request.get_return_url()?;
let return_url = item.router_data.request.get_return_url()?;
Ok(Self {
amount: item.request.amount,
currency: item.request.currency,
amount: item.amount,
currency: item.router_data.request.currency,
payment_method,
capture,
payment_method_options,
Expand Down Expand Up @@ -276,13 +311,17 @@ pub struct RapydRefundRequest {
pub currency: Option<enums::Currency>,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for RapydRefundRequest {
impl<F> TryFrom<&RapydRouterData<&types::RefundsRouterData<F>>> for RapydRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
fn try_from(item: &RapydRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
Ok(Self {
payment: item.request.connector_transaction_id.to_string(),
amount: Some(item.request.refund_amount),
currency: Some(item.request.currency),
payment: item
.router_data
.request
.connector_transaction_id
.to_string(),
amount: Some(item.amount),
currency: Some(item.router_data.request.currency),
})
}
}
Expand Down Expand Up @@ -380,11 +419,13 @@ pub struct CaptureRequest {
statement_descriptor: Option<String>,
}

impl TryFrom<&types::PaymentsCaptureRouterData> for CaptureRequest {
impl TryFrom<&RapydRouterData<&types::PaymentsCaptureRouterData>> for CaptureRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &RapydRouterData<&types::PaymentsCaptureRouterData>,
) -> Result<Self, Self::Error> {
Ok(Self {
amount: Some(item.request.amount_to_capture),
amount: Some(item.amount),
receipt_email: None,
statement_descriptor: None,
})
Expand Down

0 comments on commit 78e5cd0

Please sign in to comment.