fix(connector): [Paypal] fix amount to its currency base unit (#1780)

This commit is contained in:
Prasunna Soppa
2023-07-25 11:28:43 +05:30
committed by GitHub
parent a229c37a7c
commit f40d144178
4 changed files with 266 additions and 12 deletions

View File

@ -138,6 +138,21 @@ pub fn to_currency_base_unit(
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,
@ -149,13 +164,12 @@ pub fn to_currency_base_unit_asf64(
},
)?;
let amount_f64 = f64::from(amount_u32);
let amount = match currency {
diesel_models::enums::Currency::JPY | diesel_models::enums::Currency::KRW => amount_f64,
diesel_models::enums::Currency::BHD
| diesel_models::enums::Currency::JOD
| diesel_models::enums::Currency::KWD
| diesel_models::enums::Currency::OMR => amount_f64 / 1000.00,
_ => amount_f64 / 100.00,
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)
}