Skip to content

Commit

Permalink
refactor(connector): [Trustpay] Enhance currency Mapping with Connec…
Browse files Browse the repository at this point in the history
…torCurrencyCommon Trait (#2197)
  • Loading branch information
srujanchikke authored Sep 22, 2023
1 parent b9f25c4 commit 583b9aa
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 65 deletions.
33 changes: 29 additions & 4 deletions crates/router/src/connector/trustpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use transformers as trustpay;

use super::utils::{
collect_and_sort_values_by_removing_signature, get_error_code_error_message_based_on_priority,
ConnectorErrorType, ConnectorErrorTypeMapping,
ConnectorErrorType, ConnectorErrorTypeMapping, PaymentsPreProcessingData,
};
use crate::{
configs::settings,
Expand Down Expand Up @@ -80,6 +80,10 @@ impl ConnectorCommon for Trustpay {
"trustpay"
}

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

fn common_get_content_type(&self) -> &'static str {
"application/x-www-form-urlencoded"
}
Expand Down Expand Up @@ -422,7 +426,16 @@ impl
&self,
req: &types::PaymentsPreProcessingRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let create_intent_req = trustpay::TrustpayCreateIntentRequest::try_from(req)?;
let amount = req.request.get_amount()?;
let currency = req.request.get_currency()?;
let connector_router_data = trustpay::TrustpayRouterData::try_from((
&self.get_currency_unit(),
currency,
amount,
req,
))?;
let create_intent_req =
trustpay::TrustpayCreateIntentRequest::try_from(&connector_router_data)?;
let trustpay_req = types::RequestBody::log_and_get_request_body(
&create_intent_req,
utils::Encode::<trustpay::TrustpayCreateIntentRequest>::url_encode,
Expand Down Expand Up @@ -525,7 +538,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = trustpay::TrustpayPaymentsRequest::try_from(req)?;
let connector_router_data = trustpay::TrustpayRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_req = trustpay::TrustpayPaymentsRequest::try_from(&connector_router_data)?;
let trustpay_req_string = match req.payment_method {
diesel_models::enums::PaymentMethod::BankRedirect => {
types::RequestBody::log_and_get_request_body(
Expand Down Expand Up @@ -628,7 +647,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = trustpay::TrustpayRefundRequest::try_from(req)?;
let connector_router_data = trustpay::TrustpayRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_req = trustpay::TrustpayRefundRequest::try_from(&connector_router_data)?;
let trustpay_req_string = match req.payment_method {
diesel_models::enums::PaymentMethod::BankRedirect => {
types::RequestBody::log_and_get_request_body(
Expand Down
140 changes: 79 additions & 61 deletions crates/router/src/connector/trustpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,55 @@ use common_utils::{
errors::CustomResult,
pii::{self, Email},
};
use error_stack::{report, IntoReport, ResultExt};
use error_stack::{report, ResultExt};
use masking::{PeekInterface, Secret};
use reqwest::Url;
use serde::{Deserialize, Serialize};

use crate::{
connector::utils::{
self, AddressDetailsData, BrowserInformationData, CardData, PaymentsAuthorizeRequestData,
RouterData,
PaymentsPreProcessingData, RouterData,
},
consts,
core::errors,
services,
types::{self, api, storage::enums, BrowserInformation},
utils::OptionExt,
};

type Error = error_stack::Report<errors::ConnectorError>;

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

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for TrustpayRouterData<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> {
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
Ok(Self {
amount,
router_data: item,
})
}
}

pub struct TrustpayAuthType {
pub(super) api_key: Secret<String>,
pub(super) project_id: Secret<String>,
Expand Down Expand Up @@ -344,10 +374,17 @@ fn get_bank_redirection_request_data(
Ok(payment_request)
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for TrustpayPaymentsRequest {
impl TryFrom<&TrustpayRouterData<&types::PaymentsAuthorizeRouterData>> for TrustpayPaymentsRequest {
type Error = Error;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
let browser_info = item.request.browser_info.clone().unwrap_or_default();
fn try_from(
item: &TrustpayRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let browser_info = item
.router_data
.request
.browser_info
.clone()
.unwrap_or_default();
let default_browser_info = BrowserInformation {
color_depth: Some(browser_info.color_depth.unwrap_or(24)),
java_enabled: Some(browser_info.java_enabled.unwrap_or(false)),
Expand All @@ -360,27 +397,27 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for TrustpayPaymentsRequest {
user_agent: browser_info.user_agent,
ip_address: browser_info.ip_address,
};
let params = get_mandatory_fields(item)?;
let amount = format!(
"{:.2}",
utils::to_currency_base_unit(item.request.amount, item.request.currency)?
.parse::<f64>()
.ok()
.ok_or(errors::ConnectorError::RequestEncodingFailed)?
);
let auth = TrustpayAuthType::try_from(&item.connector_auth_type)
let params = get_mandatory_fields(item.router_data)?;
let amount = item.amount.to_owned();
let auth = TrustpayAuthType::try_from(&item.router_data.connector_auth_type)
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
match item.request.payment_method_data {
match item.router_data.request.payment_method_data {
api::PaymentMethodData::Card(ref ccard) => Ok(get_card_request_data(
item,
item.router_data,
&default_browser_info,
params,
amount,
ccard,
item.request.get_return_url()?,
item.router_data.request.get_return_url()?,
)?),
api::PaymentMethodData::BankRedirect(ref bank_redirection_data) => {
get_bank_redirection_request_data(item, bank_redirection_data, params, amount, auth)
get_bank_redirection_request_data(
item.router_data,
bank_redirection_data,
params,
amount,
auth,
)
}
_ => Err(errors::ConnectorError::NotImplemented("Payment methods".to_string()).into()),
}
Expand Down Expand Up @@ -890,51 +927,36 @@ pub struct TrustpayCreateIntentRequest {
pub reference: String,
}

impl TryFrom<&types::PaymentsPreProcessingRouterData> for TrustpayCreateIntentRequest {
impl TryFrom<&TrustpayRouterData<&types::PaymentsPreProcessingRouterData>>
for TrustpayCreateIntentRequest
{
type Error = Error;
fn try_from(item: &types::PaymentsPreProcessingRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &TrustpayRouterData<&types::PaymentsPreProcessingRouterData>,
) -> Result<Self, Self::Error> {
let is_apple_pay = item
.router_data
.request
.payment_method_type
.as_ref()
.map(|pmt| matches!(pmt, diesel_models::enums::PaymentMethodType::ApplePay));

let is_google_pay = item
.router_data
.request
.payment_method_type
.as_ref()
.map(|pmt| matches!(pmt, diesel_models::enums::PaymentMethodType::GooglePay));

let request_amount = item
.request
.amount
.get_required_value("amount")
.change_context(errors::ConnectorError::MissingRequiredField {
field_name: "amount",
})?;

let currency = item
.request
.currency
.get_required_value("currency")
.change_context(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?;

let amount = format!(
"{:.2}",
utils::to_currency_base_unit(request_amount, currency)?
.parse::<f64>()
.into_report()
.change_context(errors::ConnectorError::RequestEncodingFailed)?
);
let currency = item.router_data.request.get_currency()?;
let amount = item.amount.to_owned();

Ok(Self {
amount,
currency: currency.to_string(),
init_apple_pay: is_apple_pay,
init_google_pay: is_google_pay,
reference: item.payment_id.clone(),
reference: item.router_data.payment_id.clone(),
})
}
}
Expand Down Expand Up @@ -1278,19 +1300,15 @@ pub enum TrustpayRefundRequest {
BankRedirectRefund(Box<TrustpayRefundRequestBankRedirect>),
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for TrustpayRefundRequest {
impl<F> TryFrom<&TrustpayRouterData<&types::RefundsRouterData<F>>> for TrustpayRefundRequest {
type Error = Error;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
let amount = format!(
"{:.2}",
utils::to_currency_base_unit(item.request.refund_amount, item.request.currency)?
.parse::<f64>()
.into_report()
.change_context(errors::ConnectorError::RequestEncodingFailed)?
);
match item.payment_method {
fn try_from(
item: &TrustpayRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
let amount = item.amount.to_owned();
match item.router_data.payment_method {
diesel_models::enums::PaymentMethod::BankRedirect => {
let auth = TrustpayAuthType::try_from(&item.connector_auth_type)
let auth = TrustpayAuthType::try_from(&item.router_data.connector_auth_type)
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
Ok(Self::BankRedirectRefund(Box::new(
TrustpayRefundRequestBankRedirect {
Expand All @@ -1300,21 +1318,21 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for TrustpayRefundRequest {
payment_information: BankPaymentInformation {
amount: Amount {
amount,
currency: item.request.currency.to_string(),
currency: item.router_data.request.currency.to_string(),
},
references: References {
merchant_reference: item.request.refund_id.clone(),
merchant_reference: item.router_data.request.refund_id.clone(),
},
debtor: None,
},
},
)))
}
_ => Ok(Self::CardsRefund(Box::new(TrustpayRefundRequestCards {
instance_id: item.request.connector_transaction_id.clone(),
instance_id: item.router_data.request.connector_transaction_id.clone(),
amount,
currency: item.request.currency.to_string(),
reference: item.request.refund_id.clone(),
currency: item.router_data.request.currency.to_string(),
reference: item.router_data.request.refund_id.clone(),
}))),
}
}
Expand Down

0 comments on commit 583b9aa

Please sign in to comment.