mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 00:49:42 +08:00
refactor(payment_method_data): send optional billing details in response (#4569)
This commit is contained in:
@ -2626,7 +2626,9 @@ where
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
if let Some(payment_method_data_response) = payment_method_data_response {
|
if let Some(payment_method_data_response) = payment_method_data_response {
|
||||||
match payment_method_data_response.payment_method_data {
|
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::Reward {} => serializer.serialize_str("reward"),
|
||||||
PaymentMethodDataResponse::BankDebit {}
|
PaymentMethodDataResponse::BankDebit {}
|
||||||
| PaymentMethodDataResponse::BankRedirect {}
|
| PaymentMethodDataResponse::BankRedirect {}
|
||||||
@ -2645,6 +2647,10 @@ where
|
|||||||
payment_method_data_response.serialize(serializer)
|
payment_method_data_response.serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Can serialize directly because there is no `payment_method_data`
|
||||||
|
payment_method_data_response.serialize(serializer)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
serializer.serialize_none()
|
serializer.serialize_none()
|
||||||
}
|
}
|
||||||
@ -2675,7 +2681,7 @@ pub enum PaymentMethodDataResponse {
|
|||||||
pub struct PaymentMethodDataResponseWithBilling {
|
pub struct PaymentMethodDataResponseWithBilling {
|
||||||
// The struct is flattened in order to provide backwards compatibility
|
// The struct is flattened in order to provide backwards compatibility
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub payment_method_data: PaymentMethodDataResponse,
|
pub payment_method_data: Option<PaymentMethodDataResponse>,
|
||||||
pub billing: Option<Address>,
|
pub billing: Option<Address>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4808,6 +4814,68 @@ mod payments_request_api_contract {
|
|||||||
Some(PaymentMethodData::Reward)
|
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
|
/// Set of tests to extract billing details from payment method data
|
||||||
|
|||||||
@ -527,7 +527,9 @@ impl From<payments::PaymentsResponse> for StripePaymentIntentResponse {
|
|||||||
capture_on: resp.capture_on,
|
capture_on: resp.capture_on,
|
||||||
capture_method: resp.capture_method,
|
capture_method: resp.capture_method,
|
||||||
payment_method: resp.payment_method,
|
payment_method: resp.payment_method,
|
||||||
payment_method_data: resp.payment_method_data.map(|pmd| pmd.payment_method_data),
|
payment_method_data: resp
|
||||||
|
.payment_method_data
|
||||||
|
.and_then(|pmd| pmd.payment_method_data),
|
||||||
payment_token: resp.payment_token,
|
payment_token: resp.payment_token,
|
||||||
shipping: resp.shipping,
|
shipping: resp.shipping,
|
||||||
billing: resp.billing,
|
billing: resp.billing,
|
||||||
|
|||||||
@ -463,14 +463,17 @@ where
|
|||||||
let payment_method_data =
|
let payment_method_data =
|
||||||
additional_payment_method_data.map(api::PaymentMethodDataResponse::from);
|
additional_payment_method_data.map(api::PaymentMethodDataResponse::from);
|
||||||
|
|
||||||
let payment_method_data_response = payment_method_data.map(|payment_method_data| {
|
let payment_method_data_response = (payment_method_data.is_some()
|
||||||
api_models::payments::PaymentMethodDataResponseWithBilling {
|
|| payment_data
|
||||||
|
.address
|
||||||
|
.get_request_payment_method_billing()
|
||||||
|
.is_some())
|
||||||
|
.then_some(api_models::payments::PaymentMethodDataResponseWithBilling {
|
||||||
payment_method_data,
|
payment_method_data,
|
||||||
billing: payment_data
|
billing: payment_data
|
||||||
.address
|
.address
|
||||||
.get_request_payment_method_billing()
|
.get_request_payment_method_billing()
|
||||||
.cloned(),
|
.cloned(),
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut headers = connector_http_status_code
|
let mut headers = connector_http_status_code
|
||||||
|
|||||||
Reference in New Issue
Block a user