diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs index fa7cebba9bd..cbf1867b08b 100644 --- a/crates/router/src/connector/utils.rs +++ b/crates/router/src/connector/utils.rs @@ -1822,6 +1822,8 @@ pub trait AddressDetailsData { fn get_zip(&self) -> Result<&Secret, Error>; fn get_country(&self) -> Result<&api_models::enums::CountryAlpha2, Error>; fn get_combined_address_line(&self) -> Result, Error>; + fn to_state_code(&self) -> Result, Error>; + fn to_state_code_as_optional(&self) -> Result>, Error>; fn get_optional_line2(&self) -> Option>; fn get_optional_country(&self) -> Option; } @@ -1894,6 +1896,31 @@ impl AddressDetailsData for hyperswitch_domain_models::address::AddressDetails { self.get_line2()?.peek() ))) } + fn to_state_code(&self) -> Result, Error> { + let country = self.get_country()?; + let state = self.get_state()?; + match country { + api_models::enums::CountryAlpha2::US => Ok(Secret::new( + UsStatesAbbreviation::foreign_try_from(state.peek().to_string())?.to_string(), + )), + api_models::enums::CountryAlpha2::CA => Ok(Secret::new( + CanadaStatesAbbreviation::foreign_try_from(state.peek().to_string())?.to_string(), + )), + _ => Ok(state.clone()), + } + } + fn to_state_code_as_optional(&self) -> Result>, Error> { + self.state + .as_ref() + .map(|state| { + if state.peek().len() == 2 { + Ok(state.to_owned()) + } else { + self.to_state_code() + } + }) + .transpose() + } fn get_optional_line2(&self) -> Option> { self.line2.clone()