Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(connector): add amount conversion framework to Riskified #6359

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/common_utils/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ pub struct StringMajorUnit(String);

impl StringMajorUnit {
/// forms a new major unit from amount
fn new(value: String) -> Self {
pub fn new(value: String) -> Self {
Self(value)
}

Expand Down
41 changes: 36 additions & 5 deletions crates/router/src/connector/riskified.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod transformers;
use std::fmt::Debug;

#[cfg(feature = "frm")]
use base64::Engine;
use common_utils::types::{
AmountConvertor, MinorUnit, StringMajorUnit, StringMajorUnitForConnector,
};
#[cfg(feature = "frm")]
use common_utils::{crypto, ext_traits::ByteSliceExt, request::RequestContent};
#[cfg(feature = "frm")]
Expand All @@ -14,6 +16,7 @@ use ring::hmac;
#[cfg(feature = "frm")]
use transformers as riskified;

use super::utils::convert_amount;
#[cfg(feature = "frm")]
use super::utils::{self as connector_utils, FrmTransactionRouterDataRequest};
use crate::{
Expand All @@ -35,10 +38,18 @@ use crate::{
utils::BytesExt,
};

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

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

#[cfg(feature = "frm")]
pub fn generate_authorization_signature(
&self,
Expand Down Expand Up @@ -173,7 +184,17 @@ impl
req: &frm_types::FrmCheckoutRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let req_obj = riskified::RiskifiedPaymentsCheckoutRequest::try_from(req)?;
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.amount),
req.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?,
)?;
Comment on lines +187 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar change needs to be done wherever amount is being used like FrmTransactionRouterData .

let req_data = riskified::RiskifiedRouterData::from((amount, req));
let req_obj = riskified::RiskifiedPaymentsCheckoutRequest::try_from(&req_data)?;
Ok(RequestContent::Json(Box::new(req_obj)))
}

Expand Down Expand Up @@ -293,7 +314,17 @@ impl
Ok(RequestContent::Json(Box::new(req_obj)))
}
_ => {
let req_obj = riskified::TransactionSuccessRequest::try_from(req)?;
let amount = convert_amount(
self.amount_converter,
MinorUnit::new(req.request.amount),
req.request
.currency
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "currency",
})?,
)?;
let req_data = riskified::RiskifiedRouterData::from((amount, req));
let req_obj = riskified::TransactionSuccessRequest::try_from(&req_data)?;
Ok(RequestContent::Json(Box::new(req_obj)))
}
}
Expand Down
55 changes: 40 additions & 15 deletions crates/router/src/connector/riskified/transformers/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use api_models::payments::AdditionalPaymentData;
use common_utils::{ext_traits::ValueExt, id_type, pii::Email};
use common_utils::{ext_traits::ValueExt, id_type, pii::Email, types::StringMajorUnit};
use error_stack::{self, ResultExt};
use masking::Secret;
use serde::{Deserialize, Serialize};
Expand All @@ -18,6 +18,21 @@ use crate::{

type Error = error_stack::Report<errors::ConnectorError>;

#[derive(Debug, Serialize)]
pub struct RiskifiedRouterData<T> {
pub amount: StringMajorUnit,
pub router_data: T,
}

impl<T> From<(StringMajorUnit, T)> for RiskifiedRouterData<T> {
fn from((amount, router_data): (StringMajorUnit, T)) -> Self {
Self {
amount,
router_data,
}
}
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
pub struct RiskifiedPaymentsCheckoutRequest {
order: CheckoutRequest,
Expand All @@ -35,8 +50,8 @@ pub struct CheckoutRequest {
updated_at: PrimitiveDateTime,
gateway: Option<String>,
browser_ip: Option<std::net::IpAddr>,
total_price: i64,
total_discounts: i64,
total_price: StringMajorUnit,
total_discounts: StringMajorUnit,
cart_token: String,
referring_site: String,
line_items: Vec<LineItem>,
Expand All @@ -60,13 +75,13 @@ pub struct PaymentDetails {

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
pub struct ShippingLines {
price: i64,
price: StringMajorUnit,
title: Option<String>,
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
pub struct DiscountCodes {
amount: i64,
amount: StringMajorUnit,
code: Option<String>,
}

Expand Down Expand Up @@ -110,7 +125,7 @@ pub struct OrderAddress {

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
pub struct LineItem {
price: i64,
price: StringMajorUnit,
quantity: i32,
title: String,
product_type: Option<api_models::payments::ProductType>,
Expand All @@ -132,9 +147,14 @@ pub struct RiskifiedMetadata {
shipping_lines: Vec<ShippingLines>,
}

impl TryFrom<&frm_types::FrmCheckoutRouterData> for RiskifiedPaymentsCheckoutRequest {
impl TryFrom<&RiskifiedRouterData<&frm_types::FrmCheckoutRouterData>>
for RiskifiedPaymentsCheckoutRequest
{
type Error = Error;
fn try_from(payment_data: &frm_types::FrmCheckoutRouterData) -> Result<Self, Self::Error> {
fn try_from(
payment: &RiskifiedRouterData<&frm_types::FrmCheckoutRouterData>,
) -> Result<Self, Self::Error> {
let payment_data = payment.router_data.clone();
let metadata: RiskifiedMetadata = payment_data
.frm_metadata
.clone()
Expand All @@ -156,14 +176,14 @@ impl TryFrom<&frm_types::FrmCheckoutRouterData> for RiskifiedPaymentsCheckoutReq
created_at: common_utils::date_time::now(),
updated_at: common_utils::date_time::now(),
gateway: payment_data.request.gateway.clone(),
total_price: payment_data.request.amount,
total_price: payment.amount.clone(),
cart_token: payment_data.attempt_id.clone(),
line_items: payment_data
.request
.get_order_details()?
.iter()
.map(|order_detail| LineItem {
price: order_detail.amount,
price: StringMajorUnit::new(order_detail.amount.to_string()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the requested changes at other places.
but here the amount' is field of OrderDetailsWithAmount struct which is i64, I am unable to grasp how to use it directly.

quantity: i32::from(order_detail.quantity),
title: order_detail.product_name.clone(),
product_type: order_detail.product_type.clone(),
Expand All @@ -176,7 +196,7 @@ impl TryFrom<&frm_types::FrmCheckoutRouterData> for RiskifiedPaymentsCheckoutReq
source: Source::DesktopWeb,
billing_address: OrderAddress::try_from(billing_address).ok(),
shipping_address: OrderAddress::try_from(shipping_address).ok(),
total_discounts: 0,
total_discounts: StringMajorUnit::new("0".to_string()),
currency: payment_data.request.currency,
referring_site: "hyperswitch.io".to_owned(),
discount_codes: Vec::new(),
Expand Down Expand Up @@ -411,7 +431,7 @@ pub struct SuccessfulTransactionData {
pub struct TransactionDecisionData {
external_status: TransactionStatus,
reason: Option<String>,
amount: i64,
amount: StringMajorUnit,
currency: storage_enums::Currency,
#[serde(with = "common_utils::custom_serde::iso8601")]
decided_at: PrimitiveDateTime,
Expand All @@ -429,16 +449,21 @@ pub enum TransactionStatus {
Approved,
}

impl TryFrom<&frm_types::FrmTransactionRouterData> for TransactionSuccessRequest {
impl TryFrom<&RiskifiedRouterData<&frm_types::FrmTransactionRouterData>>
for TransactionSuccessRequest
{
type Error = Error;
fn try_from(item: &frm_types::FrmTransactionRouterData) -> Result<Self, Self::Error> {
fn try_from(
item_data: &RiskifiedRouterData<&frm_types::FrmTransactionRouterData>,
) -> Result<Self, Self::Error> {
let item = item_data.router_data.clone();
Ok(Self {
order: SuccessfulTransactionData {
id: item.attempt_id.clone(),
decision: TransactionDecisionData {
external_status: TransactionStatus::Approved,
reason: None,
amount: item.request.amount,
amount: item_data.amount.clone(),
currency: item.request.get_currency()?,
decided_at: common_utils::date_time::now(),
payment_details: [TransactionPaymentDetails {
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/api/fraud_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FraudCheckConnectorData {
Ok(ConnectorEnum::Old(Box::new(&connector::Signifyd)))
}
enums::FrmConnectors::Riskified => {
Ok(ConnectorEnum::Old(Box::new(&connector::Riskified)))
Ok(ConnectorEnum::Old(Box::new(connector::Riskified::new())))
}
}
}
Expand Down
Loading