Purchases for admins only until the opening; brand images for Stripe
PURCHASES_OPEN (default false) keeps Stripe purchases to admins, also with a live key, so nobody types a real card before the opening. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -46,8 +46,12 @@ pub struct Config {
|
||||
/// analysis unit at once, until Stripe is connected.
|
||||
pub mock_payments: bool,
|
||||
/// Stripe Checkout, when `STRIPE_SECRET_KEY` is set (it wins over
|
||||
/// `MOCK_PAYMENTS`). With a test key only admins can buy.
|
||||
/// `MOCK_PAYMENTS`).
|
||||
pub stripe: Option<StripeConfig>,
|
||||
/// `PURCHASES_OPEN` (default `false`): everyone may buy through Stripe.
|
||||
/// Until the opening only admins can (and with a test key always only
|
||||
/// admins), so nobody types a real card into a test or unfinished setup.
|
||||
pub purchases_open: bool,
|
||||
/// `USER_INVITES` (default `false`): users may invite others ("how
|
||||
/// compatible are we?" links, which also let the invitee sign up). Off
|
||||
/// until the launch; admins may always.
|
||||
@@ -166,6 +170,7 @@ impl Config {
|
||||
.trim_end_matches('/')
|
||||
.to_string(),
|
||||
}),
|
||||
purchases_open: parsed("PURCHASES_OPEN", false)?,
|
||||
user_invites: parsed("USER_INVITES", false)?,
|
||||
referral_rewards: parsed("REFERRAL_REWARDS", false)?,
|
||||
google_client_ids: list("GOOGLE_CLIENT_IDS"),
|
||||
@@ -206,6 +211,7 @@ impl Config {
|
||||
password_login: true,
|
||||
mock_payments: true,
|
||||
stripe: None,
|
||||
purchases_open: true,
|
||||
user_invites: true,
|
||||
referral_rewards: true,
|
||||
google_client_ids: vec!["test-client.apps.googleusercontent.com".into()],
|
||||
|
||||
@@ -165,8 +165,10 @@ async fn public_config(State(state): St) -> Json<Value> {
|
||||
"locales": c.supported_locales,
|
||||
"password_login": c.password_login,
|
||||
"mock_payments": payments::method(c) == Some("mock"),
|
||||
// "stripe", "mock" or null; with a Stripe test key only admins can buy.
|
||||
// "stripe", "mock" or null; Stripe purchases are for admins only
|
||||
// until PURCHASES_OPEN (and always with a test key).
|
||||
"payments": payments::method(c),
|
||||
"payments_admins_only": payments::admins_only(c),
|
||||
"payments_test": c.stripe.as_ref().is_some_and(|s| s.test_mode()),
|
||||
"user_invites": c.user_invites,
|
||||
"referral_rewards": c.referral_rewards,
|
||||
|
||||
@@ -84,11 +84,17 @@ pub fn method(config: &crate::config::Config) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Stripe, if this user may pay through it (with a test key: admins only, so
|
||||
/// nobody gets packages with Stripe's test cards).
|
||||
/// Only admins can buy through Stripe: until purchases open, and always with a
|
||||
/// test key (nobody gets packages with Stripe's test cards, nobody types a
|
||||
/// real card into a test page).
|
||||
pub fn admins_only(config: &crate::config::Config) -> bool {
|
||||
!config.purchases_open || config.stripe.as_ref().is_some_and(StripeConfig::test_mode)
|
||||
}
|
||||
|
||||
/// Stripe, if this user may pay through it.
|
||||
fn stripe_for<'a>(state: &'a AppState, auth: &AuthUser) -> Result<&'a StripeConfig, ApiError> {
|
||||
let config = state.config.stripe.as_ref().ok_or(ApiError::NotFound("payment method"))?;
|
||||
if config.test_mode() && !auth.user.has_role(Role::Admin) {
|
||||
if admins_only(&state.config) && !auth.user.has_role(Role::Admin) {
|
||||
return Err(ApiError::Forbidden("purchases are being tested; they open soon".into()));
|
||||
}
|
||||
Ok(config)
|
||||
|
||||
@@ -2375,6 +2375,7 @@ async fn stripe_test_mode_is_for_admins_only(pool: PgPool) {
|
||||
let r = app.get("/v1/config", None).await;
|
||||
assert_eq!(r.body["payments"], "stripe");
|
||||
assert_eq!(r.body["payments_test"], true);
|
||||
assert_eq!(r.body["payments_admins_only"], true);
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let r = app.post("/v1/payments/checkout", Some(&anna), json!({"package": "single"})).await;
|
||||
assert_eq!(r.status, StatusCode::FORBIDDEN);
|
||||
@@ -2383,3 +2384,20 @@ async fn stripe_test_mode_is_for_admins_only(pool: PgPool) {
|
||||
let r = app.post("/v1/payments/checkout", Some(&admin), json!({"package": "single"})).await;
|
||||
assert_eq!(r.body["error"]["code"], "payment_unavailable", "{}", r.body);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn stripe_purchases_wait_for_the_opening(pool: PgPool) {
|
||||
let app = TestApp::with_config(pool, |c| {
|
||||
with_stripe(c, "sk_live_fake");
|
||||
c.purchases_open = false;
|
||||
})
|
||||
.await;
|
||||
let r = app.get("/v1/config", None).await;
|
||||
assert_eq!((&r.body["payments_test"], &r.body["payments_admins_only"]), (&json!(false), &json!(true)));
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let r = app.post("/v1/payments/checkout", Some(&anna), json!({"package": "single"})).await;
|
||||
assert_eq!(r.status, StatusCode::FORBIDDEN);
|
||||
let admin = app.register("admin@example.com", "Admin").await;
|
||||
let r = app.post("/v1/payments/checkout", Some(&admin), json!({"package": "single"})).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user