diff --git a/Cargo.lock b/Cargo.lock index 7ce0851ba15..3f9c7ef079e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7585,18 +7585,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.31" +version = "0.7.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c4061bedbb353041c12f413700357bec76df2c7e2ca8e4df8bac24c6bf68e3d" +checksum = "e97e415490559a91254a2979b4829267a57d2fcd741a98eee8b722fb57289aa0" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.31" +version = "0.7.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a" +checksum = "dd7e48ccf166952882ca8bd778a43502c64f33bf94c12ebe2a7f08e5a0f6689f" dependencies = [ "proc-macro2", "quote", diff --git a/crates/data_models/src/payments.rs b/crates/data_models/src/payments.rs index 713003d666b..443c75399e6 100644 --- a/crates/data_models/src/payments.rs +++ b/crates/data_models/src/payments.rs @@ -15,7 +15,9 @@ pub struct PaymentIntent { pub payment_id: String, pub merchant_id: String, pub status: storage_enums::IntentStatus, - pub amount: i64, + /// This amount may not be equal to authorized_amount due to surcharge
+ /// Authorized amount can be fetched from PaymentAttempt + pub original_amount: i64, pub currency: Option, pub amount_captured: Option, pub customer_id: Option, diff --git a/crates/data_models/src/payments/payment_attempt.rs b/crates/data_models/src/payments/payment_attempt.rs index 3e6ba9e37f8..fb83e2c3edd 100644 --- a/crates/data_models/src/payments/payment_attempt.rs +++ b/crates/data_models/src/payments/payment_attempt.rs @@ -1,5 +1,6 @@ use api_models::enums::Connector; use common_enums as storage_enums; +use diesel_models::PaymentAttempt as DieselPaymentAttempt; use serde::{Deserialize, Serialize}; use time::PrimitiveDateTime; @@ -106,15 +107,12 @@ pub struct PaymentAttempt { pub merchant_id: String, pub attempt_id: String, pub status: storage_enums::AttemptStatus, - pub amount: i64, - pub net_amount: i64, + pub amount: AttemptAmount, pub currency: Option, pub save_to_locker: Option, pub connector: Option, pub error_message: Option, pub offer_amount: Option, - pub surcharge_amount: Option, - pub tax_amount: Option, pub payment_method_id: Option, pub payment_method: Option, pub connector_transaction_id: Option, @@ -157,9 +155,64 @@ pub struct PaymentAttempt { pub unified_message: Option, } -impl PaymentAttempt { - pub fn get_total_amount(&self) -> i64 { - self.amount + self.surcharge_amount.unwrap_or(0) + self.tax_amount.unwrap_or(0) +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct AttemptAmount { + amount: i64, + surcharge_amount: Option, + tax_amount: Option, + net_amount: i64, +} + +pub trait GetAttemptAmount { + fn get_attempt_amount(&self) -> AttemptAmount; +} + +impl GetAttemptAmount for DieselPaymentAttempt { + fn get_attempt_amount(&self) -> AttemptAmount { + AttemptAmount { + amount: self.amount, + surcharge_amount: self.surcharge_amount, + tax_amount: self.tax_amount, + net_amount: self.get_or_calculate_net_amount(), + } + } +} + +impl GetAttemptAmount for PaymentAttemptNew { + fn get_attempt_amount(&self) -> AttemptAmount { + AttemptAmount { + amount: self.amount, + surcharge_amount: self.surcharge_amount, + tax_amount: self.tax_amount, + net_amount: self.calculate_net_amount(), + } + } +} + +impl AttemptAmount { + pub fn set_original_amount(&mut self, amount: i64) { + self.amount = amount; + self.net_amount = self.amount + self.get_total_surcharge_amount().unwrap_or(0); + } + pub fn set_surcharge_amount(&mut self, surcharge_amount: i64) { + self.surcharge_amount = Some(surcharge_amount); + self.net_amount = self.amount + self.get_total_surcharge_amount().unwrap_or(0); + } + pub fn set_tax_amount(&mut self, tax_amount: i64) { + self.tax_amount = Some(tax_amount); + self.net_amount = self.amount + self.get_total_surcharge_amount().unwrap_or(0); + } + pub fn get_authorize_amount(&self) -> i64 { + self.net_amount + } + pub fn get_original_amount(&self) -> i64 { + self.amount + } + pub fn get_surcharge_amount(&self) -> Option { + self.surcharge_amount + } + pub fn get_tax_amount_on_surcharge(&self) -> Option { + self.tax_amount } pub fn get_total_surcharge_amount(&self) -> Option { self.surcharge_amount diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs index d08c146b0b8..1f8a481b85a 100644 --- a/crates/diesel_models/src/payment_attempt.rs +++ b/crates/diesel_models/src/payment_attempt.rs @@ -146,11 +146,6 @@ impl PaymentAttemptNew { self.amount + self.surcharge_amount.unwrap_or(0) + self.tax_amount.unwrap_or(0) } - pub fn get_or_calculate_net_amount(&self) -> i64 { - 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()); diff --git a/crates/router/src/connector/cybersource/transformers.rs b/crates/router/src/connector/cybersource/transformers.rs index e83b23603e9..a641a3b5027 100644 --- a/crates/router/src/connector/cybersource/transformers.rs +++ b/crates/router/src/connector/cybersource/transformers.rs @@ -1986,9 +1986,9 @@ impl resource_id: types::ResponseId::NoResponseId, redirection_data, mandate_reference: None, - connector_metadata: Some( - serde_json::json!({"three_ds_data":three_ds_data}), - ), + connector_metadata: Some(serde_json::json!({ + "three_ds_data": three_ds_data + })), network_txn_id: None, connector_response_reference_id, incremental_authorization_allowed: None, diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs index 8f028e37a9e..afba1478943 100644 --- a/crates/router/src/connector/utils.rs +++ b/crates/router/src/connector/utils.rs @@ -118,7 +118,8 @@ where enums::AttemptStatus::Charged => { let captured_amount = types::Capturable::get_captured_amount(&self.request, payment_data); - let total_capturable_amount = payment_data.payment_attempt.get_total_amount(); + let total_capturable_amount = + payment_data.payment_attempt.amount.get_authorize_amount(); if Some(total_capturable_amount) == captured_amount { enums::AttemptStatus::Charged } else if captured_amount.is_some() { diff --git a/crates/router/src/core/fraud_check.rs b/crates/router/src/core/fraud_check.rs index ad3a7638774..a84d4e45d84 100644 --- a/crates/router/src/core/fraud_check.rs +++ b/crates/router/src/core/fraud_check.rs @@ -81,6 +81,16 @@ where .payment_attempt .connector_transaction_id .clone(); + if let Some(surcharge_details) = &payment_data.surcharge_details { + frm_data + .payment_attempt + .amount + .set_surcharge_amount(surcharge_details.surcharge_amount); + frm_data + .payment_attempt + .amount + .set_tax_amount(surcharge_details.tax_on_surcharge_amount); + } let mut router_data = frm_data .construct_router_data( diff --git a/crates/router/src/core/fraud_check/flows/checkout_flow.rs b/crates/router/src/core/fraud_check/flows/checkout_flow.rs index 7f8993af527..722db1a8571 100644 --- a/crates/router/src/core/fraud_check/flows/checkout_flow.rs +++ b/crates/router/src/core/fraud_check/flows/checkout_flow.rs @@ -69,7 +69,7 @@ impl ConstructFlowSpecificData( connector_meta_data: merchant_connector_account.get_metadata(), amount_captured: payment_intent.amount_captured, request: FraudCheckFulfillmentData { - amount: payment_attempt.amount, + amount: payment_attempt.amount.get_authorize_amount(), order_details: payment_intent.order_details.clone(), fulfillment_req: fulfillment_request, }, diff --git a/crates/router/src/core/fraud_check/flows/record_return.rs b/crates/router/src/core/fraud_check/flows/record_return.rs index bd0ba3e4f7f..980d1e84ce4 100644 --- a/crates/router/src/core/fraud_check/flows/record_return.rs +++ b/crates/router/src/core/fraud_check/flows/record_return.rs @@ -68,7 +68,7 @@ impl ConstructFlowSpecificData>() + }; logger::debug!(mca_before_filtering=?filtered_mcas); @@ -1767,7 +1773,7 @@ pub async fn call_surcharge_decision_management( billing_address: Option, response_payment_method_types: &mut [ResponsePaymentMethodsEnabled], ) -> errors::RouterResult { - if payment_attempt.surcharge_amount.is_some() { + if payment_attempt.amount.get_surcharge_amount().is_some() { Ok(api_surcharge_decision_configs::MerchantSurchargeConfigs::default()) } else { let algorithm_ref: routing_types::RoutingAlgorithmRef = merchant_account @@ -1820,7 +1826,7 @@ pub async fn call_surcharge_decision_management_for_saved_card( payment_intent: storage::PaymentIntent, customer_payment_method_response: &mut api::CustomerPaymentMethodsListResponse, ) -> errors::RouterResult<()> { - if payment_attempt.surcharge_amount.is_some() { + if payment_attempt.amount.get_surcharge_amount().is_some() { Ok(()) } else { let algorithm_ref: routing_types::RoutingAlgorithmRef = merchant_account @@ -2311,10 +2317,10 @@ fn filter_payment_amount_based( payment_intent: &storage::PaymentIntent, pm: &RequestPaymentMethodTypes, ) -> bool { - let amount = payment_intent.amount; + let amount = payment_intent.original_amount; (pm.maximum_amount.map_or(true, |amt| amount <= amt.into()) && pm.minimum_amount.map_or(true, |amt| amount >= amt.into())) - || payment_intent.amount == 0 + || payment_intent.original_amount == 0 } async fn filter_payment_mandate_based( diff --git a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs b/crates/router/src/core/payment_methods/surcharge_decision_configs.rs index db1064b36a7..f9f5c348df7 100644 --- a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs +++ b/crates/router/src/core/payment_methods/surcharge_decision_configs.rs @@ -294,7 +294,7 @@ fn get_surcharge_details_from_surcharge_output( let surcharge_amount = match surcharge_details.surcharge.clone() { surcharge_decision_configs::SurchargeOutput::Fixed { amount } => amount, surcharge_decision_configs::SurchargeOutput::Rate(percentage) => percentage - .apply_and_ceil_result(payment_attempt.amount) + .apply_and_ceil_result(payment_attempt.amount.get_original_amount()) .change_context(ConfigError::DslExecutionError) .attach_printable("Failed to Calculate surcharge amount by applying percentage")?, }; @@ -309,8 +309,9 @@ fn get_surcharge_details_from_surcharge_output( }) .transpose()? .unwrap_or(0); + let original_amount = payment_attempt.amount.get_original_amount(); Ok(types::SurchargeDetails { - original_amount: payment_attempt.amount, + original_amount, surcharge: match surcharge_details.surcharge { surcharge_decision_configs::SurchargeOutput::Fixed { amount } => { common_utils_types::Surcharge::Fixed(amount) @@ -322,7 +323,7 @@ fn get_surcharge_details_from_surcharge_output( tax_on_surcharge: surcharge_details.tax_on_surcharge, surcharge_amount, tax_on_surcharge_amount, - final_amount: payment_attempt.amount + surcharge_amount + tax_on_surcharge_amount, + final_amount: original_amount + surcharge_amount + tax_on_surcharge_amount, }) } diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index 21cdec92ccb..319acd740bf 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -173,6 +173,10 @@ where let mut connector_http_status_code = None; let mut external_latency = None; if let Some(connector_details) = connector { + operation + .to_domain()? + .populate_payment_data(state, &mut payment_data, &merchant_account) + .await?; // Fetch and check FRM configs #[cfg(feature = "frm")] let mut frm_info = None; @@ -499,7 +503,9 @@ where .surcharge_applicable .unwrap_or(false) { - if let Some(surcharge_details) = payment_data.payment_attempt.get_surcharge_details() { + if let Some(surcharge_details) = + payment_data.payment_attempt.get_request_surcharge_details() + { // if retry payment, surcharge would have been populated from the previous attempt. Use the same surcharge let surcharge_details = types::SurchargeDetails::from((&surcharge_details, &payment_data.payment_attempt)); @@ -541,16 +547,12 @@ where payment_data.surcharge_details = calculated_surcharge_details; } else { - let surcharge_details = - payment_data - .payment_attempt - .get_surcharge_details() - .map(|surcharge_details| { - types::SurchargeDetails::from(( - &surcharge_details, - &payment_data.payment_attempt, - )) - }); + let surcharge_details = payment_data + .payment_attempt + .get_request_surcharge_details() + .map(|surcharge_details| { + types::SurchargeDetails::from((&surcharge_details, &payment_data.payment_attempt)) + }); payment_data.surcharge_details = surcharge_details; } Ok(()) @@ -578,13 +580,17 @@ pub async fn call_surcharge_decision_management_for_session_flow( where O: Send + Clone + Sync, { - if let Some(surcharge_amount) = payment_data.payment_attempt.surcharge_amount { - let tax_on_surcharge_amount = payment_data.payment_attempt.tax_amount.unwrap_or(0); - let final_amount = - payment_data.payment_attempt.amount + surcharge_amount + tax_on_surcharge_amount; + if let Some(surcharge_amount) = payment_data.payment_attempt.amount.get_surcharge_amount() { + let tax_on_surcharge_amount = payment_data + .payment_attempt + .amount + .get_tax_amount_on_surcharge() + .unwrap_or(0); + let original_amount = payment_data.payment_attempt.amount.get_original_amount(); + let final_amount = original_amount + surcharge_amount + tax_on_surcharge_amount; Ok(Some(api::SessionSurchargeDetails::PreDetermined( types::SurchargeDetails { - original_amount: payment_data.payment_attempt.amount, + original_amount, surcharge: Surcharge::Fixed(surcharge_amount), tax_on_surcharge: None, surcharge_amount, @@ -1029,11 +1035,6 @@ where merchant_connector_account.get_mca_id(); } - operation - .to_domain()? - .populate_payment_data(state, payment_data, merchant_account) - .await?; - let (pd, tokenization_action) = get_connector_tokenization_action_when_confirm_true( state, operation, diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index 7230d74e9a9..888c2560a8a 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -617,17 +617,22 @@ pub fn validate_amount_to_capture_and_capture_method( .map(|payment_attempt| payment_attempt.capture_method.unwrap_or_default())) .unwrap_or_default(); if capture_method == api_enums::CaptureMethod::Automatic { - let original_amount = request - .amount - .map(|amount| amount.into()) - .or(payment_attempt.map(|payment_attempt| payment_attempt.amount)); + let original_amount = + request + .amount + .map(|amount| amount.into()) + .or(payment_attempt + .map(|payment_attempt| payment_attempt.amount.get_original_amount())); let surcharge_amount = request .surcharge_details .map(|surcharge_details| surcharge_details.get_total_surcharge_amount()) .or_else(|| { payment_attempt.map(|payment_attempt| { - payment_attempt.surcharge_amount.unwrap_or(0) - + payment_attempt.tax_amount.unwrap_or(0) + payment_attempt.amount.get_surcharge_amount().unwrap_or(0) + + payment_attempt + .amount + .get_tax_amount_on_surcharge() + .unwrap_or(0) }) }) .unwrap_or(0); @@ -2570,7 +2575,7 @@ mod tests { payment_id: "23".to_string(), merchant_id: "22".to_string(), status: storage_enums::IntentStatus::RequiresCapture, - amount: 200, + original_amount: 200, currency: None, amount_captured: None, customer_id: None, @@ -2625,7 +2630,7 @@ mod tests { payment_id: "23".to_string(), merchant_id: "22".to_string(), status: storage_enums::IntentStatus::RequiresCapture, - amount: 200, + original_amount: 200, currency: None, amount_captured: None, customer_id: None, @@ -2679,7 +2684,7 @@ mod tests { payment_id: "23".to_string(), merchant_id: "22".to_string(), status: storage_enums::IntentStatus::RequiresCapture, - amount: 200, + original_amount: 200, currency: None, amount_captured: None, customer_id: None, @@ -3086,7 +3091,7 @@ impl AttemptType { // A new payment attempt is getting created so, used the same function which is used to populate status in PaymentCreate Flow. status: payment_attempt_status_fsm(payment_method_data, Some(true)), - amount: old_payment_attempt.amount, + amount: old_payment_attempt.amount.get_original_amount(), currency: old_payment_attempt.currency, save_to_locker: old_payment_attempt.save_to_locker, @@ -3131,14 +3136,14 @@ impl AttemptType { error_reason: None, multiple_capture_count: None, connector_response_reference_id: None, - amount_capturable: old_payment_attempt.amount, + amount_capturable: old_payment_attempt.amount.get_original_amount(), updated_by: storage_scheme.to_string(), authentication_data: None, encoded_data: None, merchant_connector_id: None, unified_code: None, unified_message: None, - net_amount: old_payment_attempt.amount, + net_amount: old_payment_attempt.amount.get_original_amount(), } } diff --git a/crates/router/src/core/payments/operations/payment_approve.rs b/crates/router/src/core/payments/operations/payment_approve.rs index cddbc89acff..eec1bfc4855 100644 --- a/crates/router/src/core/payments/operations/payment_approve.rs +++ b/crates/router/src/core/payments/operations/payment_approve.rs @@ -139,7 +139,7 @@ impl payment_method_type.or(payment_attempt.payment_method_type); payment_attempt.payment_experience = request.payment_experience; currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); helpers::validate_customer_id_mandatory_cases( request.setup_future_usage.is_some(), diff --git a/crates/router/src/core/payments/operations/payment_cancel.rs b/crates/router/src/core/payments/operations/payment_cancel.rs index 9810980cd34..40d272314ff 100644 --- a/crates/router/src/core/payments/operations/payment_cancel.rs +++ b/crates/router/src/core/payments/operations/payment_cancel.rs @@ -102,7 +102,7 @@ impl .await?; let currency = payment_attempt.currency.get_required_value("currency")?; - let amount = payment_attempt.get_total_amount().into(); + let amount = payment_attempt.amount.get_authorize_amount().into(); payment_attempt.cancellation_reason = request.cancellation_reason.clone(); diff --git a/crates/router/src/core/payments/operations/payment_capture.rs b/crates/router/src/core/payments/operations/payment_capture.rs index 3986b16ce35..51ab9a07ecd 100644 --- a/crates/router/src/core/payments/operations/payment_capture.rs +++ b/crates/router/src/core/payments/operations/payment_capture.rs @@ -124,7 +124,7 @@ impl currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); let shipping_address = helpers::create_or_find_address_for_payment_by_request( db, diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs index adc137403e5..ef0951552b8 100644 --- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs +++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs @@ -135,7 +135,7 @@ impl .payment_experience .or(payment_attempt.payment_experience); currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); helpers::validate_customer_id_mandatory_cases( request.setup_future_usage.is_some(), diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs index c81145c5de7..5af22b7e4d4 100644 --- a/crates/router/src/core/payments/operations/payment_confirm.rs +++ b/crates/router/src/core/payments/operations/payment_confirm.rs @@ -110,7 +110,7 @@ impl if let Some(order_details) = &request.order_details { helpers::validate_order_details_amount( order_details.to_owned(), - payment_intent.amount, + payment_intent.original_amount, false, )?; } @@ -374,7 +374,7 @@ impl payment_attempt.capture_method = request.capture_method.or(payment_attempt.capture_method); currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); helpers::validate_customer_id_mandatory_cases( request.setup_future_usage.is_some(), @@ -705,7 +705,7 @@ impl .surcharge_details .as_ref() .map(|surcharge_details| surcharge_details.final_amount) - .unwrap_or(payment_data.payment_attempt.amount); + .unwrap_or(payment_data.payment_attempt.amount.get_authorize_amount()); let m_payment_data_payment_attempt = payment_data.payment_attempt.clone(); let m_browser_info = browser_info.clone(); @@ -883,7 +883,7 @@ impl m_db.update_payment_attempt_with_attempt_id( m_payment_data_payment_attempt, storage::PaymentAttemptUpdate::ConfirmUpdate { - amount: payment_data.payment_attempt.amount, + amount: payment_data.payment_attempt.amount.get_original_amount(), currency: payment_data.currency, status: attempt_status, payment_method, @@ -932,7 +932,7 @@ impl m_db.update_payment_intent( m_payment_data_payment_intent, storage::PaymentIntentUpdate::Update { - amount: payment_data.payment_intent.amount, + amount: payment_data.payment_intent.original_amount, currency: payment_data.currency, setup_future_usage, status: intent_status, diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index 2b25a74deb1..5a15c085a80 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -244,7 +244,7 @@ impl if let Some(order_details) = &request.order_details { helpers::validate_order_details_amount( order_details.to_owned(), - payment_intent.amount, + payment_intent.original_amount, false, )?; } @@ -339,7 +339,7 @@ impl .map(|(payment_method_data, additional_payment_data)| { payment_method_data.apply_additional_payment_data(additional_payment_data) }); - let amount = payment_attempt.get_total_amount().into(); + let amount = payment_attempt.amount.get_authorize_amount().into(); let payment_data = PaymentData { flow: PhantomData, payment_intent, @@ -507,7 +507,7 @@ impl .payment_attempt .straight_through_algorithm .clone(); - let authorized_amount = payment_data.payment_attempt.amount; + let authorized_amount = payment_data.payment_attempt.amount.get_authorize_amount(); let merchant_connector_id = payment_data.payment_attempt.merchant_connector_id.clone(); let surcharge_amount = payment_data diff --git a/crates/router/src/core/payments/operations/payment_reject.rs b/crates/router/src/core/payments/operations/payment_reject.rs index 37c7dfd1bae..a605c81be79 100644 --- a/crates/router/src/core/payments/operations/payment_reject.rs +++ b/crates/router/src/core/payments/operations/payment_reject.rs @@ -100,7 +100,7 @@ impl .await?; let currency = payment_attempt.currency.get_required_value("currency")?; - let amount = payment_attempt.get_total_amount().into(); + let amount = payment_attempt.amount.get_authorize_amount().into(); let frm_response = db .find_fraud_check_by_payment_id(payment_intent.payment_id.clone(), merchant_account.merchant_id.clone()) diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 9ab0b4f817f..cbf17c08f4a 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -100,6 +100,8 @@ impl PostUpdateTracker, types::PaymentsIncrementalAu report!(errors::ApiErrorResponse::InternalServerError) .attach_printable("missing incremental_authorization_details in payment_data") })?; + let updated_original_amount = payment_data.payment_attempt.amount.get_original_amount() + + incremental_authorization_details.additional_amount; // Update payment_intent and payment_attempt 'amount' if incremental_authorization is successful let (option_payment_attempt_update, option_payment_intent_update) = match router_data.response.clone() { @@ -111,12 +113,12 @@ impl PostUpdateTracker, types::PaymentsIncrementalAu if status == AuthorizationStatus::Success { (Some( storage::PaymentAttemptUpdate::IncrementalAuthorizationAmountUpdate { - amount: incremental_authorization_details.total_amount, + amount: updated_original_amount, amount_capturable: incremental_authorization_details.total_amount, }, ), Some( storage::PaymentIntentUpdate::IncrementalAuthorizationAmountUpdate { - amount: incremental_authorization_details.total_amount, + amount: updated_original_amount, }, )) } else { @@ -698,7 +700,7 @@ async fn payment_response_update_tracker( multiple_capture_data.update_capture(updated_capture); } - let authorized_amount = payment_data.payment_attempt.get_total_amount(); + let authorized_amount = payment_data.payment_attempt.amount.get_authorize_amount(); payment_attempt_update = Some(storage::PaymentAttemptUpdate::AmountToCaptureUpdate { status: multiple_capture_data.get_attempt_status(authorized_amount), diff --git a/crates/router/src/core/payments/operations/payment_session.rs b/crates/router/src/core/payments/operations/payment_session.rs index 9a58dd5af76..7a84a8bfa13 100644 --- a/crates/router/src/core/payments/operations/payment_session.rs +++ b/crates/router/src/core/payments/operations/payment_session.rs @@ -82,7 +82,7 @@ impl payment_attempt.payment_method = Some(storage_enums::PaymentMethod::Wallet); - let amount = payment_attempt.get_total_amount().into(); + let amount = payment_attempt.amount.get_authorize_amount().into(); let shipping_address = helpers::create_or_find_address_for_payment_by_request( db, diff --git a/crates/router/src/core/payments/operations/payment_start.rs b/crates/router/src/core/payments/operations/payment_start.rs index 557c5c0bd8c..c33112ae01f 100644 --- a/crates/router/src/core/payments/operations/payment_start.rs +++ b/crates/router/src/core/payments/operations/payment_start.rs @@ -81,7 +81,7 @@ impl .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?; currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); let shipping_address = helpers::create_or_find_address_for_payment_by_request( db, diff --git a/crates/router/src/core/payments/operations/payment_status.rs b/crates/router/src/core/payments/operations/payment_status.rs index c6d9a30f0c9..7e9f4f8157f 100644 --- a/crates/router/src/core/payments/operations/payment_status.rs +++ b/crates/router/src/core/payments/operations/payment_status.rs @@ -242,7 +242,7 @@ async fn get_tracker_for_sync< let payment_id_str = payment_attempt.payment_id.clone(); currency = payment_attempt.currency.get_required_value("currency")?; - amount = payment_attempt.get_total_amount().into(); + amount = payment_attempt.amount.get_authorize_amount().into(); let shipping_address = helpers::get_address_by_id( db, diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs index e002b92d181..5ae4c6c4818 100644 --- a/crates/router/src/core/payments/operations/payment_update.rs +++ b/crates/router/src/core/payments/operations/payment_update.rs @@ -63,7 +63,7 @@ impl if let Some(order_details) = &request.order_details { helpers::validate_order_details_amount( order_details.to_owned(), - payment_intent.amount, + payment_intent.original_amount, false, )?; } @@ -132,7 +132,7 @@ impl request.amount_to_capture, request .surcharge_details - .or(payment_attempt.get_surcharge_details()), + .or(payment_attempt.get_request_surcharge_details()), ) .change_context(errors::ApiErrorResponse::InvalidDataFormat { field_name: "amount_to_capture".to_string(), @@ -151,7 +151,7 @@ impl let amount = request .amount - .unwrap_or_else(|| payment_attempt.amount.into()); + .unwrap_or_else(|| payment_attempt.amount.get_original_amount().into()); if request.confirm.unwrap_or(false) { helpers::validate_customer_id_mandatory_cases( @@ -277,14 +277,14 @@ impl let amount = request .amount .map(Into::into) - .unwrap_or(payment_attempt.amount); - payment_attempt.amount = amount; - payment_intent.amount = amount; + .unwrap_or(payment_attempt.amount.get_original_amount()); + payment_attempt.amount.set_original_amount(amount); + payment_intent.original_amount = amount; let surcharge_amount = request .surcharge_details .as_ref() .map(RequestSurchargeDetails::get_total_surcharge_amount) - .or(payment_attempt.get_total_surcharge_amount()); + .or(payment_attempt.amount.get_total_surcharge_amount()); (amount + surcharge_amount.unwrap_or(0)).into() }; (Box::new(operations::PaymentConfirm), amount) diff --git a/crates/router/src/core/payments/operations/payments_incremental_authorization.rs b/crates/router/src/core/payments/operations/payments_incremental_authorization.rs index 51fdff77c3b..2256a49313b 100644 --- a/crates/router/src/core/payments/operations/payments_incremental_authorization.rs +++ b/crates/router/src/core/payments/operations/payments_incremental_authorization.rs @@ -74,12 +74,6 @@ impl })? } - if request.amount < payment_intent.amount { - Err(errors::ApiErrorResponse::PreconditionFailed { - message: "Amount should be greater than original authorized amount".to_owned(), - })? - } - let attempt_id = payment_intent.active_attempt.get_id().clone(); let payment_attempt = db .find_payment_attempt_by_payment_id_merchant_id_attempt_id( @@ -91,8 +85,14 @@ impl .await .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?; + if request.amount < payment_attempt.amount.get_authorize_amount() { + Err(errors::ApiErrorResponse::PreconditionFailed { + message: "Amount should be greater than original authorized amount".to_owned(), + })? + } + let currency = payment_attempt.currency.get_required_value("currency")?; - let amount = payment_attempt.get_total_amount(); + let previously_amount = payment_attempt.amount.get_authorize_amount(); let profile_id = payment_intent .profile_id @@ -114,7 +114,7 @@ impl payment_intent, payment_attempt, currency, - amount: amount.into(), + amount: previously_amount.into(), email: None, mandate_id: None, mandate_connector: None, @@ -143,7 +143,7 @@ impl frm_message: None, payment_link_data: None, incremental_authorization_details: Some(IncrementalAuthorizationDetails { - additional_amount: request.amount - amount, + additional_amount: request.amount - previously_amount, total_amount: request.amount, reason: request.reason.clone(), authorization_id: None, @@ -213,7 +213,10 @@ impl error_code: None, error_message: None, connector_authorization_id: None, - previously_authorized_amount: payment_data.payment_intent.amount, + previously_authorized_amount: payment_data + .payment_attempt + .amount + .get_authorize_amount(), }; let authorization = db .store diff --git a/crates/router/src/core/payments/retry.rs b/crates/router/src/core/payments/retry.rs index 0fd45c5af3b..f415bfc1ade 100644 --- a/crates/router/src/core/payments/retry.rs +++ b/crates/router/src/core/payments/retry.rs @@ -463,13 +463,13 @@ pub fn make_new_payment_attempt( payment_id: old_payment_attempt.payment_id, merchant_id: old_payment_attempt.merchant_id, status: old_payment_attempt.status, - amount: old_payment_attempt.amount, + amount: old_payment_attempt.amount.get_original_amount(), currency: old_payment_attempt.currency, save_to_locker: old_payment_attempt.save_to_locker, offer_amount: old_payment_attempt.offer_amount, - surcharge_amount: old_payment_attempt.surcharge_amount, - tax_amount: old_payment_attempt.tax_amount, + surcharge_amount: old_payment_attempt.amount.get_surcharge_amount(), + tax_amount: old_payment_attempt.amount.get_tax_amount_on_surcharge(), payment_method_id: old_payment_attempt.payment_method_id, payment_method: old_payment_attempt.payment_method, payment_method_type: old_payment_attempt.payment_method_type, diff --git a/crates/router/src/core/payments/routing.rs b/crates/router/src/core/payments/routing.rs index 96cd6561519..d58d769a63b 100644 --- a/crates/router/src/core/payments/routing.rs +++ b/crates/router/src/core/payments/routing.rs @@ -150,7 +150,7 @@ where }; let payment_input = dsl_inputs::PaymentInput { - amount: payment_data.payment_intent.amount, + amount: payment_data.payment_intent.original_amount, card_bin: payment_data .payment_method_data .as_ref() @@ -772,7 +772,7 @@ pub async fn perform_session_flow_routing( }; let payment_input = dsl_inputs::PaymentInput { - amount: session_input.payment_intent.amount, + amount: session_input.payment_intent.original_amount, currency: session_input .payment_intent .currency @@ -1011,7 +1011,7 @@ pub fn make_dsl_input_for_surcharge( payment_type: None, }; let payment_input = dsl_inputs::PaymentInput { - amount: payment_attempt.amount, + amount: payment_attempt.amount.get_authorize_amount(), // currency is always populated in payment_attempt during payment create currency: payment_attempt .currency diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs index dffcff23595..5b577b5bbca 100644 --- a/crates/router/src/core/payments/transformers.rs +++ b/crates/router/src/core/payments/transformers.rs @@ -363,7 +363,7 @@ where .as_ref() .get_required_value("currency")?; let amount = currency - .to_currency_base_unit(payment_attempt.amount) + .to_currency_base_unit(payment_attempt.amount.get_authorize_amount()) .into_report() .change_context(errors::ApiErrorResponse::InvalidDataValue { field_name: "amount", @@ -439,13 +439,13 @@ where .change_context(errors::ApiErrorResponse::InvalidDataValue { field_name: "payment_method_data", })?; - let surcharge_details = - payment_attempt - .surcharge_amount - .map(|surcharge_amount| RequestSurchargeDetails { - surcharge_amount, - tax_amount: payment_attempt.tax_amount, - }); + let surcharge_details = payment_attempt + .amount + .get_surcharge_amount() + .map(|surcharge_amount| RequestSurchargeDetails { + surcharge_amount, + tax_amount: payment_attempt.amount.get_tax_amount_on_surcharge(), + }); let merchant_decision = payment_intent.merchant_decision.to_owned(); let frm_message = payment_data.frm_message.map(FrmMessage::foreign_from); @@ -565,11 +565,11 @@ where }); services::ApplicationResponse::JsonWithHeaders(( response - .set_net_amount(payment_attempt.net_amount) + .set_net_amount(payment_attempt.amount.get_authorize_amount()) .set_payment_id(Some(payment_attempt.payment_id)) .set_merchant_id(Some(payment_attempt.merchant_id)) .set_status(payment_intent.status) - .set_amount(payment_attempt.amount) + .set_amount(payment_attempt.amount.get_original_amount()) .set_amount_capturable(Some(payment_attempt.amount_capturable)) .set_amount_received(payment_intent.amount_captured) .set_surcharge_details(surcharge_details) @@ -716,11 +716,11 @@ where } None => services::ApplicationResponse::JsonWithHeaders(( api::PaymentsResponse { - net_amount: payment_attempt.net_amount, + net_amount: payment_attempt.amount.get_authorize_amount(), payment_id: Some(payment_attempt.payment_id), merchant_id: Some(payment_attempt.merchant_id), status: payment_intent.status, - amount: payment_attempt.amount, + amount: payment_attempt.amount.get_original_amount(), amount_capturable: None, amount_received: payment_intent.amount_captured, client_secret: payment_intent.client_secret.map(masking::Secret::new), @@ -859,7 +859,7 @@ impl ForeignFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::Pay payment_id: Some(pi.payment_id), merchant_id: Some(pi.merchant_id), status: pi.status, - amount: pi.amount, + amount: pi.original_amount, amount_capturable: pi.amount_captured, client_secret: pi.client_secret.map(|s| s.into()), created: Some(pi.created_at), diff --git a/crates/router/src/core/payments/types.rs b/crates/router/src/core/payments/types.rs index 41a8850ab3d..ad7976e801f 100644 --- a/crates/router/src/core/payments/types.rs +++ b/crates/router/src/core/payments/types.rs @@ -199,13 +199,14 @@ impl From<(&RequestSurchargeDetails, &PaymentAttempt)> for SurchargeDetails { ) -> Self { let surcharge_amount = request_surcharge_details.surcharge_amount; let tax_on_surcharge_amount = request_surcharge_details.tax_amount.unwrap_or(0); + let original_amount = payment_attempt.amount.get_original_amount(); Self { - original_amount: payment_attempt.amount, + original_amount, surcharge: common_types::Surcharge::Fixed(request_surcharge_details.surcharge_amount), tax_on_surcharge: None, surcharge_amount, tax_on_surcharge_amount, - final_amount: payment_attempt.amount + surcharge_amount + tax_on_surcharge_amount, + final_amount: original_amount + surcharge_amount + tax_on_surcharge_amount, } } } diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs index e60c341dedc..e29336c32cb 100644 --- a/crates/router/src/core/refunds.rs +++ b/crates/router/src/core/refunds.rs @@ -167,7 +167,7 @@ pub async fn trigger_refund_to_gateway( &routed_through, merchant_account, key_store, - (payment_attempt.amount, currency), + (payment_attempt.amount.get_authorize_amount(), currency), payment_intent, payment_attempt, refund, @@ -436,7 +436,7 @@ pub async fn sync_refund_with_gateway( &connector_id, merchant_account, key_store, - (payment_attempt.amount, currency), + (payment_attempt.amount.get_authorize_amount(), currency), payment_intent, payment_attempt, refund, @@ -612,7 +612,7 @@ pub async fn validate_and_create_refund( let total_amount_captured = payment_intent .amount_captured - .unwrap_or(payment_attempt.amount); + .unwrap_or(payment_attempt.amount.get_authorize_amount()); validator::validate_refund_amount(total_amount_captured, &all_refunds, refund_amount) .change_context(errors::ApiErrorResponse::RefundAmountExceedsPaymentAmount)?; @@ -639,7 +639,7 @@ pub async fn validate_and_create_refund( .set_connector_transaction_id(connecter_transaction_id.to_string()) .set_connector(connector) .set_refund_type(req.refund_type.unwrap_or_default().foreign_into()) - .set_total_amount(payment_attempt.amount) + .set_total_amount(payment_attempt.amount.get_authorize_amount()) .set_refund_amount(refund_amount) .set_currency(currency) .set_created_at(Some(common_utils::date_time::now())) diff --git a/crates/router/src/services/kafka/payment_attempt.rs b/crates/router/src/services/kafka/payment_attempt.rs index ea0721f418e..353ea65c6d3 100644 --- a/crates/router/src/services/kafka/payment_attempt.rs +++ b/crates/router/src/services/kafka/payment_attempt.rs @@ -48,14 +48,14 @@ impl<'a> KafkaPaymentAttempt<'a> { merchant_id: &attempt.merchant_id, attempt_id: &attempt.attempt_id, status: attempt.status, - amount: attempt.amount, + amount: attempt.amount.get_original_amount(), currency: attempt.currency, save_to_locker: attempt.save_to_locker, connector: attempt.connector.as_ref(), error_message: attempt.error_message.as_ref(), offer_amount: attempt.offer_amount, - surcharge_amount: attempt.surcharge_amount, - tax_amount: attempt.tax_amount, + surcharge_amount: attempt.amount.get_surcharge_amount(), + tax_amount: attempt.amount.get_tax_amount_on_surcharge(), payment_method_id: attempt.payment_method_id.as_ref(), payment_method: attempt.payment_method, connector_transaction_id: attempt.connector_transaction_id.as_ref(), diff --git a/crates/router/src/services/kafka/payment_intent.rs b/crates/router/src/services/kafka/payment_intent.rs index 70980a6e865..b8acde96338 100644 --- a/crates/router/src/services/kafka/payment_intent.rs +++ b/crates/router/src/services/kafka/payment_intent.rs @@ -37,7 +37,7 @@ impl<'a> KafkaPaymentIntent<'a> { payment_id: &intent.payment_id, merchant_id: &intent.merchant_id, status: intent.status, - amount: intent.amount, + amount: intent.original_amount, currency: intent.currency, amount_captured: intent.amount_captured, customer_id: intent.customer_id.as_ref(), diff --git a/crates/router/src/types.rs b/crates/router/src/types.rs index e236113e676..e8dc827a41d 100644 --- a/crates/router/src/types.rs +++ b/crates/router/src/types.rs @@ -658,7 +658,7 @@ impl Capturable for PaymentsAuthorizeData { | common_enums::IntentStatus::PartiallyCapturedAndCapturable => None, } }, - common_enums::CaptureMethod::Manual => Some(payment_data.payment_attempt.get_total_amount()), + common_enums::CaptureMethod::Manual => Some(payment_data.payment_attempt.amount.get_authorize_amount()), // In case of manual multiple, amount capturable must be inferred from all captures. common_enums::CaptureMethod::ManualMultiple | // Scheduled capture is not supported as of now @@ -735,7 +735,7 @@ impl Capturable for CompleteAuthorizeData { | common_enums::IntentStatus::PartiallyCapturedAndCapturable => None, } }, - common_enums::CaptureMethod::Manual => Some(payment_data.payment_attempt.get_total_amount()), + common_enums::CaptureMethod::Manual => Some(payment_data.payment_attempt.amount.get_authorize_amount()), // In case of manual multiple, amount capturable must be inferred from all captures. common_enums::CaptureMethod::ManualMultiple | // Scheduled capture is not supported as of now @@ -799,7 +799,7 @@ impl Capturable for PaymentsSyncData { payment_data .payment_attempt .amount_to_capture - .or_else(|| Some(payment_data.payment_attempt.get_total_amount())) + .or_else(|| Some(payment_data.payment_attempt.amount.get_authorize_amount())) } fn get_amount_capturable( &self, diff --git a/crates/router/src/types/storage/payment_attempt.rs b/crates/router/src/types/storage/payment_attempt.rs index 13b9f3dd5d5..be0f6afed05 100644 --- a/crates/router/src/types/storage/payment_attempt.rs +++ b/crates/router/src/types/storage/payment_attempt.rs @@ -15,8 +15,9 @@ pub trait PaymentAttemptExt { ) -> RouterResult; fn get_next_capture_id(&self) -> String; - fn get_total_amount(&self) -> i64; - fn get_surcharge_details(&self) -> Option; + fn get_request_surcharge_details( + &self, + ) -> Option; } impl PaymentAttemptExt for PaymentAttempt { @@ -58,17 +59,16 @@ impl PaymentAttemptExt for PaymentAttempt { let next_sequence_number = self.multiple_capture_count.unwrap_or_default() + 1; format!("{}_{}", self.attempt_id.clone(), next_sequence_number) } - fn get_surcharge_details(&self) -> Option { - self.surcharge_amount.map(|surcharge_amount| { + fn get_request_surcharge_details( + &self, + ) -> Option { + self.amount.get_surcharge_amount().map(|surcharge_amount| { api_models::payments::RequestSurchargeDetails { surcharge_amount, - tax_amount: self.tax_amount, + tax_amount: self.amount.get_tax_amount_on_surcharge(), } }) } - fn get_total_amount(&self) -> i64 { - self.amount + self.surcharge_amount.unwrap_or(0) + self.tax_amount.unwrap_or(0) - } } pub trait AttemptStatusExt { diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs index 786a8c55182..9d1060ba61a 100644 --- a/crates/router/src/types/transformers.rs +++ b/crates/router/src/types/transformers.rs @@ -817,7 +817,7 @@ impl ForeignFrom for api_models::payments::PaymentAttem Self { attempt_id: payment_attempt.attempt_id, status: payment_attempt.status, - amount: payment_attempt.amount, + amount: payment_attempt.amount.get_original_amount(), currency: payment_attempt.currency, connector: payment_attempt.connector, error_message: payment_attempt.error_reason, @@ -937,7 +937,8 @@ impl currency: payment_attempt.map(|pa| pa.currency.unwrap_or_default()), shipping: shipping.map(api_types::Address::from), billing: billing.map(api_types::Address::from), - amount: payment_attempt.map(|pa| api_types::Amount::from(pa.amount)), + amount: payment_attempt + .map(|pa| api_types::Amount::from(pa.amount.get_original_amount())), email: customer .and_then(|cust| cust.email.as_ref().map(|em| pii::Email::from(em.clone()))), phone: customer.and_then(|cust| cust.phone.as_ref().map(|p| p.clone().into_inner())), diff --git a/crates/storage_impl/src/mock_db/payment_attempt.rs b/crates/storage_impl/src/mock_db/payment_attempt.rs index 24863ddc568..52c1bfc9f16 100644 --- a/crates/storage_impl/src/mock_db/payment_attempt.rs +++ b/crates/storage_impl/src/mock_db/payment_attempt.rs @@ -3,7 +3,8 @@ use common_utils::errors::CustomResult; use data_models::{ errors::StorageError, payments::payment_attempt::{ - PaymentAttempt, PaymentAttemptInterface, PaymentAttemptNew, PaymentAttemptUpdate, + GetAttemptAmount, PaymentAttempt, PaymentAttemptInterface, PaymentAttemptNew, + PaymentAttemptUpdate, }, }; use diesel_models::enums as storage_enums; @@ -100,19 +101,16 @@ impl PaymentAttemptInterface for MockDb { let payment_attempt = payment_attempt.populate_derived_fields(); let payment_attempt = PaymentAttempt { id, + amount: payment_attempt.get_attempt_amount(), payment_id: payment_attempt.payment_id, merchant_id: payment_attempt.merchant_id, attempt_id: payment_attempt.attempt_id, status: payment_attempt.status, - amount: payment_attempt.amount, - net_amount: payment_attempt.net_amount, currency: payment_attempt.currency, save_to_locker: payment_attempt.save_to_locker, connector: payment_attempt.connector, error_message: payment_attempt.error_message, offer_amount: payment_attempt.offer_amount, - surcharge_amount: payment_attempt.surcharge_amount, - tax_amount: payment_attempt.tax_amount, payment_method_id: payment_attempt.payment_method_id, payment_method: payment_attempt.payment_method, connector_transaction_id: None, diff --git a/crates/storage_impl/src/mock_db/payment_intent.rs b/crates/storage_impl/src/mock_db/payment_intent.rs index 3f892ed9fa7..c5ac20b185a 100644 --- a/crates/storage_impl/src/mock_db/payment_intent.rs +++ b/crates/storage_impl/src/mock_db/payment_intent.rs @@ -74,7 +74,7 @@ impl PaymentIntentInterface for MockDb { payment_id: new.payment_id, merchant_id: new.merchant_id, status: new.status, - amount: new.amount, + original_amount: new.amount, currency: new.currency, amount_captured: new.amount_captured, customer_id: new.customer_id, diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs index f8f752c6bc8..57a0f113350 100644 --- a/crates/storage_impl/src/payments/payment_attempt.rs +++ b/crates/storage_impl/src/payments/payment_attempt.rs @@ -5,8 +5,8 @@ use data_models::{ mandates::{MandateAmountData, MandateDataType}, payments::{ payment_attempt::{ - PaymentAttempt, PaymentAttemptInterface, PaymentAttemptNew, PaymentAttemptUpdate, - PaymentListFilters, + GetAttemptAmount, PaymentAttempt, PaymentAttemptInterface, PaymentAttemptNew, + PaymentAttemptUpdate, PaymentListFilters, }, PaymentIntent, }, @@ -331,6 +331,7 @@ impl PaymentAttemptInterface for KVRouterStore { .await } MerchantStorageScheme::RedisKv => { + let payment_attempt = payment_attempt.populate_derived_fields(); let payment_attempt = payment_attempt.populate_derived_fields(); let key = format!( "mid_{}_pid_{}", @@ -343,15 +344,12 @@ impl PaymentAttemptInterface for KVRouterStore { merchant_id: payment_attempt.merchant_id.clone(), attempt_id: payment_attempt.attempt_id.clone(), status: payment_attempt.status, - amount: payment_attempt.amount, - net_amount: payment_attempt.net_amount, + amount: payment_attempt.get_attempt_amount(), currency: payment_attempt.currency, save_to_locker: payment_attempt.save_to_locker, connector: payment_attempt.connector.clone(), error_message: payment_attempt.error_message.clone(), offer_amount: payment_attempt.offer_amount, - surcharge_amount: payment_attempt.surcharge_amount, - tax_amount: payment_attempt.tax_amount, payment_method_id: payment_attempt.payment_method_id.clone(), payment_method: payment_attempt.payment_method, connector_transaction_id: None, @@ -1036,15 +1034,15 @@ impl DataModelExt for PaymentAttempt { merchant_id: self.merchant_id, attempt_id: self.attempt_id, status: self.status, - amount: self.amount, - net_amount: Some(self.net_amount), + amount: self.amount.get_original_amount(), + net_amount: Some(self.amount.get_authorize_amount()), currency: self.currency, save_to_locker: self.save_to_locker, connector: self.connector, error_message: self.error_message, offer_amount: self.offer_amount, - surcharge_amount: self.surcharge_amount, - tax_amount: self.tax_amount, + surcharge_amount: self.amount.get_surcharge_amount(), + tax_amount: self.amount.get_tax_amount_on_surcharge(), payment_method_id: self.payment_method_id, payment_method: self.payment_method, connector_transaction_id: self.connector_transaction_id, @@ -1084,20 +1082,17 @@ impl DataModelExt for PaymentAttempt { fn from_storage_model(storage_model: Self::StorageModel) -> Self { Self { - net_amount: storage_model.get_or_calculate_net_amount(), + amount: storage_model.get_attempt_amount(), id: storage_model.id, payment_id: storage_model.payment_id, merchant_id: storage_model.merchant_id, attempt_id: storage_model.attempt_id, status: storage_model.status, - amount: storage_model.amount, currency: storage_model.currency, save_to_locker: storage_model.save_to_locker, connector: storage_model.connector, error_message: storage_model.error_message, offer_amount: storage_model.offer_amount, - surcharge_amount: storage_model.surcharge_amount, - tax_amount: storage_model.tax_amount, payment_method_id: storage_model.payment_method_id, payment_method: storage_model.payment_method, connector_transaction_id: storage_model.connector_transaction_id, @@ -1194,7 +1189,7 @@ impl DataModelExt for PaymentAttemptNew { fn from_storage_model(storage_model: Self::StorageModel) -> Self { Self { - net_amount: storage_model.get_or_calculate_net_amount(), + net_amount: storage_model.calculate_net_amount(), payment_id: storage_model.payment_id, merchant_id: storage_model.merchant_id, attempt_id: storage_model.attempt_id, diff --git a/crates/storage_impl/src/payments/payment_intent.rs b/crates/storage_impl/src/payments/payment_intent.rs index 8d20dfe0f32..c32aa9c0110 100644 --- a/crates/storage_impl/src/payments/payment_intent.rs +++ b/crates/storage_impl/src/payments/payment_intent.rs @@ -66,7 +66,7 @@ impl PaymentIntentInterface for KVRouterStore { payment_id: new.payment_id.clone(), merchant_id: new.merchant_id.clone(), status: new.status, - amount: new.amount, + original_amount: new.amount, currency: new.currency, amount_captured: new.amount_captured, customer_id: new.customer_id.clone(), @@ -830,7 +830,7 @@ impl DataModelExt for PaymentIntent { payment_id: self.payment_id, merchant_id: self.merchant_id, status: self.status, - amount: self.amount, + amount: self.original_amount, currency: self.currency, amount_captured: self.amount_captured, customer_id: self.customer_id, @@ -876,7 +876,7 @@ impl DataModelExt for PaymentIntent { payment_id: storage_model.payment_id, merchant_id: storage_model.merchant_id, status: storage_model.status, - amount: storage_model.amount, + original_amount: storage_model.amount, currency: storage_model.currency, amount_captured: storage_model.amount_captured, customer_id: storage_model.customer_id,