From f652c21749b836ef4695c516707ae8f998550be7 Mon Sep 17 00:00:00 2001 From: Azanul Date: Wed, 11 Oct 2023 20:08:53 +0530 Subject: [PATCH 01/10] replace payment_id with connector_request_reference_id Signed-off-by: Azanul --- crates/router/src/connector/aci/transformers.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/router/src/connector/aci/transformers.rs b/crates/router/src/connector/aci/transformers.rs index 30ed9f5d8dba..7d30c80c49c9 100644 --- a/crates/router/src/connector/aci/transformers.rs +++ b/crates/router/src/connector/aci/transformers.rs @@ -203,7 +203,9 @@ impl bank_account_iban: None, billing_country: Some(country.to_owned()), merchant_customer_id: Some(Secret::new(item.get_customer_id()?)), - merchant_transaction_id: Some(Secret::new(item.payment_id.clone())), + merchant_transaction_id: Some(Secret::new( + item.connector_request_reference_id.clone(), + )), customer_email: None, })) } From 7d5c732530aff72fd5b6d4e546c4f814cc11b765 Mon Sep 17 00:00:00 2001 From: Azanul Date: Fri, 13 Oct 2023 10:27:47 +0530 Subject: [PATCH 02/10] feat(connector): [Authorizedotnet] ConnectorCommon trait Signed-off-by: Azanul --- .../router/src/connector/authorizedotnet.rs | 55 ++++++++- .../connector/authorizedotnet/transformers.rs | 113 ++++++++++++------ 2 files changed, 123 insertions(+), 45 deletions(-) diff --git a/crates/router/src/connector/authorizedotnet.rs b/crates/router/src/connector/authorizedotnet.rs index 99e87ca1edf5..bebcfcff86e0 100644 --- a/crates/router/src/connector/authorizedotnet.rs +++ b/crates/router/src/connector/authorizedotnet.rs @@ -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" } @@ -142,7 +146,13 @@ impl ConnectorIntegration CustomResult, 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.payment_amount, + req, + ))?; + let connector_req = authorizedotnet::CancelOrCaptureTransactionRequest::try_from(&connector_router_data)?; let authorizedotnet_req = types::RequestBody::log_and_get_request_body( &connector_req, @@ -315,7 +325,13 @@ impl ConnectorIntegration CustomResult, 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, @@ -407,7 +423,13 @@ impl ConnectorIntegration CustomResult, 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.unwrap(), + req.request.amount.unwrap(), + req, + ))?; + let connector_req = authorizedotnet::CancelOrCaptureTransactionRequest::try_from(&connector_router_data)?; let authorizedotnet_req = types::RequestBody::log_and_get_request_body( &connector_req, @@ -496,7 +518,13 @@ impl ConnectorIntegration, ) -> CustomResult, 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, @@ -583,7 +611,14 @@ impl ConnectorIntegration, ) -> CustomResult, 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::::encode_to_string_of_json, @@ -670,7 +705,14 @@ impl &self, req: &types::PaymentsCompleteAuthorizeRouterData, ) -> CustomResult, 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::::encode_to_string_of_json, @@ -887,3 +929,4 @@ impl services::ConnectorRedirectResponse for Authorizedotnet { } } } + diff --git a/crates/router/src/connector/authorizedotnet/transformers.rs b/crates/router/src/connector/authorizedotnet/transformers.rs index 1b7480f78e41..7c7cbe013109 100644 --- a/crates/router/src/connector/authorizedotnet/transformers.rs +++ b/crates/router/src/connector/authorizedotnet/transformers.rs @@ -31,6 +31,38 @@ pub enum TransactionType { #[serde(rename = "authCaptureContinueTransaction")] ContinueCapture, } + +#[derive(Debug, Serialize)] +pub struct AuthorizedotnetRouterData { + pub amount: String, + pub router_data: T, +} + +impl + TryFrom<( + &types::api::CurrencyUnit, + types::storage::enums::Currency, + i64, + T, + )> for AuthorizedotnetRouterData +{ + 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(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct AuthorizedotnetAuthType { @@ -99,7 +131,7 @@ pub enum WalletMethod { } fn get_pm_and_subsequent_auth_detail( - item: &types::PaymentsAuthorizeRouterData, + item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>, ) -> Result< ( PaymentDetails, @@ -109,6 +141,7 @@ fn get_pm_and_subsequent_auth_detail( error_stack::Report, > { match item + .router_data .request .mandate_id .to_owned() @@ -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(), @@ -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 { @@ -155,12 +188,12 @@ 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", })?, }, @@ -263,26 +296,26 @@ impl From for AuthorizationType { } } -impl TryFrom<&types::PaymentsAuthorizeRouterData> for CreateTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> for CreateTransactionRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result { + fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>) -> Result { 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 { + 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: utils::to_currency_base_unit_asf64(item.router_data.request.amount, item.router_data.request.currency)?, 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 { @@ -293,16 +326,16 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for CreateTransactionRequest { } } -impl TryFrom<&types::PaymentsCancelRouterData> for CancelOrCaptureTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>> for CancelOrCaptureTransactionRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsCancelRouterData) -> Result { + fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { amount: None, //amount is not required for void transaction_type: TransactionType::Void, - 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 { @@ -313,19 +346,19 @@ impl TryFrom<&types::PaymentsCancelRouterData> for CancelOrCaptureTransactionReq } } -impl TryFrom<&types::PaymentsCaptureRouterData> for CancelOrCaptureTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>> for CancelOrCaptureTransactionRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsCaptureRouterData) -> Result { + fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { amount: Some(utils::to_currency_base_unit_asf64( - item.request.amount_to_capture, - item.request.currency, + item.router_data.request.amount_to_capture, + item.router_data.request.currency, )?), 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 { @@ -648,10 +681,11 @@ pub struct CreateRefundRequest { create_transaction_request: AuthorizedotnetRefundRequest, } -impl TryFrom<&types::RefundsRouterData> for CreateRefundRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for CreateRefundRequest { type Error = error_stack::Report; - fn try_from(item: &types::RefundsRouterData) -> Result { + fn try_from(item: &AuthorizedotnetRouterData<&types::RefundsRouterData>) -> Result { let payment_details = item + .router_data .request .connector_metadata .as_ref() @@ -661,21 +695,21 @@ impl TryFrom<&types::RefundsRouterData> 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, + item.router_data.request.refund_amount, + item.router_data.request.currency, )?, 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 { @@ -750,12 +784,12 @@ pub struct AuthorizedotnetCreateSyncRequest { get_transaction_details_request: TransactionDetails, } -impl TryFrom<&types::RefundsRouterData> for AuthorizedotnetCreateSyncRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for AuthorizedotnetCreateSyncRequest { type Error = error_stack::Report; - fn try_from(item: &types::RefundsRouterData) -> Result { - 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>) -> Result { + 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 { @@ -1131,10 +1165,11 @@ pub struct PaypalQueryParams { payer_id: String, } -impl TryFrom<&types::PaymentsCompleteAuthorizeRouterData> for PaypalConfirmRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>> for PaypalConfirmRequest { type Error = error_stack::Report; - fn try_from(item: &types::PaymentsCompleteAuthorizeRouterData) -> Result { + fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>) -> Result { let params = item + .router_data .request .redirect_response .as_ref() @@ -1146,7 +1181,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, }; @@ -1155,10 +1190,10 @@ 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 { From c66d6f642a87303c80a997f3d0d33c28d0d689e0 Mon Sep 17 00:00:00 2001 From: Azanul Date: Fri, 13 Oct 2023 11:20:23 +0530 Subject: [PATCH 03/10] feat(connector): [Authorizedotnet] foramt Signed-off-by: Azanul --- .../router/src/connector/authorizedotnet.rs | 16 ++-- .../connector/authorizedotnet/transformers.rs | 95 ++++++++++++++----- 2 files changed, 80 insertions(+), 31 deletions(-) diff --git a/crates/router/src/connector/authorizedotnet.rs b/crates/router/src/connector/authorizedotnet.rs index bebcfcff86e0..2448b04c843f 100644 --- a/crates/router/src/connector/authorizedotnet.rs +++ b/crates/router/src/connector/authorizedotnet.rs @@ -152,7 +152,8 @@ impl ConnectorIntegration Ok(( - get_wallet_data(wallet_data, &item.router_data.request.complete_authorize_url)?, + get_wallet_data( + wallet_data, + &item.router_data.request.complete_authorize_url, + )?, None, None, )), @@ -296,18 +299,28 @@ impl From for AuthorizationType { } } -impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> for CreateTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> + for CreateTransactionRequest +{ type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>, + ) -> Result { let (payment_details, processing_options, subsequent_auth_information) = get_pm_and_subsequent_auth_detail(item)?; let authorization_indicator_type = - item.router_data.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.router_data.request.capture_method), - amount: utils::to_currency_base_unit_asf64(item.router_data.request.amount, item.router_data.request.currency)?, + amount: utils::to_currency_base_unit_asf64( + item.router_data.request.amount, + item.router_data.request.currency, + )?, payment: payment_details, currency_code: item.router_data.request.currency.to_string(), processing_options, @@ -315,7 +328,8 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> fo authorization_indicator_type, }; - let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; Ok(Self { create_transaction_request: AuthorizedotnetPaymentsRequest { @@ -326,16 +340,25 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> fo } } -impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>> for CancelOrCaptureTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>> + for CancelOrCaptureTransactionRequest +{ type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>, + ) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { amount: None, //amount is not required for void transaction_type: TransactionType::Void, - ref_trans_id: item.router_data.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.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; Ok(Self { create_transaction_request: AuthorizedotnetPaymentCancelOrCaptureRequest { @@ -346,19 +369,28 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>> for C } } -impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>> for CancelOrCaptureTransactionRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>> + for CancelOrCaptureTransactionRequest +{ type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>, + ) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { amount: Some(utils::to_currency_base_unit_asf64( item.router_data.request.amount_to_capture, item.router_data.request.currency, )?), transaction_type: TransactionType::Capture, - ref_trans_id: item.router_data.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.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; Ok(Self { create_transaction_request: AuthorizedotnetPaymentCancelOrCaptureRequest { @@ -683,9 +715,11 @@ pub struct CreateRefundRequest { impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for CreateRefundRequest { type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::RefundsRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::RefundsRouterData>, + ) -> Result { let payment_details = item - .router_data + .router_data .request .connector_metadata .as_ref() @@ -695,7 +729,8 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for Cr })? .clone(); - let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; let transaction_request = RefundTransactionRequest { transaction_type: TransactionType::Refund, @@ -784,12 +819,17 @@ pub struct AuthorizedotnetCreateSyncRequest { get_transaction_details_request: TransactionDetails, } -impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for AuthorizedotnetCreateSyncRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> + for AuthorizedotnetCreateSyncRequest +{ type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::RefundsRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::RefundsRouterData>, + ) -> Result { let transaction_id = item.router_data.request.get_connector_refund_id()?; - let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; let payload = Self { get_transaction_details_request: TransactionDetails { @@ -1165,9 +1205,13 @@ pub struct PaypalQueryParams { payer_id: String, } -impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>> for PaypalConfirmRequest { +impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>> + for PaypalConfirmRequest +{ type Error = error_stack::Report; - fn try_from(item: &AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>) -> Result { + fn try_from( + item: &AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterData>, + ) -> Result { let params = item .router_data .request @@ -1193,7 +1237,8 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCompleteAuthorizeRouterD ref_trans_id: item.router_data.request.connector_transaction_id.clone(), }; - let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; + let merchant_authentication = + AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; Ok(Self { create_transaction_request: PaypalConfirmTransactionRequest { From 6eaaea89519b27c5d5b9acc570984d73cab235dd Mon Sep 17 00:00:00 2001 From: Azanul Date: Fri, 13 Oct 2023 14:38:04 +0530 Subject: [PATCH 04/10] feat(connector): [Authorizedotnet] review comments Signed-off-by: Azanul --- .../router/src/connector/authorizedotnet.rs | 11 ++---- .../connector/authorizedotnet/transformers.rs | 36 +++++-------------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/crates/router/src/connector/authorizedotnet.rs b/crates/router/src/connector/authorizedotnet.rs index 2448b04c843f..7ff2098344ee 100644 --- a/crates/router/src/connector/authorizedotnet.rs +++ b/crates/router/src/connector/authorizedotnet.rs @@ -149,7 +149,7 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { - let connector_router_data = authorizedotnet::AuthorizedotnetRouterData::try_from(( - &self.get_currency_unit(), - req.request.currency.unwrap(), - req.request.amount.unwrap(), - req, - ))?; - let connector_req = - authorizedotnet::CancelOrCaptureTransactionRequest::try_from(&connector_router_data)?; + let connector_req = authorizedotnet::CancelOrCaptureTransactionRequest::try_from(req)?; let authorizedotnet_req = types::RequestBody::log_and_get_request_body( &connector_req, diff --git a/crates/router/src/connector/authorizedotnet/transformers.rs b/crates/router/src/connector/authorizedotnet/transformers.rs index 4147f00a278e..cd7e070b16fa 100644 --- a/crates/router/src/connector/authorizedotnet/transformers.rs +++ b/crates/router/src/connector/authorizedotnet/transformers.rs @@ -34,7 +34,7 @@ pub enum TransactionType { #[derive(Debug, Serialize)] pub struct AuthorizedotnetRouterData { - pub amount: String, + pub amount: f64, pub router_data: T, } @@ -55,7 +55,7 @@ impl T, ), ) -> Result { - let amount = utils::get_amount_as_string(currency_unit, amount, currency)?; + let amount = utils::get_amount_as_f64(currency_unit, amount, currency)?; Ok(Self { amount, router_data: item, @@ -317,10 +317,7 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> }); let transaction_request = TransactionRequest { transaction_type: TransactionType::from(item.router_data.request.capture_method), - amount: utils::to_currency_base_unit_asf64( - item.router_data.request.amount, - item.router_data.request.currency, - )?, + amount: item.amount, payment: payment_details, currency_code: item.router_data.request.currency.to_string(), processing_options, @@ -340,25 +337,16 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsAuthorizeRouterData>> } } -impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>> - for CancelOrCaptureTransactionRequest -{ +impl TryFrom<&types::PaymentsCancelRouterData> for CancelOrCaptureTransactionRequest { type Error = error_stack::Report; - fn try_from( - item: &AuthorizedotnetRouterData<&types::PaymentsCancelRouterData>, - ) -> Result { + fn try_from(item: &types::PaymentsCancelRouterData) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { amount: None, //amount is not required for void transaction_type: TransactionType::Void, - ref_trans_id: item - .router_data - .request - .connector_transaction_id - .to_string(), + ref_trans_id: item.request.connector_transaction_id.to_string(), }; - let merchant_authentication = - AuthorizedotnetAuthType::try_from(&item.router_data.connector_auth_type)?; + let merchant_authentication = AuthorizedotnetAuthType::try_from(&item.connector_auth_type)?; Ok(Self { create_transaction_request: AuthorizedotnetPaymentCancelOrCaptureRequest { @@ -377,10 +365,7 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>> item: &AuthorizedotnetRouterData<&types::PaymentsCaptureRouterData>, ) -> Result { let transaction_request = TransactionVoidOrCaptureRequest { - amount: Some(utils::to_currency_base_unit_asf64( - item.router_data.request.amount_to_capture, - item.router_data.request.currency, - )?), + amount: Some(item.amount), transaction_type: TransactionType::Capture, ref_trans_id: item .router_data @@ -734,10 +719,7 @@ impl TryFrom<&AuthorizedotnetRouterData<&types::RefundsRouterData>> for Cr let transaction_request = RefundTransactionRequest { transaction_type: TransactionType::Refund, - amount: utils::to_currency_base_unit_asf64( - item.router_data.request.refund_amount, - item.router_data.request.currency, - )?, + amount: item.amount, payment: payment_details .parse_value("PaymentDetails") .change_context(errors::ConnectorError::MissingRequiredField { From 4d71f2e252b83eeea8d5ace6c6e6985f33a754f1 Mon Sep 17 00:00:00 2001 From: Azanul Date: Sun, 22 Oct 2023 19:53:06 +0530 Subject: [PATCH 05/10] feat: Merchant account & key store db transaction Signed-off-by: Azanul --- crates/router/src/core/admin.rs | 4 ---- crates/router/src/db/merchant_account.rs | 23 ++++++++++++++++++++--- crates/storage_impl/src/errors.rs | 9 +++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs index 7da0b73f74db..a32094741d76 100644 --- a/crates/router/src/core/admin.rs +++ b/crates/router/src/core/admin.rs @@ -112,10 +112,6 @@ pub async fn create_merchant_account( .payment_response_hash_key .or(Some(generate_cryptographically_secure_random_string(64))); - db.insert_merchant_key_store(key_store.clone(), &master_key.to_vec().into()) - .await - .to_duplicate_response(errors::ApiErrorResponse::DuplicateMerchantAccount)?; - let parent_merchant_id = get_parent_merchant( db, req.sub_merchants_enabled, diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index 726ae55f91eb..99c5c27588ad 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -1,3 +1,4 @@ +use async_bb8_diesel::AsyncConnection; use common_utils::ext_traits::AsyncExt; use error_stack::{IntoReport, ResultExt}; #[cfg(feature = "accounts_cache")] @@ -69,12 +70,28 @@ impl MerchantAccountInterface for Store { merchant_key_store: &domain::MerchantKeyStore, ) -> CustomResult { let conn = connection::pg_connection_write(self).await?; - merchant_account + + let key_store = merchant_key_store + .clone() .construct_new() .await - .change_context(errors::StorageError::EncryptionError)? - .insert(&conn) + .change_context(errors::StorageError::EncryptionError)?; + + let account = merchant_account + .construct_new() .await + .change_context(errors::StorageError::EncryptionError)?; + + let final_account = conn + .transaction_async(|e| async move { + let _ = key_store.insert(&e).await; + account.insert(&e).await.map_err(|_| { + errors::StorageError::DatabaseConnectionError + }) + }) + .await; + + final_account .map_err(Into::into) .into_report()? .convert(merchant_key_store.key.get_inner()) diff --git a/crates/storage_impl/src/errors.rs b/crates/storage_impl/src/errors.rs index bc68986cb8ea..8fbdebfa426f 100644 --- a/crates/storage_impl/src/errors.rs +++ b/crates/storage_impl/src/errors.rs @@ -65,6 +65,8 @@ pub enum StorageError { DecryptionError, #[error("RedisError: {0:?}")] RedisError(error_stack::Report), + #[error("DB transaction failure")] + TransactionFailed(#[from] async_bb8_diesel::ConnectionError), } impl ErrorSwitch for StorageError { @@ -124,6 +126,7 @@ impl Into for &StorageError { RedisError::JsonDeserializationFailed => DataStorageError::DeserializationFailed, i => DataStorageError::RedisError(format!("{:?}", i)), }, + StorageError::TransactionFailed(i) => DataStorageError::DatabaseError(i.to_string()), } } } @@ -140,6 +143,12 @@ impl From> for StorageError { } } +impl From for StorageError { + fn from(err: diesel::result::Error) -> Self { + StorageError::TransactionFailed(err.into()) + } +} + impl StorageError { pub fn is_db_not_found(&self) -> bool { match self { From db39f53b628c1f21a0297e0c704b1afa8ea5571e Mon Sep 17 00:00:00 2001 From: Azanul Date: Mon, 23 Oct 2023 00:11:11 +0530 Subject: [PATCH 06/10] fix: fmt Signed-off-by: Azanul --- crates/router/src/db/merchant_account.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index 82f47d69a898..69670ea533b4 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -91,9 +91,10 @@ impl MerchantAccountInterface for Store { let final_account = conn .transaction_async(|e| async move { let _ = key_store.insert(&e).await; - account.insert(&e).await.map_err(|_| { - errors::StorageError::DatabaseConnectionError - }) + account + .insert(&e) + .await + .map_err(|_| errors::StorageError::DatabaseConnectionError) }) .await; From 0a777a9e6e608ee3c069ea90dccf85b4c483c3f2 Mon Sep 17 00:00:00 2001 From: Azanul Date: Wed, 25 Oct 2023 20:01:54 +0530 Subject: [PATCH 07/10] fix: review comments Signed-off-by: Azanul --- crates/diesel_models/src/errors.rs | 8 ++++ crates/router/src/db/merchant_account.rs | 53 ++++++++++++------------ crates/storage_impl/src/errors.rs | 12 ++---- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/crates/diesel_models/src/errors.rs b/crates/diesel_models/src/errors.rs index 0a8422131ae2..30e4273fdb57 100644 --- a/crates/diesel_models/src/errors.rs +++ b/crates/diesel_models/src/errors.rs @@ -10,7 +10,15 @@ pub enum DatabaseError { NoFieldsToUpdate, #[error("An error occurred when generating typed SQL query")] QueryGenerationFailed, + #[error("DB transaction failure")] + TransactionFailed(String), // InsertFailed, #[error("An unknown error occurred")] Others, } + +impl From for DatabaseError { + fn from(err: diesel::result::Error) -> Self { + Self::TransactionFailed(err.to_string()) + } +} diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index 69670ea533b4..e674fe37435f 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -73,37 +73,36 @@ impl MerchantAccountInterface for Store { async fn insert_merchant( &self, merchant_account: domain::MerchantAccount, - merchant_key_store: &domain::MerchantKeyStore, + merchant_key_store: domain::MerchantKeyStore, ) -> CustomResult { let conn = connection::pg_connection_write(self).await?; - let key_store = merchant_key_store - .clone() - .construct_new() - .await - .change_context(errors::StorageError::EncryptionError)?; - - let account = merchant_account - .construct_new() - .await - .change_context(errors::StorageError::EncryptionError)?; - - let final_account = conn - .transaction_async(|e| async move { - let _ = key_store.insert(&e).await; - account - .insert(&e) - .await - .map_err(|_| errors::StorageError::DatabaseConnectionError) - }) - .await; + conn.transaction_async(|e| async move { + let key_store = merchant_key_store + .construct_new() + .await + .change_context(errors::StorageError::EncryptionError)? + .insert(&e) + .await + .map_err(Into::into) + .into_report()? + .convert(merchant_key_store.key.get_inner()) + .await + .change_context(errors::StorageError::DecryptionError); - final_account - .map_err(Into::into) - .into_report()? - .convert(merchant_key_store.key.get_inner()) - .await - .change_context(errors::StorageError::DecryptionError) + merchant_account + .construct_new() + .await + .change_context(errors::StorageError::EncryptionError)? + .insert(&e) + .await + .map_err(Into::into) + .into_report()? + .convert(merchant_key_store.key.get_inner()) + .await + .change_context(errors::StorageError::DecryptionError) + }) + .await } async fn find_merchant_account_by_merchant_id( diff --git a/crates/storage_impl/src/errors.rs b/crates/storage_impl/src/errors.rs index 8fbdebfa426f..c8abccdf4b62 100644 --- a/crates/storage_impl/src/errors.rs +++ b/crates/storage_impl/src/errors.rs @@ -65,8 +65,6 @@ pub enum StorageError { DecryptionError, #[error("RedisError: {0:?}")] RedisError(error_stack::Report), - #[error("DB transaction failure")] - TransactionFailed(#[from] async_bb8_diesel::ConnectionError), } impl ErrorSwitch for StorageError { @@ -100,6 +98,9 @@ impl Into for &StorageError { storage_errors::DatabaseError::QueryGenerationFailed => { DataStorageError::DatabaseError("Query generation failed".to_string()) } + storage_errors::DatabaseError::TransactionFailed(e) => { + DataStorageError::DatabaseError(e.to_string()) + } storage_errors::DatabaseError::Others => { DataStorageError::DatabaseError("Unknown database error".to_string()) } @@ -126,7 +127,6 @@ impl Into for &StorageError { RedisError::JsonDeserializationFailed => DataStorageError::DeserializationFailed, i => DataStorageError::RedisError(format!("{:?}", i)), }, - StorageError::TransactionFailed(i) => DataStorageError::DatabaseError(i.to_string()), } } } @@ -143,12 +143,6 @@ impl From> for StorageError { } } -impl From for StorageError { - fn from(err: diesel::result::Error) -> Self { - StorageError::TransactionFailed(err.into()) - } -} - impl StorageError { pub fn is_db_not_found(&self) -> bool { match self { From 0db8eda65417359fb73e7c64e8538cf64698ae68 Mon Sep 17 00:00:00 2001 From: Azanul Date: Thu, 26 Oct 2023 05:30:41 +0530 Subject: [PATCH 08/10] fix: review comments Signed-off-by: Azanul --- crates/router/src/db/merchant_account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index e674fe37435f..cc457828fe07 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -86,7 +86,7 @@ impl MerchantAccountInterface for Store { .await .map_err(Into::into) .into_report()? - .convert(merchant_key_store.key.get_inner()) + .convert(self.get_master_key()) .await .change_context(errors::StorageError::DecryptionError); From b3a12ae39464c8f2c6c945722196bfd8348aac73 Mon Sep 17 00:00:00 2001 From: Azanul Date: Fri, 27 Oct 2023 01:04:49 +0530 Subject: [PATCH 09/10] fix: CI fails Signed-off-by: Azanul --- crates/router/src/db/merchant_account.rs | 2 +- crates/storage_impl/src/lib.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index cc457828fe07..5f533eabc9f6 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -86,7 +86,7 @@ impl MerchantAccountInterface for Store { .await .map_err(Into::into) .into_report()? - .convert(self.get_master_key()) + .convert(&self.get_master_key().to_vec().into()) .await .change_context(errors::StorageError::DecryptionError); diff --git a/crates/storage_impl/src/lib.rs b/crates/storage_impl/src/lib.rs index dd8d71fc701e..3dcab8d4e8da 100644 --- a/crates/storage_impl/src/lib.rs +++ b/crates/storage_impl/src/lib.rs @@ -245,6 +245,9 @@ pub(crate) fn diesel_error_to_data_error( diesel_models::errors::DatabaseError::QueryGenerationFailed => { StorageError::DatabaseError("Query generation failed".to_string()) } + store::errors::DatabaseError::TransactionFailed(_) => { + StorageError::DatabaseError("DB transaction failed".to_string()) + } diesel_models::errors::DatabaseError::Others => { StorageError::DatabaseError("Others".to_string()) } From e46f8b5bebd1134a2f935b9e99eee6e3c058784c Mon Sep 17 00:00:00 2001 From: Azanul Date: Fri, 27 Oct 2023 05:03:58 +0530 Subject: [PATCH 10/10] fix: use err Signed-off-by: Azanul --- crates/storage_impl/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/storage_impl/src/lib.rs b/crates/storage_impl/src/lib.rs index 3dcab8d4e8da..406c6ee9ab8b 100644 --- a/crates/storage_impl/src/lib.rs +++ b/crates/storage_impl/src/lib.rs @@ -245,8 +245,8 @@ pub(crate) fn diesel_error_to_data_error( diesel_models::errors::DatabaseError::QueryGenerationFailed => { StorageError::DatabaseError("Query generation failed".to_string()) } - store::errors::DatabaseError::TransactionFailed(_) => { - StorageError::DatabaseError("DB transaction failed".to_string()) + store::errors::DatabaseError::TransactionFailed(e) => { + StorageError::DatabaseError(e.to_string()) } diesel_models::errors::DatabaseError::Others => { StorageError::DatabaseError("Others".to_string())