From d8c384573e1f31ed4c8fd252b8d753a04a4df75d Mon Sep 17 00:00:00 2001 From: chikke srujan <121822803+srujanchikke@users.noreply.github.com> Date: Tue, 26 Sep 2023 12:15:37 +0530 Subject: [PATCH] refactor(connector): [Cryptopay]Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2195) --- crates/router/src/connector/cryptopay.rs | 13 ++++- .../src/connector/cryptopay/transformers.rs | 54 +++++++++++++++---- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/crates/router/src/connector/cryptopay.rs b/crates/router/src/connector/cryptopay.rs index 2ee645a27706..fd324fcc68b8 100644 --- a/crates/router/src/connector/cryptopay.rs +++ b/crates/router/src/connector/cryptopay.rs @@ -129,6 +129,10 @@ impl ConnectorCommon for Cryptopay { "cryptopay" } + fn get_currency_unit(&self) -> api::CurrencyUnit { + api::CurrencyUnit::Base + } + fn common_get_content_type(&self) -> &'static str { "application/json" } @@ -211,7 +215,14 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { - let connector_request = cryptopay::CryptopayPaymentsRequest::try_from(req)?; + let connector_router_data = cryptopay::CryptopayRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.amount, + req, + ))?; + let connector_request = + cryptopay::CryptopayPaymentsRequest::try_from(&connector_router_data)?; let cryptopay_req = types::RequestBody::log_and_get_request_body( &connector_request, Encode::::encode_to_string_of_json, diff --git a/crates/router/src/connector/cryptopay/transformers.rs b/crates/router/src/connector/cryptopay/transformers.rs index d360fbcca32e..a49380cb3656 100644 --- a/crates/router/src/connector/cryptopay/transformers.rs +++ b/crates/router/src/connector/cryptopay/transformers.rs @@ -9,6 +9,37 @@ use crate::{ types::{self, api, storage::enums}, }; +#[derive(Debug, Serialize)] +pub struct CryptopayRouterData { + pub amount: String, + pub router_data: T, +} + +impl + TryFrom<( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + )> for CryptopayRouterData +{ + type Error = error_stack::Report; + fn try_from( + (currency_unit, currency, amount, item): ( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + ), + ) -> Result { + let amount = utils::get_amount_as_string(currency_unit, amount, currency)?; + Ok(Self { + amount, + router_data: item, + }) + } +} + #[derive(Default, Debug, Serialize)] pub struct CryptopayPaymentsRequest { price_amount: String, @@ -19,22 +50,23 @@ pub struct CryptopayPaymentsRequest { custom_id: String, } -impl TryFrom<&types::PaymentsAuthorizeRouterData> for CryptopayPaymentsRequest { +impl TryFrom<&CryptopayRouterData<&types::PaymentsAuthorizeRouterData>> + for CryptopayPaymentsRequest +{ type Error = error_stack::Report; - fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result { - let cryptopay_request = match item.request.payment_method_data { + fn try_from( + item: &CryptopayRouterData<&types::PaymentsAuthorizeRouterData>, + ) -> Result { + let cryptopay_request = match item.router_data.request.payment_method_data { api::PaymentMethodData::Crypto(ref cryptodata) => { let pay_currency = cryptodata.get_pay_currency()?; Ok(Self { - price_amount: utils::to_currency_base_unit( - item.request.amount, - item.request.currency, - )?, - price_currency: item.request.currency, + price_amount: item.amount.to_owned(), + price_currency: item.router_data.request.currency, pay_currency, - success_redirect_url: item.clone().request.router_return_url, - unsuccess_redirect_url: item.clone().request.router_return_url, - custom_id: item.connector_request_reference_id.clone(), + success_redirect_url: item.router_data.request.router_return_url.clone(), + unsuccess_redirect_url: item.router_data.request.router_return_url.clone(), + custom_id: item.router_data.connector_request_reference_id.clone(), }) } _ => Err(errors::ConnectorError::NotImplemented(