Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(connector): [Stripe] make name field of StripeShippingAddress mandatory (#4111)

AkshayaFoigerab1ec2ad4e3f
diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs
index dd8fc845e2..4bbf7ee0d2 100644
--- a/crates/router/src/connector/stripe/transformers.rs
+++ b/crates/router/src/connector/stripe/transformers.rs
@@ -122,7 +122,7 @@ pub struct PaymentIntentRequest {
pub setup_mandate_details: Option<StripeMandateRequest>,
pub description: Option<String>,
#[serde(flatten)]
- pub shipping: StripeShippingAddress,
+ pub shipping: Option<StripeShippingAddress>,
#[serde(flatten)]
pub billing: StripeBillingAddress,
#[serde(flatten)]
@@ -885,26 +885,33 @@ impl TryFrom<&api_models::enums::BankNames> for StripeBankNames {
}
fn validate_shipping_address_against_payment_method(
- shipping_address: &StripeShippingAddress,
+ shipping_address: &Option<StripeShippingAddress>,
payment_method: Option<&StripePaymentMethodType>,
) -> Result<(), error_stack::Report<errors::ConnectorError>> {
- if let Some(StripePaymentMethodType::AfterpayClearpay) = payment_method {
- let missing_fields = collect_missing_value_keys!(
- ("shipping.address.first_name", shipping_address.name),
- ("shipping.address.line1", shipping_address.line1),
- ("shipping.address.country", shipping_address.country),
- ("shipping.address.zip", shipping_address.zip)
- );
-
- if !missing_fields.is_empty() {
- return Err(errors::ConnectorError::MissingRequiredFields {
- field_names: missing_fields,
+ match payment_method {
+ Some(StripePaymentMethodType::AfterpayClearpay) => match shipping_address {
+ Some(address) => {
+ let missing_fields = collect_missing_value_keys!(
+ ("shipping.address.line1", address.line1),
+ ("shipping.address.country", address.country),
+ ("shipping.address.zip", address.zip)
+ );
+
+ if !missing_fields.is_empty() {
+ return Err(errors::ConnectorError::MissingRequiredFields {
+ field_names: missing_fields,
+ })
+ .into_report();
+ }
+ Ok(())
+ }
+ None => Err(errors::ConnectorError::MissingRequiredField {
+ field_name: "shipping.address",
})
- .into_report();
- }
+ .into_report(),
+ },
+ _ => Ok(()),
}
-
- Ok(())
}
impl TryFrom<&api_models::payments::PayLaterData> for StripePaymentMethodType {
@@ -1710,23 +1717,27 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentIntentRequest {
let shipping_address = match item.get_optional_shipping() {
Some(shipping_details) => {
let shipping_address = shipping_details.address.as_ref();
- StripeShippingAddress {
+ Some(StripeShippingAddress {
city: shipping_address.and_then(|a| a.city.clone()),
country: shipping_address.and_then(|a| a.country),
line1: shipping_address.and_then(|a| a.line1.clone()),
line2: shipping_address.and_then(|a| a.line2.clone()),
zip: shipping_address.and_then(|a| a.zip.clone()),
state: shipping_address.and_then(|a| a.state.clone()),
- name: shipping_address.and_then(|a| {
- a.first_name.as_ref().map(|first_name| {
- format!(
- "{} {}",
- first_name.clone().expose(),
- a.last_name.clone().expose_option().unwrap_or_default()
- )
- .into()
+ name: shipping_address
+ .and_then(|a| {
+ a.first_name.as_ref().map(|first_name| {
+ format!(
+ "{} {}",
+ first_name.clone().expose(),
+ a.last_name.clone().expose_option().unwrap_or_default()
+ )
+ .into()
+ })
})
- }),
+ .ok_or(errors::ConnectorError::MissingRequiredField {
+ field_name: "shipping_address.first_name",
+ })?,
phone: shipping_details.phone.as_ref().map(|p| {
format!(
"{}{}",
@@ -1735,9 +1746,9 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentIntentRequest {
)
.into()
}),
- }
+ })
}
- None => StripeShippingAddress::default(),
+ None => None,
};
let billing_address = match item.get_optional_billing() {
@@ -2923,7 +2934,7 @@ pub struct StripeShippingAddress {
#[serde(rename = "shipping[address][state]")]
pub state: Option<Secret<String>>,
#[serde(rename = "shipping[name]")]
- pub name: Option<Secret<String>>,
+ pub name: Secret<String>,
#[serde(rename = "shipping[phone]")]
pub phone: Option<Secret<String>>,
}
@@ -3833,7 +3844,7 @@ mod test_validate_shipping_address_against_payment_method {
fn should_return_ok() {
// Arrange
let stripe_shipping_address = create_stripe_shipping_address(
- Some("name".to_string()),
+ "name".to_string(),
Some("line1".to_string()),
Some(CountryAlpha2::AD),
Some("zip".to_string()),
@@ -3843,7 +3854,7 @@ mod test_validate_shipping_address_against_payment_method {
//Act
let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
+ &Some(stripe_shipping_address),
Some(payment_method),
);
@@ -3851,39 +3862,11 @@ mod test_validate_shipping_address_against_payment_method {
assert!(result.is_ok());
}
- #[test]
- fn should_return_err_for_empty_name() {
- // Arrange
- let stripe_shipping_address = create_stripe_shipping_address(
- None,
- Some("line1".to_string()),
- Some(CountryAlpha2::AD),
- Some("zip".to_string()),
- );
-
- let payment_method = &StripePaymentMethodType::AfterpayClearpay;
-
- //Act
- let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
- Some(payment_method),
- );
-
- // Assert
- assert!(result.is_err());
- let missing_fields = get_missing_fields(result.unwrap_err().current_context()).to_owned();
- assert_eq!(missing_fields.len(), 1);
- assert_eq!(
- *missing_fields.first().unwrap(),
- "shipping.address.first_name"
- );
- }
-
#[test]
fn should_return_err_for_empty_line1() {
// Arrange
let stripe_shipping_address = create_stripe_shipping_address(
- Some("name".to_string()),
+ "name".to_string(),
None,
Some(CountryAlpha2::AD),
Some("zip".to_string()),
@@ -3893,7 +3876,7 @@ mod test_validate_shipping_address_against_payment_method {
//Act
let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
+ &Some(stripe_shipping_address),
Some(payment_method),
);
@@ -3908,7 +3891,7 @@ mod test_validate_shipping_address_against_payment_method {
fn should_return_err_for_empty_country() {
// Arrange
let stripe_shipping_address = create_stripe_shipping_address(
- Some("name".to_string()),
+ "name".to_string(),
Some("line1".to_string()),
None,
Some("zip".to_string()),
@@ -3918,7 +3901,7 @@ mod test_validate_shipping_address_against_payment_method {
//Act
let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
+ &Some(stripe_shipping_address),
Some(payment_method),
);
@@ -3933,7 +3916,7 @@ mod test_validate_shipping_address_against_payment_method {
fn should_return_err_for_empty_zip() {
// Arrange
let stripe_shipping_address = create_stripe_shipping_address(
- Some("name".to_string()),
+ "name".to_string(),
Some("line1".to_string()),
Some(CountryAlpha2::AD),
None,
@@ -3942,7 +3925,7 @@ mod test_validate_shipping_address_against_payment_method {
//Act
let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
+ &Some(stripe_shipping_address),
Some(payment_method),
);
@@ -3959,7 +3942,7 @@ mod test_validate_shipping_address_against_payment_method {
let expected_missing_field_names: Vec<&'static str> =
vec!["shipping.address.zip", "shipping.address.country"];
let stripe_shipping_address = create_stripe_shipping_address(
- Some("name".to_string()),
+ "name".to_string(),
Some("line1".to_string()),
None,
None,
@@ -3968,7 +3951,7 @@ mod test_validate_shipping_address_against_payment_method {
//Act
let result = validate_shipping_address_against_payment_method(
- &stripe_shipping_address,
+ &Some(stripe_shipping_address),
Some(payment_method),
);
@@ -3989,13 +3972,13 @@ mod test_validate_shipping_address_against_payment_method {
}
fn create_stripe_shipping_address(
- name: Option<String>,
+ name: String,
line1: Option<String>,
country: Option<CountryAlpha2>,
zip: Option<String>,
) -> StripeShippingAddress {
StripeShippingAddress {
- name: name.map(Secret::new),
+ name: Secret::new(name),
line1: line1.map(Secret::new),
country,
zip: zip.map(Secret::new),