refactor(payment_intent_v2): payment intent fields refactoring (#5880)

Co-authored-by: hrithikesh026 <hrithikesh.vm@juspay.in>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Narayan Bhat
2024-09-20 16:50:53 +05:30
committed by GitHub
parent 00e913c75c
commit 5335f2d21c
48 changed files with 2478 additions and 1620 deletions

View File

@ -18,11 +18,7 @@ use error_stack::ResultExt;
use masking::{ExposeInterface, PeekInterface, Secret, SwitchStrategy, WithType};
use router_derive::Setter;
use rustc_hash::FxHashMap;
use serde::{
de::{self, Unexpected, Visitor},
ser::Serializer,
Deserialize, Deserializer, Serialize,
};
use serde::{de, ser::Serializer, Deserialize, Deserializer, Serialize};
use strum::Display;
use time::{Date, PrimitiveDateTime};
use url::Url;
@ -68,154 +64,6 @@ pub struct BankCodeResponse {
pub eligible_connectors: Vec<String>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct ClientSecret {
pub payment_id: id_type::PaymentId,
pub secret: String,
}
impl ClientSecret {
pub fn get_client_secret(&self) -> String {
format!(
"{}_secret_{}",
self.payment_id.get_string_repr(),
self.secret
)
}
}
impl<'de> Deserialize<'de> for ClientSecret {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<E>(self, value: &str) -> Result<ClientSecret, E>
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_'")
})?;
let payment_id =
id_type::PaymentId::try_from(std::borrow::Cow::Owned(payment_id.to_owned()))
.map_err(de::Error::custom)?;
Ok(ClientSecret {
payment_id,
secret: secret.to_owned(),
})
}
}
deserializer.deserialize_str(ClientSecretVisitor)
}
}
impl Serialize for ClientSecret {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let combined = format!(
"{}_secret_{}",
self.payment_id.get_string_repr(),
self.secret
);
serializer.serialize_str(&combined)
}
}
#[cfg(test)]
mod client_secret_tests {
#![allow(clippy::expect_used)]
#![allow(clippy::unwrap_used)]
use serde_json;
use super::*;
#[test]
fn test_serialize_client_secret() {
let client_secret1 = ClientSecret {
payment_id: id_type::PaymentId::try_from(std::borrow::Cow::Borrowed(
"pay_3TgelAms4RQec8xSStjF",
))
.unwrap(),
secret: "fc34taHLw1ekPgNh92qr".to_string(),
};
let client_secret2 = ClientSecret {
payment_id: id_type::PaymentId::try_from(std::borrow::Cow::Borrowed(
"pay_3Tgel__Ams4RQ_secret_ec8xSStjF",
))
.unwrap(),
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: id_type::PaymentId::try_from(std::borrow::Cow::Borrowed(
"pay_3TgelAms4RQec8xSStjF",
))
.unwrap(),
secret: "fc34taHLw1ekPgNh92qr".to_string(),
};
let expected2 = ClientSecret {
payment_id: id_type::PaymentId::try_from(std::borrow::Cow::Borrowed(
"pay_3Tgel__Ams4RQ_secret_ec8xSStjF",
))
.unwrap(),
secret: "fc34taHLw1ekPgNh92qr".to_string(),
};
let expected3 = ClientSecret {
payment_id: id_type::PaymentId::try_from(std::borrow::Cow::Borrowed(
"pay_3Tgel__Ams4RQ_secret_ec8xSStjF_secret_",
))
.unwrap(),
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);
}
}
/// Passing this object creates a new customer or attaches an existing customer to the payment
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema, PartialEq)]
pub struct CustomerDetails {