Skip to content

Commit

Permalink
Added custom deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSagnik007 committed Nov 11, 2024
1 parent 16a9310 commit 2b315a1
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 180 deletions.
2 changes: 1 addition & 1 deletion crates/hyperswitch_connectors/src/connectors/airwallex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down
7 changes: 2 additions & 5 deletions crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod transformers;
use std::str::FromStr;

use api_models::webhooks::IncomingWebhookEvent;
use base64::Engine;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/braintree/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ pub struct BraintreeDisputeData {
pub amount_won: Option<String>,
pub case_number: Option<String>,
pub chargeback_protection_level: Option<String>,
pub currency_iso_code: String,
pub currency_iso_code: enums::Currency,
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
pub created_at: Option<PrimitiveDateTime>,
pub evidence: Option<DisputeEvidence>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/checkout/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ pub struct CheckoutDisputeWebhookData {
pub payment_id: Option<String>,
pub action_id: Option<String>,
pub amount: i32,
pub currency: String,
pub currency: enums::Currency,
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
pub evidence_required_by: Option<PrimitiveDateTime>,
pub reason_code: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/connector/stripe/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,8 @@ pub struct WebhookEventObjectData {
pub id: String,
pub object: WebhookEventObjectType,
pub amount: Option<i32>,
pub currency: String,
#[serde(default, deserialize_with = "connector_util::convert_uppercase")]
pub currency: enums::Currency,
pub payment_intent: Option<String>,
pub client_secret: Option<Secret<String>>,
pub reason: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/trustpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/trustpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
174 changes: 11 additions & 163 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T, D::Error>
where
D: serde::Deserializer<'de>,
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug + std::fmt::Display + std::error::Error,
{
use serde::de::Error;
let output = <&str>::deserialize(v)?;
output.to_uppercase().parse::<T>().map_err(D::Error::custom)
}
2 changes: 1 addition & 1 deletion crates/router/src/db/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/utils/user/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-- Your SQL goes here
ALTER TABLE dispute ALTER COLUMN currency TYPE "Currency" USING currency::"Currency";
ALTER TABLE dispute ALTER COLUMN currency TYPE "Currency" USING currency::"Currency"; -- Migration query to be run after deployment before running this query

0 comments on commit 2b315a1

Please sign in to comment.