Skip to content

Commit

Permalink
feat(connector): [BOA/CYB] Make billTo fields optional (#4951)
Browse files Browse the repository at this point in the history
Co-authored-by: Deepanshu Bansal <[email protected]>
  • Loading branch information
deepanshu-iiitu and Deepanshu Bansal authored Jun 12, 2024
1 parent b420522 commit 4651584
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 102 deletions.
134 changes: 83 additions & 51 deletions crates/router/src/connector/bankofamerica/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ pub struct Amount {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BillTo {
first_name: Secret<String>,
last_name: Secret<String>,
address1: Secret<String>,
locality: Secret<String>,
first_name: Option<Secret<String>>,
last_name: Option<Secret<String>>,
address1: Option<Secret<String>>,
locality: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
administrative_area: Option<Secret<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
postal_code: Option<Secret<String>>,
country: api_enums::CountryAlpha2,
country: Option<api_enums::CountryAlpha2>,
email: pii::Email,
}

Expand Down Expand Up @@ -442,47 +442,77 @@ impl<F, T>
}

// for bankofamerica each item in Billing is mandatory
// fn build_bill_to(
// address_details: &payments::Address,
// email: pii::Email,
// ) -> Result<BillTo, error_stack::Report<errors::ConnectorError>> {
// let address = address_details
// .address
// .as_ref()
// .ok_or_else(utils::missing_field_err("billing.address"))?;

// let country = address.get_country()?.to_owned();
// let first_name = address.get_first_name()?;

// let (administrative_area, postal_code) =
// if country == api_enums::CountryAlpha2::US || country == api_enums::CountryAlpha2::CA {
// let mut state = address.to_state_code()?.peek().clone();
// state.truncate(20);
// (
// Some(Secret::from(state)),
// Some(address.get_zip()?.to_owned()),
// )
// } else {
// let zip = address.zip.clone();
// let mut_state = address.state.clone().map(|state| state.expose());
// match mut_state {
// Some(mut state) => {
// state.truncate(20);
// (Some(Secret::from(state)), zip)
// }
// None => (None, zip),
// }
// };
// Ok(BillTo {
// first_name: first_name.clone(),
// last_name: address.get_last_name().unwrap_or(first_name).clone(),
// address1: address.get_line1()?.to_owned(),
// locality: Secret::new(address.get_city()?.to_owned()),
// administrative_area,
// postal_code,
// country,
// email,
// })
// }

fn build_bill_to(
address_details: &payments::Address,
address_details: Option<&payments::Address>,
email: pii::Email,
) -> Result<BillTo, error_stack::Report<errors::ConnectorError>> {
let address = address_details
.address
.as_ref()
.ok_or_else(utils::missing_field_err("billing.address"))?;

let country = address.get_country()?.to_owned();
let first_name = address.get_first_name()?;

let (administrative_area, postal_code) =
if country == api_enums::CountryAlpha2::US || country == api_enums::CountryAlpha2::CA {
let mut state = address.to_state_code()?.peek().clone();
state.truncate(20);
(
Some(Secret::from(state)),
Some(address.get_zip()?.to_owned()),
)
} else {
let zip = address.zip.clone();
let mut_state = address.state.clone().map(|state| state.expose());
match mut_state {
Some(mut state) => {
state.truncate(20);
(Some(Secret::from(state)), zip)
}
None => (None, zip),
}
};
Ok(BillTo {
first_name: first_name.clone(),
last_name: address.get_last_name().unwrap_or(first_name).clone(),
address1: address.get_line1()?.to_owned(),
locality: Secret::new(address.get_city()?.to_owned()),
administrative_area,
postal_code,
country,
email,
})
let default_address = BillTo {
first_name: None,
last_name: None,
address1: None,
locality: None,
administrative_area: None,
postal_code: None,
country: None,
email: email.clone(),
};
Ok(address_details
.and_then(|addr| {
addr.address.as_ref().map(|addr| BillTo {
first_name: addr.first_name.clone(),
last_name: addr.last_name.clone(),
address1: addr.line1.clone(),
locality: addr.city.clone(),
administrative_area: addr.to_state_code_as_optional().ok().flatten(),
postal_code: addr.zip.clone(),
country: addr.country,
email,
})
})
.unwrap_or(default_address))
}

impl From<CardIssuer> for String {
Expand Down Expand Up @@ -876,7 +906,7 @@ impl
),
) -> Result<Self, Self::Error> {
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, bill_to));

let payment_information = PaymentInformation::try_from(&ccard)?;
Expand Down Expand Up @@ -939,7 +969,7 @@ impl
),
) -> Result<Self, Self::Error> {
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));
let payment_information = PaymentInformation::try_from(&ccard)?;
let processing_information = ProcessingInformation::try_from((item, None, None))?;
Expand Down Expand Up @@ -976,7 +1006,7 @@ impl
),
) -> Result<Self, Self::Error> {
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));
let processing_information = ProcessingInformation::try_from((
item,
Expand Down Expand Up @@ -1030,7 +1060,7 @@ impl
),
) -> Result<Self, Self::Error> {
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));
let payment_information = PaymentInformation::from(&google_pay_data);
let processing_information =
Expand Down Expand Up @@ -1081,8 +1111,10 @@ impl TryFrom<&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>>
},
None => {
let email = item.router_data.request.get_email()?;
let bill_to =
build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(
item.router_data.get_optional_billing(),
email,
)?;
let order_information: OrderInformationWithBill =
OrderInformationWithBill::from((item, Some(bill_to)));
let processing_information =
Expand Down Expand Up @@ -1897,7 +1929,7 @@ impl TryFrom<&BankOfAmericaRouterData<&types::PaymentsPreProcessingRouterData>>
.1
.to_string();
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill {
amount_details,
bill_to: Some(bill_to),
Expand Down Expand Up @@ -3098,7 +3130,7 @@ impl TryFrom<&types::SetupMandateRouterData> for OrderInformationWithBill {

fn try_from(item: &types::SetupMandateRouterData) -> Result<Self, Self::Error> {
let email = item.request.get_email()?;
let bill_to = build_bill_to(item.get_billing()?, email)?;
let bill_to = build_bill_to(item.get_optional_billing(), email)?;

Ok(Self {
amount_details: Amount {
Expand Down
Loading

0 comments on commit 4651584

Please sign in to comment.