enhance(core): replace string with enum for country (#735)

This commit is contained in:
Narayan Bhat
2023-03-30 21:45:38 +05:30
committed by GitHub
parent 442bed0f6b
commit e18bfb2ae1
45 changed files with 734 additions and 649 deletions

View File

@ -27,7 +27,9 @@ impl From<StripeBillingDetails> for payments::Address {
Self {
phone: Some(payments::PhoneDetails {
number: details.phone,
country_code: details.address.as_ref().and_then(|a| a.country.clone()),
country_code: details.address.as_ref().and_then(|address| {
address.country.as_ref().map(|country| country.to_string())
}),
}),
address: details.address,
@ -109,7 +111,9 @@ impl From<Shipping> for payments::Address {
Self {
phone: Some(payments::PhoneDetails {
number: details.phone,
country_code: details.address.as_ref().and_then(|a| a.country.clone()),
country_code: details.address.as_ref().and_then(|address| {
address.country.as_ref().map(|country| country.to_string())
}),
}),
address: details.address,
}

View File

@ -98,10 +98,12 @@ pub struct CurrencyCountryFilter {
#[serde(deserialize_with = "currency_set_deser")]
pub currency: Option<HashSet<api_models::enums::Currency>>,
#[serde(deserialize_with = "string_set_deser")]
pub country: Option<HashSet<String>>,
pub country: Option<HashSet<api_models::enums::CountryCode>>,
}
fn string_set_deser<'a, D>(deserializer: D) -> Result<Option<HashSet<String>>, D::Error>
fn string_set_deser<'a, D>(
deserializer: D,
) -> Result<Option<HashSet<api_models::enums::CountryCode>>, D::Error>
where
D: Deserializer<'a>,
{
@ -110,7 +112,7 @@ where
let list = inner
.trim()
.split(',')
.map(|value| value.to_string())
.flat_map(api_models::enums::CountryCode::from_str)
.collect::<HashSet<_>>();
match list.len() {
0 => None,

View File

@ -59,7 +59,7 @@ pub struct ShopperName {
#[serde(rename_all = "camelCase")]
pub struct Address {
city: Option<String>,
country: Option<String>,
country: Option<api_enums::CountryCode>,
house_number_or_name: Option<Secret<String>>,
postal_code: Option<Secret<String>>,
state_or_province: Option<Secret<String>>,
@ -96,7 +96,7 @@ pub struct AdyenPaymentRequest<'a> {
telephone_number: Option<Secret<String>>,
billing_address: Option<Address>,
delivery_address: Option<Address>,
country_code: Option<String>,
country_code: Option<api_enums::CountryCode>,
line_items: Option<Vec<LineItem>>,
}
@ -496,7 +496,7 @@ fn get_address_info(address: Option<&api_models::payments::Address>) -> Option<A
address.and_then(|add| {
add.address.as_ref().map(|a| Address {
city: a.city.clone(),
country: a.country.clone(),
country: a.country,
house_number_or_name: a.line1.clone(),
postal_code: a.zip.clone(),
state_or_province: a.state.clone(),
@ -548,13 +548,11 @@ fn get_shopper_name(item: &types::PaymentsAuthorizeRouterData) -> Option<Shopper
})
}
fn get_country_code(item: &types::PaymentsAuthorizeRouterData) -> Option<String> {
let address = item
.address
fn get_country_code(item: &types::PaymentsAuthorizeRouterData) -> Option<api_enums::CountryCode> {
item.address
.billing
.as_ref()
.and_then(|billing| billing.address.as_ref());
address.and_then(|address| address.country.clone())
.and_then(|billing| billing.address.as_ref().and_then(|address| address.country))
}
fn get_payment_method_data<'a>(
@ -684,7 +682,7 @@ fn get_card_specific_payment_data<'a>(
fn get_sofort_extra_details(
item: &types::PaymentsAuthorizeRouterData,
) -> (Option<String>, Option<String>) {
) -> (Option<String>, Option<api_enums::CountryCode>) {
match item.request.payment_method_data {
api_models::payments::PaymentMethodData::BankRedirect(ref b) => {
if let api_models::payments::BankRedirectData::Sofort {
@ -694,7 +692,7 @@ fn get_sofort_extra_details(
{
(
Some(preferred_language.to_string()),
Some(country.to_string()),
Some(country.to_owned()),
)
} else {
(None, None)

View File

@ -64,7 +64,7 @@ pub struct SessionRequest {
#[serde(rename_all = "snake_case")]
pub struct PaymentRequest {
pub apple_pay_merchant_id: String,
pub country_code: String,
pub country_code: api_models::enums::CountryCode,
pub currency_code: String,
pub total: AmountInfo,
pub merchant_capabilities: Vec<String>,

View File

@ -8,7 +8,11 @@ use crate::{
consts,
core::errors,
pii::PeekInterface,
types::{self, api, storage::enums},
types::{
self,
api::{self, enums as api_enums},
storage::enums,
},
};
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
@ -76,7 +80,7 @@ pub struct BillTo {
locality: String,
administrative_area: Secret<String>,
postal_code: Secret<String>,
country: String,
country: api_enums::CountryCode,
email: Secret<String, pii::Email>,
phone_number: Secret<String>,
}

View File

@ -27,7 +27,7 @@ pub struct GlobalpayPaymentsRequest {
/// related currency.
pub convenience_amount: Option<String>,
/// The country in ISO-3166-1(alpha-2 code) format.
pub country: String,
pub country: api_models::enums::CountryCode,
/// The currency of the amount in ISO-4217(alpha-3)
pub currency: String,
pub currency_conversion: Option<CurrencyConversion>,

View File

@ -83,7 +83,7 @@ pub struct Address {
pub postal_code: Secret<String>,
pub city: String,
pub region: Option<Secret<String>>,
pub country: String,
pub country: api_models::enums::CountryCode,
}
impl TryFrom<&types::PaymentsAuthorizeRouterData> for MolliePaymentsRequest {

View File

@ -132,7 +132,7 @@ pub struct DeliveryObject {
house_number: Secret<String>,
zip_code: Secret<String>,
city: String,
country: String,
country: api_models::enums::CountryCode,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]

View File

@ -129,7 +129,7 @@ pub enum AlternativePaymentMethodType {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BillingAddress {
pub email: Secret<String, Email>,
pub country: String,
pub country: api_models::enums::CountryCode,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]

View File

@ -59,7 +59,7 @@ pub struct Address {
zip: Option<Secret<String>>,
state: Option<Secret<String>>,
city: Option<String>,
country: Option<String>,
country: Option<api_models::enums::CountryCode>,
}
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
@ -172,7 +172,7 @@ fn get_address_details(address_details: Option<&payments::AddressDetails>) -> Op
zip: address.zip.clone(),
state: address.state.clone(),
city: address.city.clone(),
country: address.country.clone(),
country: address.country,
})
}

View File

@ -148,7 +148,7 @@ pub enum BankSpecificData {
#[serde(rename = "payment_method_options[sofort][preferred_language]")]
preferred_language: String,
#[serde(rename = "payment_method_data[sofort][country]")]
country: String,
country: api_enums::CountryCode,
},
}
@ -1080,7 +1080,7 @@ pub struct StripeShippingAddress {
#[serde(rename = "shipping[address][city]")]
pub city: Option<String>,
#[serde(rename = "shipping[address][country]")]
pub country: Option<String>,
pub country: Option<api_enums::CountryCode>,
#[serde(rename = "shipping[address][line1]")]
pub line1: Option<Secret<String>>,
#[serde(rename = "shipping[address][line2]")]
@ -1100,7 +1100,7 @@ pub struct StripeBillingAddress {
#[serde(rename = "payment_method_data[billing_details][email]")]
pub email: Option<Secret<String, Email>>,
#[serde(rename = "payment_method_data[billing_details][address][country]")]
pub country: Option<String>,
pub country: Option<api_enums::CountryCode>,
#[serde(rename = "payment_method_data[billing_details][name]")]
pub name: Option<Secret<String>>,
}

View File

@ -120,7 +120,7 @@ pub struct PaymentRequestCards {
#[serde(rename = "billing[city]")]
pub billing_city: String,
#[serde(rename = "billing[country]")]
pub billing_country: String,
pub billing_country: api_models::enums::CountryCode,
#[serde(rename = "billing[street1]")]
pub billing_street1: Secret<String>,
#[serde(rename = "billing[postcode]")]
@ -174,7 +174,7 @@ pub enum TrustpayPaymentsRequest {
#[derive(Debug, Serialize, Eq, PartialEq)]
pub struct TrustpayMandatoryParams {
pub billing_city: String,
pub billing_country: String,
pub billing_country: api_models::enums::CountryCode,
pub billing_street1: Secret<String>,
pub billing_postcode: Secret<String>,
}

View File

@ -47,7 +47,7 @@ impl AccessTokenRequestInfo for types::RefreshTokenRouterData {
pub trait RouterData {
fn get_billing(&self) -> Result<&api::Address, Error>;
fn get_billing_country(&self) -> Result<String, Error>;
fn get_billing_country(&self) -> Result<api_models::enums::CountryCode, Error>;
fn get_billing_phone(&self) -> Result<&api::PhoneDetails, Error>;
fn get_description(&self) -> Result<String, Error>;
fn get_billing_address(&self) -> Result<&api::AddressDetails, Error>;
@ -69,12 +69,12 @@ impl<Flow, Request, Response> RouterData for types::RouterData<Flow, Request, Re
.ok_or_else(missing_field_err("billing"))
}
fn get_billing_country(&self) -> Result<String, Error> {
fn get_billing_country(&self) -> Result<api_models::enums::CountryCode, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.address.as_ref())
.and_then(|ad| ad.country.clone())
.and_then(|ad| ad.country)
.ok_or_else(missing_field_err("billing.address.country"))
}
@ -314,7 +314,7 @@ pub trait AddressDetailsData {
fn get_city(&self) -> Result<&String, Error>;
fn get_line2(&self) -> Result<&Secret<String>, Error>;
fn get_zip(&self) -> Result<&Secret<String>, Error>;
fn get_country(&self) -> Result<&String, Error>;
fn get_country(&self) -> Result<&api_models::enums::CountryCode, Error>;
fn get_combined_address_line(&self) -> Result<Secret<String>, Error>;
}
@ -355,7 +355,7 @@ impl AddressDetailsData for api::AddressDetails {
.ok_or_else(missing_field_err("address.zip"))
}
fn get_country(&self) -> Result<&String, Error> {
fn get_country(&self) -> Result<&api_models::enums::CountryCode, Error> {
self.country
.as_ref()
.ok_or_else(missing_field_err("address.country"))

View File

@ -49,7 +49,7 @@ pub struct Order {
#[serde(rename_all = "camelCase")]
pub struct BillingAddress {
pub city: Option<String>,
pub country_code: Option<String>,
pub country_code: Option<api_enums::CountryCode>,
pub house_number: Option<String>,
pub state: Option<Secret<String>>,
pub state_code: Option<String>,
@ -84,7 +84,7 @@ pub struct Name {
#[serde(rename_all = "camelCase")]
pub struct Shipping {
pub city: Option<String>,
pub country_code: Option<String>,
pub country_code: Option<api_enums::CountryCode>,
pub house_number: Option<String>,
pub name: Option<Name>,
pub state: Option<Secret<String>>,

View File

@ -170,7 +170,7 @@ pub async fn delete_customer(
let update_address = storage::AddressUpdate::Update {
city: Some(REDACTED.to_string()),
country: Some(REDACTED.to_string()),
country: None,
line1: Some(REDACTED.to_string().into()),
line2: Some(REDACTED.to_string().into()),
line3: Some(REDACTED.to_string().into()),

View File

@ -1120,7 +1120,7 @@ async fn filter_payment_methods(
&connector,
&payment_method_object.payment_method_type,
&mut payment_method_object.card_networks,
&address.and_then(|inner| inner.country.clone()),
&address.and_then(|inner| inner.country),
payment_attempt
.and_then(|value| value.currency)
.map(|value| value.foreign_into()),
@ -1149,7 +1149,7 @@ fn filter_pm_based_on_config<'a>(
connector: &'a str,
payment_method_type: &'a api_enums::PaymentMethodType,
card_network: &mut Option<Vec<api_enums::CardNetwork>>,
country: &Option<String>,
country: &Option<api_enums::CountryCode>,
currency: Option<api_enums::Currency>,
) -> bool {
config
@ -1171,7 +1171,7 @@ fn filter_pm_based_on_config<'a>(
}
fn card_network_filter(
country: &Option<String>,
country: &Option<api_enums::CountryCode>,
currency: Option<api_enums::Currency>,
card_network: &mut Option<Vec<api_enums::CardNetwork>>,
payment_method_filters: &settings::PaymentMethodFilters,
@ -1195,7 +1195,7 @@ fn card_network_filter(
fn global_country_currency_filter(
item: &settings::CurrencyCountryFilter,
country: &Option<String>,
country: &Option<api_enums::CountryCode>,
currency: Option<api_enums::Currency>,
) -> bool {
let country_condition = item
@ -1233,8 +1233,12 @@ fn filter_pm_card_network_based(
}
fn filter_pm_country_based(
accepted_countries: &Option<admin::AcceptedCountries>,
req_country_list: &Option<Vec<String>>,
) -> (Option<admin::AcceptedCountries>, Option<Vec<String>>, bool) {
req_country_list: &Option<Vec<api_enums::CountryCode>>,
) -> (
Option<admin::AcceptedCountries>,
Option<Vec<api_enums::CountryCode>>,
bool,
) {
match (accepted_countries, req_country_list) {
(None, None) => (None, None, true),
(None, Some(ref r)) => (

View File

@ -92,7 +92,7 @@ fn create_gpay_session_token(
let session_data = router_data.request.clone();
let transaction_info = payment_types::GpayTransactionInfo {
country_code: session_data.country.unwrap_or_else(|| "US".to_string()),
country_code: session_data.country.unwrap_or_default(),
currency_code: router_data.request.currency.to_string(),
total_price_status: "Final".to_string(),
total_price: router_data.request.amount,

View File

@ -561,11 +561,9 @@ impl<F: Clone> TryFrom<PaymentData<F>> for types::PaymentsSessionData {
Ok(Self {
amount: payment_data.amount.into(),
currency: payment_data.currency,
country: payment_data
.address
.billing
.and_then(|billing_address| billing_address.address.map(|address| address.country))
.flatten(),
country: payment_data.address.billing.and_then(|billing_address| {
billing_address.address.and_then(|address| address.country)
}),
order_details,
})
}

View File

@ -143,6 +143,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::enums::PaymentExperience,
api_models::enums::BankNames,
api_models::enums::CardNetwork,
api_models::enums::CountryCode,
api_models::admin::MerchantConnector,
api_models::admin::PaymentMethodsEnabled,
api_models::payments::AddressDetails,

View File

@ -205,7 +205,7 @@ pub struct PaymentsCancelData {
pub struct PaymentsSessionData {
pub amount: i64,
pub currency: storage_enums::Currency,
pub country: Option<String>,
pub country: Option<api::enums::CountryCode>,
pub order_details: Option<api_models::payments::OrderDetails>,
}

View File

@ -262,6 +262,7 @@ impl ForeignFrom<api_enums::Currency> for storage_enums::Currency {
frunk::labelled_convert_from(currency)
}
}
impl ForeignFrom<storage_enums::Currency> for api_enums::Currency {
fn foreign_from(currency: storage_enums::Currency) -> Self {
frunk::labelled_convert_from(currency)
@ -273,7 +274,7 @@ impl<'a> ForeignFrom<&'a api_types::Address> for storage::AddressUpdate {
let address = address;
Self::Update {
city: address.address.as_ref().and_then(|a| a.city.clone()),
country: address.address.as_ref().and_then(|a| a.country.clone()),
country: address.address.as_ref().and_then(|a| a.country),
line1: address.address.as_ref().and_then(|a| a.line1.clone()),
line2: address.address.as_ref().and_then(|a| a.line2.clone()),
line3: address.address.as_ref().and_then(|a| a.line3.clone()),
@ -312,7 +313,7 @@ impl<'a> ForeignFrom<&'a storage::Address> for api_types::Address {
Self {
address: Some(api_types::AddressDetails {
city: address.city.clone(),
country: address.country.clone(),
country: address.country,
line1: address.line1.clone(),
line2: address.line2.clone(),
line3: address.line3.clone(),