use crate::StripeConfig;
use std::sync::OnceLock;
static STRIPE_CLIENT: OnceLock<stripe::Client> = OnceLock::new();
static STRIPE_CONFIG: OnceLock<StripeConfig> = OnceLock::new();
pub struct Stripe;
impl Stripe {
pub fn init(config: StripeConfig) {
let client = stripe::Client::new(&config.api_key);
STRIPE_CLIENT.set(client).ok();
STRIPE_CONFIG.set(config).ok();
}
pub fn client() -> &'static stripe::Client {
STRIPE_CLIENT
.get()
.expect("Stripe::init() not called before Stripe::client()")
}
pub fn config() -> &'static StripeConfig {
STRIPE_CONFIG
.get()
.expect("Stripe::init() not called before Stripe::config()")
}
pub fn with(api_key: &str) -> stripe::Client {
stripe::Client::new(api_key)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn with_does_not_populate_global_static() {
let _scoped = Stripe::with("sk_test_scoped_key");
assert!(
STRIPE_CLIENT.get().is_none(),
"Stripe::with must not populate the global static client"
);
}
#[test]
fn with_returns_independent_client_values() {
let a: stripe::Client = Stripe::with("sk_test_a");
let b: stripe::Client = Stripe::with("sk_test_b");
drop(a);
drop(b);
}
}