refactor(connector): added amount framework to paypal, payouts and routing (#4865)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com>
Co-authored-by: Narayan Bhat <narayan.bhat@juspay.in>
This commit is contained in:
Sahkal Poddar
2024-06-28 12:01:15 +05:30
committed by GitHub
parent 82a75da314
commit b08ce22108
34 changed files with 204 additions and 181 deletions

View File

@ -19,7 +19,7 @@ pub struct PaymentMethodInput {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentInput {
pub amount: i64,
pub amount: common_utils::types::MinorUnit,
pub currency: enums::Currency,
pub authentication_type: Option<enums::AuthenticationType>,
pub card_bin: Option<String>,

View File

@ -1,5 +1,7 @@
pub mod types;
use common_utils::types::MinorUnit;
use crate::{
backend::{self, inputs, EuclidBackend},
frontend::ast,
@ -14,7 +16,7 @@ where
O: Clone,
{
fn eval_number_comparison_array(
num: i64,
num: MinorUnit,
array: &[ast::NumberComparison],
) -> Result<bool, types::InterpreterError> {
for comparison in array {

View File

@ -105,6 +105,7 @@ where
#[cfg(all(test, feature = "ast_parser"))]
mod test {
#![allow(clippy::expect_used)]
use common_utils::types::MinorUnit;
use rustc_hash::FxHashMap;
use super::*;
@ -130,7 +131,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
card_bin: None,
currency: enums::Currency::USD,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -170,7 +171,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: Some("123456".to_string()),
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -211,7 +212,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: Some("123456".to_string()),
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -252,7 +253,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: Some("123456".to_string()),
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -293,7 +294,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: Some("123456".to_string()),
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -334,7 +335,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: None,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -375,7 +376,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: None,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -416,7 +417,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
currency: enums::Currency::USD,
card_bin: None,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -459,7 +460,7 @@ mod test {
let inp = inputs::BackendInput {
metadata: Some(meta_map),
payment: inputs::PaymentInput {
amount: 32,
amount: MinorUnit::new(32),
card_bin: None,
currency: enums::Currency::USD,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -500,7 +501,7 @@ mod test {
let inp_greater = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 150,
amount: MinorUnit::new(150),
card_bin: None,
currency: enums::Currency::USD,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -522,7 +523,7 @@ mod test {
},
};
let mut inp_equal = inp_greater.clone();
inp_equal.payment.amount = 123;
inp_equal.payment.amount = MinorUnit::new(123);
let backend = VirInterpreterBackend::<DummyOutput>::with_program(program).expect("Program");
let result_greater = backend.execute(inp_greater).expect("Execution");
let result_equal = backend.execute(inp_equal).expect("Execution");
@ -550,7 +551,7 @@ mod test {
let inp_lower = inputs::BackendInput {
metadata: None,
payment: inputs::PaymentInput {
amount: 120,
amount: MinorUnit::new(120),
card_bin: None,
currency: enums::Currency::USD,
authentication_type: Some(enums::AuthenticationType::NoThreeDs),
@ -572,7 +573,7 @@ mod test {
},
};
let mut inp_equal = inp_lower.clone();
inp_equal.payment.amount = 123;
inp_equal.payment.amount = MinorUnit::new(123);
let backend = VirInterpreterBackend::<DummyOutput>::with_program(program).expect("Program");
let result_equal = backend.execute(inp_equal).expect("Execution");
let result_lower = backend.execute(inp_lower).expect("Execution");

View File

@ -3,6 +3,7 @@ pub mod lowering;
pub mod parser;
use common_enums::RoutableConnectors;
use common_utils::types::MinorUnit;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
@ -26,7 +27,7 @@ pub struct MetadataValue {
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum ValueType {
/// Represents a number literal
Number(i64),
Number(MinorUnit),
/// Represents an enum variant
EnumVariant(String),
/// Represents a Metadata variant
@ -36,7 +37,7 @@ pub enum ValueType {
/// Represents an array of numbers. This is basically used for
/// "one of the given numbers" operations
/// eg: payment.method.amount = (1, 2, 3)
NumberArray(Vec<i64>),
NumberArray(Vec<MinorUnit>),
/// Similar to NumberArray but for enum variants
/// eg: payment.method.cardtype = (debit, credit)
EnumVariantArray(Vec<String>),
@ -65,7 +66,7 @@ impl ValueType {
#[serde(rename_all = "camelCase")]
pub struct NumberComparison {
pub comparison_type: ComparisonType,
pub number: i64,
pub number: MinorUnit,
}
/// Conditional comparison type

View File

@ -1,3 +1,4 @@
use common_utils::types::MinorUnit;
use nom::{
branch, bytes::complete, character::complete as pchar, combinator, error, multi, sequence,
};
@ -115,7 +116,7 @@ pub fn percentage(input: &str) -> ParseResult<&str, u8> {
pub fn number_value(input: &str) -> ParseResult<&str, ast::ValueType> {
error::context(
"number_value",
combinator::map(num_i64, ast::ValueType::Number),
combinator::map(num_i64, |n| ast::ValueType::Number(MinorUnit::new(n))),
)(input)
}
@ -143,12 +144,15 @@ pub fn enum_variant_value(input: &str) -> ParseResult<&str, ast::ValueType> {
}
pub fn number_array_value(input: &str) -> ParseResult<&str, ast::ValueType> {
fn num_minor_unit(input: &str) -> ParseResult<&str, MinorUnit> {
combinator::map(num_i64, MinorUnit::new)(input)
}
let many_with_comma = multi::many0(sequence::preceded(
skip_ws(complete::tag(",")),
skip_ws(num_i64),
skip_ws(num_minor_unit),
));
let full_sequence = sequence::pair(skip_ws(num_i64), many_with_comma);
let full_sequence = sequence::pair(skip_ws(num_minor_unit), many_with_comma);
error::context(
"number_array_value",
@ -158,7 +162,7 @@ pub fn number_array_value(input: &str) -> ParseResult<&str, ast::ValueType> {
full_sequence,
skip_ws(complete::tag(")")),
),
|tup: (i64, Vec<i64>)| {
|tup: (MinorUnit, Vec<MinorUnit>)| {
let mut rest = tup.1;
rest.insert(0, tup.0);
ast::ValueType::NumberArray(rest)
@ -215,7 +219,7 @@ pub fn number_comparison(input: &str) -> ParseResult<&str, ast::NumberComparison
sequence::pair(operator, num_i64),
|tup: (ast::ComparisonType, i64)| ast::NumberComparison {
comparison_type: tup.0,
number: tup.1,
number: MinorUnit::new(tup.1),
},
),
)(input)

View File

@ -5,6 +5,7 @@ pub mod transformers;
use strum::IntoEnumIterator;
// use common_utils::types::MinorUnit;
use crate::{enums as euclid_enums, frontend::ast, types};
#[macro_export]
@ -26,7 +27,7 @@ macro_rules! dirval {
($key:ident = $num:literal) => {{
$crate::frontend::dir::DirValue::$key($crate::types::NumValue {
number: $num,
number: common_utils::types::MinorUnit::new($num),
refinement: None,
})
}};
@ -74,7 +75,7 @@ macro_rules! dirval {
($key:ident = $num:literal) => {{
$crate::frontend::dir::DirValue::$key($crate::types::NumValue {
number: $num,
number: common_utils::types::MinorUnit::new($num),
refinement: None,
})
}};

View File

@ -1,5 +1,6 @@
pub mod transformers;
use common_utils::types::MinorUnit;
use euclid_macros::EnumNums;
use serde::{Deserialize, Serialize};
use strum::VariantNames;
@ -191,7 +192,7 @@ pub struct MetadataValue {
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct NumValue {
pub number: i64,
pub number: MinorUnit,
pub refinement: Option<NumValueRefinement>,
}
@ -291,11 +292,11 @@ mod global_type_tests {
#[test]
fn test_num_value_fits_greater_than() {
let val1 = NumValue {
number: 10,
number: MinorUnit::new(10),
refinement: Some(NumValueRefinement::GreaterThan),
};
let val2 = NumValue {
number: 30,
number: MinorUnit::new(30),
refinement: Some(NumValueRefinement::GreaterThan),
};
@ -305,11 +306,11 @@ mod global_type_tests {
#[test]
fn test_num_value_fits_less_than() {
let val1 = NumValue {
number: 30,
number: MinorUnit::new(30),
refinement: Some(NumValueRefinement::LessThan),
};
let val2 = NumValue {
number: 10,
number: MinorUnit::new(10),
refinement: Some(NumValueRefinement::LessThan),
};