Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(relay): enforce payout amount limits for unreferenced refunds (#15242)

Aniket Burmandd1b96503e05
diff --git a/crates/api_models/src/unreferenced_refund.rs b/crates/api_models/src/unreferenced_refund.rs
index ac3fa2d691..88ac5b121a 100644
--- a/crates/api_models/src/unreferenced_refund.rs
+++ b/crates/api_models/src/unreferenced_refund.rs
@@ -1,5 +1,5 @@
use cards::CardNumber;
-use common_utils::types::MinorUnit;
+use common_utils::{fp_utils, types::MinorUnit};
use hyperswitch_masking::{PeekInterface, Secret};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
@@ -34,6 +34,31 @@ pub struct UnreferencedRefundRequest {
pub recipient_payment_method_data: RecipientPaymentMethodData,
}
+impl UnreferencedRefundRequest {
+ pub fn validate_amount(
+ &self,
+ min_amount: MinorUnit,
+ max_amount: MinorUnit,
+ ) -> Result<(), String> {
+ fp_utils::when(min_amount > max_amount, || {
+ Err(
+ "unreferenced refund amount configuration has a minimum greater than its maximum"
+ .to_string(),
+ )
+ })?;
+
+ fp_utils::when(self.amount < min_amount || self.amount > max_amount, || {
+ Err(format!(
+ "unreferenced refund amount must be between {} and {} in the currency's lowest denomination",
+ min_amount.get_amount_as_i64(),
+ max_amount.get_amount_as_i64(),
+ ))
+ })?;
+
+ Ok(())
+ }
+}
+
#[derive(Debug, ToSchema, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RecipientPaymentMethodData {
diff --git a/crates/router/src/core/relay.rs b/crates/router/src/core/relay.rs
index c7fda862cf..0558925b30 100644
--- a/crates/router/src/core/relay.rs
+++ b/crates/router/src/core/relay.rs
@@ -18,7 +18,10 @@ use hyperswitch_interfaces::{
use hyperswitch_masking::Secret;
use router_env::tracing::instrument;
-use super::errors::{self, ConnectorErrorExt, RouterResponse, RouterResult, StorageErrorExt};
+use super::{
+ configs::dimension_state,
+ errors::{self, ConnectorErrorExt, RouterResponse, RouterResult, StorageErrorExt},
+};
use crate::{
connector::utils::RouterData,
core::payments::{self, access_token as access_token_core},
@@ -1205,6 +1208,28 @@ async fn process_relay_unreferenced_refund(
id: profile_id.get_string_repr().to_owned(),
})?;
+ let dimensions = dimension_state::Dimensions::new()
+ .with_processor_merchant_id(platform.get_processor().get_processor_merchant_id())
+ .with_provider_merchant_id(platform.get_provider().get_provider_merchant_id())
+ .with_organization_id(
+ platform
+ .get_processor()
+ .get_account()
+ .get_org_id()
+ .to_owned(),
+ )
+ .with_profile_id(profile_id.clone())
+ .with_currency(request.currency);
+ let min_amount = dimensions
+ .get_payout_min_amount(db, state.superposition_service.as_ref(), Some(profile_id))
+ .await;
+ let max_amount = dimensions
+ .get_payout_max_amount(db, state.superposition_service.as_ref(), Some(profile_id))
+ .await;
+ request
+ .validate_amount(min_amount, max_amount)
+ .map_err(|message| errors::ApiErrorResponse::PreconditionFailed { message })?;
+
process_unreferenced_refund(state, platform, profile_id, connector_resource_id, request).await
}