Skip to content

Commit

Permalink
refactor(connector): [Klarna] Add support for Klarna Optional Shippin…
Browse files Browse the repository at this point in the history
…g Address (#4876)
  • Loading branch information
swangi-kumari authored Jun 4, 2024
1 parent 1eaba9a commit 3509b45
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
73 changes: 39 additions & 34 deletions crates/router/src/connector/klarna/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -96,15 +98,15 @@ pub struct KlarnaSessionRequest {

#[derive(Debug, Serialize)]
pub struct KlarnaShippingAddress {
city: Option<String>,
country: Option<enums::CountryAlpha2>,
email: Option<pii::Email>,
given_name: Option<Secret<String>>,
family_name: Option<Secret<String>>,
phone: Option<Secret<String>>,
postal_code: Option<Secret<String>>,
region: Option<Secret<String>>,
street_address: Option<Secret<String>>,
city: String,
country: enums::CountryAlpha2,
email: pii::Email,
given_name: Secret<String>,
family_name: Secret<String>,
phone: Secret<String>,
postal_code: Secret<String>,
region: Secret<String>,
street_address: Secret<String>,
street_address2: Option<Secret<String>>,
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand All @@ -224,6 +206,29 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaP
}
}

fn get_address_info(
address: Option<&payments::Address>,
) -> Option<Result<KlarnaShippingAddress, error_stack::Report<errors::ConnectorError>>> {
address.and_then(|add| {
add.address.as_ref().map(
|address_details| -> Result<KlarnaShippingAddress, error_stack::Report<errors::ConnectorError>> {
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<types::PaymentsResponseRouterData<KlarnaPaymentsResponse>>
for types::PaymentsAuthorizeRouterData
{
Expand Down
24 changes: 24 additions & 0 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,25 @@ impl<Flow, Request, Response> RouterData for types::RouterData<Flow, Request, Re
}
}

pub trait AddressData {
fn get_email(&self) -> Result<Email, Error>;
fn get_phone_with_country_code(&self) -> Result<Secret<String>, Error>;
}

impl AddressData for api::Address {
fn get_email(&self) -> Result<Email, Error> {
self.email.clone().ok_or_else(missing_field_err("email"))
}

fn get_phone_with_country_code(&self) -> Result<Secret<String>, 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<Email, Error>;
fn get_payment_method_type(&self) -> Result<enums::PaymentMethodType, Error>;
Expand Down Expand Up @@ -1519,6 +1538,7 @@ pub trait AddressDetailsData {
fn get_combined_address_line(&self) -> Result<Secret<String>, Error>;
fn to_state_code(&self) -> Result<Secret<String>, Error>;
fn to_state_code_as_optional(&self) -> Result<Option<Secret<String>>, Error>;
fn get_optional_line2(&self) -> Option<Secret<String>>;
}

impl AddressDetailsData for api::AddressDetails {
Expand Down Expand Up @@ -1614,6 +1634,10 @@ impl AddressDetailsData for api::AddressDetails {
})
.transpose()
}

fn get_optional_line2(&self) -> Option<Secret<String>> {
self.line2.clone()
}
}

pub trait MandateData {
Expand Down

0 comments on commit 3509b45

Please sign in to comment.