From 3509b45e1b855dce4561beb5cded4ba490be6f8c Mon Sep 17 00:00:00 2001 From: Swangi Kumari <85639103+swangi-kumari@users.noreply.github.com> Date: Wed, 5 Jun 2024 00:01:59 +0530 Subject: [PATCH] refactor(connector): [Klarna] Add support for Klarna Optional Shipping Address (#4876) --- .../src/connector/klarna/transformers.rs | 73 ++++++++++--------- crates/router/src/connector/utils.rs | 24 ++++++ 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/crates/router/src/connector/klarna/transformers.rs b/crates/router/src/connector/klarna/transformers.rs index 69b9dfc67bb9..a3d84a3546ab 100644 --- a/crates/router/src/connector/klarna/transformers.rs +++ b/crates/router/src/connector/klarna/transformers.rs @@ -5,7 +5,9 @@ use masking::{ExposeInterface, Secret}; use serde::{Deserialize, Serialize}; use crate::{ - connector::utils::{self, PaymentsAuthorizeRequestData, RouterData}, + connector::utils::{ + self, AddressData, AddressDetailsData, PaymentsAuthorizeRequestData, RouterData, + }, core::errors, types::{self, api, storage::enums, transformers::ForeignFrom}, }; @@ -96,15 +98,15 @@ pub struct KlarnaSessionRequest { #[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>, + city: String, + country: enums::CountryAlpha2, + email: pii::Email, + given_name: Secret, + family_name: Secret, + phone: Secret, + postal_code: Secret, + region: Secret, + street_address: Secret, street_address2: Option>, } @@ -139,18 +141,8 @@ 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(), - }), + shipping_address: get_address_info(item.router_data.get_optional_shipping()) + .transpose()?, }), None => Err(report!(errors::ConnectorError::MissingRequiredField { field_name: "order_details", @@ -204,18 +196,8 @@ 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(), - }), + shipping_address: get_address_info(item.router_data.get_optional_shipping()) + .transpose()?, }), None => Err(report!(errors::ConnectorError::MissingRequiredField { field_name: "order_details" @@ -224,6 +206,29 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaP } } +fn get_address_info( + address: Option<&payments::Address>, +) -> Option>> { + address.and_then(|add| { + add.address.as_ref().map( + |address_details| -> Result> { + Ok(KlarnaShippingAddress { + city: address_details.get_city()?.to_owned(), + country: address_details.get_country()?.to_owned(), + email: add.get_email()?.to_owned(), + postal_code: address_details.get_zip()?.to_owned(), + region: address_details.to_state_code()?.to_owned(), + street_address: address_details.get_line1()?.to_owned(), + street_address2: address_details.get_optional_line2(), + given_name: address_details.get_first_name()?.to_owned(), + family_name: address_details.get_last_name()?.to_owned(), + phone: add.get_phone_with_country_code()?.to_owned(), + }) + }, + ) + }) +} + impl TryFrom> for types::PaymentsAuthorizeRouterData { diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs index 873e6f18d23c..898a368436ea 100644 --- a/crates/router/src/connector/utils.rs +++ b/crates/router/src/connector/utils.rs @@ -541,6 +541,25 @@ impl RouterData for types::RouterData Result; + fn get_phone_with_country_code(&self) -> Result, Error>; +} + +impl AddressData for api::Address { + fn get_email(&self) -> Result { + self.email.clone().ok_or_else(missing_field_err("email")) + } + + fn get_phone_with_country_code(&self) -> Result, Error> { + self.phone + .clone() + .map(|phone_details| phone_details.get_number_with_country_code()) + .transpose()? + .ok_or_else(missing_field_err("phone")) + } +} + pub trait PaymentsPreProcessingData { fn get_email(&self) -> Result; fn get_payment_method_type(&self) -> Result; @@ -1519,6 +1538,7 @@ pub trait AddressDetailsData { fn get_combined_address_line(&self) -> Result, Error>; fn to_state_code(&self) -> Result, Error>; fn to_state_code_as_optional(&self) -> Result>, Error>; + fn get_optional_line2(&self) -> Option>; } impl AddressDetailsData for api::AddressDetails { @@ -1614,6 +1634,10 @@ impl AddressDetailsData for api::AddressDetails { }) .transpose() } + + fn get_optional_line2(&self) -> Option> { + self.line2.clone() + } } pub trait MandateData {