mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-30 01:27:31 +08:00
refactor: create separate struct for surcharge details response (#3027)
This commit is contained in:
@ -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 value
|
||||
pub 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_data
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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] = &[
|
||||
|
||||
Reference in New Issue
Block a user