diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs index 6bccfd1124c3..1be9762c76cf 100644 --- a/crates/api_models/src/payments.rs +++ b/crates/api_models/src/payments.rs @@ -1,4 +1,4 @@ -use std::num::NonZeroI64; +use std::{fmt, num::NonZeroI64}; use cards::CardNumber; use common_utils::{ @@ -8,6 +8,10 @@ use common_utils::{ }; use masking::Secret; use router_derive::Setter; +use serde::{ + de::{self, Unexpected, Visitor}, + Deserialize, Deserializer, Serialize, Serializer, +}; use time::PrimitiveDateTime; use url::Url; use utoipa::ToSchema; @@ -51,6 +55,120 @@ pub struct BankCodeResponse { pub eligible_connectors: Vec, } +#[derive(Debug, PartialEq)] +pub struct ClientSecret { + pub payment_id: String, + pub secret: String, +} + +impl<'de> Deserialize<'de> for ClientSecret { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct ClientSecretVisitor; + + impl<'de> Visitor<'de> for ClientSecretVisitor { + type Value = ClientSecret; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a string in the format '{payment_id}_secret_{secret}'") + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + let (payment_id, secret) = value.rsplit_once("_secret_").ok_or_else(|| { + E::invalid_value(Unexpected::Str(value), &"a string with '_secret_'") + })?; + + Ok(ClientSecret { + payment_id: payment_id.to_owned(), + secret: secret.to_owned(), + }) + } + } + + deserializer.deserialize_str(ClientSecretVisitor) + } +} + +impl Serialize for ClientSecret { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let combined = format!("{}_secret_{}", self.payment_id, self.secret); + serializer.serialize_str(&combined) + } +} + +#[cfg(test)] +mod client_secret_tests { + #![allow(clippy::expect_used)] + + use serde_json; + + use super::*; + + #[test] + fn test_serialize_client_secret() { + let client_secret1 = ClientSecret { + payment_id: "pay_3TgelAms4RQec8xSStjF".to_string(), + secret: "fc34taHLw1ekPgNh92qr".to_string(), + }; + let client_secret2 = ClientSecret { + payment_id: "pay_3Tgel__Ams4RQ_secret_ec8xSStjF".to_string(), + secret: "fc34taHLw1ekPgNh92qr".to_string(), + }; + + let expected_str1 = r#""pay_3TgelAms4RQec8xSStjF_secret_fc34taHLw1ekPgNh92qr""#; + let expected_str2 = r#""pay_3Tgel__Ams4RQ_secret_ec8xSStjF_secret_fc34taHLw1ekPgNh92qr""#; + + let actual_str1 = + serde_json::to_string(&client_secret1).expect("Failed to serialize client_secret1"); + let actual_str2 = + serde_json::to_string(&client_secret2).expect("Failed to serialize client_secret2"); + + assert_eq!(expected_str1, actual_str1); + assert_eq!(expected_str2, actual_str2); + } + + #[test] + fn test_deserialize_client_secret() { + let client_secret_str1 = r#""pay_3TgelAms4RQec8xSStjF_secret_fc34taHLw1ekPgNh92qr""#; + let client_secret_str2 = + r#""pay_3Tgel__Ams4RQ_secret_ec8xSStjF_secret_fc34taHLw1ekPgNh92qr""#; + let client_secret_str3 = + r#""pay_3Tgel__Ams4RQ_secret_ec8xSStjF_secret__secret_fc34taHLw1ekPgNh92qr""#; + + let expected1 = ClientSecret { + payment_id: "pay_3TgelAms4RQec8xSStjF".to_string(), + secret: "fc34taHLw1ekPgNh92qr".to_string(), + }; + let expected2 = ClientSecret { + payment_id: "pay_3Tgel__Ams4RQ_secret_ec8xSStjF".to_string(), + secret: "fc34taHLw1ekPgNh92qr".to_string(), + }; + let expected3 = ClientSecret { + payment_id: "pay_3Tgel__Ams4RQ_secret_ec8xSStjF_secret_".to_string(), + secret: "fc34taHLw1ekPgNh92qr".to_string(), + }; + + let actual1: ClientSecret = serde_json::from_str(client_secret_str1) + .expect("Failed to deserialize client_secret_str1"); + let actual2: ClientSecret = serde_json::from_str(client_secret_str2) + .expect("Failed to deserialize client_secret_str2"); + let actual3: ClientSecret = serde_json::from_str(client_secret_str3) + .expect("Failed to deserialize client_secret_str3"); + + assert_eq!(expected1, actual1); + assert_eq!(expected2, actual2); + assert_eq!(expected3, actual3); + } +} + #[derive(Default, Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)] pub struct CustomerDetails { /// The identifier for the customer.