Skip to content

Commit

Permalink
do-amount-conversion-for-klarna
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranKBR committed Jul 17, 2024
1 parent 35c9b8a commit 19d54b0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
4 changes: 4 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,10 @@ pub struct OrderDetailsWithAmount {
pub brand: Option<String>,
/// Type of the product that is being purchased
pub product_type: Option<ProductType>,

//minor amount change for OrderDetailsWithAmount
pub minor_amount: MinorUnit,

}

#[derive(Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
Expand Down
58 changes: 34 additions & 24 deletions crates/router/src/connector/klarna.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod transformers;
use std::fmt::Debug;

use api_models::enums;
use base64::Engine;
use common_utils::request::RequestContent;
use common_utils::{request::RequestContent, types::{AmountConvertor, MinorUnit, MinorUnitForConnector}};
use error_stack::{report, ResultExt};
use masking::PeekInterface;
use router_env::logger;
Expand All @@ -29,8 +28,19 @@ use crate::{
utils::BytesExt,
};

#[derive(Debug, Clone)]
pub struct Klarna;
#[derive(Clone)]
pub struct Klarna {
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
}


impl Klarna {
pub fn new() -> &'static Self {
&Self {
amount_converter: &MinorUnitForConnector,
}
}
}

impl ConnectorCommon for Klarna {
fn id(&self) -> &'static str {
Expand Down Expand Up @@ -215,12 +225,12 @@ impl
req: &types::PaymentsSessionRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = klarna::KlarnaRouterData::try_from((
&self.get_currency_unit(),
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_amount,
req.request.currency,
req.request.amount,
req,
))?;
)?;
let connector_router_data = klarna::KlarnaRouterData::from((amount, req));

let connector_req = klarna::KlarnaSessionRequest::try_from(&connector_router_data)?;
// encode only for for urlencoded things.
Expand Down Expand Up @@ -342,12 +352,12 @@ impl
req: &types::PaymentsCaptureRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = klarna::KlarnaRouterData::try_from((
&self.get_currency_unit(),
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_amount_to_capture,
req.request.currency,
req.request.amount_to_capture,
req,
))?;
)?;
let connector_router_data = klarna::KlarnaRouterData::from((amount, req));
let connector_req = klarna::KlarnaCaptureRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down Expand Up @@ -667,12 +677,12 @@ impl
req: &types::PaymentsAuthorizeRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = klarna::KlarnaRouterData::try_from((
&self.get_currency_unit(),
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_amount,
req.request.currency,
req.request.amount,
req,
))?;
)?;
let connector_router_data = klarna::KlarnaRouterData::from((amount, req));
let connector_req = klarna::KlarnaPaymentsRequest::try_from(&connector_router_data)?;

