Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): update response for PaymentsDynamicTaxCalculationResponse #5910

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion api-reference-v2/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -13537,7 +13559,8 @@
"type": "object",
"required": [
"payment_id",
"net_amount"
"net_amount",
"display_amount"
],
"properties": {
"payment_id": {
Expand All @@ -13562,6 +13585,9 @@
}
],
"nullable": true
},
"display_amount": {
"$ref": "#/components/schemas/DisplayAmountOnSdk"
}
}
},
Expand Down
28 changes: 27 additions & 1 deletion api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -17535,7 +17557,8 @@
"type": "object",
"required": [
"payment_id",
"net_amount"
"net_amount",
"display_amount"
],
"properties": {
"payment_id": {
Expand All @@ -17560,6 +17583,9 @@
}
],
"nullable": true
},
"display_amount": {
"$ref": "#/components/schemas/DisplayAmountOnSdk"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4619,6 +4619,21 @@ pub struct PaymentsDynamicTaxCalculationResponse {
pub order_tax_amount: Option<MinorUnit>,
/// shipping cost for the order
pub shipping_cost: Option<MinorUnit>,
/// 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<StringMajorUnit>,
/// shipping cost for the order
#[schema(value_type = String)]
pub shipping_cost: Option<StringMajorUnit>,
}

#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
Expand Down
1 change: 1 addition & 0 deletions crates/openapi/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)]
Expand Down
1 change: 1 addition & 0 deletions crates/openapi/src/openapi_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)]
Expand Down
71 changes: 69 additions & 2 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<MinorUnit>, Option<MinorUnit>, Currency)>
for api_models::payments::DisplayAmountOnSdk
{
type Error = error_stack::Report<errors::ApiErrorResponse>;

fn foreign_try_from(
(net_amount, shipping_cost, order_tax_amount, currency): (
MinorUnit,
Option<MinorUnit>,
Option<MinorUnit>,
Currency,
),
) -> Result<Self, Self::Error> {
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<F, Op, D> ToResponse<F, D, Op> for api::VerifyResponse
where
F: Clone,
Expand Down
Loading