diff --git a/crates/hyperswitch_connectors/src/connectors/airwallex.rs b/crates/hyperswitch_connectors/src/connectors/airwallex.rs index fe7949d88b67..26bfcd1e07e3 100644 --- a/crates/hyperswitch_connectors/src/connectors/airwallex.rs +++ b/crates/hyperswitch_connectors/src/connectors/airwallex.rs @@ -1074,7 +1074,7 @@ impl IncomingWebhook for Airwallex { .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?; Ok(DisputePayload { amount: dispute_details.dispute_amount.to_string(), - currency: connector_utils::string_to_currency(dispute_details.dispute_currency), + currency: dispute_details.dispute_currency, dispute_stage: api_models::enums::DisputeStage::from(dispute_details.stage.clone()), connector_dispute_id: dispute_details.dispute_id, connector_reason: dispute_details.dispute_reason_type, diff --git a/crates/hyperswitch_connectors/src/connectors/airwallex/transformers.rs b/crates/hyperswitch_connectors/src/connectors/airwallex/transformers.rs index 5345c550cd18..3374db7429cc 100644 --- a/crates/hyperswitch_connectors/src/connectors/airwallex/transformers.rs +++ b/crates/hyperswitch_connectors/src/connectors/airwallex/transformers.rs @@ -828,7 +828,7 @@ pub struct AirwallexObjectData { pub struct AirwallexDisputeObject { pub payment_intent_id: String, pub dispute_amount: i64, - pub dispute_currency: String, + pub dispute_currency: enums::Currency, pub stage: AirwallexDisputeStage, pub dispute_id: String, pub dispute_reason_type: Option, diff --git a/crates/router/src/connector/braintree.rs b/crates/router/src/connector/braintree.rs index f8034755e627..2451a7318854 100644 --- a/crates/router/src/connector/braintree.rs +++ b/crates/router/src/connector/braintree.rs @@ -1,5 +1,4 @@ pub mod transformers; -use std::str::FromStr; use api_models::webhooks::IncomingWebhookEvent; use base64::Engine; @@ -999,14 +998,12 @@ impl api::IncomingWebhook for Braintree { match response.dispute { Some(dispute_data) => { - let currency = enums::Currency::from_str(dispute_data.currency_iso_code.as_str()) - .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?; Ok(api::disputes::DisputePayload { amount: connector_utils::to_currency_lower_unit( dispute_data.amount_disputed.to_string(), - currency, + dispute_data.currency_iso_code, )?, - currency: connector_utils::string_to_currency(dispute_data.currency_iso_code), + currency: dispute_data.currency_iso_code, dispute_stage: transformers::get_dispute_stage(dispute_data.kind.as_str())?, connector_dispute_id: dispute_data.id, connector_reason: dispute_data.reason, diff --git a/crates/router/src/connector/braintree/transformers.rs b/crates/router/src/connector/braintree/transformers.rs index f606e671a8d9..052d37823327 100644 --- a/crates/router/src/connector/braintree/transformers.rs +++ b/crates/router/src/connector/braintree/transformers.rs @@ -1762,7 +1762,7 @@ pub struct BraintreeDisputeData { pub amount_won: Option, pub case_number: Option, pub chargeback_protection_level: Option, - pub currency_iso_code: String, + pub currency_iso_code: enums::Currency, #[serde(default, with = "common_utils::custom_serde::iso8601::option")] pub created_at: Option, pub evidence: Option, diff --git a/crates/router/src/connector/checkout.rs b/crates/router/src/connector/checkout.rs index b96120acc9e7..3fca0b86aeba 100644 --- a/crates/router/src/connector/checkout.rs +++ b/crates/router/src/connector/checkout.rs @@ -1332,7 +1332,7 @@ impl api::IncomingWebhook for Checkout { .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?; Ok(api::disputes::DisputePayload { amount: dispute_details.data.amount.to_string(), - currency: connector_utils::string_to_currency(dispute_details.data.currency), + currency: dispute_details.data.currency, dispute_stage: api_models::enums::DisputeStage::from( dispute_details.transaction_type.clone(), ), diff --git a/crates/router/src/connector/checkout/transformers.rs b/crates/router/src/connector/checkout/transformers.rs index 686a9d57ac3b..126046f72b0c 100644 --- a/crates/router/src/connector/checkout/transformers.rs +++ b/crates/router/src/connector/checkout/transformers.rs @@ -1250,7 +1250,7 @@ pub struct CheckoutDisputeWebhookData { pub payment_id: Option, pub action_id: Option, pub amount: i32, - pub currency: String, + pub currency: enums::Currency, #[serde(default, with = "common_utils::custom_serde::iso8601::option")] pub evidence_required_by: Option, pub reason_code: Option, diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index 3c908a962e5b..1125c0af6dc8 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -2432,7 +2432,7 @@ impl api::IncomingWebhook for Stripe { field_name: "amount", })? .to_string(), - currency: connector_utils::string_to_currency(details.event_data.event_object.currency), + currency: details.event_data.event_object.currency, dispute_stage: api_models::enums::DisputeStage::Dispute, connector_dispute_id: details.event_data.event_object.id, connector_reason: details.event_data.event_object.reason, diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index af82bda3e6af..59c127429469 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -3636,7 +3636,8 @@ pub struct WebhookEventObjectData { pub id: String, pub object: WebhookEventObjectType, pub amount: Option, - pub currency: String, + #[serde(default, deserialize_with = "connector_util::convert_uppercase")] + pub currency: enums::Currency, pub payment_intent: Option, pub client_secret: Option>, pub reason: Option, diff --git a/crates/router/src/connector/trustpay.rs b/crates/router/src/connector/trustpay.rs index 284438a786ec..6562ba319760 100644 --- a/crates/router/src/connector/trustpay.rs +++ b/crates/router/src/connector/trustpay.rs @@ -990,7 +990,7 @@ impl api::IncomingWebhook for Trustpay { .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?; Ok(api::disputes::DisputePayload { amount: payment_info.amount.amount.to_string(), - currency: connector_utils::string_to_currency(payment_info.amount.currency), + currency: payment_info.amount.currency, dispute_stage: api_models::enums::DisputeStage::Dispute, connector_dispute_id, connector_reason: reason.reason.reject_reason, diff --git a/crates/router/src/connector/trustpay/transformers.rs b/crates/router/src/connector/trustpay/transformers.rs index 3b109a7f02e3..544ec00a4e32 100644 --- a/crates/router/src/connector/trustpay/transformers.rs +++ b/crates/router/src/connector/trustpay/transformers.rs @@ -1789,7 +1789,7 @@ pub struct WebhookReferences { #[serde(rename_all = "PascalCase")] pub struct WebhookAmount { pub amount: f64, - pub currency: String, + pub currency: enums::Currency, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs index a1d6477b3816..b56d3898968a 100644 --- a/crates/router/src/connector/utils.rs +++ b/crates/router/src/connector/utils.rs @@ -28,7 +28,7 @@ use hyperswitch_domain_models::{ SyncIntegrityObject, }, }; -use masking::{ExposeInterface, Secret}; +use masking::{ExposeInterface, Secret, Deserialize}; use once_cell::sync::Lazy; use regex::Regex; use serde::Serializer; @@ -3140,165 +3140,13 @@ impl NetworkTokenData for domain::NetworkTokenData { } } -pub fn string_to_currency(currency: String) -> enums::Currency { - match currency.to_lowercase().as_str() { - "aed" => enums::Currency::AED, - "afn" => enums::Currency::AFN, - "all" => enums::Currency::ALL, - "amd" => enums::Currency::AMD, - "ang" => enums::Currency::ANG, - "aoa" => enums::Currency::AOA, - "ars" => enums::Currency::ARS, - "aud" => enums::Currency::AUD, - "awg" => enums::Currency::AWG, - "azn" => enums::Currency::AZN, - "bam" => enums::Currency::BAM, - "bbd" => enums::Currency::BBD, - "bdt" => enums::Currency::BDT, - "bgn" => enums::Currency::BGN, - "bhd" => enums::Currency::BHD, - "bif" => enums::Currency::BIF, - "bmd" => enums::Currency::BMD, - "bnd" => enums::Currency::BND, - "bob" => enums::Currency::BOB, - "brl" => enums::Currency::BRL, - "bsd" => enums::Currency::BSD, - "btn" => enums::Currency::BTN, - "bwp" => enums::Currency::BWP, - "byn" => enums::Currency::BYN, - "bzd" => enums::Currency::BZD, - "cad" => enums::Currency::CAD, - "cdf" => enums::Currency::CDF, - "chf" => enums::Currency::CHF, - "clp" => enums::Currency::CLP, - "cny" => enums::Currency::CNY, - "cop" => enums::Currency::COP, - "crc" => enums::Currency::CRC, - "cup" => enums::Currency::CUP, - "cve" => enums::Currency::CVE, - "czk" => enums::Currency::CZK, - "djf" => enums::Currency::DJF, - "dkk" => enums::Currency::DKK, - "dop" => enums::Currency::DOP, - "dzd" => enums::Currency::DZD, - "egp" => enums::Currency::EGP, - "ern" => enums::Currency::ERN, - "etb" => enums::Currency::ETB, - "eur" => enums::Currency::EUR, - "fjd" => enums::Currency::FJD, - "fkp" => enums::Currency::FKP, - "gbp" => enums::Currency::GBP, - "gel" => enums::Currency::GEL, - "ghs" => enums::Currency::GHS, - "gip" => enums::Currency::GIP, - "gmd" => enums::Currency::GMD, - "gnf" => enums::Currency::GNF, - "gtq" => enums::Currency::GTQ, - "gyd" => enums::Currency::GYD, - "hkd" => enums::Currency::HKD, - "hnl" => enums::Currency::HNL, - "hrk" => enums::Currency::HRK, - "htg" => enums::Currency::HTG, - "huf" => enums::Currency::HUF, - "idr" => enums::Currency::IDR, - "ils" => enums::Currency::ILS, - "inr" => enums::Currency::INR, - "iqd" => enums::Currency::IQD, - "irr" => enums::Currency::IRR, - "isk" => enums::Currency::ISK, - "jmd" => enums::Currency::JMD, - "jod" => enums::Currency::JOD, - "jpy" => enums::Currency::JPY, - "kes" => enums::Currency::KES, - "kgs" => enums::Currency::KGS, - "khr" => enums::Currency::KHR, - "kmf" => enums::Currency::KMF, - "kpw" => enums::Currency::KPW, - "krw" => enums::Currency::KRW, - "kwd" => enums::Currency::KWD, - "kyd" => enums::Currency::KYD, - "kzt" => enums::Currency::KZT, - "lak" => enums::Currency::LAK, - "lbp" => enums::Currency::LBP, - "lkr" => enums::Currency::LKR, - "lrd" => enums::Currency::LRD, - "lsl" => enums::Currency::LSL, - "lyd" => enums::Currency::LYD, - "mad" => enums::Currency::MAD, - "mdl" => enums::Currency::MDL, - "mga" => enums::Currency::MGA, - "mkd" => enums::Currency::MKD, - "mmk" => enums::Currency::MMK, - "mnt" => enums::Currency::MNT, - "mop" => enums::Currency::MOP, - "mru" => enums::Currency::MRU, - "mur" => enums::Currency::MUR, - "mvr" => enums::Currency::MVR, - "mwk" => enums::Currency::MWK, - "mxn" => enums::Currency::MXN, - "myr" => enums::Currency::MYR, - "mzn" => enums::Currency::MZN, - "nad" => enums::Currency::NAD, - "ngn" => enums::Currency::NGN, - "nio" => enums::Currency::NIO, - "nok" => enums::Currency::NOK, - "npr" => enums::Currency::NPR, - "nzd" => enums::Currency::NZD, - "omr" => enums::Currency::OMR, - "pab" => enums::Currency::PAB, - "pen" => enums::Currency::PEN, - "pgk" => enums::Currency::PGK, - "php" => enums::Currency::PHP, - "pkr" => enums::Currency::PKR, - "pln" => enums::Currency::PLN, - "pyg" => enums::Currency::PYG, - "qar" => enums::Currency::QAR, - "ron" => enums::Currency::RON, - "rsd" => enums::Currency::RSD, - "rub" => enums::Currency::RUB, - "rwf" => enums::Currency::RWF, - "sar" => enums::Currency::SAR, - "sbd" => enums::Currency::SBD, - "scr" => enums::Currency::SCR, - "sdg" => enums::Currency::SDG, - "sek" => enums::Currency::SEK, - "sgd" => enums::Currency::SGD, - "shp" => enums::Currency::SHP, - "sle" => enums::Currency::SLE, - "sll" => enums::Currency::SLL, - "sos" => enums::Currency::SOS, - "srd" => enums::Currency::SRD, - "ssp" => enums::Currency::SSP, - "stn" => enums::Currency::STN, - "svc" => enums::Currency::SVC, - "syp" => enums::Currency::SYP, - "szl" => enums::Currency::SZL, - "thb" => enums::Currency::THB, - "tjs" => enums::Currency::TJS, - "tmt" => enums::Currency::TMT, - "tnd" => enums::Currency::TND, - "top" => enums::Currency::TOP, - "try" => enums::Currency::TRY, - "ttd" => enums::Currency::TTD, - "twd" => enums::Currency::TWD, - "tzs" => enums::Currency::TZS, - "uah" => enums::Currency::UAH, - "ugx" => enums::Currency::UGX, - "usd" => enums::Currency::USD, - "uyu" => enums::Currency::UYU, - "uzs" => enums::Currency::UZS, - "ves" => enums::Currency::VES, - "vnd" => enums::Currency::VND, - "vuv" => enums::Currency::VUV, - "wst" => enums::Currency::WST, - "xaf" => enums::Currency::XAF, - "xcd" => enums::Currency::XCD, - "xof" => enums::Currency::XOF, - "xpf" => enums::Currency::XPF, - "yer" => enums::Currency::YER, - "zar" => enums::Currency::ZAR, - "zmw" => enums::Currency::ZMW, - "zwl" => enums::Currency::ZWL, - _ => enums::Currency::USD, - } -} +pub fn convert_uppercase<'de, D, T>(v: D) -> Result +where + D: serde::Deserializer<'de>, + T: FromStr, + ::Err: std::fmt::Debug + std::fmt::Display + std::error::Error, +{ + use serde::de::Error; + let output = <&str>::deserialize(v)?; + output.to_uppercase().parse::().map_err(D::Error::custom) +} \ No newline at end of file diff --git a/crates/router/src/db/dispute.rs b/crates/router/src/db/dispute.rs index 077629d2ce6c..0526854691ae 100644 --- a/crates/router/src/db/dispute.rs +++ b/crates/router/src/db/dispute.rs @@ -483,7 +483,7 @@ mod tests { DisputeNew { dispute_id: dispute_ids.dispute_id, amount: "amount".into(), - currency: common_enums::Currency::USD, + currency: common_enums::Currency::default(), dispute_stage: DisputeStage::Dispute, dispute_status: DisputeStatus::DisputeOpened, payment_id: dispute_ids.payment_id, diff --git a/crates/router/src/utils/user/sample_data.rs b/crates/router/src/utils/user/sample_data.rs index 44258e75ddae..687a3caa0197 100644 --- a/crates/router/src/utils/user/sample_data.rs +++ b/crates/router/src/utils/user/sample_data.rs @@ -412,7 +412,7 @@ pub async fn generate_sample_data( amount: (amount * 100).to_string(), currency: payment_intent .currency - .unwrap_or(common_enums::Currency::USD), + .unwrap_or(common_enums::Currency::default()), dispute_stage: storage_enums::DisputeStage::Dispute, dispute_status: storage_enums::DisputeStatus::DisputeOpened, payment_id: payment_id.clone(), diff --git a/migrations/2024-10-28-125949_change_currency_type_in_dispute/up.sql b/migrations/2024-10-28-125949_change_currency_type_in_dispute/up.sql index 47b5fa05e8db..247541fde706 100644 --- a/migrations/2024-10-28-125949_change_currency_type_in_dispute/up.sql +++ b/migrations/2024-10-28-125949_change_currency_type_in_dispute/up.sql @@ -1,2 +1,2 @@ -- Your SQL goes here -ALTER TABLE dispute ALTER COLUMN currency TYPE "Currency" USING currency::"Currency"; \ No newline at end of file +ALTER TABLE dispute ALTER COLUMN currency TYPE "Currency" USING currency::"Currency"; -- Migration query to be run after deployment before running this query \ No newline at end of file