//! The cancel kit: where to cancel a known provider, and a polite email to send when there is no page. use crate::subscriptions::Subscription; use serde::{Deserialize, Serialize}; /// `(a word in the merchant key, provider, the page where its subscription is cancelled)`. /// Kept short on purpose: only pages that are the provider's own account or cancel page. const PROVIDERS: &[(&str, &str, &str)] = &[ ("netflix", "Netflix", "https://www.netflix.com/cancelplan"), ("spotify", "Spotify", "https://www.spotify.com/account/subscription/"), ("youtube", "YouTube", "https://www.youtube.com/paid_memberships"), ("disney", "Disney+", "https://www.disneyplus.com/account"), ("apple", "Apple", "https://apps.apple.com/account/subscriptions"), ("google", "Google Play", "https://play.google.com/store/account/subscriptions"), ("microsoft", "Microsoft", "https://account.microsoft.com/services"), ("xbox", "Microsoft", "https://account.microsoft.com/services"), ("adobe", "Adobe", "https://account.adobe.com/plans"), ]; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct CancelKit { pub provider: Option, pub url: Option, pub email_subject: String, pub email_body: String, } impl CancelKit { /// A `mailto:` link with the subject and body filled in and no recipient (the user adds the provider's /// address). pub fn mailto(&self) -> String { format!("mailto:?subject={}&body={}", percent(&self.email_subject), percent(&self.email_body)) } } fn percent(s: &str) -> String { let mut out = String::with_capacity(s.len() * 3); for b in s.bytes() { if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') { out.push(b as char); } else { out.push_str(&format!("%{b:02X}")); } } out } pub fn cancel_kit(sub: &Subscription) -> CancelKit { let words: Vec<&str> = sub.merchant_key.split(' ').collect(); let known = PROVIDERS.iter().find(|(w, _, _)| words.contains(w)); let name = known.map(|(_, p, _)| p.to_string()).unwrap_or_else(|| sub.merchant.clone()); let cadence = match sub.cadence { crate::subscriptions::Cadence::Monthly => "monthly", crate::subscriptions::Cadence::Yearly => "yearly", }; let last = sub.charges.last().map(|c| c.date.to_string()).unwrap_or_default(); CancelKit { provider: known.map(|(_, p, _)| p.to_string()), url: known.map(|(_, _, u)| u.to_string()), email_subject: format!("Cancel my {name} subscription"), email_body: format!( "Hello,\n\nplease cancel my {name} subscription ({cadence}, {} CZK, last charged on {last}) \ with effect from the end of the current period, and confirm the cancellation by email.\n\n\ Please do not charge my card again.\n\nThank you.", sub.cost.to_czk() ), } }