Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor(router): modify net_amount to be a struct in the domain model of payment_attempt and handle amount changes across all flows #6252

Merged
merged 17 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 8 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,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
Loading