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(payment-methods): add ph code validation in customer create flow (#13752)

Prasunna Soppaebd6fda93c54
diff --git a/crates/common_utils/src/consts.rs b/crates/common_utils/src/consts.rs
index df65bfcd9f..657d8449df 100644
--- a/crates/common_utils/src/consts.rs
+++ b/crates/common_utils/src/consts.rs
@@ -153,6 +153,10 @@ pub const MAX_ALLOWED_MERCHANT_NAME_LENGTH: usize = 64;
/// Maximum allowed length for CardIssuerName
pub const MAX_ALLOWED_CARD_ISSUER_NAME_LENGTH: usize = 255;
+/// Maximum allowed length for a phone country (calling) code, matching the `VARCHAR(8)`
+/// column used to store it
+pub const MAX_PHONE_COUNTRY_CODE_LENGTH: usize = 8;
+
/// Default locale
pub const DEFAULT_LOCALE: &str = "en";
diff --git a/crates/common_utils/src/validation.rs b/crates/common_utils/src/validation.rs
index e8810de155..c90a9facbd 100644
--- a/crates/common_utils/src/validation.rs
+++ b/crates/common_utils/src/validation.rs
@@ -10,7 +10,10 @@ use regex::Regex;
#[cfg(feature = "logs")]
use router_env::logger;
-use crate::errors::{CustomResult, ValidationError};
+use crate::{
+ consts,
+ errors::{CustomResult, ValidationError},
+};
/// Validates a given phone number using the [phonenumber] crate
///
@@ -23,6 +26,31 @@ pub fn validate_phone_number(phone_number: &str) -> Result<(), ValidationError>
Ok(())
}
+/// Validates a phone country (calling) code, e.g. `"+1"` or `"91"`.
+///
+/// It returns a [ValidationError::InvalidValue] if the value is empty, longer than
+/// [`consts::MAX_PHONE_COUNTRY_CODE_LENGTH`], or contains characters other than digits and an
+/// optional leading `+`.
+pub fn validate_phone_country_code(phone_country_code: &str) -> Result<(), ValidationError> {
+ let is_valid = !phone_country_code.is_empty()
+ && phone_country_code.len() <= consts::MAX_PHONE_COUNTRY_CODE_LENGTH
+ && phone_country_code
+ .strip_prefix('+')
+ .unwrap_or(phone_country_code)
+ .chars()
+ .all(|character| character.is_ascii_digit());
+
+ if is_valid {
+ Ok(())
+ } else {
+ Err(ValidationError::InvalidValue {
+ message: format!(
+ "phone_country_code must be a valid country calling code (e.g. \"+1\"), got \"{phone_country_code}\""
+ ),
+ })
+ }
+}
+
/// Performs a simple validation against a provided email address.
pub fn validate_email(email: &str) -> CustomResult<(), ValidationError> {
#[deny(clippy::invalid_regex)]
diff --git a/crates/router/src/core/customers.rs b/crates/router/src/core/customers.rs
index db033cf536..8c3352d339 100644
--- a/crates/router/src/core/customers.rs
+++ b/crates/router/src/core/customers.rs
@@ -11,6 +11,7 @@ use common_utils::{
keymanager::{Identifier, KeyManagerState, ToEncryptable},
Description,
},
+ validation::validate_phone_country_code,
};
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::{
@@ -84,6 +85,15 @@ pub async fn create_customer(
message: err.to_string(),
})?;
+ customer_data
+ .phone_country_code
+ .as_deref()
+ .map(validate_phone_country_code)
+ .transpose()
+ .map_err(|err| errors::CustomersErrorResponse::InvalidRequestData {
+ message: err.to_string(),
+ })?;
+
let db: &dyn StorageInterface = state.store.as_ref();
let key_manager_state = &(&state).into();
@@ -1163,6 +1173,16 @@ pub async fn update_customer(
message: err.to_string(),
})?;
+ update_customer
+ .request
+ .phone_country_code
+ .as_deref()
+ .map(validate_phone_country_code)
+ .transpose()
+ .map_err(|err| errors::CustomersErrorResponse::InvalidRequestData {
+ message: err.to_string(),
+ })?;
+
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
//Add this in update call if customer can be updated anywhere else