From 865007717c5c7e617ca1b447ea5f9bb3d274cac3 Mon Sep 17 00:00:00 2001 From: Swangi Kumari <85639103+swangi-kumari@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:24:56 +0530 Subject: [PATCH] refactor(connector): [Klarna] Add shipping Address in Klarna Session and Payment Request (#4836) --- crates/router/src/connector/klarna.rs | 1 + .../src/connector/klarna/transformers.rs | 40 ++++++++ crates/router/src/connector/utils.rs | 95 +++++++++++++++++++ 3 files changed, 136 insertions(+) diff --git a/crates/router/src/connector/klarna.rs b/crates/router/src/connector/klarna.rs index 3258e08ba991..2059b00ac363 100644 --- a/crates/router/src/connector/klarna.rs +++ b/crates/router/src/connector/klarna.rs @@ -665,6 +665,7 @@ impl req, ))?; let connector_req = klarna::KlarnaPaymentsRequest::try_from(&connector_router_data)?; + Ok(RequestContent::Json(Box::new(connector_req))) } diff --git a/crates/router/src/connector/klarna/transformers.rs b/crates/router/src/connector/klarna/transformers.rs index c14af3aad318..69b9dfc67bb9 100644 --- a/crates/router/src/connector/klarna/transformers.rs +++ b/crates/router/src/connector/klarna/transformers.rs @@ -75,6 +75,7 @@ pub struct KlarnaPaymentsRequest { purchase_country: enums::CountryAlpha2, purchase_currency: enums::Currency, merchant_reference1: Option, + shipping_address: Option, } #[derive(Debug, Deserialize, Serialize)] @@ -90,6 +91,21 @@ pub struct KlarnaSessionRequest { purchase_currency: enums::Currency, order_amount: i64, order_lines: Vec, + shipping_address: Option, +} + +#[derive(Debug, Serialize)] +pub struct KlarnaShippingAddress { + city: Option, + country: Option, + email: Option, + given_name: Option>, + family_name: Option>, + phone: Option>, + postal_code: Option>, + region: Option>, + street_address: Option>, + street_address2: Option>, } #[derive(Deserialize, Serialize, Debug)] @@ -123,6 +139,18 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsSessionRouterData>> for KlarnaSes total_amount: i64::from(data.quantity) * (data.amount), }) .collect(), + shipping_address: Some(KlarnaShippingAddress { + city: item.router_data.get_optional_shipping_city(), + country: item.router_data.get_optional_shipping_country(), + email: item.router_data.get_optional_shipping_email(), + given_name: item.router_data.get_optional_shipping_first_name(), + family_name: item.router_data.get_optional_shipping_last_name(), + phone: item.router_data.get_optional_shipping_phone_number(), + postal_code: item.router_data.get_optional_shipping_zip(), + region: item.router_data.get_optional_shipping_state(), + street_address: item.router_data.get_optional_shipping_line1(), + street_address2: item.router_data.get_optional_shipping_line2(), + }), }), None => Err(report!(errors::ConnectorError::MissingRequiredField { field_name: "order_details", @@ -176,6 +204,18 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaP .collect(), merchant_reference1: Some(item.router_data.connector_request_reference_id.clone()), auto_capture: request.is_auto_capture()?, + shipping_address: Some(KlarnaShippingAddress { + city: item.router_data.get_optional_shipping_city(), + country: item.router_data.get_optional_shipping_country(), + email: item.router_data.get_optional_shipping_email(), + given_name: item.router_data.get_optional_shipping_first_name(), + family_name: item.router_data.get_optional_shipping_last_name(), + phone: item.router_data.get_optional_shipping_phone_number(), + postal_code: item.router_data.get_optional_shipping_zip(), + region: item.router_data.get_optional_shipping_state(), + street_address: item.router_data.get_optional_shipping_line1(), + street_address2: item.router_data.get_optional_shipping_line2(), + }), }), None => Err(report!(errors::ConnectorError::MissingRequiredField { field_name: "order_details" diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs index ba0ef166808c..6737bbf3af0f 100644 --- a/crates/router/src/connector/utils.rs +++ b/crates/router/src/connector/utils.rs @@ -101,6 +101,16 @@ pub trait RouterData { fn get_optional_billing(&self) -> Option<&api::Address>; fn get_optional_shipping(&self) -> Option<&api::Address>; + fn get_optional_shipping_line1(&self) -> Option>; + fn get_optional_shipping_line2(&self) -> Option>; + fn get_optional_shipping_city(&self) -> Option; + fn get_optional_shipping_country(&self) -> Option; + fn get_optional_shipping_zip(&self) -> Option>; + fn get_optional_shipping_state(&self) -> Option>; + fn get_optional_shipping_first_name(&self) -> Option>; + fn get_optional_shipping_last_name(&self) -> Option>; + fn get_optional_shipping_phone_number(&self) -> Option>; + fn get_optional_shipping_email(&self) -> Option; fn get_optional_billing_full_name(&self) -> Option>; fn get_optional_billing_line1(&self) -> Option>; @@ -199,6 +209,91 @@ impl RouterData for types::RouterData Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.first_name) + }) + } + + fn get_optional_shipping_last_name(&self) -> Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.last_name) + }) + } + + fn get_optional_shipping_line1(&self) -> Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.line1) + }) + } + + fn get_optional_shipping_line2(&self) -> Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.line2) + }) + } + + fn get_optional_shipping_city(&self) -> Option { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.city) + }) + } + + fn get_optional_shipping_state(&self) -> Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.state) + }) + } + + fn get_optional_shipping_country(&self) -> Option { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.country) + }) + } + + fn get_optional_shipping_zip(&self) -> Option> { + self.address.get_shipping().and_then(|shipping_address| { + shipping_address + .clone() + .address + .and_then(|shipping_address_details| shipping_address_details.zip) + }) + } + + fn get_optional_shipping_email(&self) -> Option { + self.address + .get_shipping() + .and_then(|shipping_address| shipping_address.clone().email) + } + + fn get_optional_shipping_phone_number(&self) -> Option> { + self.address + .get_shipping() + .and_then(|shipping_address| shipping_address.clone().phone) + .and_then(|phone_details| phone_details.get_number_with_country_code().ok()) + } + fn get_description(&self) -> Result { self.description .clone()