Skip to content

Commit d401975

Browse files
authored
refactor(connector): [Authorizedotnet] Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2570)
Signed-off-by: Azanul <[email protected]>
1 parent 9f03a41 commit d401975

File tree

2 files changed

+148
-46
lines changed

2 files changed

+148
-46
lines changed

crates/router/src/connector/authorizedotnet.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl ConnectorCommon for Authorizedotnet {
4949
"authorizedotnet"
5050
}
5151

52+
fn get_currency_unit(&self) -> api::CurrencyUnit {
53+
api::CurrencyUnit::Base
54+
}
55+
5256
fn common_get_content_type(&self) -> &'static str {
5357
"application/json"
5458
}
@@ -142,7 +146,14 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
142146
&self,
143147
req: &types::PaymentsCaptureRouterData,
144148
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
145-
let connector_req = authorizedotnet::CancelOrCaptureTransactionRequest::try_from(req)?;
149+
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
150+
&self.get_currency_unit(),
151+
req.request.currency,
152+
req.request.amount_to_capture,
153+
req,
154+
))?;
155+
let connector_req =
156+
authorizedotnet::CancelOrCaptureTransactionRequest::try_from(&connector_router_data)?;
146157

147158
let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
148159
&connector_req,
@@ -315,7 +326,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
315326
&self,
316327
req: &types::PaymentsAuthorizeRouterData,
317328
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
318-
let connector_req = authorizedotnet::CreateTransactionRequest::try_from(req)?;
329+
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
330+
&self.get_currency_unit(),
331+
req.request.currency,
332+
req.request.amount,
333+
req,
334+
))?;
335+
let connector_req =
336+
authorizedotnet::CreateTransactionRequest::try_from(&connector_router_data)?;
319337

320338
let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
321339
&connector_req,
@@ -496,7 +514,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
496514
&self,
497515
req: &types::RefundsRouterData<api::Execute>,
498516
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
499-
let connector_req = authorizedotnet::CreateRefundRequest::try_from(req)?;
517+
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
518+
&self.get_currency_unit(),
519+
req.request.currency,
520+
req.request.refund_amount,
521+
req,
522+
))?;
523+
let connector_req = authorizedotnet::CreateRefundRequest::try_from(&connector_router_data)?;
500524

