refactor(payment_methods): add a new domain type for payment method data to be used in connector module (#4140)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Narayan Bhat
2024-04-02 13:46:20 +05:30
committed by GitHub
parent 5587a12684
commit 9cce1520e3
120 changed files with 2232 additions and 1679 deletions

View File

@ -1292,26 +1292,14 @@ pub trait GetAddressFromPaymentMethodData {
impl GetAddressFromPaymentMethodData for PaymentMethodData {
fn get_billing_address(&self) -> Option<Address> {
match self {
Self::Card(card_data) => {
card_data
.card_holder_name
.as_ref()
.map(|card_holder_name| Address {
address: Some(AddressDetails {
first_name: Some(card_holder_name.clone()),
..AddressDetails::default()
}),
email: None,
phone: None,
})
}
Self::Card(_) => None,
Self::CardRedirect(_) => None,
Self::Wallet(wallet_data) => wallet_data.get_billing_address(),
Self::PayLater(pay_later_data) => pay_later_data.get_billing_address(),
Self::BankRedirect(bank_redirect_data) => bank_redirect_data.get_billing_address(),
Self::BankDebit(bank_debit_data) => bank_debit_data.get_billing_address(),
Self::BankTransfer(bank_transfer_data) => bank_transfer_data.get_billing_address(),
Self::Voucher(voucher_data) => voucher_data.get_billing_address(),
Self::Wallet(_) => None,
Self::PayLater(_) => None,
Self::BankRedirect(_) => None,
Self::BankDebit(_) => None,
Self::BankTransfer(_) => None,
Self::Voucher(_) => None,
Self::Crypto(_)
| Self::Reward
| Self::Upi(_)
@ -2597,6 +2585,21 @@ pub struct Address {
pub email: Option<Email>,
}
impl Address {
/// Unify the address, giving priority to `self` when details are present in both
pub fn unify_address(self, other: Option<&Self>) -> Self {
let other_address_details = other.and_then(|address| address.address.as_ref());
Self {
address: self
.address
.map(|address| address.unify_address_details(other_address_details))
.or(other_address_details.cloned()),
email: self.email.or(other.and_then(|other| other.email.clone())),
phone: self.phone.or(other.and_then(|other| other.phone.clone())),
}
}
}
// used by customers also, could be moved outside
/// Address details
#[derive(Clone, Default, Debug, Eq, serde::Deserialize, serde::Serialize, PartialEq, ToSchema)]
@ -2639,6 +2642,32 @@ pub struct AddressDetails {
pub last_name: Option<Secret<String>>,
}
impl AddressDetails {
pub fn unify_address_details(self, other: Option<&Self>) -> Self {
if let Some(other) = other {
let (first_name, last_name) = if self.first_name.is_some() {
(self.first_name, self.last_name)
} else {
(other.first_name.clone(), other.last_name.clone())
};
Self {
first_name,
last_name,
city: self.city.or(other.city.clone()),
country: self.country.or(other.country),
line1: self.line1.or(other.line1.clone()),
line2: self.line2.or(other.line2.clone()),
line3: self.line3.or(other.line3.clone()),
zip: self.zip.or(other.zip.clone()),
state: self.state.or(other.state.clone()),
}
} else {
self
}
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq, ToSchema, serde::Deserialize, serde::Serialize)]
pub struct PhoneDetails {
/// The contact number