Skip to content

Commit

Permalink
refactor(connector): [Klarna] Add shipping Address in Klarna Session …
Browse files Browse the repository at this point in the history
…and Payment Request (#4836)
  • Loading branch information
swangi-kumari authored Jun 3, 2024
1 parent 69b2f76 commit 8650077
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/router/src/connector/klarna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ impl
req,
))?;
let connector_req = klarna::KlarnaPaymentsRequest::try_from(&connector_router_data)?;

Ok(RequestContent::Json(Box::new(connector_req)))
}

Expand Down
40 changes: 40 additions & 0 deletions crates/router/src/connector/klarna/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct KlarnaPaymentsRequest {
purchase_country: enums::CountryAlpha2,
purchase_currency: enums::Currency,
merchant_reference1: Option<String>,
shipping_address: Option<KlarnaShippingAddress>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -90,6 +91,21 @@ pub struct KlarnaSessionRequest {
purchase_currency: enums::Currency,
order_amount: i64,
order_lines: Vec<OrderLines>,
shipping_address: Option<KlarnaShippingAddress>,
}

#[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>>,
street_address2: Option<Secret<String>>,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
95 changes: 95 additions & 0 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Secret<String>>;
fn get_optional_shipping_line2(&self) -> Option<Secret<String>>;
fn get_optional_shipping_city(&self) -> Option<String>;
fn get_optional_shipping_country(&self) -> Option<enums::CountryAlpha2>;
fn get_optional_shipping_zip(&self) -> Option<Secret<String>>;
fn get_optional_shipping_state(&self) -> Option<Secret<String>>;
fn get_optional_shipping_first_name(&self) -> Option<Secret<String>>;
fn get_optional_shipping_last_name(&self) -> Option<Secret<String>>;
fn get_optional_shipping_phone_number(&self) -> Option<Secret<String>>;
fn get_optional_shipping_email(&self) -> Option<Email>;

fn get_optional_billing_full_name(&self) -> Option<Secret<String>>;
fn get_optional_billing_line1(&self) -> Option<Secret<String>>;
Expand Down Expand Up @@ -199,6 +209,91 @@ impl<Flow, Request, Response> RouterData for types::RouterData<Flow, Request, Re
self.address.get_shipping()
}

fn get_optional_shipping_first_name(&self) -> Option<Secret<String>> {
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<Secret<String>> {
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<Secret<String>> {
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<Secret<String>> {
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<String> {
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<Secret<String>> {
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<enums::CountryAlpha2> {
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<Secret<String>> {
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<Email> {
self.address
.get_shipping()
.and_then(|shipping_address| shipping_address.clone().email)
}

fn get_optional_shipping_phone_number(&self) -> Option<Secret<String>> {
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<String, Error> {
self.description
.clone()
Expand Down

0 comments on commit 8650077

Please sign in to comment.