Skip to content

Commit

Permalink
refactor: api
Browse files Browse the repository at this point in the history
  • Loading branch information
swangi-kumari committed May 23, 2024
1 parent 42e5ef1 commit 43103d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/deployments/production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ gocardless.base_url = "https://api.gocardless.com"
gpayments.base_url = "https://{{merchant_endpoint_prefix}}-test.api.as1.gpayments.net"
helcim.base_url = "https://api.helcim.com/"
iatapay.base_url = "https://iata-pay.iata.org/api/v1"
klarna.base_url = "https://api-na.playground.klarna.com/"
klarna.base_url = "https://api-na.klarna.com/"
mifinity.base_url = "https://secure.mifinity.com/"
mollie.base_url = "https://api.mollie.com/v2/"
mollie.secondary_base_url = "https://api.cc.mollie.com/v1/"
Expand Down
31 changes: 17 additions & 14 deletions crates/router/src/connector/klarna/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use masking::{ExposeInterface, Secret};
use serde::{Deserialize, Serialize};

use crate::{
connector::utils::RouterData,
core::errors,
types::{self, storage::enums},
};
Expand Down Expand Up @@ -36,12 +37,12 @@ impl<T> TryFrom<(&types::api::CurrencyUnit, enums::Currency, i64, T)> for Klarna
pub struct KlarnaPaymentsRequest {
order_lines: Vec<OrderLines>,
order_amount: i64,
purchase_country: String,
purchase_country: enums::CountryAlpha2,
purchase_currency: enums::Currency,
merchant_reference1: String,
merchant_reference1: Option<String>,
}

#[derive(Default, Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct KlarnaPaymentsResponse {
order_id: String,
fraud_status: KlarnaFraudStatus,
Expand All @@ -50,9 +51,8 @@ pub struct KlarnaPaymentsResponse {
#[derive(Debug, Serialize)]
pub struct KlarnaSessionRequest {
intent: KlarnaSessionIntent,
purchase_country: String,
purchase_country: enums::CountryAlpha2,
purchase_currency: enums::Currency,
locale: String,
order_amount: i64,
order_lines: Vec<OrderLines>,
}
Expand All @@ -70,15 +70,18 @@ impl TryFrom<&types::PaymentsSessionRouterData> for KlarnaSessionRequest {
match request.order_details.clone() {
Some(order_details) => Ok(Self {
intent: KlarnaSessionIntent::Buy,
purchase_country: "US".to_string(),
purchase_country: request.country.ok_or(
errors::ConnectorError::MissingRequiredField {
field_name: "purchase_country",
},
)?,
purchase_currency: request.currency,
order_amount: request.amount,
locale: "en-US".to_string(),
order_lines: order_details
.iter()
.map(|data| OrderLines {
name: data.product_name.clone(),
quantity: data.quantity,
quantity: i64::from(data.quantity),
unit_price: data.amount,
total_amount: i64::from(data.quantity) * (data.amount),
})
Expand Down Expand Up @@ -122,19 +125,19 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaP
let request = &item.router_data.request;
match request.order_details.clone() {
Some(order_details) => Ok(Self {
purchase_country: "US".to_string(),
purchase_country: item.router_data.get_billing_country()?,
purchase_currency: request.currency,
order_amount: request.amount,
order_lines: order_details
.iter()
.map(|data| OrderLines {
name: data.product_name.clone(),
quantity: data.quantity,
quantity: i64::from(data.quantity),
unit_price: data.amount,
total_amount: i64::from(data.quantity) * (data.amount),
})
.collect(),
merchant_reference1: item.router_data.connector_request_reference_id.clone(),
merchant_reference1: Some(item.router_data.connector_request_reference_id.clone()),
}),
None => Err(report!(errors::ConnectorError::MissingRequiredField {
field_name: "product_name"
Expand Down Expand Up @@ -167,10 +170,11 @@ impl TryFrom<types::PaymentsResponseRouterData<KlarnaPaymentsResponse>>
})
}
}

#[derive(Debug, Serialize)]
pub struct OrderLines {
name: String,
quantity: u16,
quantity: i64,
unit_price: i64,
total_amount: i64,
}
Expand Down Expand Up @@ -201,11 +205,10 @@ impl TryFrom<&types::ConnectorAuthType> for KlarnaAuthType {
}
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum KlarnaFraudStatus {
Accepted,
#[default]
Pending,
}

Expand Down

0 comments on commit 43103d2

Please sign in to comment.