refactor(payment_method_data): send optional billing details in response (#4569)

This commit is contained in:
Narayan Bhat
2024-05-10 15:05:25 +05:30
committed by GitHub
parent fef28c3345
commit 86e05501cb
3 changed files with 100 additions and 27 deletions

View File

@ -2626,24 +2626,30 @@ where
S: Serializer,
{
if let Some(payment_method_data_response) = payment_method_data_response {
match payment_method_data_response.payment_method_data {
PaymentMethodDataResponse::Reward {} => serializer.serialize_str("reward"),
PaymentMethodDataResponse::BankDebit {}
| PaymentMethodDataResponse::BankRedirect {}
| PaymentMethodDataResponse::Card(_)
| PaymentMethodDataResponse::CardRedirect {}
| PaymentMethodDataResponse::CardToken {}
| PaymentMethodDataResponse::Crypto {}
| PaymentMethodDataResponse::MandatePayment {}
| PaymentMethodDataResponse::GiftCard {}
| PaymentMethodDataResponse::PayLater {}
| PaymentMethodDataResponse::Paypal {}
| PaymentMethodDataResponse::Upi {}
| PaymentMethodDataResponse::Wallet {}
| PaymentMethodDataResponse::BankTransfer {}
| PaymentMethodDataResponse::Voucher {} => {
payment_method_data_response.serialize(serializer)
if let Some(payment_method_data) = payment_method_data_response.payment_method_data.as_ref()
{
match payment_method_data {
PaymentMethodDataResponse::Reward {} => serializer.serialize_str("reward"),
PaymentMethodDataResponse::BankDebit {}
| PaymentMethodDataResponse::BankRedirect {}
| PaymentMethodDataResponse::Card(_)
| PaymentMethodDataResponse::CardRedirect {}
| PaymentMethodDataResponse::CardToken {}
| PaymentMethodDataResponse::Crypto {}
| PaymentMethodDataResponse::MandatePayment {}
| PaymentMethodDataResponse::GiftCard {}
| PaymentMethodDataResponse::PayLater {}
| PaymentMethodDataResponse::Paypal {}
| PaymentMethodDataResponse::Upi {}
| PaymentMethodDataResponse::Wallet {}
| PaymentMethodDataResponse::BankTransfer {}
| PaymentMethodDataResponse::Voucher {} => {
payment_method_data_response.serialize(serializer)
}
}
} else {
// Can serialize directly because there is no `payment_method_data`
payment_method_data_response.serialize(serializer)
}
} else {
serializer.serialize_none()
@ -2675,7 +2681,7 @@ pub enum PaymentMethodDataResponse {
pub struct PaymentMethodDataResponseWithBilling {
// The struct is flattened in order to provide backwards compatibility
#[serde(flatten)]
pub payment_method_data: PaymentMethodDataResponse,
pub payment_method_data: Option<PaymentMethodDataResponse>,
pub billing: Option<Address>,
}
@ -4808,6 +4814,68 @@ mod payments_request_api_contract {
Some(PaymentMethodData::Reward)
);
}
#[test]
fn test_payment_method_data_with_payment_method_billing() {
let payments_request = r#"
{
"amount": 6540,
"currency": "USD",
"payment_method_data": {
"billing": {
"address": {
"line1": "1467",
"line2": "Harrison Street",
"city": "San Fransico",
"state": "California",
"zip": "94122",
"country": "US",
"first_name": "Narayan",
"last_name": "Bhat"
}
}
}
}
"#;
let payments_request = serde_json::from_str::<PaymentsRequest>(payments_request);
assert!(payments_request.is_ok());
assert!(payments_request
.unwrap()
.payment_method_data
.unwrap()
.billing
.is_some());
}
}
#[cfg(test)]
mod payments_response_api_contract {
#![allow(clippy::unwrap_used)]
use super::*;
#[derive(Debug, serde::Serialize)]
struct TestPaymentsResponse {
#[serde(serialize_with = "serialize_payment_method_data_response")]
payment_method_data: Option<PaymentMethodDataResponseWithBilling>,
}
#[test]
fn test_reward_payment_response() {
let payment_method_response_with_billing = PaymentMethodDataResponseWithBilling {
payment_method_data: Some(PaymentMethodDataResponse::Reward {}),
billing: None,
};
let payments_response = TestPaymentsResponse {
payment_method_data: Some(payment_method_response_with_billing),
};
let expected_response = r#"{"payment_method_data":"reward"}"#;
let stringified_payments_response = payments_response.encode_to_string_of_json();
assert_eq!(stringified_payments_response.unwrap(), expected_response);
}
}
/// Set of tests to extract billing details from payment method data