Skip to content

Commit

Permalink
refactor(connector): [Cryptopay]Enhance currency Mapping with Connect…
Browse files Browse the repository at this point in the history
…orCurrencyCommon Trait (#2195)
  • Loading branch information
srujanchikke authored Sep 26, 2023
1 parent 2e97869 commit d8c3845
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
13 changes: 12 additions & 1 deletion crates/router/src/connector/cryptopay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -211,7 +215,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, 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::<cryptopay::CryptopayPaymentsRequest>::encode_to_string_of_json,
Expand Down
54 changes: 43 additions & 11 deletions crates/router/src/connector/cryptopay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ use crate::{
types::{self, api, storage::enums},
};

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

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for CryptopayRouterData<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,
})
}
}

#[derive(Default, Debug, Serialize)]
pub struct CryptopayPaymentsRequest {
price_amount: String,
Expand All @@ -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<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
let cryptopay_request = match item.request.payment_method_data {
fn try_from(
item: &CryptopayRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
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(
Expand Down

0 comments on commit d8c3845

Please sign in to comment.