From e0d42252b8ce013b94fca2afb1709db007998aa2 Mon Sep 17 00:00:00 2001 From: Swangi Kumari <85639103+swangi-kumari@users.noreply.github.com> Date: Mon, 16 Sep 2024 19:37:43 +0530 Subject: [PATCH] refactor(core): update response for PaymentsDynamicTaxCalculationResponse (#5909) --- api-reference-v2/openapi_spec.json | 28 +++++++- api-reference/openapi_spec.json | 28 +++++++- crates/api_models/src/payments.rs | 15 ++++ crates/openapi/src/openapi.rs | 1 + crates/openapi/src/openapi_v2.rs | 1 + .../router/src/core/payments/transformers.rs | 71 ++++++++++++++++++- 6 files changed, 140 insertions(+), 4 deletions(-) diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.json index bda5fab29c0..8a2a8c98a08 100644 --- a/api-reference-v2/openapi_spec.json +++ b/api-reference-v2/openapi_spec.json @@ -6212,6 +6212,28 @@ "BRW" ] }, + "DisplayAmountOnSdk": { + "type": "object", + "required": [ + "net_amount", + "order_tax_amount", + "shipping_cost" + ], + "properties": { + "net_amount": { + "type": "string", + "description": "net amount = amount + order_tax_amount + shipping_cost" + }, + "order_tax_amount": { + "type": "string", + "description": "order tax amount calculated by tax connectors" + }, + "shipping_cost": { + "type": "string", + "description": "shipping cost for the order" + } + } + }, "DisputeResponse": { "type": "object", "required": [ @@ -13537,7 +13559,8 @@ "type": "object", "required": [ "payment_id", - "net_amount" + "net_amount", + "display_amount" ], "properties": { "payment_id": { @@ -13562,6 +13585,9 @@ } ], "nullable": true + }, + "display_amount": { + "$ref": "#/components/schemas/DisplayAmountOnSdk" } } }, diff --git a/api-reference/openapi_spec.json b/api-reference/openapi_spec.json index 9d6074fe16b..3e4994023e4 100644 --- a/api-reference/openapi_spec.json +++ b/api-reference/openapi_spec.json @@ -9831,6 +9831,28 @@ "BRW" ] }, + "DisplayAmountOnSdk": { + "type": "object", + "required": [ + "net_amount", + "order_tax_amount", + "shipping_cost" + ], + "properties": { + "net_amount": { + "type": "string", + "description": "net amount = amount + order_tax_amount + shipping_cost" + }, + "order_tax_amount": { + "type": "string", + "description": "order tax amount calculated by tax connectors" + }, + "shipping_cost": { + "type": "string", + "description": "shipping cost for the order" + } + } + }, "DisputeResponse": { "type": "object", "required": [ @@ -17535,7 +17557,8 @@ "type": "object", "required": [ "payment_id", - "net_amount" + "net_amount", + "display_amount" ], "properties": { "payment_id": { @@ -17560,6 +17583,9 @@ } ], "nullable": true + }, + "display_amount": { + "$ref": "#/components/schemas/DisplayAmountOnSdk" } } }, diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs index abd795361bb..9211a065506 100644 --- a/crates/api_models/src/payments.rs +++ b/crates/api_models/src/payments.rs @@ -4619,6 +4619,21 @@ pub struct PaymentsDynamicTaxCalculationResponse { pub order_tax_amount: Option, /// shipping cost for the order pub shipping_cost: Option, + /// amount in Base Unit display format + pub display_amount: DisplayAmountOnSdk, +} + +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)] +pub struct DisplayAmountOnSdk { + /// net amount = amount + order_tax_amount + shipping_cost + #[schema(value_type = String)] + pub net_amount: StringMajorUnit, + /// order tax amount calculated by tax connectors + #[schema(value_type = String)] + pub order_tax_amount: Option, + /// shipping cost for the order + #[schema(value_type = String)] + pub shipping_cost: Option, } #[derive(Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)] diff --git a/crates/openapi/src/openapi.rs b/crates/openapi/src/openapi.rs index 3b63b7ac20f..5c1597c7a88 100644 --- a/crates/openapi/src/openapi.rs +++ b/crates/openapi/src/openapi.rs @@ -606,6 +606,7 @@ Never share your secret api keys. Keep them guarded and secure. api_models::payments::additional_info::UpiCollectAdditionalData, api_models::payments::PaymentsDynamicTaxCalculationRequest, api_models::payments::PaymentsDynamicTaxCalculationResponse, + api_models::payments::DisplayAmountOnSdk, )), modifiers(&SecurityAddon) )] diff --git a/crates/openapi/src/openapi_v2.rs b/crates/openapi/src/openapi_v2.rs index 5393e8d6fae..31feb8e85b1 100644 --- a/crates/openapi/src/openapi_v2.rs +++ b/crates/openapi/src/openapi_v2.rs @@ -522,6 +522,7 @@ Never share your secret api keys. Keep them guarded and secure. api_models::payments::additional_info::UpiCollectAdditionalData, api_models::payments::PaymentsDynamicTaxCalculationRequest, api_models::payments::PaymentsDynamicTaxCalculationResponse, + api_models::payments::DisplayAmountOnSdk, )), modifiers(&SecurityAddon) )] diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs index 36961f33753..d94b413e626 100644 --- a/crates/router/src/core/payments/transformers.rs +++ b/crates/router/src/core/payments/transformers.rs @@ -4,8 +4,13 @@ use api_models::payments::{ Address, CustomerDetails, CustomerDetailsResponse, FrmMessage, PaymentChargeRequest, PaymentChargeResponse, RequestSurchargeDetails, }; -use common_enums::RequestIncrementalAuthorization; -use common_utils::{consts::X_HS_LATENCY, fp_utils, pii::Email, types::MinorUnit}; +use common_enums::{Currency, RequestIncrementalAuthorization}; +use common_utils::{ + consts::X_HS_LATENCY, + fp_utils, + pii::Email, + types::{AmountConvertor, MinorUnit, StringMajorUnitForConnector}, +}; use diesel_models::ephemeral_key; use error_stack::{report, ResultExt}; use hyperswitch_domain_models::{payments::payment_intent::CustomerData, router_request_types}; @@ -503,18 +508,80 @@ where amount = amount + tax_amount; } + let currency = payment_data + .get_payment_attempt() + .currency + .get_required_value("currency")?; + Ok(services::ApplicationResponse::JsonWithHeaders(( Self { net_amount: amount, payment_id: payment_data.get_payment_attempt().payment_id.clone(), order_tax_amount, shipping_cost, + display_amount: api_models::payments::DisplayAmountOnSdk::foreign_try_from(( + amount, + shipping_cost, + order_tax_amount, + currency, + ))?, }, vec![], ))) } } +impl ForeignTryFrom<(MinorUnit, Option, Option, Currency)> + for api_models::payments::DisplayAmountOnSdk +{ + type Error = error_stack::Report; + + fn foreign_try_from( + (net_amount, shipping_cost, order_tax_amount, currency): ( + MinorUnit, + Option, + Option, + Currency, + ), + ) -> Result { + let major_unit_convertor = StringMajorUnitForConnector; + + let sdk_net_amount = major_unit_convertor + .convert(net_amount, currency) + .change_context(errors::ApiErrorResponse::PreconditionFailed { + message: "Failed to convert net_amount to base unit".to_string(), + }) + .attach_printable("Failed to convert net_amount to string major unit")?; + + let sdk_shipping_cost = shipping_cost + .map(|cost| { + major_unit_convertor + .convert(cost, currency) + .change_context(errors::ApiErrorResponse::PreconditionFailed { + message: "Failed to convert shipping_cost to base unit".to_string(), + }) + .attach_printable("Failed to convert shipping_cost to string major unit") + }) + .transpose()?; + + let sdk_order_tax_amount = order_tax_amount + .map(|cost| { + major_unit_convertor + .convert(cost, currency) + .change_context(errors::ApiErrorResponse::PreconditionFailed { + message: "Failed to convert order_tax_amount to base unit".to_string(), + }) + .attach_printable("Failed to convert order_tax_amount to string major unit") + }) + .transpose()?; + Ok(Self { + net_amount: sdk_net_amount, + shipping_cost: sdk_shipping_cost, + order_tax_amount: sdk_order_tax_amount, + }) + } +} + impl ToResponse for api::VerifyResponse where F: Clone,