feat(pm_auth): Added balance check for PM auth bank account (#5054)

This commit is contained in:
Sarthak Soni
2024-07-03 18:17:52 +05:30
committed by GitHub
parent e85407fc53
commit f513c8e4da
4 changed files with 130 additions and 45 deletions

View File

@ -15,6 +15,7 @@ use common_utils::{
crypto::{HmacSha256, SignMessage},
ext_traits::AsyncExt,
generate_id,
types::{self as util_types, AmountConvertor},
};
use error_stack::ResultExt;
use helpers::PaymentAuthConnectorDataExt;
@ -66,6 +67,19 @@ pub async fn create_link_token(
let pm_auth_key = format!("pm_auth_{}", payload.payment_id);
redis_conn
.exists::<Vec<u8>>(&pm_auth_key)
.await
.change_context(ApiErrorResponse::InvalidRequestData {
message: "Incorrect payment_id provided in request".to_string(),
})
.attach_printable("Corresponding pm_auth_key does not exist in redis")?
.then_some(())
.ok_or(ApiErrorResponse::InvalidRequestData {
message: "Incorrect payment_id provided in request".to_string(),
})
.attach_printable("Corresponding pm_auth_key does not exist in redis")?;
let pm_auth_configs = redis_conn
.get_and_deserialize_key::<Vec<api_models::pm_auth::PaymentMethodAuthConnectorChoice>>(
pm_auth_key.as_str(),
@ -637,6 +651,19 @@ async fn get_selected_config_from_redis(
let pm_auth_key = format!("pm_auth_{}", payload.payment_id);
redis_conn
.exists::<Vec<u8>>(&pm_auth_key)
.await
.change_context(ApiErrorResponse::InvalidRequestData {
message: "Incorrect payment_id provided in request".to_string(),
})
.attach_printable("Corresponding pm_auth_key does not exist in redis")?
.then_some(())
.ok_or(ApiErrorResponse::InvalidRequestData {
message: "Incorrect payment_id provided in request".to_string(),
})
.attach_printable("Corresponding pm_auth_key does not exist in redis")?;
let pm_auth_configs = redis_conn
.get_and_deserialize_key::<Vec<api_models::pm_auth::PaymentMethodAuthConnectorChoice>>(
pm_auth_key.as_str(),
@ -720,6 +747,21 @@ pub async fn retrieve_payment_method_from_auth_service(
.ok_or(ApiErrorResponse::InternalServerError)
.attach_printable("Bank account details not found")?;
if let (Some(balance), Some(currency)) = (bank_account.balance, payment_intent.currency) {
let required_conversion = util_types::FloatMajorUnitForConnector;
let converted_amount = required_conversion
.convert_back(balance, currency)
.change_context(ApiErrorResponse::InternalServerError)
.attach_printable("Could not convert FloatMajorUnit to MinorUnit")?;
if converted_amount < payment_intent.amount {
return Err((ApiErrorResponse::PreconditionFailed {
message: "selected bank account has insufficient balance".to_string(),
})
.into());
}
}
let mut bank_type = None;
if let Some(account_type) = bank_account.account_type.clone() {
bank_type = common_enums::BankType::from_str(account_type.as_str())