From e45e33c89702a5fc7a687dd893a9c8388181eabb Mon Sep 17 00:00:00 2001 From: Nishant Joshi Date: Thu, 1 Dec 2022 13:58:26 +0530 Subject: [PATCH] refactor(RouterData): move amount information inside `request` in `RouterData` (#36) Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com> --- crates/router/src/connector/aci/transformers.rs | 6 +++--- crates/router/src/connector/adyen/transformers.rs | 4 ++-- .../router/src/connector/authorizedotnet/transformers.rs | 6 +++--- crates/router/src/connector/checkout/transformers.rs | 4 ++-- crates/router/src/connector/stripe/transformers.rs | 4 ++-- crates/router/src/core/payments/transformers.rs | 5 ++--- crates/router/src/core/refunds.rs | 4 ++-- crates/router/src/core/utils.rs | 4 ++-- crates/router/src/types.rs | 8 ++++++-- crates/router/tests/connectors/aci.rs | 8 ++++---- crates/router/tests/connectors/authorizedotnet.rs | 8 ++++---- crates/router/tests/connectors/checkout.rs | 8 ++++---- 12 files changed, 36 insertions(+), 33 deletions(-) diff --git a/crates/router/src/connector/aci/transformers.rs b/crates/router/src/connector/aci/transformers.rs index 48ff3fda76..a6f2ca30fb 100644 --- a/crates/router/src/connector/aci/transformers.rs +++ b/crates/router/src/connector/aci/transformers.rs @@ -120,8 +120,8 @@ impl TryFrom<&types::PaymentsRouterData> for AciPaymentsRequest { let aci_payment_request = AciPaymentsRequest { payment_method: payment_details, entity_id: auth.entity_id, - amount: item.amount, - currency: item.currency.to_string(), + amount: item.request.amount, + currency: item.request.currency.to_string(), payment_type: AciPaymentType::Debit, }; Ok(aci_payment_request) @@ -237,7 +237,7 @@ impl TryFrom<&types::RefundsRouterData> for AciRefundRequest { type Error = error_stack::Report; fn try_from(item: &types::RefundsRouterData) -> Result { let amount = item.request.refund_amount; - let currency = item.currency; + let currency = item.request.currency; let payment_type = AciPaymentType::Refund; let auth = AciAuthType::try_from(&item.connector_auth_type)?; diff --git a/crates/router/src/connector/adyen/transformers.rs b/crates/router/src/connector/adyen/transformers.rs index fa3ad7beac..d82bb755b0 100644 --- a/crates/router/src/connector/adyen/transformers.rs +++ b/crates/router/src/connector/adyen/transformers.rs @@ -245,8 +245,8 @@ impl TryFrom<&types::PaymentsRouterData> for AdyenPaymentRequest { let auth_type = AdyenAuthType::try_from(&item.connector_auth_type)?; let reference = item.payment_id.to_string(); let amount = Amount { - currency: item.currency.to_string(), - value: item.amount, + currency: item.request.currency.to_string(), + value: item.request.amount, }; let ccard = match item.request.payment_method_data { api::PaymentMethod::Card(ref ccard) => Some(ccard), diff --git a/crates/router/src/connector/authorizedotnet/transformers.rs b/crates/router/src/connector/authorizedotnet/transformers.rs index bacf0b296b..2dc4cb4ad7 100644 --- a/crates/router/src/connector/authorizedotnet/transformers.rs +++ b/crates/router/src/connector/authorizedotnet/transformers.rs @@ -155,9 +155,9 @@ impl TryFrom<&types::PaymentsRouterData> for CreateTransactionRequest { }); let transaction_request = TransactionRequest { transaction_type: TransactionType::Payment, - amount: item.amount, + amount: item.request.amount, payment: payment_details, - currency_code: item.currency.to_string(), + currency_code: item.request.currency.to_string(), authorization_indicator_type, }; @@ -365,7 +365,7 @@ impl TryFrom<&types::RefundsRouterData> for CreateRefundRequest { transaction_type: TransactionType::Refund, amount: item.request.refund_amount, payment: payment_details, - currency_code: item.currency.to_string(), + currency_code: item.request.currency.to_string(), reference_transaction_id: item.request.connector_transaction_id.clone(), }; diff --git a/crates/router/src/connector/checkout/transformers.rs b/crates/router/src/connector/checkout/transformers.rs index f4766c3485..8811d5bd8b 100644 --- a/crates/router/src/connector/checkout/transformers.rs +++ b/crates/router/src/connector/checkout/transformers.rs @@ -111,8 +111,8 @@ impl TryFrom<&types::PaymentsRouterData> for PaymentsRequest { let processing_channel_id = auth_type.processing_channel_id; Ok(PaymentsRequest { source: source_var, - amount: item.amount, - currency: item.currency.to_string(), + amount: item.request.amount, + currency: item.request.currency.to_string(), processing_channel_id, three_ds, return_url, diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index 790aa216ca..f4367967d5 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -192,8 +192,8 @@ impl TryFrom<&types::PaymentsRouterData> for PaymentIntentRequest { }; Ok(PaymentIntentRequest { - amount: item.amount, //hopefully we don't loose some cents here - currency: item.currency.to_string(), //we need to copy the value and not transfer ownership + amount: item.request.amount, //hopefully we don't loose some cents here + currency: item.request.currency.to_string(), //we need to copy the value and not transfer ownership statement_descriptor_suffix: item.request.statement_descriptor_suffix.clone(), metadata_order_id, metadata_txn_id, diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs index 574d367872..7b65f321b2 100644 --- a/crates/router/src/core/payments/transformers.rs +++ b/crates/router/src/core/payments/transformers.rs @@ -85,8 +85,6 @@ where connector: merchant_connector_account.connector_name, payment_id: payment_data.payment_attempt.payment_id.clone(), status: payment_data.payment_attempt.status, - amount: payment_data.amount, - currency: payment_data.currency, payment_method, connector_auth_type: auth_type, description: payment_data.payment_intent.description.clone(), @@ -100,7 +98,6 @@ where .unwrap_or_default(), request: T::try_from(payment_data.clone())?, - response: response.map_or_else(|| Err(types::ErrorResponse::default()), Ok), }; @@ -282,6 +279,8 @@ impl TryFrom> for types::PaymentsRequestData { confirm: payment_data.payment_attempt.confirm, statement_descriptor_suffix: payment_data.payment_intent.statement_descriptor_suffix, capture_method: payment_data.payment_attempt.capture_method, + amount: payment_data.amount, + currency: payment_data.currency, browser_info, }) } diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs index 5ec87aaf44..a93c6a72be 100644 --- a/crates/router/src/core/refunds.rs +++ b/crates/router/src/core/refunds.rs @@ -446,8 +446,8 @@ impl TryFrom> for refunds::RefundResponse { Ok(refunds::RefundResponse { payment_id: data.payment_id, refund_id, - amount: data.amount / 100, - currency: data.currency.to_string(), + amount: data.request.amount / 100, + currency: data.request.currency.to_string(), reason: Some("TODO: Not propagated".to_string()), // TODO: Not propagated status, metadata: None, diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs index 8408a6a5de..3538dd7128 100644 --- a/crates/router/src/core/utils.rs +++ b/crates/router/src/core/utils.rs @@ -70,8 +70,6 @@ pub async fn construct_refund_router_data<'a, F>( connector: merchant_connector_account.connector_name, payment_id: payment_attempt.payment_id.clone(), status, - amount, - currency, payment_method: payment_method_type, connector_auth_type: auth_type, description: None, @@ -87,6 +85,8 @@ pub async fn construct_refund_router_data<'a, F>( payment_method_data, connector_transaction_id: refund.transaction_id.clone(), refund_amount: refund.refund_amount, + currency, + amount, }, response: Ok(types::RefundsResponseData { diff --git a/crates/router/src/types.rs b/crates/router/src/types.rs index 99a146e8ed..b16be77725 100644 --- a/crates/router/src/types.rs +++ b/crates/router/src/types.rs @@ -40,8 +40,6 @@ pub struct RouterData { pub connector: String, pub payment_id: String, pub status: enums::AttemptStatus, - pub amount: i32, - pub currency: enums::Currency, pub payment_method: enums::PaymentMethodType, pub connector_auth_type: ConnectorAuthType, pub description: Option, @@ -63,6 +61,8 @@ pub struct RouterData { #[derive(Debug, Clone)] pub struct PaymentsRequestData { pub payment_method_data: payments::PaymentMethod, + pub amount: i32, + pub currency: enums::Currency, pub confirm: bool, pub statement_descriptor_suffix: Option, // redirect form not used https://juspay.atlassian.net/browse/ORCA-301 @@ -107,6 +107,10 @@ pub struct RefundsRequestData { pub refund_id: String, pub payment_method_data: payments::PaymentMethod, pub connector_transaction_id: String, + pub currency: enums::Currency, + /// Amount for the payment against which this refund is issued + pub amount: i32, + /// Amount to be refunded pub refund_amount: i32, } diff --git a/crates/router/tests/connectors/aci.rs b/crates/router/tests/connectors/aci.rs index 60b403493d..c7573a5571 100644 --- a/crates/router/tests/connectors/aci.rs +++ b/crates/router/tests/connectors/aci.rs @@ -23,15 +23,15 @@ fn construct_payment_router_data() -> types::PaymentsRouterData { connector: "aci".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 1000, auth_type: enums::AuthenticationType::NoThreeDs, - currency: enums::Currency::USD, payment_method: enums::PaymentMethodType::Card, connector_auth_type: auth.into(), description: Some("This is a test".to_string()), orca_return_url: None, return_url: None, request: types::PaymentsRequestData { + amount: 1000, + currency: enums::Currency::USD, payment_method_data: types::api::PaymentMethod::Card(types::api::CCard { card_number: Secret::new("4200000000000000".to_string()), card_exp_month: Secret::new("10".to_string()), @@ -65,15 +65,15 @@ fn construct_refund_router_data() -> types::RefundsRouterData { connector: "aci".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 1000, orca_return_url: None, - currency: enums::Currency::USD, payment_method: enums::PaymentMethodType::Card, auth_type: enums::AuthenticationType::NoThreeDs, connector_auth_type: auth.into(), description: Some("This is a test".to_string()), return_url: None, request: types::RefundsRequestData { + amount: 1000, + currency: enums::Currency::USD, refund_id: uuid::Uuid::new_v4().to_string(), payment_method_data: types::api::PaymentMethod::Card(types::api::CCard { card_number: Secret::new("4200000000000000".to_string()), diff --git a/crates/router/tests/connectors/authorizedotnet.rs b/crates/router/tests/connectors/authorizedotnet.rs index e38c99efd7..22440574ff 100644 --- a/crates/router/tests/connectors/authorizedotnet.rs +++ b/crates/router/tests/connectors/authorizedotnet.rs @@ -23,15 +23,15 @@ fn construct_payment_router_data() -> types::PaymentsRouterData { connector: "authorizedotnet".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 100, orca_return_url: None, - currency: enums::Currency::USD, payment_method: enums::PaymentMethodType::Card, connector_auth_type: auth.into(), auth_type: enums::AuthenticationType::NoThreeDs, description: Some("This is a test".to_string()), return_url: None, request: types::PaymentsRequestData { + amount: 100, + currency: enums::Currency::USD, payment_method_data: types::api::PaymentMethod::Card(types::api::CCard { card_number: Secret::new("5424000000000015".to_string()), card_exp_month: Secret::new("10".to_string()), @@ -65,15 +65,15 @@ fn construct_refund_router_data() -> types::RefundsRouterData { connector: "authorizedotnet".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 100, orca_return_url: None, - currency: enums::Currency::USD, auth_type: enums::AuthenticationType::NoThreeDs, payment_method: enums::PaymentMethodType::Card, connector_auth_type: auth.into(), description: Some("This is a test".to_string()), return_url: None, request: router::types::RefundsRequestData { + amount: 100, + currency: enums::Currency::USD, refund_id: uuid::Uuid::new_v4().to_string(), payment_method_data: types::api::PaymentMethod::Card(types::api::CCard { card_number: Secret::new("5424000000000015".to_string()), diff --git a/crates/router/tests/connectors/checkout.rs b/crates/router/tests/connectors/checkout.rs index ba95161bf0..5f084a6590 100644 --- a/crates/router/tests/connectors/checkout.rs +++ b/crates/router/tests/connectors/checkout.rs @@ -19,15 +19,15 @@ fn construct_payment_router_data() -> types::PaymentsRouterData { connector: "checkout".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 100, orca_return_url: None, - currency: enums::Currency::USD, auth_type: enums::AuthenticationType::NoThreeDs, payment_method: enums::PaymentMethodType::Card, connector_auth_type: auth.into(), description: Some("This is a test".to_string()), return_url: None, request: types::PaymentsRequestData { + amount: 100, + currency: enums::Currency::USD, payment_method_data: types::api::PaymentMethod::Card(api::CCard { card_number: "4242424242424242".to_string().into(), card_exp_month: "10".to_string().into(), @@ -61,8 +61,6 @@ fn construct_refund_router_data() -> types::RefundsRouterData { connector: "checkout".to_string(), payment_id: uuid::Uuid::new_v4().to_string(), status: enums::AttemptStatus::default(), - amount: 100, - currency: enums::Currency::USD, orca_return_url: None, payment_method: enums::PaymentMethodType::Card, auth_type: enums::AuthenticationType::NoThreeDs, @@ -70,6 +68,8 @@ fn construct_refund_router_data() -> types::RefundsRouterData { description: Some("This is a test".to_string()), return_url: None, request: types::RefundsRequestData { + amount: 100, + currency: enums::Currency::USD, refund_id: uuid::Uuid::new_v4().to_string(), payment_method_data: types::api::PaymentMethod::Card(api::CCard { card_number: "4242424242424242".to_string().into(),