99 lines
3.9 KiB
Rust
99 lines
3.9 KiB
Rust
//! 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 fiscus_ledger::invoice::Language;
|
|
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<String>,
|
|
pub url: Option<String>,
|
|
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
|
|
}
|
|
|
|
/// The kit with its email in English.
|
|
pub fn cancel_kit(sub: &Subscription) -> CancelKit {
|
|
cancel_kit_in(sub, Language::En)
|
|
}
|
|
|
|
/// The kit with its email in Czech or English (the app's language: most Czech providers read Czech).
|
|
pub fn cancel_kit_in(sub: &Subscription, language: Language) -> 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 monthly = sub.cadence == crate::subscriptions::Cadence::Monthly;
|
|
let last = sub.charges.last().map(|c| c.date);
|
|
let cost = sub.cost.to_czk();
|
|
let (email_subject, email_body) = match language {
|
|
Language::En => (
|
|
format!("Cancel my {name} subscription"),
|
|
format!(
|
|
"Hello,
|
|
|
|
please cancel my {name} subscription ({}, {cost} CZK, last charged on {}) with effect from the end of the current period, and confirm the cancellation by email.
|
|
|
|
Please do not charge my card again.
|
|
|
|
Thank you.",
|
|
if monthly { "monthly" } else { "yearly" },
|
|
last.map(|d| d.to_string()).unwrap_or_default(),
|
|
),
|
|
),
|
|
Language::Cs => (
|
|
format!("Zrušení předplatného {name}"),
|
|
format!(
|
|
"Dobrý den,
|
|
|
|
prosím o zrušení mého předplatného {name} ({}, {cost} Kč, naposledy strženo {}) ke konci aktuálního období a o potvrzení zrušení e-mailem.
|
|
|
|
Prosím, dále mi nestrhávejte platby z karty.
|
|
|
|
Děkuji.",
|
|
if monthly { "měsíční" } else { "roční" },
|
|
last.map(|d| {
|
|
let (y, m, day) = d.ymd();
|
|
format!("{day}. {m}. {y}")
|
|
})
|
|
.unwrap_or_default(),
|
|
),
|
|
),
|
|
};
|
|
CancelKit { provider: known.map(|(_, p, _)| p.to_string()), url: known.map(|(_, _, u)| u.to_string()), email_subject, email_body }
|
|
}
|