fix: payment_id and client_secret in session response (#199)

This commit is contained in:
Sangamesh Kulkarni
2022-12-23 00:57:27 +05:30
committed by GitHub
parent 9e0deac3c5
commit de30489ce3
3 changed files with 62 additions and 2 deletions

View File

@ -586,9 +586,12 @@ impl From<PaymentsSessionRequest> for PaymentsResponse {
}
impl From<PaymentsSessionRequest> for PaymentsSessionResponse {
fn from(_item: PaymentsSessionRequest) -> Self {
fn from(item: PaymentsSessionRequest) -> Self {
let client_secret: Secret<String, pii::ClientSecret> = Secret::new(item.client_secret);
Self {
session_token: vec![],
payment_id: item.payment_id,
client_secret,
}
}
}
@ -790,6 +793,8 @@ pub enum SessionToken {
#[derive(Default, Debug, serde::Serialize, Clone)]
pub struct PaymentsSessionResponse {
pub payment_id: String,
pub client_secret: Secret<String, pii::ClientSecret>,
pub session_token: Vec<SessionToken>,
}

View File

@ -52,6 +52,38 @@ where
}
*/
/// Client secret
#[derive(Debug)]
pub struct ClientSecret;
impl<T> Strategy<T> for ClientSecret
where
T: AsRef<str>,
{
fn fmt(val: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val_str: &str = val.as_ref();
let client_secret_segments: Vec<&str> = val_str.split('_').collect();
if client_secret_segments.len() != 4
|| !client_secret_segments.contains(&"pay")
|| !client_secret_segments.contains(&"secret")
{
return WithType::fmt(val, f);
}
write!(
f,
"{}_{}_{}",
client_secret_segments[0],
client_secret_segments[1],
"*".repeat(
val_str.len()
- (client_secret_segments[0].len() + client_secret_segments[1].len() + 2)
)
)
}
}
/// Email address
#[derive(Debug)]
pub struct Email;
@ -106,7 +138,7 @@ where
mod pii_masking_strategy_tests {
use masking::Secret;
use super::{CardNumber, Email, IpAddress};
use super::{CardNumber, ClientSecret, Email, IpAddress};
#[test]
fn test_valid_card_number_masking() {
@ -169,4 +201,21 @@ mod pii_masking_strategy_tests {
let secret: Secret<String, IpAddress> = Secret::new("123..4.56".to_string());
assert_eq!("*** alloc::string::String ***", format!("{:?}", secret));
}
#[test]
fn test_valid_client_secret_masking() {
let secret: Secret<String, ClientSecret> =
Secret::new("pay_uszFB2QGe9MmLY65ojhT_secret_tLjTz9tAQxUVEFqfmOIP".to_string());
assert_eq!(
"pay_uszFB2QGe9MmLY65ojhT_***************************",
format!("{:?}", secret)
);
}
#[test]
fn test_invalid_lient_secret_masking() {
let secret: Secret<String, IpAddress> =
Secret::new("pay_uszFB2QGe9MmLY65ojhT_secret".to_string());
assert_eq!("*** alloc::string::String ***", format!("{:?}", secret));
}
}

View File

@ -165,6 +165,12 @@ where
) -> RouterResponse<Self> {
Ok(services::BachResponse::Json(Self {
session_token: payment_data.sessions_token,
payment_id: payment_data.payment_attempt.payment_id,
client_secret: payment_data
.payment_intent
.client_secret
.get_required_value("client_secret")?
.into(),
}))
}
}