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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat: add support for updating surcharge_applicable field intent (#2647)
Hrithikesh949937e36443
diff --git a/crates/common_utils/src/errors.rs b/crates/common_utils/src/errors.rsindex 6541633a56..7f34306570 100644--- a/crates/common_utils/src/errors.rs+++ b/crates/common_utils/src/errors.rs@@ -77,11 +77,20 @@ pub enum QrCodeError {}/// Api Models construction error-#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]-pub enum ApiModelsError {+#[derive(Debug, Clone, thiserror::Error, PartialEq)]+pub enum PercentageError {/// Percentage Value provided was invalid#[error("Invalid Percentage value")]InvalidPercentageValue,++ /// Error occurred while calculating percentage+ #[error("Failed apply percentage of {percentage} on {amount}")]+ UnableToApplyPercentage {+ /// percentage value+ percentage: f32,+ /// amount value+ amount: i64,+ },}/// Allows [error_stack::Report] to change between error contextsdiff --git a/crates/common_utils/src/types.rs b/crates/common_utils/src/types.rsindex d745334a21..b28bffe0dc 100644--- a/crates/common_utils/src/types.rs+++ b/crates/common_utils/src/types.rs@@ -2,7 +2,7 @@use error_stack::{IntoReport, ResultExt};use serde::{de::Visitor, Deserialize, Deserializer};-use crate::errors::{ApiModelsError, CustomResult};+use crate::errors::{CustomResult, PercentageError};/// Represents Percentage Value between 0 and 100 both inclusive#[derive(Clone, Default, Debug, PartialEq, serde::Serialize)]@@ -21,16 +21,16 @@ fn get_invalid_percentage_error_message(precision: u8) -> String {impl<const PRECISION: u8> Percentage<PRECISION> {/// construct percentage using a string representation of float value- pub fn from_string(value: String) -> CustomResult<Self, ApiModelsError> {+ pub fn from_string(value: String) -> CustomResult<Self, PercentageError> {if Self::is_valid_string_value(&value)? {Ok(Self {percentage: value.parse().into_report()- .change_context(ApiModelsError::InvalidPercentageValue)?,+ .change_context(PercentageError::InvalidPercentageValue)?,})} else {- Err(ApiModelsError::InvalidPercentageValue.into())+ Err(PercentageError::InvalidPercentageValue.into()).attach_printable(get_invalid_percentage_error_message(PRECISION))}}@@ -38,15 +38,37 @@ impl<const PRECISION: u8> Percentage<PRECISION> {pub fn get_percentage(&self) -> f32 {self.percentage}- fn is_valid_string_value(value: &str) -> CustomResult<bool, ApiModelsError> {++ /// apply the percentage to amount and ceil the result+ #[allow(clippy::as_conversions)]+ pub fn apply_and_ceil_result(&self, amount: i64) -> CustomResult<i64, PercentageError> {+ let max_amount = i64::MAX / 10000;+ if amount > max_amount {+ // value gets rounded off after i64::MAX/10000+ Err(PercentageError::UnableToApplyPercentage {+ percentage: self.percentage,+ amount,+ }+ .into())+ .attach_printable(format!(+ "Cannot calculate percentage for amount greater than {}",+ max_amount+ ))+ } else {+ let percentage_f64 = f64::from(self.percentage);+ let result = (amount as f64 * (percentage_f64 / 100.0)).ceil() as i64;+ Ok(result)+ }+ }+ fn is_valid_string_value(value: &str) -> CustomResult<bool, PercentageError> {let float_value = Self::is_valid_float_string(value)?;Ok(Self::is_valid_range(float_value) && Self::is_valid_precision_length(value))}- fn is_valid_float_string(value: &str) -> CustomResult<f32, ApiModelsError> {+ fn is_valid_float_string(value: &str) -> CustomResult<f32, PercentageError> {value.parse().into_report()- .change_context(ApiModelsError::InvalidPercentageValue)+ .change_context(PercentageError::InvalidPercentageValue)}fn is_valid_range(value: f32) -> bool {(0.0..=100.0).contains(&value)diff --git a/crates/common_utils/tests/percentage.rs b/crates/common_utils/tests/percentage.rsindex 95c1125233..0858c98fd6 100644--- a/crates/common_utils/tests/percentage.rs+++ b/crates/common_utils/tests/percentage.rs@@ -1,5 +1,5 @@#![allow(clippy::panic_in_result_fn)]-use common_utils::{errors::ApiModelsError, types::Percentage};+use common_utils::{errors::PercentageError, types::Percentage};const PRECISION_2: u8 = 2;const PRECISION_0: u8 = 0;@@ -10,7 +10,7 @@ fn invalid_range_more_than_100() -> Result<(), Box<dyn std::error::Error + Sendif let Err(err) = percentage {assert_eq!(*err.current_context(),- ApiModelsError::InvalidPercentageValue+ PercentageError::InvalidPercentageValue)}Ok(())@@ -22,7 +22,7 @@ fn invalid_range_less_than_0() -> Result<(), Box<dyn std::error::Error + Send +if let Err(err) = percentage {assert_eq!(*err.current_context(),- ApiModelsError::InvalidPercentageValue+ PercentageError::InvalidPercentageValue)}Ok(())@@ -35,7 +35,7 @@ fn invalid_string() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {if let Err(err) = percentage {assert_eq!(*err.current_context(),- ApiModelsError::InvalidPercentageValue+ PercentageError::InvalidPercentageValue)}Ok(())@@ -92,7 +92,7 @@ fn invalid_precision() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {if let Err(err) = percentage {assert_eq!(*err.current_context(),- ApiModelsError::InvalidPercentageValue+ PercentageError::InvalidPercentageValue)}Ok(())diff --git a/crates/data_models/src/payments/payment_intent.rs b/crates/data_models/src/payments/payment_intent.rsindex 342818f43e..5b41c74bd6 100644--- a/crates/data_models/src/payments/payment_intent.rs+++ b/crates/data_models/src/payments/payment_intent.rs@@ -178,6 +178,10 @@ pub enum PaymentIntentUpdate {merchant_decision: Option<String>,updated_by: String,},+ SurchargeApplicableUpdate {+ surcharge_applicable: bool,+ updated_by: String,+ },}#[derive(Clone, Debug, Default)]@@ -386,6 +390,14 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {updated_by,..Default::default()},+ PaymentIntentUpdate::SurchargeApplicableUpdate {+ surcharge_applicable,+ updated_by,+ } => Self {+ surcharge_applicable: Some(surcharge_applicable),+ updated_by,+ ..Default::default()+ },}}}diff --git a/crates/diesel_models/src/payment_intent.rs b/crates/diesel_models/src/payment_intent.rsindex 31d157edb5..2ffa857026 100644--- a/crates/diesel_models/src/payment_intent.rs+++ b/crates/diesel_models/src/payment_intent.rs@@ -177,6 +177,10 @@ pub enum PaymentIntentUpdate {merchant_decision: Option<String>,updated_by: String,},+ SurchargeApplicableUpdate {+ surcharge_applicable: Option<bool>,+ updated_by: String,+ },}#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]@@ -408,6 +412,14 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {updated_by,..Default::default()},+ PaymentIntentUpdate::SurchargeApplicableUpdate {+ surcharge_applicable,+ updated_by,+ } => Self {+ surcharge_applicable,+ updated_by,+ ..Default::default()+ },}}}diff --git a/crates/storage_impl/src/payments/payment_intent.rs b/crates/storage_impl/src/payments/payment_intent.rsindex 4631df5d15..7c0939414a 100644--- a/crates/storage_impl/src/payments/payment_intent.rs+++ b/crates/storage_impl/src/payments/payment_intent.rs@@ -1009,6 +1009,13 @@ impl DataModelExt for PaymentIntentUpdate {merchant_decision,updated_by,},+ Self::SurchargeApplicableUpdate {+ surcharge_applicable,+ updated_by,+ } => DieselPaymentIntentUpdate::SurchargeApplicableUpdate {+ surcharge_applicable: Some(surcharge_applicable),+ updated_by,+ },}}