refactor(router): include currency conversion utility functions as Currency methods (#1790)

This commit is contained in:
udev
2023-08-01 12:33:29 +05:30
committed by GitHub
parent d06adc705c
commit 2c9c8f081d
5 changed files with 73 additions and 74 deletions

View File

@ -129,51 +129,6 @@ impl<E> ConnectorResponseExt
}
}
/// Convert the amount to its base denomination based on Currency and return String
pub fn to_currency_base_unit(
amount: i64,
currency: diesel_models::enums::Currency,
) -> Result<String, error_stack::Report<errors::ValidationError>> {
let amount_f64 = to_currency_base_unit_asf64(amount, currency)?;
Ok(format!("{amount_f64:.2}"))
}
/// Convert the amount to its base denomination based on Currency and check for zero decimal currency and return String
/// Paypal Connector accepts Zero and Two decimal currency but not three decimal and it should be updated as required for 3 decimal currencies.
/// Paypal Ref - https://developer.paypal.com/docs/reports/reference/paypal-supported-currencies/
pub fn to_currency_base_unit_with_zero_decimal_check(
amount: i64,
currency: diesel_models::enums::Currency,
) -> Result<String, error_stack::Report<errors::ValidationError>> {
let amount_f64 = to_currency_base_unit_asf64(amount, currency)?;
if currency.is_zero_decimal_currency() {
Ok(amount_f64.to_string())
} else {
Ok(format!("{amount_f64:.2}"))
}
}
/// Convert the amount to its base denomination based on Currency and return f64
pub fn to_currency_base_unit_asf64(
amount: i64,
currency: diesel_models::enums::Currency,
) -> Result<f64, error_stack::Report<errors::ValidationError>> {
let amount_u32 = u32::try_from(amount).into_report().change_context(
errors::ValidationError::InvalidValue {
message: amount.to_string(),
},
)?;
let amount_f64 = f64::from(amount_u32);
let amount = if currency.is_zero_decimal_currency() {
amount_f64
} else if currency.is_three_decimal_currency() {
amount_f64 / 1000.00
} else {
amount_f64 / 100.00
};
Ok(amount)
}
#[inline]
pub fn get_payment_attempt_id(payment_id: impl std::fmt::Display, attempt_count: i16) -> String {
format!("{payment_id}_{attempt_count}")