Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hrithikesh026 committed Dec 4, 2023
1 parent 5845bfa commit a80d968
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 119 deletions.
87 changes: 35 additions & 52 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use common_utils::{
types::{Percentage, Surcharge},
};
use serde::de;
use utoipa::ToSchema;
use utoipa::{schema, ToSchema};

#[cfg(feature = "payouts")]
use crate::payouts;
Expand Down Expand Up @@ -264,22 +264,6 @@ pub struct CardNetworkTypes {
pub card_network: api_enums::CardNetwork,

/// surcharge details for this card network
#[schema(example = r#"
{
"surcharge": {
"type": "rate",
"value": {
"percentage": 2.5
}
},
"tax_on_surcharge": {
"percentage": 1.5
}
"display_surcharge": 12,
"display_tax_on_surcharge_amount": 12,
"display_final_amount: 1234
}
"#)]
pub surcharge_details: Option<SurchargeDetailsResponse>,

/// The list of eligible connectors for a given card network
Expand Down Expand Up @@ -316,34 +300,19 @@ pub struct ResponsePaymentMethodTypes {
pub required_fields: Option<HashMap<String, RequiredFieldInfo>>,

/// surcharge details for this payment method type if exists
#[schema(example = r#"
{
"surcharge": {
"type": "rate",
"value": {
"percentage": 2.5
}
},
"tax_on_surcharge": {
"percentage": 1.5
}
"display_surcharge": 12,
"display_tax_on_surcharge_amount": 12,
"display_final_amount: 1234
}
"#)]
pub surcharge_details: Option<SurchargeDetailsResponse>,

/// auth service connector label for this payment method type, if exists
pub pm_auth_connector: Option<String>,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]

#[derive(Clone, Debug, PartialEq, serde::Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub struct SurchargeDetailsResponse {
/// surcharge value
pub surcharge: Surcharge,
pub surcharge: SurchargeResponse,
/// tax on surcharge value
pub tax_on_surcharge: Option<Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>>,
pub tax_on_surcharge: Option<SurchargePercentage>,
/// surcharge amount for this payment
pub display_surcharge_amount: f64,
/// tax on surcharge amount for this payment
Expand All @@ -354,6 +323,36 @@ pub struct SurchargeDetailsResponse {
pub display_final_amount: f64,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, ToSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum SurchargeResponse {
/// Fixed Surcharge value
Fixed(i64),
/// Surcharge percentage
Rate(SurchargePercentage),
}

impl From<Surcharge> for SurchargeResponse {
fn from(value: Surcharge) -> Self {
match value {
Surcharge::Fixed(amount) => Self::Fixed(amount),
Surcharge::Rate(percentage) => Self::Rate(percentage.into()),
}
}
}

#[derive(Clone, Default, Debug, PartialEq, serde::Serialize, ToSchema)]
pub struct SurchargePercentage {
percentage: f32,
}

impl From<Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>> for SurchargePercentage {
fn from(value: Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>) -> Self {
Self {
percentage: value.get_percentage(),
}
}
}
/// Required fields info used while listing the payment_method_data
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq, ToSchema, Hash)]
pub struct RequiredFieldInfo {
Expand Down Expand Up @@ -723,22 +722,6 @@ pub struct CustomerPaymentMethod {
pub bank: Option<MaskedBankDetails>,

/// Surcharge details for this saved card
#[schema(example = r#"
{
"surcharge": {
"type": "rate",
"value": {
"percentage": 2.5
}
},
"tax_on_surcharge": {
"percentage": 1.5
}
"display_surcharge": 12,
"display_tax_on_surcharge_amount": 12,
"display_final_amount: 1234
}
"#)]
pub surcharge_details: Option<SurchargeDetailsResponse>,

/// Whether this payment method requires CVV to be collected
Expand Down
5 changes: 2 additions & 3 deletions crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Types that can be used in other crates
use error_stack::{IntoReport, ResultExt};
use serde::{de::Visitor, Deserialize, Deserializer};
use utoipa::ToSchema;

use crate::{
consts,
errors::{CustomResult, PercentageError},
};

/// Represents Percentage Value between 0 and 100 both inclusive
#[derive(Clone, Default, Debug, PartialEq, serde::Serialize, ToSchema)]
#[derive(Clone, Default, Debug, PartialEq, serde::Serialize)]
pub struct Percentage<const PRECISION: u8> {
// this value will range from 0 to 100, decimal length defined by precision macro
/// Percentage value ranging between 0 and 100
Expand Down Expand Up @@ -142,7 +141,7 @@ impl<'de, const PRECISION: u8> Deserialize<'de> for Percentage<PRECISION> {
}

/// represents surcharge type and value
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum Surcharge {
/// Fixed Surcharge value
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payments/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ impl ForeignTryFrom<(&SurchargeDetails, &PaymentAttempt)> for SurchargeDetailsRe
let display_final_amount =
currency.to_currency_base_unit_asf64(surcharge_details.final_amount)?;
Ok(Self {
surcharge: surcharge_details.surcharge.clone(),
tax_on_surcharge: surcharge_details.tax_on_surcharge.clone(),
surcharge: surcharge_details.surcharge.clone().into(),
tax_on_surcharge: surcharge_details.tax_on_surcharge.clone().map(Into::into),
display_surcharge_amount,
display_tax_on_surcharge_amount,
display_total_surcharge_amount: display_surcharge_amount
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::payment_methods::RequiredFieldInfo,
api_models::payment_methods::MaskedBankDetails,
api_models::payment_methods::SurchargeDetailsResponse,
api_models::payment_methods::SurchargeResponse,
api_models::payment_methods::SurchargePercentage,
api_models::refunds::RefundListRequest,
api_models::refunds::RefundListResponse,
api_models::payments::TimeRange,
Expand Down Expand Up @@ -363,8 +365,6 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::payments::RetrievePaymentLinkResponse,
api_models::payments::PaymentLinkInitiateRequest,
api_models::payments::PaymentLinkObject,
common_utils::types::Surcharge,
common_utils::types::Percentage<{common_utils::consts::SURCHARGE_PERCENTAGE_PRECISION_LENGTH}>
)),
modifiers(&SecurityAddon)
)]
Expand Down
117 changes: 57 additions & 60 deletions openapi/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -11166,20 +11166,6 @@
}
}
},
"Percentage": {
"type": "object",
"description": "Represents Percentage Value between 0 and 100 both inclusive",
"required": [
"percentage"
],
"properties": {
"percentage": {
"type": "number",
"format": "float",
"description": "Percentage value ranging between 0 and 100"
}
}
},
"PhoneDetails": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -11997,7 +11983,62 @@
}
}
},
"Surcharge": {
"SurchargeDetailsResponse": {
"type": "object",
"required": [
"surcharge",
"display_surcharge_amount",
"display_tax_on_surcharge_amount",
"display_total_surcharge_amount",
"display_final_amount"
],
"properties": {
"surcharge": {
"$ref": "#/components/schemas/SurchargeResponse"
},
"tax_on_surcharge": {
"allOf": [
{
"$ref": "#/components/schemas/SurchargePercentage"
}
],
"nullable": true
},
"display_surcharge_amount": {
"type": "number",
"format": "double",
"description": "surcharge amount for this payment"
},
"display_tax_on_surcharge_amount": {
"type": "number",
"format": "double",
"description": "tax on surcharge amount for this payment"
},
"display_total_surcharge_amount": {
"type": "number",
"format": "double",
"description": "sum of display_surcharge_amount and display_tax_on_surcharge_amount"
},
"display_final_amount": {
"type": "number",
"format": "double",
"description": "sum of original amount,"
}
}
},
"SurchargePercentage": {
"type": "object",
"required": [
"percentage"
],
"properties": {
"percentage": {
"type": "number",
"format": "float"
}
}
},
"SurchargeResponse": {
"oneOf": [
{
"type": "object",
Expand Down Expand Up @@ -12033,59 +12074,15 @@
]
},
"value": {
"$ref": "#/components/schemas/Percentage"
"$ref": "#/components/schemas/SurchargePercentage"
}
}
}
],
"description": "represents surcharge type and value",
"discriminator": {
"propertyName": "type"
}
},
"SurchargeDetailsResponse": {
"type": "object",
"required": [
"surcharge",
"display_surcharge_amount",
"display_tax_on_surcharge_amount",
"display_total_surcharge_amount",
"display_final_amount"
],
"properties": {
"surcharge": {
"$ref": "#/components/schemas/Surcharge"
},
"tax_on_surcharge": {
"allOf": [
{
"$ref": "#/components/schemas/Percentage"
}
],
"nullable": true
},
"display_surcharge_amount": {
"type": "number",
"format": "double",
"description": "surcharge amount for this payment"
},
"display_tax_on_surcharge_amount": {
"type": "number",
"format": "double",
"description": "tax on surcharge amount for this payment"
},
"display_total_surcharge_amount": {
"type": "number",
"format": "double",
"description": "sum of display_surcharge_amount and display_tax_on_surcharge_amount"
},
"display_final_amount": {
"type": "number",
"format": "double",
"description": "sum of original amount,"
}
}
},
"SwishQrData": {
"type": "object"
},
Expand Down

0 comments on commit a80d968

Please sign in to comment.