fix(connector): convert cents to dollar before sending to connector (#699)

Co-authored-by: Jagan Elavarasan <jaganelavarasan@gmail.com>
Co-authored-by: Arjun Karthik <m.arjunkarthik@gmail.com>
Co-authored-by: Arun Raj M <jarnura47@gmail.com>
This commit is contained in:
SamraatBansal
2023-03-01 23:53:10 +05:30
committed by GitHub
parent 7bd2008ae0
commit 3e88319222
6 changed files with 121 additions and 45 deletions

View File

@ -323,3 +323,35 @@ pub fn get_header_key_value<'a>(
errors::ConnectorError::WebhookSourceVerificationFailed
))?
}
pub fn to_currency_base_unit_from_optional_amount(
amount: Option<i64>,
currency: storage_models::enums::Currency,
) -> Result<String, error_stack::Report<errors::ConnectorError>> {
match amount {
Some(a) => to_currency_base_unit(a, currency),
_ => Err(errors::ConnectorError::MissingRequiredField {
field_name: "amount",
}
.into()),
}
}
pub fn to_currency_base_unit(
amount: i64,
currency: storage_models::enums::Currency,
) -> Result<String, error_stack::Report<errors::ConnectorError>> {
let amount_u32 = u32::try_from(amount)
.into_report()
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
match currency {
storage_models::enums::Currency::JPY | storage_models::enums::Currency::KRW => {
Ok(amount.to_string())
}
storage_models::enums::Currency::BHD
| storage_models::enums::Currency::JOD
| storage_models::enums::Currency::KWD
| storage_models::enums::Currency::OMR => Ok((f64::from(amount_u32) / 1000.0).to_string()),
_ => Ok((f64::from(amount_u32) / 100.0).to_string()),
}
}