refactor(payment_method): [Klarna] store and populate payment_type for klarna_sdk Paylater in response (#4956)

This commit is contained in:
Swangi Kumari
2024-06-20 17:24:32 +05:30
committed by GitHub
parent 61571625dc
commit c9bfb89f7e
4 changed files with 85 additions and 5 deletions

View File

@ -1814,7 +1814,9 @@ pub enum AdditionalPaymentData {
Wallet {
apple_pay: Option<ApplepayPaymentMethod>,
},
PayLater {},
PayLater {
klarna_sdk: Option<KlarnaSdkPaymentMethod>,
},
BankTransfer {},
Crypto {},
BankDebit {},
@ -1828,6 +1830,12 @@ pub enum AdditionalPaymentData {
CardToken {},
}
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct KlarnaSdkPaymentMethod {
pub payment_type: Option<String>,
}
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum BankRedirectData {
@ -2761,7 +2769,7 @@ where
| PaymentMethodDataResponse::Crypto {}
| PaymentMethodDataResponse::MandatePayment {}
| PaymentMethodDataResponse::GiftCard {}
| PaymentMethodDataResponse::PayLater {}
| PaymentMethodDataResponse::PayLater(_)
| PaymentMethodDataResponse::Paypal {}
| PaymentMethodDataResponse::RealTimePayment {}
| PaymentMethodDataResponse::Upi {}
@ -2787,7 +2795,7 @@ pub enum PaymentMethodDataResponse {
Card(Box<CardResponse>),
BankTransfer {},
Wallet {},
PayLater {},
PayLater(Box<PaylaterResponse>),
Paypal {},
BankRedirect {},
Crypto {},
@ -2802,6 +2810,17 @@ pub enum PaymentMethodDataResponse {
CardToken {},
}
#[derive(Eq, PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PaylaterResponse {
klarna_sdk: Option<KlarnaSdkPaymentMethodResponse>,
}
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct KlarnaSdkPaymentMethodResponse {
pub payment_type: Option<String>,
}
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, ToSchema, serde::Serialize)]
pub struct PaymentMethodDataResponseWithBilling {
// The struct is flattened in order to provide backwards compatibility
@ -3900,11 +3919,24 @@ impl From<AdditionalCardInfo> for CardResponse {
}
}
impl From<KlarnaSdkPaymentMethod> for PaylaterResponse {
fn from(klarna_sdk: KlarnaSdkPaymentMethod) -> Self {
Self {
klarna_sdk: Some(KlarnaSdkPaymentMethodResponse {
payment_type: klarna_sdk.payment_type,
}),
}
}
}
impl From<AdditionalPaymentData> for PaymentMethodDataResponse {
fn from(payment_method_data: AdditionalPaymentData) -> Self {
match payment_method_data {
AdditionalPaymentData::Card(card) => Self::Card(Box::new(CardResponse::from(*card))),
AdditionalPaymentData::PayLater {} => Self::PayLater {},
AdditionalPaymentData::PayLater { klarna_sdk } => match klarna_sdk {
Some(sdk) => Self::PayLater(Box::new(PaylaterResponse::from(sdk))),
None => Self::PayLater(Box::new(PaylaterResponse { klarna_sdk: None })),
},
AdditionalPaymentData::Wallet { .. } => Self::Wallet {},
AdditionalPaymentData::BankRedirect { .. } => Self::BankRedirect {},
AdditionalPaymentData::Crypto {} => Self::Crypto {},

View File

@ -173,6 +173,14 @@ pub enum AdditionalPaymentMethodConnectorResponse {
/// Various payment checks that are done for a payment
payment_checks: Option<serde_json::Value>,
},
PayLater {
klarna_sdk: Option<KlarnaSdkResponse>,
},
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KlarnaSdkResponse {
pub payment_type: Option<String>,
}
#[derive(Clone, Debug, serde::Serialize)]

View File

@ -1,6 +1,7 @@
use api_models::payments;
use common_utils::pii;
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::router_data::KlarnaSdkResponse;
use masking::{ExposeInterface, Secret};
use serde::{Deserialize, Serialize};
@ -84,6 +85,23 @@ pub struct KlarnaPaymentsRequest {
pub struct KlarnaPaymentsResponse {
order_id: String,
fraud_status: KlarnaFraudStatus,
authorized_payment_method: Option<AuthorizedPaymentMethod>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AuthorizedPaymentMethod {
#[serde(rename = "type")]
payment_type: String,
}
impl From<AuthorizedPaymentMethod> for types::AdditionalPaymentMethodConnectorResponse {
fn from(item: AuthorizedPaymentMethod) -> Self {
Self::PayLater {
klarna_sdk: Some(KlarnaSdkResponse {
payment_type: Some(item.payment_type),
}),
}
}
}
#[derive(Debug, Serialize)]
@ -236,6 +254,17 @@ impl TryFrom<types::PaymentsResponseRouterData<KlarnaPaymentsResponse>>
fn try_from(
item: types::PaymentsResponseRouterData<KlarnaPaymentsResponse>,
) -> Result<Self, Self::Error> {
let connector_response = types::ConnectorResponseData::with_additional_payment_method_data(
match item.response.authorized_payment_method {
Some(authorized_payment_method) => {
types::AdditionalPaymentMethodConnectorResponse::from(authorized_payment_method)
}
None => {
types::AdditionalPaymentMethodConnectorResponse::PayLater { klarna_sdk: None }
}
},
);
Ok(Self {
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(
@ -253,6 +282,7 @@ impl TryFrom<types::PaymentsResponseRouterData<KlarnaPaymentsResponse>>
item.response.fraud_status,
item.data.request.is_auto_capture()?,
)),
connector_response: Some(connector_response),
..item.data
})
}

View File

@ -18,6 +18,7 @@ use futures::future::Either;
use hyperswitch_domain_models::{
mandates::MandateData,
payments::{payment_attempt::PaymentAttempt, PaymentIntent},
router_data::KlarnaSdkResponse,
};
use josekit::jwe;
use masking::{ExposeInterface, PeekInterface};
@ -3857,7 +3858,7 @@ pub async fn get_additional_payment_data(
_ => api_models::payments::AdditionalPaymentData::Wallet { apple_pay: None },
},
api_models::payments::PaymentMethodData::PayLater(_) => {
api_models::payments::AdditionalPaymentData::PayLater {}
api_models::payments::AdditionalPaymentData::PayLater { klarna_sdk: None }
}
api_models::payments::PaymentMethodData::BankTransfer(_) => {
api_models::payments::AdditionalPaymentData::BankTransfer {}
@ -4501,6 +4502,15 @@ pub fn add_connector_response_to_additional_payment_data(
..*additional_card_data.clone()
},
)),
(
api_models::payments::AdditionalPaymentData::PayLater { .. },
AdditionalPaymentMethodConnectorResponse::PayLater {
klarna_sdk: Some(KlarnaSdkResponse { payment_type }),
},
) => api_models::payments::AdditionalPaymentData::PayLater {
klarna_sdk: Some(api_models::payments::KlarnaSdkPaymentMethod { payment_type }),
},
_ => additional_payment_data,
}
}