Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(billing): prefill first Checkout email (#7529)

LinYushen7f828b44e77c
diff --git a/packages/core/api/client.ts b/packages/core/api/client.ts
index 0423094439..50b2701fe2 100644
--- a/packages/core/api/client.ts
+++ b/packages/core/api/client.ts
@@ -1810,9 +1810,6 @@ export class ApiClient {
body: JSON.stringify({
interval: data.interval,
idempotency_key: data.idempotencyKey,
- ...(data.customerEmail
- ? { customer_email: data.customerEmail }
- : {}),
}),
extraHeaders: {
"Content-Type": "application/json",
diff --git a/packages/core/types/billing.ts b/packages/core/types/billing.ts
index fba065b8ff..a3c942d4c3 100644
--- a/packages/core/types/billing.ts
+++ b/packages/core/types/billing.ts
@@ -267,7 +267,6 @@ export interface WorkspaceSubscriptionPrices {
export interface CreateWorkspaceSubscriptionCheckoutRequest {
interval: WorkspaceSubscriptionInterval;
idempotencyKey: string;
- customerEmail?: string;
}
export interface CreateWorkspaceSubscriptionCheckoutResponse {
diff --git a/server/internal/handler/cloud_billing.go b/server/internal/handler/cloud_billing.go
index 582291704d..edda4265e3 100644
--- a/server/internal/handler/cloud_billing.go
+++ b/server/internal/handler/cloud_billing.go
@@ -54,7 +54,6 @@ const maxCloudSubscriptionSeats = 10_000
type cloudSubscriptionCheckoutRequest struct {
Interval string `json:"interval"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
- CustomerEmail string `json:"customer_email,omitempty"`
}
// This is an intentional allowlist: additive cloud fields are not forwarded
@@ -205,8 +204,9 @@ func (h *Handler) GetCloudWorkspaceSubscriptionPrices(w http.ResponseWriter, r *
}
// CreateCloudWorkspaceSubscriptionCheckout injects the middleware-resolved
-// workspace into the upstream body. A caller cannot use a valid role in one
-// workspace to smuggle a different workspace_id through JSON.
+// workspace and authenticated user's stored email into the upstream body. A
+// caller cannot smuggle a different workspace or Stripe payer identity through
+// JSON; Cloud remains authoritative for the price, quantity, and Checkout.
func (h *Handler) CreateCloudWorkspaceSubscriptionCheckout(w http.ResponseWriter, r *http.Request) {
workspaceID, userID, ok := h.requireCloudSubscriptionWorkspace(w, r, "owner", "admin")
if !ok {
@@ -229,11 +229,25 @@ func (h *Handler) CreateCloudWorkspaceSubscriptionCheckout(w http.ResponseWriter
if !ok {
return
}
+ payerUserID, ok := parseUUIDOrBadRequest(w, userID, "user id")
+ if !ok {
+ return
+ }
+ user, err := h.Queries.GetUser(r.Context(), payerUserID)
+ if err != nil {
+ writeError(w, http.StatusInternalServerError, "failed to resolve checkout payer")
+ return
+ }
+ customerEmail := strings.TrimSpace(user.Email)
+ if customerEmail == "" {
+ writeError(w, http.StatusInternalServerError, "checkout payer email is unavailable")
+ return
+ }
upstreamBody, err := json.Marshal(cloudSubscriptionCheckoutUpstreamRequest{
WorkspaceID: workspaceID,
Interval: in.Interval,
IdempotencyKey: idempotencyKey,
- CustomerEmail: in.CustomerEmail,
+ CustomerEmail: customerEmail,
})
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to build subscription request")
diff --git a/server/internal/handler/cloud_billing_test.go b/server/internal/handler/cloud_billing_test.go
index e52992a256..566e552728 100644
--- a/server/internal/handler/cloud_billing_test.go
+++ b/server/internal/handler/cloud_billing_test.go
@@ -376,7 +376,7 @@ func TestCloudWorkspaceSubscriptionReadAndWritesUseScopedPaths(t *testing.T) {
}
}
-func TestCreateCloudWorkspaceSubscriptionCheckoutInjectsAuthoritativeWorkspace(t *testing.T) {
+func TestCreateCloudWorkspaceSubscriptionCheckoutInjectsAuthoritativeWorkspaceAndEmail(t *testing.T) {
withFeatureFlag(t, testHandler, featureflags.BillingWorkspaceSubscriptions, true)
proxy := &fakeCloudRuntimeProxy{
enabled: true,
@@ -391,7 +391,7 @@ func TestCreateCloudWorkspaceSubscriptionCheckoutInjectsAuthoritativeWorkspace(t
"workspace_id": "00000000-0000-0000-0000-000000000001",
"interval": "year",
"idempotency_key": "checkout-request-1",
- "customer_email": "[email protected]",
+ "customer_email": "[email protected]",
})
req.Header.Set(idempotencyKeyHeader, "checkout-header-1")
req = withCloudSubscriptionWorkspace(req, "owner")
@@ -411,7 +411,7 @@ func TestCreateCloudWorkspaceSubscriptionCheckoutInjectsAuthoritativeWorkspace(t
if body.WorkspaceID != testWorkspaceID {
t.Fatalf("upstream workspace_id = %q, want middleware workspace %q", body.WorkspaceID, testWorkspaceID)
}
- if body.Interval != "year" || body.IdempotencyKey != "checkout-request-1" || body.CustomerEmail != "[email protected]" {
+ if body.Interval != "year" || body.IdempotencyKey != "checkout-request-1" || body.CustomerEmail != handlerTestEmail {
t.Fatalf("upstream body = %+v", body)
}
if got := proxy.req.Headers.Get(idempotencyKeyHeader); got != "checkout-header-1" {
@@ -419,6 +419,74 @@ func TestCreateCloudWorkspaceSubscriptionCheckoutInjectsAuthoritativeWorkspace(t
}
}
+func TestCreateCloudWorkspaceSubscriptionCheckoutFailsWhenPayerCannotBeResolved(t *testing.T) {
+ withFeatureFlag(t, testHandler, featureflags.BillingWorkspaceSubscriptions, true)
+ proxy := &fakeCloudRuntimeProxy{enabled: true}
+ useCloudRuntimeProxy(t, proxy)
+
+ req := newRequest(http.MethodPost, "/api/cloud-subscriptions/checkout-sessions", map[string]any{
+ "interval": "month",
+ "idempotency_key": "checkout-request-missing-payer",
+ })
+ req.Header.Set("X-User-ID", "00000000-0000-0000-0000-000000000099")
+ req = withCloudSubscriptionWorkspace(req, "owner")
+ w := httptest.NewRecorder()
+ testHandler.CreateCloudWorkspaceSubscriptionCheckout(w, req)
+
+ if w.Code != http.StatusInternalServerError || !strings.Contains(w.Body.String(), "failed to resolve checkout payer") {
+ t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
+ }
+ if proxy.called {
+ t.Fatal("upstream must not be called without an authoritative payer email")
+ }
+}
+
+func TestCreateCloudWorkspaceSubscriptionCheckoutRejectsInvalidPayerID(t *testing.T) {
+ withFeatureFlag(t, testHandler, featureflags.BillingWorkspaceSubscriptions, true)
+ proxy := &fakeCloudRuntimeProxy{enabled: true}
+ useCloudRuntimeProxy(t, proxy)
+
+ req := newRequest(http.MethodPost, "/api/cloud-subscriptions/checkout-sessions", map[string]any{
+ "interval": "month",
+ "idempotency_key": "checkout-request-invalid-payer",
+ })
+ req.Header.Set("X-User-ID", "not-a-uuid")
+ req = withCloudSubscriptionWorkspace(req, "owner")
+ w := httptest.NewRecorder()
+ testHandler.CreateCloudWorkspaceSubscriptionCheckout(w, req)
+
+ if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "invalid user id") {
+ t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
+ }
+ if proxy.called {
+ t.Fatal("upstream must not be called with an invalid payer id")
+ }
+}
+
+func TestCreateCloudWorkspaceSubscriptionCheckoutRejectsEmptyPayerEmail(t *testing.T) {
+ withFeatureFlag(t, testHandler, featureflags.BillingWorkspaceSubscriptions, true)
+ proxy := &fakeCloudRuntimeProxy{enabled: true}
+ useCloudRuntimeProxy(t, proxy)
+ emptyEmailUserID := dbfx.User(t, "Empty Checkout Email", " ")
+ dbfx.Member(t, testWorkspaceID, emptyEmailUserID, "owner")
+
+ req := newRequest(http.MethodPost, "/api/cloud-subscriptions/checkout-sessions", map[string]any{
+ "interval": "month",
+ "idempotency_key": "checkout-request-empty-email",
+ })
+ req.Header.Set("X-User-ID", emptyEmailUserID)
+ req = withCloudSubscriptionWorkspace(req, "owner")
+ w := httptest.NewRecorder()
+ testHandler.CreateCloudWorkspaceSubscriptionCheckout(w, req)
+
+ if w.Code != http.StatusInternalServerError || !strings.Contains(w.Body.String(), "checkout payer email is unavailable") {
+ t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
+ }
+ if proxy.called {
+ t.Fatal("upstream must not be called without a payer email")
+ }
+}
+
func TestCreateCloudWorkspaceSubscriptionCheckoutAcceptsHeaderIdempotencyKey(t *testing.T) {
withFeatureFlag(t, testHandler, featureflags.BillingWorkspaceSubscriptions, true)
proxy := &fakeCloudRuntimeProxy{