Skip to content

Commit

Permalink
refactor(router): revert [Stripe/Itau/Paypal/Bambora/Cybs] prevent pa…
Browse files Browse the repository at this point in the history
…rtial submission of billing address and add required fields for all payment methods (#5745)
  • Loading branch information
AkshayaFoiger authored Aug 29, 2024
1 parent 6c266b5 commit 18f912d
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 794 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use base64::Engine;
use common_enums::enums;
use common_utils::{
ext_traits::ValueExt,
pii::{Email, IpAddress},
};
use common_utils::{ext_traits::ValueExt, pii::IpAddress};
use error_stack::ResultExt;
use hyperswitch_domain_models::{
payment_method_data::PaymentMethodData,
Expand Down Expand Up @@ -89,7 +86,6 @@ pub struct BamboraPaymentsRequest {
customer_ip: Option<Secret<String, IpAddress>>,
term_url: Option<String>,
card: BamboraCard,
billing: AddressData,
}

#[derive(Default, Debug, Serialize)]
Expand Down Expand Up @@ -177,37 +173,13 @@ impl TryFrom<BamboraRouterData<&types::PaymentsAuthorizeRouterData>> for Bambora
complete: item.router_data.request.is_auto_capture()?,
};

let province = item
.router_data
.get_optional_billing_state()
.and_then(|state| {
if state.clone().expose().len() > 2 {
None
} else {
Some(state)
}
});

let billing = AddressData {
name: item.router_data.get_optional_billing_full_name(),
address_line1: item.router_data.get_optional_billing_line1(),
address_line2: item.router_data.get_optional_billing_line2(),
city: item.router_data.get_optional_billing_city(),
province,
country: item.router_data.get_optional_billing_country(),
postal_code: item.router_data.get_optional_billing_zip(),
phone_number: item.router_data.get_optional_billing_phone_number(),
email_address: item.router_data.get_optional_billing_email(),
};

Ok(Self {
order_number: item.router_data.connector_request_reference_id.clone(),
amount: item.amount,
payment_method: PaymentMethod::Card,
card,
customer_ip,
term_url: item.router_data.request.complete_authorize_url.clone(),
billing,
})
}
PaymentMethodData::CardRedirect(_)
Expand Down Expand Up @@ -370,15 +342,15 @@ pub struct AvsObject {

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AddressData {
name: Option<Secret<String>>,
address_line1: Option<Secret<String>>,
address_line2: Option<Secret<String>>,
city: Option<String>,
province: Option<Secret<String>>,
country: Option<enums::CountryAlpha2>,
postal_code: Option<Secret<String>>,
phone_number: Option<Secret<String>>,
email_address: Option<Email>,
name: Secret<String>,
address_line1: Secret<String>,
address_line2: Secret<String>,
city: String,
province: String,
country: String,
postal_code: Secret<String>,
phone_number: Secret<String>,
email_address: Secret<String>,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
707 changes: 61 additions & 646 deletions crates/router/src/configs/defaults.rs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions crates/router/src/connector/cybersource/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::connector::utils::PayoutsData;
use crate::{
connector::utils::{
self, AddressDetailsData, ApplePayDecrypt, CardData, PaymentsAuthorizeRequestData,
PaymentsCompleteAuthorizeRequestData, PaymentsPreProcessingData, PaymentsSyncRequestData,
RecurringMandateData, RouterData,
PaymentsCompleteAuthorizeRequestData, PaymentsPreProcessingData,
PaymentsSetupMandateRequestData, PaymentsSyncRequestData, RecurringMandateData, RouterData,
},
consts,
core::errors,
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct CybersourceZeroMandateRequest {
impl TryFrom<&types::SetupMandateRouterData> for CybersourceZeroMandateRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::SetupMandateRouterData) -> Result<Self, Self::Error> {
let email = item.get_billing_email()?;
let email = item.request.get_email()?;
let bill_to = build_bill_to(item.get_optional_billing(), email)?;

let order_information = OrderInformationWithBill {
Expand Down Expand Up @@ -1033,7 +1033,7 @@ impl
domain::Card,
),
) -> Result<Self, Self::Error> {
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));

Expand Down Expand Up @@ -1113,7 +1113,7 @@ impl
domain::Card,
),
) -> Result<Self, Self::Error> {
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, bill_to));

Expand Down Expand Up @@ -1196,7 +1196,7 @@ impl
domain::ApplePayWalletData,
),
) -> Result<Self, Self::Error> {
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_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((
Expand Down Expand Up @@ -1265,7 +1265,7 @@ impl
domain::GooglePayWalletData,
),
) -> Result<Self, Self::Error> {
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));

