Skip to content

Commit

Permalink
refactor(connector): [Dlocal] Currency Unit Conversion (#2615)
Browse files Browse the repository at this point in the history
Co-authored-by: chikke srujan <[email protected]>
  • Loading branch information
SagarDevAchar and srujanchikke authored Oct 19, 2023
1 parent 9ea5830 commit 1f2fe51
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
22 changes: 19 additions & 3 deletions crates/router/src/connector/dlocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl ConnectorCommon for Dlocal {
"dlocal"
}

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

fn common_get_content_type(&self) -> &'static str {
"application/json"
}
Expand Down Expand Up @@ -207,7 +211,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_request = dlocal::DlocalPaymentsRequest::try_from(req)?;
let connector_router_data = dlocal::DlocalRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_request = dlocal::DlocalPaymentsRequest::try_from(&connector_router_data)?;
let dlocal_payments_request = types::RequestBody::log_and_get_request_body(
&connector_request,
utils::Encode::<dlocal::DlocalPaymentsRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -508,10 +518,16 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_request = dlocal::RefundRequest::try_from(req)?;
let connector_router_data = dlocal::DlocalRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_request = dlocal::DlocalRefundRequest::try_from(&connector_router_data)?;
let dlocal_refund_request = types::RequestBody::log_and_get_request_body(
&connector_request,
utils::Encode::<dlocal::RefundRequest>::encode_to_string_of_json,
utils::Encode::<dlocal::DlocalRefundRequest>::encode_to_string_of_json,
)
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some(dlocal_refund_request))
Expand Down
81 changes: 61 additions & 20 deletions crates/router/src/connector/dlocal/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ pub enum PaymentMethodFlow {
ReDirect,
}

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

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for DlocalRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(
(_currency_unit, _currency, amount, router_data): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data,
})
}
}

#[derive(Default, Debug, Serialize, Eq, PartialEq)]
pub struct DlocalPaymentsRequest {
pub amount: i64,
Expand All @@ -66,22 +97,24 @@ pub struct DlocalPaymentsRequest {
pub description: Option<String>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for DlocalPaymentsRequest {
impl TryFrom<&DlocalRouterData<&types::PaymentsAuthorizeRouterData>> for DlocalPaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
let email = item.request.email.clone();
let address = item.get_billing_address()?;
fn try_from(
item: &DlocalRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let email = item.router_data.request.email.clone();
let address = item.router_data.get_billing_address()?;
let country = address.get_country()?;
let name = get_payer_name(address);
match item.request.payment_method_data {
match item.router_data.request.payment_method_data {
api::PaymentMethodData::Card(ref ccard) => {
let should_capture = matches!(
item.request.capture_method,
item.router_data.request.capture_method,
Some(enums::CaptureMethod::Automatic)
);
let payment_request = Self {
amount: item.request.amount,
currency: item.request.currency,
amount: item.amount,
currency: item.router_data.request.currency,
payment_method_id: PaymentMethodId::Card,
payment_method_flow: PaymentMethodFlow::Direct,
country: country.to_string(),
Expand All @@ -99,22 +132,28 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for DlocalPaymentsRequest {
expiration_year: ccard.card_exp_year.clone(),
capture: should_capture.to_string(),
installments_id: item
.router_data
.request
.mandate_id
.as_ref()
.map(|ids| ids.mandate_id.clone()),
// [#595[FEATURE] Pass Mandate history information in payment flows/request]
installments: item.request.mandate_id.clone().map(|_| "1".to_string()),
installments: item
.router_data
.request
.mandate_id
.clone()
.map(|_| "1".to_string()),
}),
order_id: item.payment_id.clone(),
three_dsecure: match item.auth_type {
order_id: item.router_data.payment_id.clone(),
three_dsecure: match item.router_data.auth_type {
diesel_models::enums::AuthenticationType::ThreeDs => {
Some(ThreeDSecureReqData { force: true })
}
diesel_models::enums::AuthenticationType::NoThreeDs => None,
},
callback_url: Some(item.request.get_router_return_url()?),
description: item.description.clone(),
callback_url: Some(item.router_data.request.get_router_return_url()?),
description: item.router_data.description.clone(),
};
Ok(payment_request)
}
Expand Down Expand Up @@ -399,22 +438,24 @@ impl<F, T>

// REFUND :
#[derive(Default, Debug, Serialize)]
pub struct RefundRequest {
pub struct DlocalRefundRequest {
pub amount: String,
pub payment_id: String,
pub currency: enums::Currency,
pub id: String,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for RefundRequest {
impl<F> TryFrom<&DlocalRouterData<&types::RefundsRouterData<F>>> for DlocalRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
let amount_to_refund = item.request.refund_amount.to_string();
fn try_from(
item: &DlocalRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
let amount_to_refund = item.router_data.request.refund_amount.to_string();
Ok(Self {
amount: amount_to_refund,
payment_id: item.request.connector_transaction_id.clone(),
currency: item.request.currency,
id: item.request.refund_id.clone(),
payment_id: item.router_data.request.connector_transaction_id.clone(),
currency: item.router_data.request.currency,
id: item.router_data.request.refund_id.clone(),
})
}
}
Expand Down

0 comments on commit 1f2fe51

Please sign in to comment.