Purchases for admins only until the opening; brand images for Stripe
CI / test (push) Successful in 1m22s
CI / build (push) Successful in 7m0s
CI / deploy (push) Skipped

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:
feco
2026-11-20 06:57:00 +00:00
co-authored by Claude Opus 5.5
parent 42a982010d
commit d9e71ed349
13 changed files with 76 additions and 11 deletions
+7 -1
View File
@@ -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()],
+3 -1
View File
@@ -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,
+9 -3
View File
@@ -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)
+18
View File
@@ -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);
}
+6 -3
View File
@@ -1,9 +1,12 @@
import type { Me, PayMethod, PublicConfig } from "./types";
/** How this user can buy packages (with a Stripe test key: admins only). */
/**
* How this user can buy packages. Until purchases open (and with a Stripe
* test key) only admins can; everyone else sees "soon".
*/
export function payMethod(config: PublicConfig | null | undefined, me: Me): PayMethod | null {
if (config?.payments === "mock") return "mock";
if (config?.payments !== "stripe") return null;
if (!config.payments_test) return "stripe";
return me.user.roles.includes("admin") ? "stripe_test" : null;
if (config.payments_admins_only && !me.user.roles.includes("admin")) return null;
return config.payments_test ? "stripe_test" : "stripe";
}
+3 -1
View File
@@ -94,7 +94,9 @@ export interface PublicConfig {
mock_payments: boolean;
/** How packages are paid: Stripe Checkout, the stand-in, or not at all. */
payments: "stripe" | "mock" | null;
/** Stripe has a test key: only admins can buy. */
/** Only admins can buy through Stripe (until the opening, and with a test key). */
payments_admins_only: boolean;
/** Stripe has a test key (test cards work, no real money). */
payments_test: boolean;
/** "invite": new accounts need an invitation. */
signup_mode: "invite" | "open";