Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(connector): [Klarna] Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2414)

korratee7efd05adbe
diff --git a/crates/router/src/connector/klarna.rs b/crates/router/src/connector/klarna.rs
index 1814f9de80..2611c062eb 100644
--- a/crates/router/src/connector/klarna.rs
+++ b/crates/router/src/connector/klarna.rs
@@ -32,6 +32,10 @@ impl ConnectorCommon for Klarna {
"klarna"
}
+ fn get_currency_unit(&self) -> api::CurrencyUnit {
+ api::CurrencyUnit::Minor
+ }
+
fn common_get_content_type(&self) -> &'static str {
"application/json"
}
@@ -406,7 +410,13 @@ impl
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
- let connector_req = klarna::KlarnaPaymentsRequest::try_from(req)?;
+ let connector_router_data = klarna::KlarnaRouterData::try_from((
+ &self.get_currency_unit(),
+ req.request.currency,
+ req.request.amount,
+ req,
+ ))?;
+ let connector_req = klarna::KlarnaPaymentsRequest::try_from(&connector_router_data)?;
let klarna_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<klarna::KlarnaPaymentsRequest>::encode_to_string_of_json,
diff --git a/crates/router/src/connector/klarna/transformers.rs b/crates/router/src/connector/klarna/transformers.rs
index 3607818353..b31d56d51b 100644
--- a/crates/router/src/connector/klarna/transformers.rs
+++ b/crates/router/src/connector/klarna/transformers.rs
@@ -8,6 +8,37 @@ use crate::{
types::{self, storage::enums},
};
+#[derive(Debug, Serialize)]
+pub struct KlarnaRouterData<T> {
+ amount: i64,
+ router_data: T,
+}
+
+impl<T>
+ TryFrom<(
+ &types::api::CurrencyUnit,
+ types::storage::enums::Currency,
+ i64,
+ T,
+ )> for KlarnaRouterData<T>
+{
+ type Error = error_stack::Report<errors::ConnectorError>;
+
+ fn try_from(
+ (_currency_unit, _currency, amount, router_data): (
+ &types::api::CurrencyUnit,
+ types::storage::enums::Currency,
+ i64,
+ T,
+ ),
+ ) -> Result<Self, Self::Error> {
+ Ok(Self {
+ amount,
+ router_data,
+ })
+ }
+}
+
#[derive(Default, Debug, Serialize)]
pub struct KlarnaPaymentsRequest {
order_lines: Vec<OrderLines>,
@@ -88,10 +119,13 @@ impl TryFrom<types::PaymentsSessionResponseRouterData<KlarnaSessionResponse>>
}
}
-impl TryFrom<&types::PaymentsAuthorizeRouterData> for KlarnaPaymentsRequest {
+impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaPaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
- fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
- let request = &item.request;
+
+ fn try_from(
+ item: &KlarnaRouterData<&types::PaymentsAuthorizeRouterData>,
+ ) -> Result<Self, Self::Error> {
+ let request = &item.router_data.request;
match request.order_details.clone() {
Some(order_details) => Ok(Self {
purchase_country: "US".to_string(),