Skip to content

Commit

Permalink
fix: handle session and confirm flow discrepancy in surcharge details (
Browse files Browse the repository at this point in the history
…#2696)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
hrithikesh026 and github-actions[bot] authored Nov 14, 2023
1 parent 856c7af commit cafea45
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 77 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/api_models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ mime = "0.3.17"
reqwest = { version = "0.11.18", optional = true }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
serde_with = "3.0.0"
strum = { version = "0.24.1", features = ["derive"] }
time = { version = "0.3.21", features = ["serde", "serde-well-known", "std"] }
url = { version = "2.4.0", features = ["serde"] }
Expand Down
83 changes: 76 additions & 7 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use common_utils::{
types::Percentage,
};
use serde::de;
use serde_with::serde_as;
use utoipa::ToSchema;

#[cfg(feature = "payouts")]
Expand All @@ -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)]
Expand Down Expand Up @@ -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>,
Expand Down
48 changes: 48 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
admin, disputes,
enums::{self as api_enums},
ephemeral_key::EphemeralKeyCreateResponse,
payment_methods::{Surcharge, SurchargeDetailsResponse},
refunds,
};

Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions crates/data_models/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,17 @@ pub enum PaymentAttemptUpdate {
business_sub_label: Option<String>,
amount_to_capture: Option<i64>,
capture_method: Option<storage_enums::CaptureMethod>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
},
UpdateTrackers {
payment_token: Option<String>,
connector: Option<String>,
straight_through_algorithm: Option<serde_json::Value>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
merchant_connector_id: Option<String>,
},
Expand All @@ -255,8 +259,6 @@ pub enum PaymentAttemptUpdate {
error_code: Option<Option<String>>,
error_message: Option<Option<String>>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
merchant_connector_id: Option<String>,
},
Expand Down Expand Up @@ -285,6 +287,8 @@ pub enum PaymentAttemptUpdate {
error_reason: Option<Option<String>>,
connector_response_reference_id: Option<String>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
authentication_data: Option<serde_json::Value>,
encoded_data: Option<String>,
Expand Down
24 changes: 18 additions & 6 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ pub enum PaymentAttemptUpdate {
business_sub_label: Option<String>,
amount_to_capture: Option<i64>,
capture_method: Option<storage_enums::CaptureMethod>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
},
UpdateTrackers {
payment_token: Option<String>,
connector: Option<String>,
straight_through_algorithm: Option<serde_json::Value>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
merchant_connector_id: Option<String>,
},
Expand All @@ -172,8 +176,6 @@ pub enum PaymentAttemptUpdate {
error_code: Option<Option<String>>,
error_message: Option<Option<String>>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
merchant_connector_id: Option<String>,
},
Expand Down Expand Up @@ -202,6 +204,8 @@ pub enum PaymentAttemptUpdate {
error_reason: Option<Option<String>>,
connector_response_reference_id: Option<String>,
amount_capturable: Option<i64>,
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
updated_by: String,
authentication_data: Option<serde_json::Value>,
encoded_data: Option<String>,
Expand Down Expand Up @@ -370,6 +374,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
updated_by,
} => Self {
amount: Some(amount),
Expand All @@ -386,6 +392,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
updated_by,
..Default::default()
},
Expand Down Expand Up @@ -415,8 +423,6 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id,
} => Self {
Expand All @@ -437,8 +443,6 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id,
..Default::default()
Expand Down Expand Up @@ -479,6 +483,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
error_reason,
connector_response_reference_id,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
authentication_data,
encoded_data,
Expand All @@ -498,6 +504,8 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
connector_response_reference_id,
amount_capturable,
updated_by,
surcharge_amount,
tax_amount,
authentication_data,
encoded_data,
..Default::default()
Expand Down Expand Up @@ -531,13 +539,17 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
connector,
straight_through_algorithm,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id,
} => Self {
payment_token,
connector,
straight_through_algorithm,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id,
..Default::default()
Expand Down
5 changes: 2 additions & 3 deletions crates/redis_interface/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl super::RedisConnectionPool {
&self,
key: &str,
values: V,
ttl: Option<u32>,
ttl: Option<i64>,
) -> CustomResult<(), errors::RedisError>
where
V: TryInto<RedisMap> + Debug + Send + Sync,
Expand All @@ -260,11 +260,10 @@ impl super::RedisConnectionPool {
.await
.into_report()
.change_context(errors::RedisError::SetHashFailed);

// setting expiry for the key
output
.async_and_then(|_| {
self.set_expiry(key, ttl.unwrap_or(self.config.default_hash_ttl).into())
self.set_expiry(key, ttl.unwrap_or(self.config.default_hash_ttl.into()))
})
.await
}
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ pub async fn list_payment_methods(
amount_capturable: None,
updated_by: merchant_account.storage_scheme.to_string(),
merchant_connector_id: None,
surcharge_amount: None,
tax_amount: None,
};

state
Expand Down
Loading

0 comments on commit cafea45

Please sign in to comment.