Skip to content

Commit

Permalink
refactor(connector): [Authorizedotnet] Enhance currency Mapping with …
Browse files Browse the repository at this point in the history
…ConnectorCurrencyCommon Trait (#2570)

Signed-off-by: Azanul <[email protected]>
  • Loading branch information
Azanul authored Oct 13, 2023
1 parent 9f03a41 commit d401975
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 46 deletions.
50 changes: 45 additions & 5 deletions crates/router/src/connector/authorizedotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl ConnectorCommon for Authorizedotnet {
"authorizedotnet"
}

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

fn common_get_content_type(&self) -> &'static str {
"application/json"
}
Expand Down Expand Up @@ -142,7 +146,14 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
&self,
req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = authorizedotnet::CancelOrCaptureTransactionRequest::try_from(req)?;
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount_to_capture,
req,
))?;
let connector_req =
authorizedotnet::CancelOrCaptureTransactionRequest::try_from(&connector_router_data)?;

let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
&connector_req,
Expand Down Expand Up @@ -315,7 +326,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = authorizedotnet::CreateTransactionRequest::try_from(req)?;
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_req =
authorizedotnet::CreateTransactionRequest::try_from(&connector_router_data)?;

let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
&connector_req,
Expand Down Expand Up @@ -496,7 +514,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = authorizedotnet::CreateRefundRequest::try_from(req)?;
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_req = authorizedotnet::CreateRefundRequest::try_from(&connector_router_data)?;

let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
&connector_req,
Expand Down Expand Up @@ -583,7 +607,15 @@ impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponse
&self,
req: &types::RefundsRouterData<api::RSync>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = authorizedotnet::AuthorizedotnetCreateSyncRequest::try_from(req)?;
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_req =
authorizedotnet::AuthorizedotnetCreateSyncRequest::try_from(&connector_router_data)?;

let sync_request = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<authorizedotnet::AuthorizedotnetCreateSyncRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -670,7 +702,15 @@ impl
&self,
req: &types::PaymentsCompleteAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = authorizedotnet::PaypalConfirmRequest::try_from(req)?;
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_req =
authorizedotnet::PaypalConfirmRequest::try_from(&connector_router_data)?;