Ok(RequestContent::Json(Box::new(connector_req)))
Expand Down Expand Up @@ -844,12 +854,12 @@ impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::Ref
req: &types::RefundsRouterData<api::Execute>,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = klarna::KlarnaRouterData::try_from((
&self.get_currency_unit(),
let amount = connector_utils::convert_amount(
self.amount_converter,
req.request.minor_refund_amount,
req.request.currency,
req.request.refund_amount,
req,
))?;
)?;
let connector_router_data = klarna::KlarnaRouterData::from((amount, req));
let connector_req = klarna::KlarnaRefundRequest::try_from(&connector_router_data)?;
Ok(RequestContent::Json(Box::new(connector_req)))
}
Expand Down
42 changes: 19 additions & 23 deletions crates/router/src/connector/klarna/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use api_models::payments;
use common_utils::pii;
use common_utils::{pii, types::MinorUnit};
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::router_data::KlarnaSdkResponse;
use masking::{ExposeInterface, Secret};
Expand All @@ -15,25 +15,21 @@ use crate::{

#[derive(Debug, Serialize)]
pub struct KlarnaRouterData<T> {
amount: i64,
amount: MinorUnit,
router_data: T,
}

impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for KlarnaRouterData<T> {
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(
(_currency_unit, _currency, amount, router_data): (
&api::CurrencyUnit,
enums::Currency,
i64,
impl<T> From<(MinorUnit, T)> for KlarnaRouterData<T> {
fn from(
(amount, router_data): (
MinorUnit,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
) -> Self {
Self {
amount,
router_data,
})
}
}
}

Expand Down Expand Up @@ -74,7 +70,7 @@ impl TryFrom<&Option<pii::SecretSerdeValue>> for KlarnaConnectorMetadataObject {
pub struct KlarnaPaymentsRequest {
auto_capture: bool,
order_lines: Vec<OrderLines>,
order_amount: i64,
order_amount: MinorUnit,
purchase_country: enums::CountryAlpha2,
purchase_currency: enums::Currency,
merchant_reference1: Option<String>,
Expand Down Expand Up @@ -110,7 +106,7 @@ pub struct KlarnaSessionRequest {
intent: KlarnaSessionIntent,
purchase_country: enums::CountryAlpha2,
purchase_currency: enums::Currency,
order_amount: i64,
order_amount: MinorUnit,
order_lines: Vec<OrderLines>,
shipping_address: Option<KlarnaShippingAddress>,
}
Expand Down Expand Up @@ -156,8 +152,8 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsSessionRouterData>> for KlarnaSes
.map(|data| OrderLines {
name: data.product_name.clone(),
quantity: data.quantity,
unit_price: data.amount,
total_amount: i64::from(data.quantity) * (data.amount),
unit_price: data.minor_amount,
total_amount: MinorUnit::new(i64::from(data.quantity) * (data.minor_amount.get_amount_as_i64())),
})
.collect(),
shipping_address: get_address_info(item.router_data.get_optional_shipping())
Expand Down Expand Up @@ -209,8 +205,8 @@ impl TryFrom<&KlarnaRouterData<&types::PaymentsAuthorizeRouterData>> for KlarnaP
.map(|data| OrderLines {
name: data.product_name.clone(),
quantity: data.quantity,
unit_price: data.amount,
total_amount: i64::from(data.quantity) * (data.amount),
unit_price: data.minor_amount,
total_amount: MinorUnit::new(i64::from(data.quantity) * (data.minor_amount.get_amount_as_i64())),
})
.collect(),
merchant_reference1: Some(item.router_data.connector_request_reference_id.clone()),
Expand Down Expand Up @@ -294,8 +290,8 @@ impl TryFrom<types::PaymentsResponseRouterData<KlarnaPaymentsResponse>>
pub struct OrderLines {
name: String,
quantity: u16,
unit_price: i64,
total_amount: i64,
unit_price: MinorUnit,
total_amount: MinorUnit,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -412,7 +408,7 @@ impl<F, T>

#[derive(Debug, Serialize)]
pub struct KlarnaCaptureRequest {
captured_amount: i64,
captured_amount: MinorUnit,
reference: Option<String>,
}

Expand Down Expand Up @@ -490,7 +486,7 @@ impl<F>

#[derive(Default, Debug, Serialize)]
pub struct KlarnaRefundRequest {
refunded_amount: i64,
refunded_amount: MinorUnit,
reference: Option<String>,
}

Expand Down
5 changes: 3 additions & 2 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,20 +1185,21 @@ pub fn voucher_next_steps_check(
}

pub fn change_order_details_to_new_type(
order_amount: i64,
order_amount: MinorUnit,
order_details: api_models::payments::OrderDetails,
) -> Option<Vec<api_models::payments::OrderDetailsWithAmount>> {
Some(vec![api_models::payments::OrderDetailsWithAmount {
product_name: order_details.product_name,
quantity: order_details.quantity,
amount: order_amount,
amount: order_amount.get_amount_as_i64(),
product_img_link: order_details.product_img_link,
requires_shipping: order_details.requires_shipping,
product_id: order_details.product_id,
category: order_details.category,
sub_category: order_details.sub_category,
brand: order_details.brand,
product_type: order_details.product_type,
minor_amount:order_amount,
}])
}

Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl ConnectorData {
enums::Connector::Iatapay => {
Ok(ConnectorEnum::Old(Box::new(connector::Iatapay::new())))
}
enums::Connector::Klarna => Ok(ConnectorEnum::Old(Box::new(&connector::Klarna))),
enums::Connector::Klarna => Ok(ConnectorEnum::Old(Box::new(connector::Klarna::new()))),
enums::Connector::Mollie => Ok(ConnectorEnum::Old(Box::new(&connector::Mollie))),
enums::Connector::Nmi => Ok(ConnectorEnum::Old(Box::new(connector::Nmi::new()))),
enums::Connector::Noon => Ok(ConnectorEnum::Old(Box::new(connector::Noon::new()))),
Expand Down

0 comments on commit 19d54b0

Please sign in to comment.