Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api_response): ghost payment_method_billing being populated in the response #4085

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,7 @@ pub mod payment_address {
pub struct PaymentAddress {
shipping: Option<api::Address>,
billing: Option<api::Address>,
unified_payment_method_billing: Option<api::Address>,
payment_method_billing: Option<api::Address>,
}

Expand All @@ -2131,20 +2132,26 @@ pub mod payment_address {
billing: Option<api::Address>,
payment_method_billing: Option<api::Address>,
) -> Self {
let payment_method_billing = match (payment_method_billing, billing.clone()) {
(Some(payment_method_billing), Some(order_billing)) => Some(api::Address {
address: payment_method_billing.address.or(order_billing.address),
phone: payment_method_billing.phone.or(order_billing.phone),
email: payment_method_billing.email.or(order_billing.email),
}),
(Some(payment_method_billing), None) => Some(payment_method_billing),
(None, Some(order_billing)) => Some(order_billing),
(None, None) => None,
};
// Merge the billing details field from both `payment.billing` and `payment.payment_method_data.billing`
// The unified payment_method_billing will be used as billing address and passed to the connector module
// This unification is required in order to provide backwards compatibility
// so that if `payment.billing` is passed it should be sent to the connector module
let unified_payment_method_billing =
match (payment_method_billing.clone(), billing.clone()) {
(Some(payment_method_billing), Some(order_billing)) => Some(api::Address {
address: payment_method_billing.address.or(order_billing.address),
phone: payment_method_billing.phone.or(order_billing.phone),
email: payment_method_billing.email.or(order_billing.email),
}),
(Some(payment_method_billing), None) => Some(payment_method_billing),
(None, Some(order_billing)) => Some(order_billing),
(None, None) => None,
};

Self {
shipping,
billing,
unified_payment_method_billing,
payment_method_billing,
}
}
Expand All @@ -2154,6 +2161,10 @@ pub mod payment_address {
}

pub fn get_payment_method_billing(&self) -> Option<&api::Address> {
self.unified_payment_method_billing.as_ref()
}

pub fn get_request_payment_method_billing(&self) -> Option<&api::Address> {
self.payment_method_billing.as_ref()
}

Expand Down
5 changes: 4 additions & 1 deletion crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ where
let payment_method_data_response = payment_method_data.map(|payment_method_data| {
api_models::payments::PaymentMethodDataResponseWithBilling {
payment_method_data,
billing: payment_data.address.get_payment_method_billing().cloned(),
billing: payment_data
.address
.get_request_payment_method_billing()
.cloned(),
}
});

Expand Down
17 changes: 12 additions & 5 deletions crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ impl<'a> ForeignFrom<&'a api_types::ConfigUpdate> for storage::ConfigUpdate {

impl<'a> From<&'a domain::Address> for api_types::Address {
fn from(address: &domain::Address) -> Self {
// If all the fields of address are none, then
// If all the fields of address are none, then pass the address as None
let address_details = if address.city.is_none()
&& address.line1.is_none()
&& address.line2.is_none()
Expand All @@ -608,12 +608,19 @@ impl<'a> From<&'a domain::Address> for api_types::Address {
})
};

Self {
address: address_details,
phone: Some(api_types::PhoneDetails {
// If all the fields of phone are none, then pass the phone as None
let phone_details = if address.phone_number.is_none() && address.country_code.is_none() {
None
} else {
Some(api_types::PhoneDetails {
number: address.phone_number.clone().map(Encryptable::into_inner),
country_code: address.country_code.clone(),
}),
})
};

Self {
address: address_details,
phone: phone_details,
email: address.email.clone().map(pii::Email::from),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ pm.test(
.true;
},
);

// Response body should not have "payment.billing"
pm.test(
"[POST]::/payments - Content check if 'payment.billing' is null",
function () {
pm.expect(jsonData?.billing).to.be.null
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@
"email": "example@juspay.in"
}
},
"billing": {
"address": {
"line1": "1467",
"line2": "Harrison Street",
"line3": "Harrison Street",
"city": "San Fransico",
"state": "California",
"zip": "94122",
"country": "US",
"first_name": "sundari",
"last_name": "sundari"
}
},
"shipping": {
"address": {
"line1": "1467",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ pm.test(
.true;
},
);

// Response body should not have "payment_method_data.billing"
pm.test(
"[POST]::/payments - Content check if 'payment_method_data.billing' should be null",
function () {
pm.expect(jsonData?.payment_method_data?.billing).to.be.null;
},
);
18 changes: 17 additions & 1 deletion postman/collection-json/stripe.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,14 @@
" .true;",
" },",
");",
"",
"// Response body should not have \"payment_method_data.billing\"",
"pm.test(",
" \"[POST]::/payments - Content check if 'payment_method_data.billing' should be null\",",
" function () {",
" pm.expect(jsonData?.payment_method_data?.billing).to.be.null;",
" },",
");",
""
],
"type": "text/javascript"
Expand Down Expand Up @@ -20865,6 +20873,14 @@
" .true;",
" },",
");",
"",
"// Response body should not have \"payment.billing\"",
"pm.test(",
" \"[POST]::/payments - Content check if 'payment.billing' is null\",",
" function () {",
" pm.expect(jsonData?.billing).to.be.null",
" },",
");",
""
],
"type": "text/javascript"
Expand All @@ -20890,7 +20906,7 @@
"language": "json"
}
},
"raw": "{\"amount\":6540,\"currency\":\"USD\",\"confirm\":true,\"business_country\":\"US\",\"business_label\":\"default\",\"capture_method\":\"automatic\",\"capture_on\":\"2022-09-10T10:11:12Z\",\"amount_to_capture\":6540,\"customer_id\":\"bernard123\",\"email\":\"guest@example.com\",\"name\":\"John Doe\",\"phone\":\"999999999\",\"phone_country_code\":\"+65\",\"description\":\"Its my first payment request\",\"authentication_type\":\"no_three_ds\",\"return_url\":\"https://duck.com\",\"setup_future_usage\":\"on_session\",\"payment_method\":\"card\",\"payment_method_type\":\"debit\",\"payment_method_data\":{\"card\":{\"card_number\":\"4242424242424242\",\"card_exp_month\":\"01\",\"card_exp_year\":\"26\",\"card_holder_name\":\"joseph Doe\",\"card_cvc\":\"123\"},\"billing\":{\"address\":{\"line1\":\"1467\",\"line2\":\"Harrison Street\",\"line3\":\"Harrisoff Street\",\"city\":\"San Fransico\",\"state\":\"California\",\"zip\":\"94122\",\"country\":\"US\",\"first_name\":\"Narayan\",\"last_name\":\"Doe\"},\"email\":\"example@juspay.in\"}},\"billing\":{\"address\":{\"line1\":\"1467\",\"line2\":\"Harrison Street\",\"line3\":\"Harrison Street\",\"city\":\"San Fransico\",\"state\":\"California\",\"zip\":\"94122\",\"country\":\"US\",\"first_name\":\"sundari\",\"last_name\":\"sundari\"}},\"shipping\":{\"address\":{\"line1\":\"1467\",\"line2\":\"Harrison Street\",\"line3\":\"Harrison Street\",\"city\":\"San Fransico\",\"state\":\"California\",\"zip\":\"94122\",\"country\":\"US\",\"first_name\":\"sundari\",\"last_name\":\"sundari\"}},\"statement_descriptor_name\":\"joseph\",\"statement_descriptor_suffix\":\"JS\",\"metadata\":{\"udf1\":\"value1\",\"new_customer\":\"true\",\"login_date\":\"2019-09-10T10:11:12Z\"},\"routing\":{\"type\":\"single\",\"data\":\"stripe\"}}"
"raw": "{\"amount\":6540,\"currency\":\"USD\",\"confirm\":true,\"business_country\":\"US\",\"business_label\":\"default\",\"capture_method\":\"automatic\",\"capture_on\":\"2022-09-10T10:11:12Z\",\"amount_to_capture\":6540,\"customer_id\":\"bernard123\",\"email\":\"guest@example.com\",\"name\":\"John Doe\",\"phone\":\"999999999\",\"phone_country_code\":\"+65\",\"description\":\"Its my first payment request\",\"authentication_type\":\"no_three_ds\",\"return_url\":\"https://duck.com\",\"setup_future_usage\":\"on_session\",\"payment_method\":\"card\",\"payment_method_type\":\"debit\",\"payment_method_data\":{\"card\":{\"card_number\":\"4242424242424242\",\"card_exp_month\":\"01\",\"card_exp_year\":\"26\",\"card_holder_name\":\"joseph Doe\",\"card_cvc\":\"123\"},\"billing\":{\"address\":{\"line1\":\"1467\",\"line2\":\"Harrison Street\",\"line3\":\"Harrisoff Street\",\"city\":\"San Fransico\",\"state\":\"California\",\"zip\":\"94122\",\"country\":\"US\",\"first_name\":\"Narayan\",\"last_name\":\"Doe\"},\"email\":\"example@juspay.in\"}},\"shipping\":{\"address\":{\"line1\":\"1467\",\"line2\":\"Harrison Street\",\"line3\":\"Harrison Street\",\"city\":\"San Fransico\",\"state\":\"California\",\"zip\":\"94122\",\"country\":\"US\",\"first_name\":\"sundari\",\"last_name\":\"sundari\"}},\"statement_descriptor_name\":\"joseph\",\"statement_descriptor_suffix\":\"JS\",\"metadata\":{\"udf1\":\"value1\",\"new_customer\":\"true\",\"login_date\":\"2019-09-10T10:11:12Z\"},\"routing\":{\"type\":\"single\",\"data\":\"stripe\"}}"
},
"url": {
"raw": "{{baseUrl}}/payments",
Expand Down
Loading