Expand Down Expand Up @@ -1327,7 +1327,7 @@ impl TryFrom<&CybersourceRouterData<&types::PaymentsAuthorizeRouterData>>
}
},
None => {
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(
item.router_data.get_optional_billing(),
email,
Expand Down Expand Up @@ -1479,10 +1479,10 @@ impl
let payment_instrument = CybersoucrePaymentInstrument {
id: connector_mandate_id.into(),
};
let bill_to = item
.router_data
.get_optional_billing_email()
.and_then(|email| build_bill_to(item.router_data.get_optional_billing(), email).ok());
let bill_to =
item.router_data.request.get_email().ok().and_then(|email| {
build_bill_to(item.router_data.get_optional_billing(), email).ok()
});
let order_information = OrderInformationWithBill::from((item, bill_to));
let payment_information =
PaymentInformation::MandatePayment(Box::new(MandatePaymentInformation {
Expand Down Expand Up @@ -2310,7 +2310,7 @@ impl TryFrom<&CybersourceRouterData<&types::PaymentsPreProcessingRouterData>>
})?
.1
.to_string();
let email = item.router_data.get_billing_email()?;
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill {
amount_details,
Expand Down
41 changes: 24 additions & 17 deletions crates/router/src/connector/paypal/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,22 @@ pub struct ShippingAddress {
name: Option<ShippingName>,
}

impl From<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for ShippingAddress {
fn from(item: &PaypalRouterData<&types::PaymentsAuthorizeRouterData>) -> Self {
Self {
address: get_address_info(item.router_data.get_optional_shipping()),
impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for ShippingAddress {
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(
item: &PaypalRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
Ok(Self {
address: get_address_info(item.router_data.get_optional_shipping())?,
name: Some(ShippingName {
full_name: item
.router_data
.get_optional_shipping()
.and_then(|inner_data| inner_data.address.as_ref())
.and_then(|inner_data| inner_data.first_name.clone()),
}),
}
})
}
}

Expand Down Expand Up @@ -248,17 +252,20 @@ pub struct PaypalPaymentsRequest {
payment_source: Option<PaymentSourceItem>,
}

fn get_address_info(payment_address: Option<&api_models::payments::Address>) -> Option<Address> {
fn get_address_info(
payment_address: Option<&api_models::payments::Address>,
) -> Result<Option<Address>, error_stack::Report<errors::ConnectorError>> {
let address = payment_address.and_then(|payment_address| payment_address.address.as_ref());
match address {
Some(address) => address.get_optional_country().map(|country| Address {
country_code: country.to_owned(),
let address = match address {
Some(address) => Some(Address {
country_code: address.get_country()?.to_owned(),
address_line_1: address.line1.clone(),
postal_code: address.zip.clone(),
admin_area_2: address.city.clone(),
}),
None => None,
}
};
Ok(address)
}
fn get_payment_source(
item: &types::PaymentsAuthorizeRouterData,
Expand All @@ -272,7 +279,7 @@ fn get_payment_source(
experience_context: ContextStruct {
return_url: item.request.complete_authorize_url.clone(),
cancel_url: item.request.complete_authorize_url.clone(),
shipping_preference: if item.get_optional_shipping_country().is_some() {
shipping_preference: if item.get_optional_shipping().is_some() {
ShippingPreference::SetProvidedAddress
} else {
ShippingPreference::GetFromFile
Expand All @@ -288,7 +295,7 @@ fn get_payment_source(
experience_context: ContextStruct {
return_url: item.request.complete_authorize_url.clone(),
cancel_url: item.request.complete_authorize_url.clone(),
shipping_preference: if item.get_optional_shipping_country().is_some() {
shipping_preference: if item.get_optional_shipping().is_some() {
ShippingPreference::SetProvidedAddress
} else {
ShippingPreference::GetFromFile
Expand All @@ -304,7 +311,7 @@ fn get_payment_source(
experience_context: ContextStruct {
return_url: item.request.complete_authorize_url.clone(),
cancel_url: item.request.complete_authorize_url.clone(),
shipping_preference: if item.get_optional_shipping_country().is_some() {
shipping_preference: if item.get_optional_shipping().is_some() {
ShippingPreference::SetProvidedAddress
} else {
ShippingPreference::GetFromFile
Expand All @@ -321,7 +328,7 @@ fn get_payment_source(
experience_context: ContextStruct {
return_url: item.request.complete_authorize_url.clone(),
cancel_url: item.request.complete_authorize_url.clone(),
shipping_preference: if item.get_optional_shipping_country().is_some() {
shipping_preference: if item.get_optional_shipping().is_some() {
ShippingPreference::SetProvidedAddress
} else {
ShippingPreference::GetFromFile
Expand Down Expand Up @@ -385,7 +392,7 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
let connector_request_reference_id =
item.router_data.connector_request_reference_id.clone();

let shipping_address = ShippingAddress::from(item);
let shipping_address = ShippingAddress::try_from(item)?;
let item_details = vec![ItemDetails::from(item)];

let purchase_units = vec![PurchaseUnitRequest {
Expand Down Expand Up @@ -413,7 +420,7 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
};

let payment_source = Some(PaymentSourceItem::Card(CardRequest {
billing_address: get_address_info(item.router_data.get_optional_billing()),
billing_address: get_address_info(item.router_data.get_optional_billing())?,
expiry,
name: item
.router_data
Expand All @@ -439,7 +446,7 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
cancel_url: item.router_data.request.complete_authorize_url.clone(),
shipping_preference: if item
.router_data
.get_optional_shipping_country()
.get_optional_shipping()
.is_some()
{
ShippingPreference::SetProvidedAddress
Expand Down
2 changes: 0 additions & 2 deletions crates/router/src/connector/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ impl ConnectorValidation for Stripe {
PaymentMethodDataType::ApplePay,
PaymentMethodDataType::GooglePay,
PaymentMethodDataType::AchBankDebit,
PaymentMethodDataType::BacsBankDebit,
PaymentMethodDataType::BecsBankDebit,
PaymentMethodDataType::SepaBankDebit,
PaymentMethodDataType::Sofort,
PaymentMethodDataType::Ideal,
Expand Down
Loading

0 comments on commit 18f912d

Please sign in to comment.