mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
refactor: create separate struct for surcharge details response (#3027)
This commit is contained in:
@ -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>
|
||||
where
|
||||
O: 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 @@ where
|
||||
let 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 @@ where
|
||||
Ok(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,
|
||||
|
||||
@ -15,10 +15,9 @@ use std::{fmt::Debug, marker::PhantomData, ops::Deref, time::Instant, vec::IntoI
|
||||
|
||||
use 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 @@ where
|
||||
let 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 @@ where
|
||||
pub 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>,
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -286,8 +286,8 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
|
||||
// The operation merges mandate data from both request and payment_attempt
|
||||
let 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::Paymen
|
||||
helpers::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)?;
|
||||
|
||||
@ -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::Paymen
|
||||
helpers::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(),
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@ use crate::core::utils::IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_DISPUTE_FLO
|
||||
use 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>>,
|
||||
}
|
||||
|
||||
|
||||
@ -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 type
|
||||
pub 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)
|
||||
|
||||
Reference in New Issue
Block a user