let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<authorizedotnet::PaypalConfirmRequest>::encode_to_string_of_json,
Expand Down
144 changes: 103 additions & 41 deletions crates/router/src/connector/authorizedotnet/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ pub enum TransactionType {
#[serde(rename = "authCaptureContinueTransaction")]
ContinueCapture,
}

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

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

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthorizedotnetAuthType {
Expand Down Expand Up @@ -99,7 +131,7 @@ pub enum WalletMethod {
}

fn get_pm_and_subsequent_auth_detail(
item: &types::PaymentsAuthorizeRouterData,
item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<
(
PaymentDetails,
Expand All @@ -109,6 +141,7 @@ fn get_pm_and_subsequent_auth_detail(
error_stack::Report<errors::ConnectorError>,
> {
match item
.router_data
.request
.mandate_id
.to_owned()
Expand All @@ -124,7 +157,7 @@ fn get_pm_and_subsequent_auth_detail(
original_network_trans_id,
reason: Reason::Resubmission,
});
match item.request.payment_method_data {
match item.router_data.request.payment_method_data {
api::PaymentMethodData::Card(ref ccard) => {
let payment_details = PaymentDetails::CreditCard(CreditCardDetails {
card_number: (*ccard.card_number).clone(),
Expand All @@ -134,12 +167,12 @@ fn get_pm_and_subsequent_auth_detail(
Ok((payment_details, processing_options, subseuent_auth_info))
}
_ => Err(errors::ConnectorError::NotSupported {
message: format!("{:?}", item.request.payment_method_data),
message: format!("{:?}", item.router_data.request.payment_method_data),
connector: "AuthorizeDotNet",
})?,
}
}
_ => match item.request.payment_method_data {
_ => match item.router_data.request.payment_method_data {
api::PaymentMethodData::Card(ref ccard) => {
Ok((
PaymentDetails::CreditCard(CreditCardDetails {
Expand All @@ -155,12 +188,15 @@ fn get_pm_and_subsequent_auth_detail(
))
}
api::PaymentMethodData::Wallet(ref wallet_data) => Ok((
get_wallet_data(wallet_data, &item.request.complete_authorize_url)?,
get_wallet_data(
wallet_data,
&item.router_data.request.complete_authorize_url,
)?,
None,
None,
)),
_ => Err(errors::ConnectorError::NotSupported {
message: format!("{:?}", item.request.payment_method_data),
message: format!("{:?}", item.router_data.request.payment_method_data),
connector: "AuthorizeDotNet",
})?,
},
Expand Down Expand Up @@ -263,26 +299,34 @@ impl From<enums::CaptureMethod> for AuthorizationType {
}
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for CreateTransactionRequest {
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>>
for CreateTransactionRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let (payment_details, processing_options, subsequent_auth_information) =
get_pm_and_subsequent_auth_detail(item)?;
let authorization_indicator_type =
item.request.capture_method.map(|c| AuthorizationIndicator {
authorization_indicator: c.into(),
});
item.router_data
.request
.capture_method
.map(|c| AuthorizationIndicator {
authorization_indicator: c.into(),
});
let transaction_request = TransactionRequest {
transaction_type: TransactionType::from(item.request.capture_method),
amount: utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?,
transaction_type: TransactionType::from(item.router_data.request.capture_method),
amount: item.amount,
payment: payment_details,
currency_code: item.request.currency.to_string(),
currency_code: item.router_data.request.currency.to_string(),
processing_options,
subsequent_auth_information,
authorization_indicator_type,
};

let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
let merchant_authentication =
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;

Ok(Self {
create_transaction_request: AuthorizedotnetPaymentsRequest {
Expand Down Expand Up @@ -313,19 +357,25 @@ impl TryFrom<&types::PaymentsCancelRouterData> for CancelOrCaptureTransactionReq
}
}

impl TryFrom<&types::PaymentsCaptureRouterData> for CancelOrCaptureTransactionRequest {
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>>
for CancelOrCaptureTransactionRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>,
) -> Result<Self, Self::Error> {
let transaction_request = TransactionVoidOrCaptureRequest {
amount: Some(utils::to_currency_base_unit_asf64(
item.request.amount_to_capture,
item.request.currency,
)?),
amount: Some(item.amount),
transaction_type: TransactionType::Capture,
ref_trans_id: item.request.connector_transaction_id.to_string(),
ref_trans_id: item
.router_data
.request
.connector_transaction_id
.to_string(),
};

let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
let merchant_authentication =
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;

Ok(Self {
create_transaction_request: AuthorizedotnetPaymentCancelOrCaptureRequest {
Expand Down Expand Up @@ -648,10 +698,13 @@ pub struct CreateRefundRequest {
create_transaction_request: AuthorizedotnetRefundRequest,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for CreateRefundRequest {
impl<F> TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData<F>>> for CreateRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
fn try_from(
item: &AuthorizedotnetRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
let payment_details = item
.router_data
.request
.connector_metadata
.as_ref()
Expand All @@ -661,21 +714,19 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for CreateRefundRequest {
})?
.clone();

let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
let merchant_authentication =
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;

let transaction_request = RefundTransactionRequest {
transaction_type: TransactionType::Refund,
amount: utils::to_currency_base_unit_asf64(
item.request.refund_amount,
item.request.currency,
)?,
amount: item.amount,
payment: payment_details
.parse_value("PaymentDetails")
.change_context(errors::ConnectorError::MissingRequiredField {
field_name: "payment_details",
})?,
currency_code: item.request.currency.to_string(),
reference_transaction_id: item.request.connector_transaction_id.clone(),
currency_code: item.router_data.request.currency.to_string(),
reference_transaction_id: item.router_data.request.connector_transaction_id.clone(),
};

Ok(Self {
Expand Down Expand Up @@ -750,12 +801,17 @@ pub struct AuthorizedotnetCreateSyncRequest {
get_transaction_details_request: TransactionDetails,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for AuthorizedotnetCreateSyncRequest {
impl<F> TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData<F>>>
for AuthorizedotnetCreateSyncRequest
{
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
let transaction_id = item.request.get_connector_refund_id()?;
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
fn try_from(
item: &AuthorizedotnetRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
let transaction_id = item.router_data.request.get_connector_refund_id()?;
let merchant_authentication =
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;

let payload = Self {
get_transaction_details_request: TransactionDetails {
Expand Down Expand Up @@ -1131,10 +1187,15 @@ pub struct PaypalQueryParams {
payer_id: String,
}

impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmRequest {
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>>
for PaypalConfirmRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsCompleteAuthorizeRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let params = item
.router_data
.request
.redirect_response
.as_ref()
Expand All @@ -1146,7 +1207,7 @@ impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmReque
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?
.payer_id,
);
let transaction_type = match item.request.capture_method {
let transaction_type = match item.router_data.request.capture_method {
Some(enums::CaptureMethod::Manual) => TransactionType::ContinueAuthorization,
_ => TransactionType::ContinueCapture,
};
Expand All @@ -1155,10 +1216,11 @@ impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmReque
payment: PaypalPaymentConfirm {
pay_pal: Paypal { payer_id },
},
ref_trans_id: item.request.connector_transaction_id.clone(),
ref_trans_id: item.router_data.request.connector_transaction_id.clone(),
};

let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
let merchant_authentication =
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;

Ok(Self {
create_transaction_request: PaypalConfirmTransactionRequest {
Expand Down

0 comments on commit d401975

Please sign in to comment.