Skip to content

Commit

Permalink
refactor(router): modify net_amount to be a struct in the domain mo…
Browse files Browse the repository at this point in the history
…del of payment_attempt and handle amount changes across all flows (#6252)

Co-authored-by: swangi-kumari <[email protected]>
Co-authored-by: Swangi Kumari <[email protected]>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Oct 15, 2024
1 parent dc2e5cb commit 20056ac
Show file tree
Hide file tree
Showing 36 changed files with 830 additions and 1,134 deletions.
8 changes: 1 addition & 7 deletions api-reference-v2/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -19522,8 +19522,7 @@
"surcharge",
"display_surcharge_amount",
"display_tax_on_surcharge_amount",
"display_total_surcharge_amount",
"display_final_amount"
"display_total_surcharge_amount"
],
"properties": {
"surcharge": {
Expand Down Expand Up @@ -19551,11 +19550,6 @@
"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,"
}
}
},
Expand Down
8 changes: 1 addition & 7 deletions api-reference/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -23361,8 +23361,7 @@
"surcharge",
"display_surcharge_amount",
"display_tax_on_surcharge_amount",
"display_total_surcharge_amount",
"display_final_amount"
"display_total_surcharge_amount"
],
"properties": {
"surcharge": {
Expand Down Expand Up @@ -23390,11 +23389,6 @@
"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,"
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,6 @@ pub struct SurchargeDetailsResponse {
pub display_tax_on_surcharge_amount: f64,
/// sum of display_surcharge_amount and display_tax_on_surcharge_amount
pub display_total_surcharge_amount: f64,
/// sum of original amount,
pub display_final_amount: f64,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, ToSchema)]
Expand Down
8 changes: 8 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,14 @@ impl RequestSurchargeDetails {
pub fn get_total_surcharge_amount(&self) -> MinorUnit {
self.surcharge_amount + self.tax_amount.unwrap_or_default()
}

pub fn get_surcharge_amount(&self) -> MinorUnit {
self.surcharge_amount
}

pub fn get_tax_amount(&self) -> Option<MinorUnit> {
self.tax_amount
}
}

#[derive(Default, Debug, Clone)]
Expand Down
54 changes: 0 additions & 54 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,6 @@ pub struct PaymentAttempt {
pub order_tax_amount: Option<MinorUnit>,
}

#[cfg(feature = "v1")]
impl PaymentAttempt {
pub fn get_or_calculate_net_amount(&self) -> MinorUnit {
self.net_amount.unwrap_or(
self.amount
+ self.surcharge_amount.unwrap_or(MinorUnit::new(0))
+ self.tax_amount.unwrap_or(MinorUnit::new(0))
+ self.shipping_cost.unwrap_or(MinorUnit::new(0))
+ self.order_tax_amount.unwrap_or(MinorUnit::new(0)),
)
}
}

#[derive(Clone, Debug, Eq, PartialEq, Queryable, Serialize, Deserialize)]
pub struct PaymentListFilters {
pub connector: Vec<String>,
Expand Down Expand Up @@ -298,47 +285,6 @@ pub struct PaymentAttemptNew {
pub order_tax_amount: Option<MinorUnit>,
}

#[cfg(feature = "v1")]
impl PaymentAttemptNew {
/// returns amount + surcharge_amount + tax_amount (surcharge) + shipping_cost + order_tax_amount
pub fn calculate_net_amount(&self) -> MinorUnit {
self.amount
+ self.surcharge_amount.unwrap_or(MinorUnit::new(0))
+ self.tax_amount.unwrap_or(MinorUnit::new(0))
+ self.shipping_cost.unwrap_or(MinorUnit::new(0))
}

pub fn get_or_calculate_net_amount(&self) -> MinorUnit {
self.net_amount
.unwrap_or_else(|| self.calculate_net_amount())
}

pub fn populate_derived_fields(self) -> Self {
let mut payment_attempt_new = self;
payment_attempt_new.net_amount = Some(payment_attempt_new.calculate_net_amount());
payment_attempt_new
}
}

#[cfg(feature = "v2")]
impl PaymentAttemptNew {
/// returns amount + surcharge_amount + tax_amount
pub fn calculate_net_amount(&self) -> MinorUnit {
todo!();
}

pub fn get_or_calculate_net_amount(&self) -> MinorUnit {
self.net_amount
.unwrap_or_else(|| self.calculate_net_amount())
}

pub fn populate_derived_fields(self) -> Self {
let mut payment_attempt_new = self;
payment_attempt_new.net_amount = Some(payment_attempt_new.calculate_net_amount());
payment_attempt_new
}
}

#[cfg(feature = "v1")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PaymentAttemptUpdate {
Expand Down
2 changes: 1 addition & 1 deletion crates/diesel_models/src/query/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{

impl PaymentAttemptNew {
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<PaymentAttempt> {
generics::generic_insert(conn, self.populate_derived_fields()).await
generics::generic_insert(conn, self).await
}
}

Expand Down
Loading

0 comments on commit 20056ac

Please sign in to comment.