use std::fmt;
use serde::Deserialize;
use serde_json::Value;
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutRecoveryStatus {
pub payment_id: String,
pub current_payment_id: String,
pub profile_id: String,
pub rule_enabled: bool,
pub automatic: String,
#[serde(default)]
pub reasons: Vec<String>,
#[serde(default)]
pub next_reminder_at: Option<String>,
#[serde(default)]
pub next_scenario: Option<String>,
pub reminders_sent: u32,
pub max_reminders: u32,
pub stopped: bool,
#[serde(default)]
pub stopped_at: Option<String>,
#[serde(default)]
pub stopped_by: Option<String>,
#[serde(default)]
pub recipient: Option<String>,
pub recipient_unsubscribed: bool,
pub destination: String,
#[serde(default)]
pub manual_blocked: Option<String>,
#[serde(default)]
pub reminders: Vec<CheckoutReminder>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl fmt::Debug for CheckoutRecoveryStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CheckoutRecoveryStatus")
.field("payment_id", &self.payment_id)
.field("current_payment_id", &self.current_payment_id)
.field("profile_id", &self.profile_id)
.field("rule_enabled", &self.rule_enabled)
.field("automatic", &self.automatic)
.field("reasons", &self.reasons)
.field("next_reminder_at", &self.next_reminder_at)
.field("next_scenario", &self.next_scenario)
.field("reminders_sent", &self.reminders_sent)
.field("max_reminders", &self.max_reminders)
.field("stopped", &self.stopped)
.field("recipient", &self.recipient.as_ref().map(|_| "<redacted>"))
.field("recipient_unsubscribed", &self.recipient_unsubscribed)
.field("destination", &self.destination)
.field("manual_blocked", &self.manual_blocked)
.field("reminders", &self.reminders)
.finish_non_exhaustive()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutReminder {
pub id: String,
pub payment_id: String,
pub profile_id: String,
pub origin: String,
pub scenario: String,
#[serde(default)]
pub sequence: Option<i32>,
pub status: String,
#[serde(default)]
pub recipient: Option<String>,
#[serde(default)]
pub locale: Option<String>,
#[serde(default)]
pub subject: Option<String>,
#[serde(default)]
pub body: Option<String>,
pub test_mode: bool,
pub attempts: i32,
#[serde(default)]
pub error_code: Option<String>,
#[serde(default)]
pub error_message: Option<String>,
#[serde(default)]
pub requested_by: Option<String>,
pub created_at: String,
pub modified_at: String,
#[serde(default)]
pub sent_at: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl fmt::Debug for CheckoutReminder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CheckoutReminder")
.field("id", &self.id)
.field("payment_id", &self.payment_id)
.field("profile_id", &self.profile_id)
.field("origin", &self.origin)
.field("scenario", &self.scenario)
.field("sequence", &self.sequence)
.field("status", &self.status)
.field("recipient", &self.recipient.as_ref().map(|_| "<redacted>"))
.field("locale", &self.locale)
.field("test_mode", &self.test_mode)
.field("attempts", &self.attempts)
.field("error_code", &self.error_code)
.field("created_at", &self.created_at)
.field("sent_at", &self.sent_at)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutReminderList {
#[serde(default)]
pub data: Vec<CheckoutReminder>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutReactivationPreview {
pub payment_id: String,
pub current_payment_id: String,
pub profile_id: String,
pub status: String,
pub available: bool,
#[serde(default)]
pub blocked: Option<String>,
#[serde(default)]
pub merchant_order_reference_id: Option<String>,
#[serde(default)]
pub description: Option<String>,
pub amount: i64,
#[serde(default)]
pub currency: Option<String>,
#[serde(default)]
pub checkout_url: Option<String>,
#[serde(default)]
pub checkout_expires_at: Option<String>,
pub checkout_expired: bool,
pub default_validity_seconds: u32,
pub min_validity_seconds: u32,
pub max_validity_seconds: u32,
#[serde(default)]
pub reactivations: Vec<CheckoutReactivationRecord>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutReactivationRecord {
pub payment_id: String,
pub previous_payment_id: String,
pub reactivated_by: String,
pub reactivated_at: String,
#[serde(default)]
pub previous_expires_at: Option<String>,
#[serde(default)]
pub expires_at: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutReactivation {
pub payment_id: String,
pub previous_payment_id: String,
pub current_payment_id: String,
pub checkout_url: String,
#[serde(default)]
pub expires_at: Option<String>,
#[serde(default)]
pub previous_expires_at: Option<String>,
pub already_active: bool,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct ManualEmailPreview {
pub scenario: String,
#[serde(default)]
pub recipient: Option<String>,
pub from_name: String,
#[serde(default)]
pub from_address: Option<String>,
#[serde(default)]
pub reply_to: Option<String>,
pub language: String,
pub subject: String,
pub text_body: String,
pub html_body: String,
pub link: String,
pub destination: String,
#[serde(default)]
pub blocked: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl fmt::Debug for ManualEmailPreview {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ManualEmailPreview")
.field("scenario", &self.scenario)
.field("recipient", &self.recipient.as_ref().map(|_| "<redacted>"))
.field("from_name", &self.from_name)
.field("language", &self.language)
.field("subject", &self.subject)
.field("text_body", &"<redacted>")
.field("html_body", &"<redacted>")
.field("link", &"<redacted>")
.field("destination", &self.destination)
.field("blocked", &self.blocked)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CandidatePreviewResponse {
pub evaluated: u32,
pub eligible: u32,
pub purchase_history_complete: bool,
#[serde(default)]
pub candidates: Vec<CandidatePreview>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct CandidatePreview {
pub payment_id: String,
pub current_payment_id: String,
pub created_at: String,
pub amount: i64,
#[serde(default)]
pub currency: Option<String>,
#[serde(default)]
pub recipient: Option<String>,
pub test_mode: bool,
pub automatic: String,
#[serde(default)]
pub reasons: Vec<String>,
#[serde(default)]
pub next_reminder_at: Option<String>,
#[serde(default)]
pub next_scenario: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl fmt::Debug for CandidatePreview {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CandidatePreview")
.field("payment_id", &self.payment_id)
.field("current_payment_id", &self.current_payment_id)
.field("created_at", &self.created_at)
.field("amount", &self.amount)
.field("currency", &self.currency)
.field("recipient", &self.recipient.as_ref().map(|_| "<redacted>"))
.field("test_mode", &self.test_mode)
.field("automatic", &self.automatic)
.field("reasons", &self.reasons)
.field("next_reminder_at", &self.next_reminder_at)
.field("next_scenario", &self.next_scenario)
.finish_non_exhaustive()
}
}
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct TestEmailResponse {
pub recipient: String,
pub sent: bool,
#[serde(default)]
pub error_code: Option<String>,
#[serde(default)]
pub error_message: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl fmt::Debug for TestEmailResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestEmailResponse")
.field("recipient", &"<redacted>")
.field("sent", &self.sent)
.field("error_code", &self.error_code)
.field("error_message", &self.error_message)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recovery_status_debug_redacts_the_recipient() {
let status: CheckoutRecoveryStatus = serde_json::from_str(
r#"{"payment_id":"pay_1","current_payment_id":"pay_1","profile_id":"pro_1",
"rule_enabled":true,"automatic":"scheduled","reasons":[],"reminders_sent":0,
"max_reminders":2,"stopped":false,"recipient":"j***@buyer.example",
"recipient_unsubscribed":false,"destination":"open_checkout",
"reminders":[{"id":"rem_1","payment_id":"pay_1","profile_id":"pro_1",
"origin":"manual","scenario":"unfinished_checkout","status":"sent",
"recipient":"j***@buyer.example","test_mode":false,"attempts":1,
"created_at":"t","modified_at":"t"}]}"#,
)
.unwrap();
let printed = format!("{status:?}");
assert!(!printed.contains("buyer.example"), "{printed}");
assert!(printed.contains("<redacted>"));
assert!(printed.contains("pay_1"));
}
#[test]
fn manual_email_preview_debug_redacts_recipient_bodies_and_link() {
let preview: ManualEmailPreview = serde_json::from_str(
r#"{"scenario":"unfinished_checkout","recipient":"ada@buyer.example",
"from_name":"Shop","language":"en","subject":"Your order",
"text_body":"Hi Ada Lovelace","html_body":"<p>Hi Ada Lovelace</p>",
"link":"https://pay.example/secret-token","destination":"open_checkout"}"#,
)
.unwrap();
let printed = format!("{preview:?}");
for leaked in ["buyer.example", "Ada Lovelace", "secret-token"] {
assert!(!printed.contains(leaked), "{leaked} leaked: {printed}");
}
assert!(printed.contains("Your order"));
}
#[test]
fn candidate_and_test_email_debug_redact_the_recipient() {
let candidate: CandidatePreview = serde_json::from_str(
r#"{"payment_id":"pay_1","current_payment_id":"pay_1","created_at":"t",
"amount":100,"recipient":"ada@buyer.example","test_mode":false,
"automatic":"due","reasons":[]}"#,
)
.unwrap();
assert!(!format!("{candidate:?}").contains("buyer.example"));
let test: TestEmailResponse =
serde_json::from_str(r#"{"recipient":"me@team.example","sent":true}"#).unwrap();
assert!(!format!("{test:?}").contains("team.example"));
}
}