Skip to content

Commit

Permalink
Merge branch 'main' into noon_issue_2237
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarDevAchar authored Nov 5, 2023
2 parents 95a61a6 + 1b45a30 commit 9ebd1ac
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
28 changes: 25 additions & 3 deletions crates/router/src/connector/nmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl ConnectorCommon for Nmi {
"nmi"
}

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

fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str {
connectors.nmi.base_url.as_ref()
}
Expand Down Expand Up @@ -210,7 +214,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = nmi::NmiPaymentsRequest::try_from(req)?;
let connector_router_data = nmi::NmiRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_req = nmi::NmiPaymentsRequest::try_from(&connector_router_data)?;
let nmi_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<nmi::NmiPaymentsRequest>::url_encode,
Expand Down Expand Up @@ -351,7 +361,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
&self,
req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = nmi::NmiCaptureRequest::try_from(req)?;
let connector_router_data = nmi::NmiRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount_to_capture,
req,
))?;
let connector_req = nmi::NmiCaptureRequest::try_from(&connector_router_data)?;
let nmi_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<NmiCaptureRequest>::url_encode,
Expand Down Expand Up @@ -491,7 +507,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = nmi::NmiRefundRequest::try_from(req)?;
let connector_router_data = nmi::NmiRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_req = nmi::NmiRefundRequest::try_from(&connector_router_data)?;
let nmi_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<nmi::NmiRefundRequest>::url_encode,
Expand Down
79 changes: 54 additions & 25 deletions crates/router/src/connector/nmi/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ impl TryFrom<&ConnectorAuthType> for NmiAuthType {
}
}

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

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for NmiRouterData<T>
{
type Error = 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: utils::to_currency_base_unit_asf64(amount, currency)?,
router_data,
})
}
}

#[derive(Debug, Serialize)]
pub struct NmiPaymentsRequest {
#[serde(rename = "type")]
Expand Down Expand Up @@ -77,25 +108,27 @@ pub struct ApplePayData {
applepay_payment_data: Secret<String>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for NmiPaymentsRequest {
impl TryFrom<&NmiRouterData<&types::PaymentsAuthorizeRouterData>> for NmiPaymentsRequest {
type Error = Error;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
let transaction_type = match item.request.is_auto_capture()? {
fn try_from(
item: &NmiRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let transaction_type = match item.router_data.request.is_auto_capture()? {
true => TransactionType::Sale,
false => TransactionType::Auth,
};
let auth_type: NmiAuthType = (&item.connector_auth_type).try_into()?;
let amount =
utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?;
let payment_method = PaymentMethod::try_from(&item.request.payment_method_data)?;
let auth_type: NmiAuthType = (&item.router_data.connector_auth_type).try_into()?;
let amount = item.amount;
let payment_method =
PaymentMethod::try_from(&item.router_data.request.payment_method_data)?;

Ok(Self {
transaction_type,
security_key: auth_type.api_key,
amount,
currency: item.request.currency,
currency: item.router_data.request.currency,
payment_method,
orderid: item.connector_request_reference_id.clone(),
orderid: item.router_data.connector_request_reference_id.clone(),
})
}
}
Expand Down Expand Up @@ -243,18 +276,17 @@ pub struct NmiCaptureRequest {
pub amount: Option<f64>,
}

impl TryFrom<&types::PaymentsCaptureRouterData> for NmiCaptureRequest {
impl TryFrom<&NmiRouterData<&types::PaymentsCaptureRouterData>> for NmiCaptureRequest {
type Error = Error;
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
let auth = NmiAuthType::try_from(&item.connector_auth_type)?;
fn try_from(
item: &NmiRouterData<&types::PaymentsCaptureRouterData>,
) -> Result<Self, Self::Error> {
let auth = NmiAuthType::try_from(&item.router_data.connector_auth_type)?;
Ok(Self {
transaction_type: TransactionType::Capture,
security_key: auth.api_key,
transactionid: item.request.connector_transaction_id.clone(),
amount: Some(utils::to_currency_base_unit_asf64(
item.request.amount_to_capture,
item.request.currency,
)?),
transactionid: item.router_data.request.connector_transaction_id.clone(),
amount: Some(item.amount),
})
}
}
Expand Down Expand Up @@ -577,18 +609,15 @@ pub struct NmiRefundRequest {
amount: f64,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for NmiRefundRequest {
impl<F> TryFrom<&NmiRouterData<&types::RefundsRouterData<F>>> for NmiRefundRequest {
type Error = Error;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
let auth_type: NmiAuthType = (&item.connector_auth_type).try_into()?;
fn try_from(item: &NmiRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
let auth_type: NmiAuthType = (&item.router_data.connector_auth_type).try_into()?;
Ok(Self {
transaction_type: TransactionType::Refund,
security_key: auth_type.api_key,
transactionid: item.request.connector_transaction_id.clone(),
amount: utils::to_currency_base_unit_asf64(
item.request.refund_amount,
item.request.currency,
)?,
transactionid: item.router_data.request.connector_transaction_id.clone(),
amount: item.amount,
})
}
}
Expand Down

0 comments on commit 9ebd1ac

Please sign in to comment.