use crate::Error;
#[derive(Debug, Clone)]
pub struct StripeConfig {
pub api_key: String,
pub webhook_secret: String,
pub connect_webhook_secret: Option<String>,
pub application_fee_percent: Option<f64>,
}
impl StripeConfig {
pub fn from_env() -> Result<Self, Error> {
let api_key = std::env::var("STRIPE_SECRET_KEY")
.map_err(|_| Error::Config("STRIPE_SECRET_KEY not set".to_string()))?;
let webhook_secret = std::env::var("STRIPE_WEBHOOK_SECRET")
.map_err(|_| Error::Config("STRIPE_WEBHOOK_SECRET not set".to_string()))?;
let connect_webhook_secret = std::env::var("STRIPE_CONNECT_WEBHOOK_SECRET").ok();
let application_fee_percent = std::env::var("STRIPE_APPLICATION_FEE_PERCENT")
.ok()
.and_then(|v| v.parse::<f64>().ok());
Ok(Self {
api_key,
webhook_secret,
connect_webhook_secret,
application_fee_percent,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_env_returns_config_error_when_key_missing() {
std::env::remove_var("STRIPE_SECRET_KEY");
std::env::remove_var("STRIPE_WEBHOOK_SECRET");
let result = StripeConfig::from_env();
assert!(matches!(result, Err(Error::Config(_))));
}
#[test]
fn config_loads_from_provided_values() {
let config = StripeConfig {
api_key: "sk_test_xxx".to_string(),
webhook_secret: "whsec_xxx".to_string(),
connect_webhook_secret: Some("whsec_connect_xxx".to_string()),
application_fee_percent: Some(2.5),
};
assert_eq!(config.api_key, "sk_test_xxx");
assert_eq!(config.webhook_secret, "whsec_xxx");
assert_eq!(
config.connect_webhook_secret.as_deref(),
Some("whsec_connect_xxx")
);
assert_eq!(config.application_fee_percent, Some(2.5));
}
}