501525
let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
502526
&connector_req,
@@ -583,7 +607,15 @@ impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponse
583607
&self,
584608
req: &types::RefundsRouterData<api::RSync>,
585609
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
586-
let connector_req = authorizedotnet::AuthorizedotnetCreateSyncRequest::try_from(req)?;
610+
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
611+
&self.get_currency_unit(),
612+
req.request.currency,
613+
req.request.refund_amount,
614+
req,
615+
))?;
616+
let connector_req =
617+
authorizedotnet::AuthorizedotnetCreateSyncRequest::try_from(&connector_router_data)?;
618+
587619
let sync_request = types::RequestBody::log_and_get_request_body(
588620
&connector_req,
589621
utils::Encode::<authorizedotnet::AuthorizedotnetCreateSyncRequest>::encode_to_string_of_json,
@@ -670,7 +702,15 @@ impl
670702
&self,
671703
req: &types::PaymentsCompleteAuthorizeRouterData,
672704
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
673-
let connector_req = authorizedotnet::PaypalConfirmRequest::try_from(req)?;
705+
let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from((
706+
&self.get_currency_unit(),
707+
req.request.currency,
708+
req.request.amount,
709+
req,
710+
))?;
711+
let connector_req =
712+
authorizedotnet::PaypalConfirmRequest::try_from(&connector_router_data)?;
713+
674714
let authorizedotnet_req = types::RequestBody::log_and_get_request_body(
675715
&connector_req,
676716
utils::Encode::<authorizedotnet::PaypalConfirmRequest>::encode_to_string_of_json,

crates/router/src/connector/authorizedotnet/transformers.rs

Lines changed: 103 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ pub enum TransactionType {
3131
#[serde(rename = "authCaptureContinueTransaction")]
3232
ContinueCapture,
3333
}
34+
35+
#[derive(Debug, Serialize)]
36+
pub struct AuthorizedotnetRouterData<T> {
37+
pub amount: f64,
38+
pub router_data: T,
39+
}
40+
41+
impl<T>
42+
TryFrom<(
43+
&types::api::CurrencyUnit,
44+
types::storage::enums::Currency,
45+
i64,
46+
T,
47+
)> for AuthorizedotnetRouterData<T>
48+
{
49+
type Error = error_stack::Report<errors::ConnectorError>;
50+
fn try_from(
51+
(currency_unit, currency, amount, item): (
52+
&types::api::CurrencyUnit,
53+
types::storage::enums::Currency,
54+
i64,
55+
T,
56+
),
57+
) -> Result<Self, Self::Error> {
58+
let amount = utils::get_amount_as_f64(currency_unit, amount, currency)?;
59+
Ok(Self {
60+
amount,
61+
router_data: item,
62+
})
63+
}
64+
}
65+
3466
#[derive(Debug, Serialize)]
3567
#[serde(rename_all = "camelCase")]
3668
pub struct AuthorizedotnetAuthType {
@@ -99,7 +131,7 @@ pub enum WalletMethod {
99131
}
100132

101133
fn get_pm_and_subsequent_auth_detail(
102-
item: &types::PaymentsAuthorizeRouterData,
134+
item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>,
103135
) -> Result<
104136
(
105137
PaymentDetails,
@@ -109,6 +141,7 @@ fn get_pm_and_subsequent_auth_detail(
109141
error_stack::Report<errors::ConnectorError>,
110142
> {
111143
match item
144+
.router_data
112145
.request
113146
.mandate_id
114147
.to_owned()
@@ -124,7 +157,7 @@ fn get_pm_and_subsequent_auth_detail(
124157
original_network_trans_id,
125158
reason: Reason::Resubmission,
126159
});
127-
match item.request.payment_method_data {
160+
match item.router_data.request.payment_method_data {
128161
api::PaymentMethodData::Card(ref ccard) => {
129162
let payment_details = PaymentDetails::CreditCard(CreditCardDetails {
130163
card_number: (*ccard.card_number).clone(),
@@ -134,12 +167,12 @@ fn get_pm_and_subsequent_auth_detail(
134167
Ok((payment_details, processing_options, subseuent_auth_info))
135168
}
136169
_ => Err(errors::ConnectorError::NotSupported {
137-
message: format!("{:?}", item.request.payment_method_data),
170+
message: format!("{:?}", item.router_data.request.payment_method_data),
138171
connector: "AuthorizeDotNet",
139172
})?,
140173
}
141174
}
142-
_ => match item.request.payment_method_data {
175+
_ => match item.router_data.request.payment_method_data {
143176
api::PaymentMethodData::Card(ref ccard) => {
144177
Ok((
145178
PaymentDetails::CreditCard(CreditCardDetails {
@@ -155,12 +188,15 @@ fn get_pm_and_subsequent_auth_detail(
155188
))
156189
}
157190
api::PaymentMethodData::Wallet(ref wallet_data) => Ok((
158-
get_wallet_data(wallet_data, &item.request.complete_authorize_url)?,
191+
get_wallet_data(
192+
wallet_data,
193+
&item.router_data.request.complete_authorize_url,
194+
)?,
159195
None,
160196
None,
161197
)),
162198
_ => Err(errors::ConnectorError::NotSupported {
163-
message: format!("{:?}", item.request.payment_method_data),
199+
message: format!("{:?}", item.router_data.request.payment_method_data),
164200
connector: "AuthorizeDotNet",
165201
})?,
166202
},
@@ -263,26 +299,34 @@ impl From<enums::CaptureMethod> for AuthorizationType {
263299
}
264300
}
265301

266-
impl TryFrom<&types::PaymentsAuthorizeRouterData> for CreateTransactionRequest {
302+
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>>
303+
for CreateTransactionRequest
304+
{
267305
type Error = error_stack::Report<errors::ConnectorError>;
268-
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
306+
fn try_from(
307+
item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>,
308+
) -> Result<Self, Self::Error> {
269309
let (payment_details, processing_options, subsequent_auth_information) =
270310
get_pm_and_subsequent_auth_detail(item)?;
271311
let authorization_indicator_type =
272-
item.request.capture_method.map(|c| AuthorizationIndicator {
273-
authorization_indicator: c.into(),
274-
});
312+
item.router_data
313+
.request
314+
.capture_method
315+
.map(|c| AuthorizationIndicator {
316+
authorization_indicator: c.into(),
317+
});
275318
let transaction_request = TransactionRequest {
276-
transaction_type: TransactionType::from(item.request.capture_method),
277-
amount: utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?,
319+
transaction_type: TransactionType::from(item.router_data.request.capture_method),
320+
amount: item.amount,
278321
payment: payment_details,
279-
currency_code: item.request.currency.to_string(),
322+
currency_code: item.router_data.request.currency.to_string(),
280323
processing_options,
281324
subsequent_auth_information,
282325
authorization_indicator_type,
283326
};
284327

285-
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
328+
let merchant_authentication =
329+
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;
286330

287331
Ok(Self {
288332
create_transaction_request: AuthorizedotnetPaymentsRequest {
@@ -313,19 +357,25 @@ impl TryFrom<&types::PaymentsCancelRouterData> for CancelOrCaptureTransactionReq
313357
}
314358
}
315359

316-
impl TryFrom<&types::PaymentsCaptureRouterData> for CancelOrCaptureTransactionRequest {
360+
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>>
361+
for CancelOrCaptureTransactionRequest
362+
{
317363
type Error = error_stack::Report<errors::ConnectorError>;
318-
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
364+
fn try_from(
365+
item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>,
366+
) -> Result<Self, Self::Error> {
319367
let transaction_request = TransactionVoidOrCaptureRequest {
320-
amount: Some(utils::to_currency_base_unit_asf64(
321-
item.request.amount_to_capture,
322-
item.request.currency,
323-
)?),
368+
amount: Some(item.amount),
324369
transaction_type: TransactionType::Capture,
325-
ref_trans_id: item.request.connector_transaction_id.to_string(),
370+
ref_trans_id: item
371+
.router_data
372+
.request
373+
.connector_transaction_id
374+
.to_string(),
326375
};
327376

328-
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
377+
let merchant_authentication =
378+
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;
329379

330380
Ok(Self {
331381
create_transaction_request: AuthorizedotnetPaymentCancelOrCaptureRequest {
@@ -648,10 +698,13 @@ pub struct CreateRefundRequest {
648698
create_transaction_request: AuthorizedotnetRefundRequest,
649699
}
650700

651-
impl<F> TryFrom<&types::RefundsRouterData<F>> for CreateRefundRequest {
701+
impl<F> TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData<F>>> for CreateRefundRequest {
652702
type Error = error_stack::Report<errors::ConnectorError>;
653-
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
703+
fn try_from(
704+
item: &AuthorizedotnetRouterData<&types::RefundsRouterData<F>>,
705+
) -> Result<Self, Self::Error> {
654706
let payment_details = item
707+
.router_data
655708
.request
656709
.connector_metadata
657710
.as_ref()
@@ -661,21 +714,19 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for CreateRefundRequest {
661714
})?
662715
.clone();
663716

664-
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
717+
let merchant_authentication =
718+
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;
665719

666720
let transaction_request = RefundTransactionRequest {
667721
transaction_type: TransactionType::Refund,
668-
amount: utils::to_currency_base_unit_asf64(
669-
item.request.refund_amount,
670-
item.request.currency,
671-
)?,
722+
amount: item.amount,
672723
payment: payment_details
673724
.parse_value("PaymentDetails")
674725
.change_context(errors::ConnectorError::MissingRequiredField {
675726
field_name: "payment_details",
676727
})?,
677-
currency_code: item.request.currency.to_string(),
678-
reference_transaction_id: item.request.connector_transaction_id.clone(),
728+
currency_code: item.router_data.request.currency.to_string(),
729+
reference_transaction_id: item.router_data.request.connector_transaction_id.clone(),
679730
};
680731

681732
Ok(Self {
@@ -750,12 +801,17 @@ pub struct AuthorizedotnetCreateSyncRequest {
750801
get_transaction_details_request: TransactionDetails,
751802
}
752803

753-
impl<F> TryFrom<&types::RefundsRouterData<F>> for AuthorizedotnetCreateSyncRequest {
804+
impl<F> TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData<F>>>
805+
for AuthorizedotnetCreateSyncRequest
806+
{
754807
type Error = error_stack::Report<errors::ConnectorError>;
755808

756-
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
757-
let transaction_id = item.request.get_connector_refund_id()?;
758-
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
809+
fn try_from(
810+
item: &AuthorizedotnetRouterData<&types::RefundsRouterData<F>>,
811+
) -> Result<Self, Self::Error> {
812+
let transaction_id = item.router_data.request.get_connector_refund_id()?;
813+
let merchant_authentication =
814+
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;
759815

760816
let payload = Self {
761817
get_transaction_details_request: TransactionDetails {
@@ -1131,10 +1187,15 @@ pub struct PaypalQueryParams {
11311187
payer_id: String,
11321188
}
11331189

1134-
impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmRequest {
1190+
impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>>
1191+
for PaypalConfirmRequest
1192+
{
11351193
type Error = error_stack::Report<errors::ConnectorError>;
1136-
fn try_from(item: &types::PaymentsCompleteAuthorizeRouterData) -> Result<Self, Self::Error> {
1194+
fn try_from(
1195+
item: &AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>,
1196+
) -> Result<Self, Self::Error> {
11371197
let params = item
1198+
.router_data
11381199
.request
11391200
.redirect_response
11401201
.as_ref()
@@ -1146,7 +1207,7 @@ impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmReque
11461207
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?
11471208
.payer_id,
11481209
);
1149-
let transaction_type = match item.request.capture_method {
1210+
let transaction_type = match item.router_data.request.capture_method {
11501211
Some(enums::CaptureMethod::Manual) => TransactionType::ContinueAuthorization,
11511212
_ => TransactionType::ContinueCapture,
11521213
};
@@ -1155,10 +1216,11 @@ impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmReque
11551216
payment: PaypalPaymentConfirm {
11561217
pay_pal: Paypal { payer_id },
11571218
},
1158-
ref_trans_id: item.request.connector_transaction_id.clone(),
1219+
ref_trans_id: item.router_data.request.connector_transaction_id.clone(),
11591220
};
11601221

1161-
let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?;
1222+
let merchant_authentication =
1223+
AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?;
11621224

11631225
Ok(Self {
11641226
create_transaction_request: PaypalConfirmTransactionRequest {

0 commit comments

Comments
 (0)