Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor: create separate struct for surcharge details response (#3027)
Hrithikesh57591f819c79
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rsindex dfb8e89997..3343becaaa 100644--- a/crates/api_models/src/payment_methods.rs+++ b/crates/api_models/src/payment_methods.rs@@ -2,8 +2,10 @@ use std::collections::HashMap;use cards::CardNumber;use common_utils::{- consts::SURCHARGE_PERCENTAGE_PRECISION_LENGTH, crypto::OptionalEncryptableName, pii,- types::Percentage,+ consts::SURCHARGE_PERCENTAGE_PRECISION_LENGTH,+ crypto::OptionalEncryptableName,+ pii,+ types::{Percentage, Surcharge},};use serde::de;use utoipa::ToSchema;@@ -14,7 +16,7 @@ use crate::{admin,customers::CustomerId,enums as api_enums,- payments::{self, BankCodeResponse, RequestSurchargeDetails},+ payments::{self, BankCodeResponse},};#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]@@ -337,117 +339,11 @@ pub struct SurchargeDetailsResponse {/// tax on surcharge valuepub tax_on_surcharge: Option<Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>>,/// surcharge amount for this payment- pub surcharge_amount: i64,+ pub display_surcharge_amount: f64,/// tax on surcharge amount for this payment- pub tax_on_surcharge_amount: i64,+ pub display_tax_on_surcharge_amount: f64,/// sum of original amount,- pub final_amount: i64,-}--impl SurchargeDetailsResponse {- pub fn is_request_surcharge_matching(- &self,- request_surcharge_details: RequestSurchargeDetails,- ) -> bool {- request_surcharge_details.surcharge_amount == self.surcharge_amount- && request_surcharge_details.tax_amount.unwrap_or(0) == self.tax_on_surcharge_amount- }- pub fn get_total_surcharge_amount(&self) -> i64 {- self.surcharge_amount + self.tax_on_surcharge_amount- }-}--#[derive(Clone, Debug)]-pub struct SurchargeMetadata {- surcharge_results: HashMap<- (- common_enums::PaymentMethod,- common_enums::PaymentMethodType,- Option<common_enums::CardNetwork>,- ),- SurchargeDetailsResponse,- >,- pub payment_attempt_id: String,-}--impl SurchargeMetadata {- pub fn new(payment_attempt_id: String) -> Self {- Self {- surcharge_results: HashMap::new(),- payment_attempt_id,- }- }- pub fn is_empty_result(&self) -> bool {- self.surcharge_results.is_empty()- }- pub fn get_surcharge_results_size(&self) -> usize {- self.surcharge_results.len()- }- pub fn insert_surcharge_details(- &mut self,- payment_method: &common_enums::PaymentMethod,- payment_method_type: &common_enums::PaymentMethodType,- card_network: Option<&common_enums::CardNetwork>,- surcharge_details: SurchargeDetailsResponse,- ) {- let key = (- payment_method.to_owned(),- payment_method_type.to_owned(),- card_network.cloned(),- );- self.surcharge_results.insert(key, surcharge_details);- }- pub fn get_surcharge_details(- &self,- payment_method: &common_enums::PaymentMethod,- payment_method_type: &common_enums::PaymentMethodType,- card_network: Option<&common_enums::CardNetwork>,- ) -> Option<&SurchargeDetailsResponse> {- let key = &(- payment_method.to_owned(),- payment_method_type.to_owned(),- card_network.cloned(),- );- self.surcharge_results.get(key)- }- pub fn get_surcharge_metadata_redis_key(payment_attempt_id: &str) -> String {- format!("surcharge_metadata_{}", payment_attempt_id)- }- pub fn get_individual_surcharge_key_value_pairs(- &self,- ) -> Vec<(String, SurchargeDetailsResponse)> {- self.surcharge_results- .iter()- .map(|((pm, pmt, card_network), surcharge_details)| {- let key =- Self::get_surcharge_details_redis_hashset_key(pm, pmt, card_network.as_ref());- (key, surcharge_details.to_owned())- })- .collect()- }- pub fn get_surcharge_details_redis_hashset_key(- payment_method: &common_enums::PaymentMethod,- payment_method_type: &common_enums::PaymentMethodType,- card_network: Option<&common_enums::CardNetwork>,- ) -> String {- if let Some(card_network) = card_network {- format!(- "{}_{}_{}",- payment_method, payment_method_type, card_network- )- } else {- format!("{}_{}", payment_method, payment_method_type)- }- }-}--#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]-#[serde(rename_all = "snake_case", tag = "type", content = "value")]-pub enum Surcharge {- /// Fixed Surcharge value- Fixed(i64),- /// Surcharge percentage- Rate(Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>),+ pub display_final_amount: f64,}/// Required fields info used while listing the payment_method_datadiff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex 49f2781a18..eaf0937ef2 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -16,7 +16,6 @@ use crate::{admin, disputes,enums::{self as api_enums},ephemeral_key::EphemeralKeyCreateResponse,- payment_methods::{Surcharge, SurchargeDetailsResponse},refunds,};@@ -340,17 +339,6 @@ impl RequestSurchargeDetails {pub fn is_surcharge_zero(&self) -> bool {self.surcharge_amount == 0 && self.tax_amount.unwrap_or(0) == 0}- pub fn get_surcharge_details_object(&self, original_amount: i64) -> SurchargeDetailsResponse {- let surcharge_amount = self.surcharge_amount;- let tax_on_surcharge_amount = self.tax_amount.unwrap_or(0);- SurchargeDetailsResponse {- surcharge: Surcharge::Fixed(self.surcharge_amount),- tax_on_surcharge: None,- surcharge_amount,- tax_on_surcharge_amount,- final_amount: original_amount + surcharge_amount + tax_on_surcharge_amount,- }- }pub fn get_total_surcharge_amount(&self) -> i64 {self.surcharge_amount + self.tax_amount.unwrap_or(0)}diff --git a/crates/api_models/src/surcharge_decision_configs.rs b/crates/api_models/src/surcharge_decision_configs.rsindex 3ebf8f4274..7ead279455 100644--- a/crates/api_models/src/surcharge_decision_configs.rs+++ b/crates/api_models/src/surcharge_decision_configs.rs@@ -7,21 +7,21 @@ use serde::{Deserialize, Serialize};#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]-pub struct SurchargeDetails {- pub surcharge: Surcharge,+pub struct SurchargeDetailsOutput {+ pub surcharge: SurchargeOutput,pub tax_on_surcharge: Option<Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>>,}#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "snake_case", tag = "type", content = "value")]-pub enum Surcharge {- Fixed(i64),+pub enum SurchargeOutput {+ Fixed { amount: i64 },Rate(Percentage<SURCHARGE_PERCENTAGE_PRECISION_LENGTH>),}#[derive(Debug, Default, Clone, Serialize, Deserialize)]pub struct SurchargeDecisionConfigs {- pub surcharge_details: Option<SurchargeDetails>,+ pub surcharge_details: Option<SurchargeDetailsOutput>,}impl EuclidDirFilter for SurchargeDecisionConfigs {const ALLOWED: &'static [DirKeyKind] = &[diff --git a/crates/common_utils/src/types.rs b/crates/common_utils/src/types.rsindex 111f0f43c0..cf94f2fe26 100644--- a/crates/common_utils/src/types.rs+++ b/crates/common_utils/src/types.rs@@ -2,7 +2,10 @@use error_stack::{IntoReport, ResultExt};use serde::{de::Visitor, Deserialize, Deserializer};-use crate::errors::{CustomResult, PercentageError};+use crate::{+ consts,+ errors::{CustomResult, PercentageError},+};/// Represents Percentage Value between 0 and 100 both inclusive#[derive(Clone, Default, Debug, PartialEq, serde::Serialize)]@@ -136,3 +139,13 @@ impl<'de, const PRECISION: u8> Deserialize<'de> for Percentage<PRECISION> {data.deserialize_map(PercentageVisitor::<PRECISION> {})}}++/// represents surcharge type and value+#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]+#[serde(rename_all = "snake_case", tag = "type", content = "value")]+pub enum Surcharge {+ /// Fixed Surcharge value+ Fixed(i64),+ /// Surcharge percentage+ Rate(Percentage<{ consts::SURCHARGE_PERCENTAGE_PRECISION_LENGTH }>),+}diff --git a/crates/diesel_models/src/payment_intent.rs b/crates/diesel_models/src/payment_intent.rsindex 8d75246610..40d8fd92ca 100644--- a/crates/diesel_models/src/payment_intent.rs+++ b/crates/diesel_models/src/payment_intent.rs@@ -106,7 +106,6 @@ pub struct PaymentIntentNew {pub merchant_decision: Option<String>,pub payment_link_id: Option<String>,pub payment_confirm_source: Option<storage_enums::PaymentSource>,-pub updated_by: String,pub surcharge_applicable: Option<bool>,pub request_incremental_authorization: RequestIncrementalAuthorization,diff --git a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs b/crates/router/src/core/payment_methods/surcharge_decision_configs.rsindex 9a65ec76f2..38ae71754b 100644--- a/crates/router/src/core/payment_methods/surcharge_decision_configs.rs+++ b/crates/router/src/core/payment_methods/surcharge_decision_configs.rs@@ -1,12 +1,10 @@use api_models::{- payment_methods::{self, SurchargeDetailsResponse, SurchargeMetadata},+ payment_methods::SurchargeDetailsResponse,payments::Address,routing,- surcharge_decision_configs::{- self, SurchargeDecisionConfigs, SurchargeDecisionManagerRecord, SurchargeDetails,- },+ surcharge_decision_configs::{self, SurchargeDecisionConfigs, SurchargeDecisionManagerRecord},};-use common_utils::{ext_traits::StringExt, static_cache::StaticCache};+use common_utils::{ext_traits::StringExt, static_cache::StaticCache, types as common_utils_types};use error_stack::{self, IntoReport, ResultExt};use euclid::{backend,@@ -14,7 +12,11 @@ use euclid::{};use router_env::{instrument, tracing};-use crate::{core::payments::PaymentData, db::StorageInterface, types::storage as oss_storage};+use crate::{+ core::payments::{types, PaymentData},+ db::StorageInterface,+ types::{storage as oss_storage, transformers::ForeignTryFrom},+};static CONF_CACHE: StaticCache<VirInterpreterBackendCacheWrapper> = StaticCache::new();use crate::{core::{@@ -55,10 +57,10 @@ pub async fn perform_surcharge_decision_management_for_payment_method_list(billing_address: Option<Address>,response_payment_method_types: &mut [api_models::payment_methods::ResponsePaymentMethodsEnabled],) -> ConditionalConfigResult<(- SurchargeMetadata,+ types::SurchargeMetadata,surcharge_decision_configs::MerchantSurchargeConfigs,)> {- let mut surcharge_metadata = SurchargeMetadata::new(payment_attempt.attempt_id.clone());+ let mut surcharge_metadata = types::SurchargeMetadata::new(payment_attempt.attempt_id.clone());let algorithm_id = if let Some(id) = algorithm_ref.surcharge_config_algo_id {id} else {@@ -101,20 +103,27 @@ pub async fn perform_surcharge_decision_management_for_payment_method_list(Some(card_network_type.card_network.clone());let surcharge_output =execute_dsl_and_get_conditional_config(backend_input.clone(), interpreter)?;+ // let surcharge_details =card_network_type.surcharge_details = surcharge_output.surcharge_details.map(|surcharge_details| {- get_surcharge_details_response(surcharge_details, payment_attempt).map(- |surcharge_details_response| {- surcharge_metadata.insert_surcharge_details(- &payment_methods_enabled.payment_method,- &payment_method_type_response.payment_method_type,- Some(&card_network_type.card_network),- surcharge_details_response.clone(),- );- surcharge_details_response- },- )+ let surcharge_details = get_surcharge_details_from_surcharge_output(+ surcharge_details,+ payment_attempt,+ )?;+ surcharge_metadata.insert_surcharge_details(+ &payment_methods_enabled.payment_method,+ &payment_method_type_response.payment_method_type,+ Some(&card_network_type.card_network),+ surcharge_details.clone(),+ );+ SurchargeDetailsResponse::foreign_try_from((+ &surcharge_details,+ payment_attempt,+ ))+ .into_report()+ .change_context(ConfigError::DslExecutionError)+ .attach_printable("Error while constructing Surcharge response type")}).transpose()?;}@@ -124,17 +133,23 @@ pub async fn perform_surcharge_decision_management_for_payment_method_list(payment_method_type_response.surcharge_details = surcharge_output.surcharge_details.map(|surcharge_details| {- get_surcharge_details_response(surcharge_details, payment_attempt).map(- |surcharge_details_response| {- surcharge_metadata.insert_surcharge_details(- &payment_methods_enabled.payment_method,- &payment_method_type_response.payment_method_type,- None,- surcharge_details_response.clone(),- );- surcharge_details_response- },- )+ let surcharge_details = get_surcharge_details_from_surcharge_output(+ surcharge_details,+ payment_attempt,+ )?;+ surcharge_metadata.insert_surcharge_details(+ &payment_methods_enabled.payment_method,+ &payment_method_type_response.payment_method_type,+ None,+ surcharge_details.clone(),+ );+ SurchargeDetailsResponse::foreign_try_from((+ &surcharge_details,+ payment_attempt,+ ))+ .into_report()+ .change_context(ConfigError::DslExecutionError)+ .attach_printable("Error while constructing Surcharge response type")}).transpose()?;}@@ -148,12 +163,12 @@ pub async fn perform_surcharge_decision_management_for_session_flow<O>(algorithm_ref: routing::RoutingAlgorithmRef,payment_data: &mut PaymentData<O>,payment_method_type_list: &Vec<common_enums::PaymentMethodType>,-) -> ConditionalConfigResult<SurchargeMetadata>+) -> ConditionalConfigResult<types::SurchargeMetadata>whereO: Send + Clone,{let mut surcharge_metadata =- SurchargeMetadata::new(payment_data.payment_attempt.attempt_id.clone());+ types::SurchargeMetadata::new(payment_data.payment_attempt.attempt_id.clone());let algorithm_id = if let Some(id) = algorithm_ref.surcharge_config_algo_id {id} else {@@ -186,8 +201,10 @@ wherelet surcharge_output =execute_dsl_and_get_conditional_config(backend_input.clone(), interpreter)?;if let Some(surcharge_details) = surcharge_output.surcharge_details {- let surcharge_details_response =- get_surcharge_details_response(surcharge_details, &payment_data.payment_attempt)?;+ let surcharge_details_response = get_surcharge_details_from_surcharge_output(+ surcharge_details,+ &payment_data.payment_attempt,+ )?;surcharge_metadata.insert_surcharge_details(&payment_method_type.to_owned().into(),payment_method_type,@@ -199,13 +216,13 @@ whereOk(surcharge_metadata)}-fn get_surcharge_details_response(- surcharge_details: SurchargeDetails,+fn get_surcharge_details_from_surcharge_output(+ surcharge_details: surcharge_decision_configs::SurchargeDetailsOutput,payment_attempt: &oss_storage::PaymentAttempt,-) -> ConditionalConfigResult<SurchargeDetailsResponse> {+) -> ConditionalConfigResult<types::SurchargeDetails> {let surcharge_amount = match surcharge_details.surcharge.clone() {- surcharge_decision_configs::Surcharge::Fixed(value) => value,- surcharge_decision_configs::Surcharge::Rate(percentage) => percentage+ surcharge_decision_configs::SurchargeOutput::Fixed { amount } => amount,+ surcharge_decision_configs::SurchargeOutput::Rate(percentage) => percentage.apply_and_ceil_result(payment_attempt.amount).change_context(ConfigError::DslExecutionError).attach_printable("Failed to Calculate surcharge amount by applying percentage")?,@@ -221,13 +238,13 @@ fn get_surcharge_details_response(}).transpose()?.unwrap_or(0);- Ok(SurchargeDetailsResponse {+ Ok(types::SurchargeDetails {surcharge: match surcharge_details.surcharge {- surcharge_decision_configs::Surcharge::Fixed(surcharge_amount) => {- payment_methods::Surcharge::Fixed(surcharge_amount)+ surcharge_decision_configs::SurchargeOutput::Fixed { amount } => {+ common_utils_types::Surcharge::Fixed(amount)}- surcharge_decision_configs::Surcharge::Rate(percentage) => {- payment_methods::Surcharge::Rate(percentage)+ surcharge_decision_configs::SurchargeOutput::Rate(percentage) => {+ common_utils_types::Surcharge::Rate(percentage)}},tax_on_surcharge: surcharge_details.tax_on_surcharge,diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex 33afa29397..67b1dbcc98 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -15,10 +15,9 @@ use std::{fmt::Debug, marker::PhantomData, ops::Deref, time::Instant, vec::IntoIuse api_models::{self, enums,- payment_methods::{Surcharge, SurchargeDetailsResponse},payments::{self, HeaderPayload},};-use common_utils::{ext_traits::AsyncExt, pii};+use common_utils::{ext_traits::AsyncExt, pii, types::Surcharge};use data_models::mandates::MandateData;use diesel_models::{ephemeral_key, fraud_check::FraudCheck};use error_stack::{IntoReport, ResultExt};@@ -42,6 +41,7 @@ use self::{helpers::get_key_params_for_surcharge_details,operations::{payment_complete_authorize, BoxedOperation, Operation},routing::{self as self_routing, SessionFlowRoutingInput},+ types::SurchargeDetails,};use super::{errors::StorageErrorExt, payment_methods::surcharge_decision_configs, utils as core_utils,@@ -475,8 +475,7 @@ where.payment_attempt.get_surcharge_details().map(|surcharge_details| {- surcharge_details- .get_surcharge_details_object(payment_data.payment_attempt.amount)+ SurchargeDetails::from((&surcharge_details, &payment_data.payment_attempt))});payment_data.surcharge_details = surcharge_details;}@@ -509,7 +508,7 @@ wherelet final_amount =payment_data.payment_attempt.amount + surcharge_amount + tax_on_surcharge_amount;Ok(Some(api::SessionSurchargeDetails::PreDetermined(- SurchargeDetailsResponse {+ SurchargeDetails {surcharge: Surcharge::Fixed(surcharge_amount),tax_on_surcharge: None,surcharge_amount,@@ -1882,7 +1881,7 @@ wherepub recurring_mandate_payment_data: Option<RecurringMandatePaymentData>,pub ephemeral_key: Option<ephemeral_key::EphemeralKey>,pub redirect_response: Option<api_models::payments::RedirectResponse>,- pub surcharge_details: Option<SurchargeDetailsResponse>,+ pub surcharge_details: Option<SurchargeDetails>,pub frm_message: Option<FraudCheck>,pub payment_link_data: Option<api_models::payments::PaymentLinkResponse>,}diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rsindex 0cce91bebe..59b2b53c65 100644--- a/crates/router/src/core/payments/helpers.rs+++ b/crates/router/src/core/payments/helpers.rs@@ -1,6 +1,6 @@use std::borrow::Cow;-use api_models::payments::{CardToken, GetPaymentMethodType};+use api_models::payments::{CardToken, GetPaymentMethodType, RequestSurchargeDetails};use base64::Engine;use common_utils::{ext_traits::{AsyncExt, ByteSliceExt, ValueExt},@@ -572,6 +572,7 @@ pub fn validate_merchant_id(pub fn validate_request_amount_and_amount_to_capture(op_amount: Option<api::Amount>,op_amount_to_capture: Option<i64>,+ surcharge_details: Option<RequestSurchargeDetails>,) -> CustomResult<(), errors::ApiErrorResponse> {match (op_amount, op_amount_to_capture) {(None, _) => Ok(()),@@ -581,7 +582,11 @@ pub fn validate_request_amount_and_amount_to_capture(api::Amount::Value(amount_inner) => {// If both amount and amount to capture is present// then amount to be capture should be less than or equal to request amount- utils::when(!amount_to_capture.le(&amount_inner.get()), || {+ let total_capturable_amount = amount_inner.get()+ + surcharge_details+ .map(|surcharge_details| surcharge_details.get_total_surcharge_amount())+ .unwrap_or(0);+ utils::when(!amount_to_capture.le(&total_capturable_amount), || {Err(report!(errors::ApiErrorResponse::PreconditionFailed {message: format!("amount_to_capture is greater than amount capture_amount: {amount_to_capture:?} request_amount: {amount:?}"@@ -603,13 +608,34 @@ pub fn validate_request_amount_and_amount_to_capture(/// if capture method = automatic, amount_to_capture(if provided) must be equal to amount#[instrument(skip_all)]-pub fn validate_amount_to_capture_in_create_call_request(+pub fn validate_amount_to_capture_and_capture_method(+ payment_attempt: Option<&PaymentAttempt>,request: &api_models::payments::PaymentsRequest,) -> CustomResult<(), errors::ApiErrorResponse> {- if request.capture_method.unwrap_or_default() == api_enums::CaptureMethod::Automatic {- let total_capturable_amount = request.get_total_capturable_amount();- if let Some((amount_to_capture, total_capturable_amount)) =- request.amount_to_capture.zip(total_capturable_amount)+ let capture_method = request+ .capture_method+ .or(payment_attempt+ .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 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)+ })+ })+ .unwrap_or(0);+ let total_capturable_amount =+ original_amount.map(|original_amount| original_amount + surcharge_amount);+ if let Some((total_capturable_amount, amount_to_capture)) =+ total_capturable_amount.zip(request.amount_to_capture){utils::when(amount_to_capture != total_capturable_amount, || {Err(report!(errors::ApiErrorResponse::PreconditionFailed {diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rsindex ac387076d1..bb7d0a931e 100644--- a/crates/router/src/core/payments/operations/payment_create.rs+++ b/crates/router/src/core/payments/operations/payment_create.rs@@ -286,8 +286,8 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>// The operation merges mandate data from both request and payment_attemptlet setup_mandate = setup_mandate.map(MandateData::from);- let surcharge_details = request.surcharge_details.map(|surcharge_details| {- surcharge_details.get_surcharge_details_object(payment_attempt.amount)+ let surcharge_details = request.surcharge_details.map(|request_surcharge_details| {+ payments::SurchargeDetails::from((&request_surcharge_details, &payment_attempt))});let payment_data = PaymentData {@@ -540,14 +540,14 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenhelpers::validate_request_amount_and_amount_to_capture(request.amount,request.amount_to_capture,+ request.surcharge_details,).change_context(errors::ApiErrorResponse::InvalidDataFormat {field_name: "amount_to_capture".to_string(),expected_format: "amount_to_capture lesser than amount".to_string(),})?;- helpers::validate_amount_to_capture_in_create_call_request(request)?;-+ helpers::validate_amount_to_capture_and_capture_method(None, request)?;helpers::validate_card_data(request.payment_method_data.clone())?;helpers::validate_payment_method_fields_present(request)?;diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rsindex 1176eeb1dd..68f064c539 100644--- a/crates/router/src/core/payments/operations/payment_update.rs+++ b/crates/router/src/core/payments/operations/payment_update.rs@@ -21,7 +21,7 @@ use crate::{types::{api::{self, PaymentIdTypeExt},domain,- storage::{self, enums as storage_enums},+ storage::{self, enums as storage_enums, payment_attempt::PaymentAttemptExt},},utils::OptionExt,};@@ -80,6 +80,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>&[storage_enums::IntentStatus::Failed,storage_enums::IntentStatus::Succeeded,+ storage_enums::IntentStatus::PartiallyCaptured,storage_enums::IntentStatus::RequiresCapture,],"update",@@ -134,6 +135,20 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>.await.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;+ helpers::validate_amount_to_capture_and_capture_method(Some(&payment_attempt), request)?;++ helpers::validate_request_amount_and_amount_to_capture(+ request.amount,+ request.amount_to_capture,+ request+ .surcharge_details+ .or(payment_attempt.get_surcharge_details()),+ )+ .change_context(errors::ApiErrorResponse::InvalidDataFormat {+ field_name: "amount_to_capture".to_string(),+ expected_format: "amount_to_capture lesser than or equal to amount".to_string(),+ })?;+currency = request.currency.or(payment_attempt.currency)@@ -322,7 +337,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>})?;let surcharge_details = request.surcharge_details.map(|request_surcharge_details| {- request_surcharge_details.get_surcharge_details_object(payment_attempt.amount)+ payments::SurchargeDetails::from((&request_surcharge_details, &payment_attempt))});let payment_data = PaymentData {@@ -629,6 +644,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenhelpers::validate_request_amount_and_amount_to_capture(request.amount,request.amount_to_capture,+ request.surcharge_details,).change_context(errors::ApiErrorResponse::InvalidDataFormat {field_name: "amount_to_capture".to_string(),diff --git a/crates/router/src/core/payments/types.rs b/crates/router/src/core/payments/types.rsindex 5e150a33d5..7a50254e6f 100644--- a/crates/router/src/core/payments/types.rs+++ b/crates/router/src/core/payments/types.rs@@ -1,10 +1,16 @@-use std::collections::HashMap;+use std::{collections::HashMap, num::TryFromIntError};+use api_models::{payment_methods::SurchargeDetailsResponse, payments::RequestSurchargeDetails};+use common_utils::{consts, types as common_types};+use data_models::payments::payment_attempt::PaymentAttempt;use error_stack::{IntoReport, ResultExt};use crate::{core::errors::{self, RouterResult},- types::storage::{self, enums as storage_enums},+ types::{+ storage::{self, enums as storage_enums},+ transformers::ForeignTryFrom,+ },};#[derive(Clone, Debug)]@@ -164,3 +170,151 @@ impl MultipleCaptureData {.collect()}}++#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]+pub struct SurchargeDetails {+ /// surcharge value+ pub surcharge: common_types::Surcharge,+ /// tax on surcharge value+ pub tax_on_surcharge:+ Option<common_types::Percentage<{ consts::SURCHARGE_PERCENTAGE_PRECISION_LENGTH }>>,+ /// surcharge amount for this payment+ pub surcharge_amount: i64,+ /// tax on surcharge amount for this payment+ pub tax_on_surcharge_amount: i64,+ /// sum of original amount,+ pub final_amount: i64,+}++impl From<(&RequestSurchargeDetails, &PaymentAttempt)> for SurchargeDetails {+ fn from(+ (request_surcharge_details, payment_attempt): (&RequestSurchargeDetails, &PaymentAttempt),+ ) -> Self {+ let surcharge_amount = request_surcharge_details.surcharge_amount;+ let tax_on_surcharge_amount = request_surcharge_details.tax_amount.unwrap_or(0);+ Self {+ 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,+ }+ }+}++impl ForeignTryFrom<(&SurchargeDetails, &PaymentAttempt)> for SurchargeDetailsResponse {+ type Error = TryFromIntError;+ fn foreign_try_from(+ (surcharge_details, payment_attempt): (&SurchargeDetails, &PaymentAttempt),+ ) -> Result<Self, Self::Error> {+ let currency = payment_attempt.currency.unwrap_or_default();+ let display_surcharge_amount =+ currency.to_currency_base_unit_asf64(surcharge_details.surcharge_amount)?;+ let display_tax_on_surcharge_amount =+ currency.to_currency_base_unit_asf64(surcharge_details.tax_on_surcharge_amount)?;+ 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(),+ display_surcharge_amount,+ display_tax_on_surcharge_amount,+ display_final_amount,+ })+ }+}++impl SurchargeDetails {+ pub fn is_request_surcharge_matching(+ &self,+ request_surcharge_details: RequestSurchargeDetails,+ ) -> bool {+ request_surcharge_details.surcharge_amount == self.surcharge_amount+ && request_surcharge_details.tax_amount.unwrap_or(0) == self.tax_on_surcharge_amount+ }+ pub fn get_total_surcharge_amount(&self) -> i64 {+ self.surcharge_amount + self.tax_on_surcharge_amount+ }+}++#[derive(Clone, Debug)]+pub struct SurchargeMetadata {+ surcharge_results: HashMap<+ (+ common_enums::PaymentMethod,+ common_enums::PaymentMethodType,+ Option<common_enums::CardNetwork>,+ ),+ SurchargeDetails,+ >,+ pub payment_attempt_id: String,+}++impl SurchargeMetadata {+ pub fn new(payment_attempt_id: String) -> Self {+ Self {+ surcharge_results: HashMap::new(),+ payment_attempt_id,+ }+ }+ pub fn is_empty_result(&self) -> bool {+ self.surcharge_results.is_empty()+ }+ pub fn get_surcharge_results_size(&self) -> usize {+ self.surcharge_results.len()+ }+ pub fn insert_surcharge_details(+ &mut self,+ payment_method: &common_enums::PaymentMethod,+ payment_method_type: &common_enums::PaymentMethodType,+ card_network: Option<&common_enums::CardNetwork>,+ surcharge_details: SurchargeDetails,+ ) {+ let key = (+ payment_method.to_owned(),+ payment_method_type.to_owned(),+ card_network.cloned(),+ );+ self.surcharge_results.insert(key, surcharge_details);+ }+ pub fn get_surcharge_details(+ &self,+ payment_method: &common_enums::PaymentMethod,+ payment_method_type: &common_enums::PaymentMethodType,+ card_network: Option<&common_enums::CardNetwork>,+ ) -> Option<&SurchargeDetails> {+ let key = &(+ payment_method.to_owned(),+ payment_method_type.to_owned(),+ card_network.cloned(),+ );+ self.surcharge_results.get(key)+ }+ pub fn get_surcharge_metadata_redis_key(payment_attempt_id: &str) -> String {+ format!("surcharge_metadata_{}", payment_attempt_id)+ }+ pub fn get_individual_surcharge_key_value_pairs(&self) -> Vec<(String, SurchargeDetails)> {+ self.surcharge_results+ .iter()+ .map(|((pm, pmt, card_network), surcharge_details)| {+ let key =+ Self::get_surcharge_details_redis_hashset_key(pm, pmt, card_network.as_ref());+ (key, surcharge_details.to_owned())+ })+ .collect()+ }+ pub fn get_surcharge_details_redis_hashset_key(+ payment_method: &common_enums::PaymentMethod,+ payment_method_type: &common_enums::PaymentMethodType,+ card_network: Option<&common_enums::CardNetwork>,+ ) -> String {+ if let Some(card_network) = card_network {+ format!(+ "{}_{}_{}",+ payment_method, payment_method_type, card_network+ )+ } else {+ format!("{}_{}", payment_method, payment_method_type)+ }+ }+}diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex 670c25c814..6d82c44d80 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -1,9 +1,6 @@use std::{marker::PhantomData, str::FromStr};-use api_models::{- enums::{DisputeStage, DisputeStatus},- payment_methods::{SurchargeDetailsResponse, SurchargeMetadata},-};+use api_models::enums::{DisputeStage, DisputeStatus};use common_enums::RequestIncrementalAuthorization;#[cfg(feature = "payouts")]use common_utils::{crypto::Encryptable, pii::Email};@@ -17,7 +14,7 @@ use redis_interface::errors::RedisError;use router_env::{instrument, tracing};use uuid::Uuid;-use super::payments::{helpers, PaymentAddress};+use super::payments::{helpers, types as payments_types, PaymentAddress};#[cfg(feature = "payouts")]use super::payouts::PayoutData;#[cfg(feature = "payouts")]@@ -1075,7 +1072,7 @@ pub fn get_flow_name<F>() -> RouterResult<String> {pub async fn persist_individual_surcharge_details_in_redis(state: &AppState,merchant_account: &domain::MerchantAccount,- surcharge_metadata: &SurchargeMetadata,+ surcharge_metadata: &payments_types::SurchargeMetadata,) -> RouterResult<()> {if !surcharge_metadata.is_empty_result() {let redis_conn = state@@ -1083,7 +1080,7 @@ pub async fn persist_individual_surcharge_details_in_redis(.get_redis_conn().change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to get redis connection")?;- let redis_key = SurchargeMetadata::get_surcharge_metadata_redis_key(+ let redis_key = payments_types::SurchargeMetadata::get_surcharge_metadata_redis_key(&surcharge_metadata.payment_attempt_id,);@@ -1094,7 +1091,7 @@ pub async fn persist_individual_surcharge_details_in_redis({value_list.push((key,- Encode::<SurchargeDetailsResponse>::encode_to_string_of_json(&value)+ Encode::<payments_types::SurchargeDetails>::encode_to_string_of_json(&value).change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to encode to string of json")?,));@@ -1118,20 +1115,21 @@ pub async fn get_individual_surcharge_detail_from_redis(payment_method_type: &euclid_enums::PaymentMethodType,card_network: Option<euclid_enums::CardNetwork>,payment_attempt_id: &str,-) -> CustomResult<SurchargeDetailsResponse, RedisError> {+) -> CustomResult<payments_types::SurchargeDetails, RedisError> {let redis_conn = state.store.get_redis_conn().attach_printable("Failed to get redis connection")?;- let redis_key = SurchargeMetadata::get_surcharge_metadata_redis_key(payment_attempt_id);- let value_key = SurchargeMetadata::get_surcharge_details_redis_hashset_key(+ let redis_key =+ payments_types::SurchargeMetadata::get_surcharge_metadata_redis_key(payment_attempt_id);+ let value_key = payments_types::SurchargeMetadata::get_surcharge_details_redis_hashset_key(payment_method,payment_method_type,card_network.as_ref(),);redis_conn- .get_hash_field_and_deserialize(&redis_key, &value_key, "SurchargeDetailsResponse")+ .get_hash_field_and_deserialize(&redis_key, &value_key, "SurchargeDetails").await}diff --git a/crates/router/src/types.rs b/crates/router/src/types.rsindex c267a54cc5..595f487e70 100644--- a/crates/router/src/types.rs+++ b/crates/router/src/types.rs@@ -30,7 +30,7 @@ use crate::core::utils::IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_DISPUTE_FLOuse crate::{core::{errors::{self, RouterResult},- payments::{PaymentData, RecurringMandatePaymentData},+ payments::{types, PaymentData, RecurringMandatePaymentData},},services,types::{storage::payment_attempt::PaymentAttemptExt, transformers::ForeignFrom},@@ -379,7 +379,7 @@ pub struct PaymentsAuthorizeData {pub related_transaction_id: Option<String>,pub payment_experience: Option<storage_enums::PaymentExperience>,pub payment_method_type: Option<storage_enums::PaymentMethodType>,- pub surcharge_details: Option<api_models::payment_methods::SurchargeDetailsResponse>,+ pub surcharge_details: Option<types::SurchargeDetails>,pub customer_id: Option<String>,pub request_incremental_authorization: bool,}@@ -441,7 +441,7 @@ pub struct PaymentsPreProcessingData {pub router_return_url: Option<String>,pub webhook_url: Option<String>,pub complete_authorize_url: Option<String>,- pub surcharge_details: Option<api_models::payment_methods::SurchargeDetailsResponse>,+ pub surcharge_details: Option<types::SurchargeDetails>,pub browser_info: Option<BrowserInformation>,pub connector_transaction_id: Option<String>,}@@ -517,7 +517,7 @@ pub struct PaymentsSessionData {pub amount: i64,pub currency: storage_enums::Currency,pub country: Option<api::enums::CountryAlpha2>,- pub surcharge_details: Option<api_models::payment_methods::SurchargeDetailsResponse>,+ pub surcharge_details: Option<types::SurchargeDetails>,pub order_details: Option<Vec<api_models::payments::OrderDetailsWithAmount>>,}diff --git a/crates/router/src/types/api.rs b/crates/router/src/types/api.rsindex 96bcaca3ed..ea2ea8b701 100644--- a/crates/router/src/types/api.rs+++ b/crates/router/src/types/api.rs@@ -19,7 +19,6 @@ pub mod webhooks;use std::{fmt::Debug, str::FromStr};-use api_models::payment_methods::{SurchargeDetailsResponse, SurchargeMetadata};use error_stack::{report, IntoReport, ResultExt};pub use self::{@@ -30,7 +29,10 @@ use super::ErrorResponse;use crate::{configs::settings::Connectors,connector, consts,- core::errors::{self, CustomResult},+ core::{+ errors::{self, CustomResult},+ payments::types as payments_types,+ },services::{request, ConnectorIntegration, ConnectorRedirectResponse, ConnectorValidation},types::{self, api::enums as api_enums},};@@ -222,9 +224,9 @@ pub struct SessionConnectorData {/// Session Surcharge typepub enum SessionSurchargeDetails {/// Surcharge is calculated by hyperswitch- Calculated(SurchargeMetadata),+ Calculated(payments_types::SurchargeMetadata),/// Surcharge is sent by merchant- PreDetermined(SurchargeDetailsResponse),+ PreDetermined(payments_types::SurchargeDetails),}impl SessionSurchargeDetails {@@ -233,7 +235,7 @@ impl SessionSurchargeDetails {payment_method: &enums::PaymentMethod,payment_method_type: &enums::PaymentMethodType,card_network: Option<&enums::CardNetwork>,- ) -> Option<SurchargeDetailsResponse> {+ ) -> Option<payments_types::SurchargeDetails> {match self {Self::Calculated(surcharge_metadata) => surcharge_metadata.get_surcharge_details(payment_method, payment_method_type, card_network)