mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-31 01:57:45 +08:00
fix: handle session and confirm flow discrepancy in surcharge details (#2696)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
@ -6,7 +6,6 @@ use common_utils::{
|
||||
types::Percentage,
|
||||
};
|
||||
use serde::de;
|
||||
use serde_with::serde_as;
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[cfg(feature = "payouts")]
|
||||
@ -15,7 +14,7 @@ use crate::{
|
||||
admin,
|
||||
customers::CustomerId,
|
||||
enums as api_enums,
|
||||
payments::{self, BankCodeResponse},
|
||||
payments::{self, BankCodeResponse, RequestSurchargeDetails},
|
||||
};
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
|
||||
@ -342,15 +341,85 @@ pub struct SurchargeDetailsResponse {
|
||||
pub final_amount: i64,
|
||||
}
|
||||
|
||||
#[serde_as]
|
||||
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SurchargeMetadata {
|
||||
#[serde_as(as = "HashMap<_, _>")]
|
||||
pub surcharge_results: HashMap<String, SurchargeDetailsResponse>,
|
||||
surcharge_results: HashMap<
|
||||
(
|
||||
common_enums::PaymentMethod,
|
||||
common_enums::PaymentMethodType,
|
||||
Option<common_enums::CardNetwork>,
|
||||
),
|
||||
SurchargeDetailsResponse,
|
||||
>,
|
||||
pub payment_attempt_id: String,
|
||||
}
|
||||
|
||||
impl SurchargeMetadata {
|
||||
pub fn get_key_for_surcharge_details_hash_map(
|
||||
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>,
|
||||
|
||||
@ -16,6 +16,7 @@ use crate::{
|
||||
admin, disputes,
|
||||
enums::{self as api_enums},
|
||||
ephemeral_key::EphemeralKeyCreateResponse,
|
||||
payment_methods::{Surcharge, SurchargeDetailsResponse},
|
||||
refunds,
|
||||
};
|
||||
|
||||
@ -319,6 +320,23 @@ pub struct RequestSurchargeDetails {
|
||||
pub tax_amount: Option<i64>,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
pub struct HeaderPayload {
|
||||
pub payment_confirm_source: Option<api_enums::PaymentSource>,
|
||||
@ -810,6 +828,36 @@ pub enum PaymentMethodData {
|
||||
GiftCard(Box<GiftCardData>),
|
||||
}
|
||||
|
||||
impl PaymentMethodData {
|
||||
pub fn get_payment_method_type_if_session_token_type(
|
||||
&self,
|
||||
) -> Option<api_enums::PaymentMethodType> {
|
||||
match self {
|
||||
Self::Wallet(wallet) => match wallet {
|
||||
WalletData::ApplePay(_) => Some(api_enums::PaymentMethodType::ApplePay),
|
||||
WalletData::GooglePay(_) => Some(api_enums::PaymentMethodType::GooglePay),
|
||||
WalletData::PaypalSdk(_) => Some(api_enums::PaymentMethodType::Paypal),
|
||||
_ => None,
|
||||
},
|
||||
Self::PayLater(pay_later) => match pay_later {
|
||||
PayLaterData::KlarnaSdk { .. } => Some(api_enums::PaymentMethodType::Klarna),
|
||||
_ => None,
|
||||
},
|
||||
Self::Card(_)
|
||||
| Self::CardRedirect(_)
|
||||
| Self::BankRedirect(_)
|
||||
| Self::BankDebit(_)
|
||||
| Self::BankTransfer(_)
|
||||
| Self::Crypto(_)
|
||||
| Self::MandatePayment
|
||||
| Self::Reward
|
||||
| Self::Upi(_)
|
||||
| Self::Voucher(_)
|
||||
| Self::GiftCard(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetPaymentMethodType {
|
||||
fn get_payment_method_type(&self) -> api_enums::PaymentMethodType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user