billing: POST /api/v1/billing/promotion/validate — HTTP wrapper around plans.validatePromotion#47
Merged
Conversation
…d plans.ValidatePromotion Adds the agent-facing endpoint that the dashboard's PromoCodePanel (PR #38) submits to before checkout. Returns 200 + ok:true with the structured discount on success and 200 + ok:false with a typed error + agent_action on rejection — the dashboard renders the red state through its normal success-path parser without a catch. Rate-limited per-team per-hour (30/hr) to make brute-forcing the seed-code namespace impractical; Redis errors fail open so a cache outage can't block a user mid-checkout. Adds the AgentActionPromotionInvalid constant which the TestAgentActionContract gate covers automatically.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /api/v1/billing/promotion/validate— the agent-facing endpoint the dashboard's PromoCodePanel (PR obs: provisioner service observability — slog default, NR agent, gRPC interceptor, healthz sidecar (track 5/8) #38) submits to before checkout.plans.Registry.ValidatePromotionwith: JWT auth gate, per-team per-hour rate limit (30/hr), structured discount payload on success, typederror+agent_actionon rejection.catchon the fetch promise. 400 is reserved for empty/malformed bodies (developer error, not user error).AgentActionPromotionInvalidconstant covered by theTestAgentActionContractgate.Response shapes
Success (200):
{ "ok": true, "code": "LAUNCH50", "discount": { "kind": "percent_off", "value": 50, "applies_to": ["pro", "team"], "max_uses": 1000, "description": "50% off Pro or Team for the first 1000 signups" }, "valid_until": "2099-12-31T23:59:59Z" }Invalid code (200):
{ "ok": false, "error": "promotion_invalid", "message": "Promotion code \"SAVE20\" is not valid for the pro plan.", "agent_action": "Tell the user this promo code isn't valid for the requested plan. Have them try a different code at https://instanode.dev/billing — promotion codes are case-insensitive." }Expired:
error: "promotion_expired". Same envelope shape.Divergence from the brief spec
The brief described
discount.applies_toas an integer ("3 billing cycles") with aunitfield ("months"). The actualplans.Promotionstruct has no such concept — it carriesAppliesTo []string(the list of tier names the code applies to). Per the brief's iron rule "don't reshape the struct", I keptapplies_toas[]stringand addedmax_uses+description(which the struct does carry). Droppedunit. The dashboard agent should adjust its rendering accordingly.Rate limit
Implemented inline (not
middleware.RateLimit, which is fingerprint+day-scoped). Per-team per-hour bucket:promo_validate:<team_id>:<YYYY-MM-DDTHH>withEXPIRE 65m. Fail-open on Redis errors. Tests cover the 31st-call-is-429 path + the per-team-bucket isolation.Test plan
make test-unitgreen — all packages includinginternal/handlers(23s) andinternal/router(0.7s).TestAgentActionContractincludesAgentActionPromotionInvalid(passes the 4-rule contract + <280 char ceiling).billing_promotion_test.go:/openapi.jsondocuments both success and invalid response shapes with examples.🤖 Generated with Claude Code