Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: avoid hardcode stripe promotion code (#19815)
ecmadao7f0cc0db397c
diff --git a/backend/api/v1/subscription_service.go b/backend/api/v1/subscription_service.goindex a18c877a39..255b1e4e8e 100644--- a/backend/api/v1/subscription_service.go+++ b/backend/api/v1/subscription_service.go@@ -196,7 +196,7 @@ func (s *SubscriptionService) createCheckout(workspaceID string, v1Plan v1pb.PlaPriceModel: priceModel,SuccessURL: redirectURL + "?session_id={CHECKOUT_SESSION_ID}",CancelURL: redirectURL,- PromotionCode: priceModel.GetPromotionCode(),+ PromotionCode: pricing.GetPromotionCode(plan, interval),})if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to create checkout session"))@@ -352,7 +352,10 @@ func (s *SubscriptionService) ListPurchasePlans(_ context.Context, _ *connect.Refor _, bm := range limit.BillingMethods {method := &v1pb.PurchaseBillingMethod{Interval: convertStoreIntervalToV1(bm.Interval),- Discount: bm.Discount,+ }+ // Look up discount from Stripe if a promotion code is configured.+ if code := pricing.GetPromotionCode(p, bm.Interval); code != "" {+ method.Discount = stripeplugin.GetPromotionDiscount(code)}plan.BillingMethods = append(plan.BillingMethods, method)}diff --git a/backend/enterprise/pricing/plan.go b/backend/enterprise/pricing/plan.goindex 8c1add5564..8d619d4440 100644--- a/backend/enterprise/pricing/plan.go+++ b/backend/enterprise/pricing/plan.go@@ -2,15 +2,31 @@package pricingimport (+ "fmt"+ "os"+ "strings"+storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1")-// BillingMethodConfig defines a billing method with optional discount.+// BillingMethodConfig defines a billing method.type BillingMethodConfig struct {- Interval storepb.SubscriptionPayload_BillingInterval- PromotionCode string // Stripe promotion code (e.g., "FIRSTMONTH90")- Discount *v1pb.PurchaseDiscount+ Interval storepb.SubscriptionPayload_BillingInterval+}++// GetPromotionCode returns the Stripe promotion code for the given plan and interval+// from environment variables. The env var name follows the pattern:+//+// STRIPE_PROMOTION_CODE_<PLAN>_<INTERVAL>+//+// For example: STRIPE_PROMOTION_CODE_TEAM_MONTH=FIRSTMONTH90+//+// Returns empty string if not set.+func GetPromotionCode(plan storepb.SubscriptionPayload_Plan, interval storepb.SubscriptionPayload_BillingInterval) string {+ planName := strings.TrimPrefix(plan.String(), "PLAN_")+ intervalName := strings.TrimPrefix(interval.String(), "BILLING_INTERVAL_")+ key := fmt.Sprintf("STRIPE_PROMOTION_CODE_%s_%s", planName, intervalName)+ return os.Getenv(key)}// PlanLimitConfig defines pricing and limits for a plan.@@ -33,17 +49,8 @@ var planLimits = map[storepb.SubscriptionPayload_Plan]*PlanLimitConfig{PricePerSeatPerMonth: 2000, // $20/user/monthInstanceCount: 10,BillingMethods: []BillingMethodConfig{- {- Interval: storepb.SubscriptionPayload_MONTH,- PromotionCode: "FISRTMONTH90",- Discount: &v1pb.PurchaseDiscount{- Type: v1pb.PurchaseDiscount_PERCENTAGE_OFF,- Value: 90,- },- },- {- Interval: storepb.SubscriptionPayload_YEAR,- },+ {Interval: storepb.SubscriptionPayload_MONTH},+ {Interval: storepb.SubscriptionPayload_YEAR},},},storepb.SubscriptionPayload_ENTERPRISE: {diff --git a/backend/enterprise/pricing/pricing.go b/backend/enterprise/pricing/pricing.goindex 2f9039c9fc..820683a26c 100644--- a/backend/enterprise/pricing/pricing.go+++ b/backend/enterprise/pricing/pricing.go@@ -62,16 +62,6 @@ func (m *PriceModel) GetStripeInterval() string {return "month"}-// GetPromotionCode returns the promotion code for the current interval, or empty string.-func (m *PriceModel) GetPromotionCode() string {- for _, bm := range m.Plan.BillingMethods {- if bm.Interval == m.Interval {- return bm.PromotionCode- }- }- return ""-}-// GetPlanText returns a display name for the plan.func (m *PriceModel) GetPlanText() string {switch m.Plan.Plan {diff --git a/backend/plugin/stripe/stripe.go b/backend/plugin/stripe/stripe.goindex b072884774..3fc7168c61 100644--- a/backend/plugin/stripe/stripe.go+++ b/backend/plugin/stripe/stripe.go@@ -19,6 +19,7 @@ import ("github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/enterprise/pricing"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1")// WebhookSource is used to filter webhook events from our checkout sessions.@@ -90,6 +91,10 @@ func CreateCheckoutSession(params *CheckoutParams) (*CheckoutResult, error) {{PromotionCode: stripego.String(promo.ID)},}}+ } else {+ // Allow customers to manually enter promotion codes at checkout.+ // This is mutually exclusive with the Discounts param above.+ checkoutParams.AllowPromotionCodes = stripego.Bool(true)}s, err := checkoutsession.New(checkoutParams)@@ -382,6 +387,38 @@ func CreateBillingPortalSession(customerID string, returnURL string) (string, erreturn s.URL, nil}+// GetPromotionDiscount looks up an active promotion code and returns its discount info+// for display in the frontend. Returns nil if the code is invalid or inactive.+func GetPromotionDiscount(code string) *v1pb.PurchaseDiscount {+ iter := promotioncode.List(&stripego.PromotionCodeListParams{+ Code: stripego.String(code),+ Active: stripego.Bool(true),+ })+ for iter.Next() {+ promo := iter.PromotionCode()+ if promo.Coupon == nil {+ continue+ }+ c := promo.Coupon+ if c.PercentOff > 0 {+ return &v1pb.PurchaseDiscount{+ Type: v1pb.PurchaseDiscount_PERCENTAGE_OFF,+ Value: int32(c.PercentOff),+ }+ }+ if c.DurationInMonths > 0 && c.AmountOff > 0 {+ return &v1pb.PurchaseDiscount{+ Type: v1pb.PurchaseDiscount_FIXED_MONTH_OFF,+ Value: int32(c.DurationInMonths),+ }+ }+ }+ if err := iter.Err(); err != nil {+ slog.Error("failed to look up promotion discount", log.BBError(err), slog.String("code", code))+ }+ return nil+}+// GetPromotionByCode looks up an active promotion code eligible for first-time customers.func GetPromotionByCode(code string, priceInCents int64) *stripego.PromotionCode {iter := promotioncode.List(&stripego.PromotionCodeListParams{diff --git a/frontend/src/react/components/AdvancedSearch.tsx b/frontend/src/react/components/AdvancedSearch.tsxindex 474156dcde..18ae7e7c82 100644--- a/frontend/src/react/components/AdvancedSearch.tsx+++ b/frontend/src/react/components/AdvancedSearch.tsx@@ -405,7 +405,7 @@ export function AdvancedSearch({<div ref={containerRef} className="w-full min-w-0 relative">{/* Input container */}<div- className="flex items-center border border-gray-300 rounded-xs bg-white transition-colors"+ className="flex items-center h-9 border border-gray-300 rounded-xs bg-white transition-colors"onClick={() => inputRef.current?.focus()}>{/* Prefix: filter icon + tags */}@@ -448,7 +448,7 @@ export function AdvancedSearch({{/* Input */}<inputref={inputRef}- className="flex-1 min-w-[120px] h-9 bg-transparent border-none px-2 text-sm text-main placeholder:text-control-placeholder focus:outline-none focus:border-none focus:ring-0 focus:shadow-none"+ className="flex-1 min-w-[120px] bg-transparent border-none px-2 text-sm text-main placeholder:text-control-placeholder focus:outline-none focus:border-none focus:ring-0 focus:shadow-none"value={inputText}placeholder={visibleTags.length > 0 ? "" : placeholder}onClick={handleInputClick}diff --git a/frontend/src/react/components/ui/alert.tsx b/frontend/src/react/components/ui/alert.tsxindex 376a53d048..828550d17e 100644--- a/frontend/src/react/components/ui/alert.tsx+++ b/frontend/src/react/components/ui/alert.tsx@@ -9,7 +9,7 @@ import type { ComponentProps } from "react";import { cn } from "@/react/lib/utils";const alertVariants = cva(- "relative w-full rounded-xs border px-4 py-3 text-sm flex gap-x-3 items-center",+ "relative w-full rounded-xs border px-4 py-3 text-sm flex gap-x-3 items-start",{variants: {variant: {diff --git a/frontend/src/react/pages/settings/AuditLogPage.tsx b/frontend/src/react/pages/settings/AuditLogPage.tsxindex e932cc1ed0..afe2419119 100644--- a/frontend/src/react/pages/settings/AuditLogPage.tsx+++ b/frontend/src/react/pages/settings/AuditLogPage.tsx@@ -208,7 +208,7 @@ function JSONStringView({ jsonString }: { jsonString: string }) {onClick={() => setShowModal(false)}><div- className="bg-white rounded-md shadow-lg flex flex-col"+ className="bg-white rounded-sm shadow-lg flex flex-col"style={{width: "calc(100vw - 12rem)",height: "calc(100vh - 12rem)",diff --git a/frontend/src/react/pages/settings/DatabasesPage.tsx b/frontend/src/react/pages/settings/DatabasesPage.tsxindex fd852e3071..2c9559dfc4 100644--- a/frontend/src/react/pages/settings/DatabasesPage.tsx+++ b/frontend/src/react/pages/settings/DatabasesPage.tsx@@ -24,6 +24,7 @@ import {import { EditEnvironmentDrawer } from "@/react/components/EditEnvironmentDrawer";import { EnvironmentLabel } from "@/react/components/EnvironmentLabel";import { LabelsDisplay } from "@/react/components/LabelsDisplay";+import { PermissionGuard } from "@/react/components/PermissionGuard";import { Button } from "@/react/components/ui/button";import { Combobox } from "@/react/components/ui/combobox";import { useClickOutside } from "@/react/hooks/useClickOutside";@@ -1595,13 +1596,20 @@ export function DatabasesPage() {placeholder={t("database.filter-database")}scopeOptions={scopeOptions}/>- {hasWorkspacePermissionV2("bb.instances.list") &&- hasWorkspacePermissionV2("bb.issues.create") && (- <Button onClick={() => setShowCreateDrawer(true)}>- <Plus className="h-4 w-4 mr-1" />- {t("quick-action.new-db")}- </Button>- )}+ <PermissionGuard+ permissions={["bb.instances.list", "bb.issues.create"]}+ >+ <Button+ disabled={+ !hasWorkspacePermissionV2("bb.instances.list") ||+ !hasWorkspacePermissionV2("bb.issues.create")+ }+ onClick={() => setShowCreateDrawer(true)}+ >+ <Plus className="h-4 w-4 mr-1" />+ {t("quick-action.new-db")}+ </Button>+ </PermissionGuard></div><div className="flex flex-col gap-y-4">diff --git a/frontend/src/react/pages/settings/EnvironmentsPage.tsx b/frontend/src/react/pages/settings/EnvironmentsPage.tsxindex e5514bd81f..8311eb10aa 100644--- a/frontend/src/react/pages/settings/EnvironmentsPage.tsx+++ b/frontend/src/react/pages/settings/EnvironmentsPage.tsx@@ -12,6 +12,7 @@ import {} from "react";import { useTranslation } from "react-i18next";import { FeatureAttention } from "@/react/components/FeatureAttention";+import { FeatureBadge } from "@/react/components/FeatureBadge";import { PermissionGuard } from "@/react/components/PermissionGuard";import { ResourceIdField } from "@/react/components/ResourceIdField";import { Button } from "@/react/components/ui/button";@@ -816,8 +817,9 @@ function EnvironmentDetail({{/* Tier section */}<div className="flex flex-col gap-y-2"><div className="gap-y-1">- <label className="font-medium flex items-center">+ <label className="font-medium flex items-center gap-x-1">{t("policy.environment-tier.name")}+ <FeatureBadge feature={PlanFeature.FEATURE_ENVIRONMENT_TIERS} /></label><p className="text-sm text-gray-600">{t("policy.environment-tier.description", { newline: "\n" })}diff --git a/frontend/src/react/pages/settings/general/BrandingSection.tsx b/frontend/src/react/pages/settings/general/BrandingSection.tsxindex bf63346ba5..a0ef9f594d 100644--- a/frontend/src/react/pages/settings/general/BrandingSection.tsx+++ b/frontend/src/react/pages/settings/general/BrandingSection.tsx@@ -239,7 +239,9 @@ export const BrandingSection = forwardRef<SectionHandle, BrandingSectionProps>({/* Upload overlay */}<divclassName={`flex flex-col gap-y-1 text-center justify-center items-center absolute top-0 bottom-0 left-0 right-0 ${- logoUrl ? "opacity-0 hover:opacity-90" : ""+ logoUrl+ ? "opacity-0 hover:opacity-80 hover:bg-gray-100"+ : ""}`}>{!logoUrl && (diff --git a/frontend/src/react/pages/settings/general/GeneralSection.tsx b/frontend/src/react/pages/settings/general/GeneralSection.tsxindex bc7cb85500..d6ad2095c8 100644--- a/frontend/src/react/pages/settings/general/GeneralSection.tsx+++ b/frontend/src/react/pages/settings/general/GeneralSection.tsx@@ -132,7 +132,7 @@ export const GeneralSection = forwardRef<SectionHandle, GeneralSectionProps>(<div className="mb-4 font-medium">{t("settings.general.workspace.default-landing-page.self")}</div>- <div className="w-full flex flex-col gap-8">+ <div className="w-full flex flex-col gap-4"><label className="flex items-start gap-x-3 cursor-pointer"><inputtype="radio"diff --git a/frontend/src/react/pages/settings/general/SQLEditorSection.tsx b/frontend/src/react/pages/settings/general/SQLEditorSection.tsxindex 114828037d..dc15d3ee17 100644--- a/frontend/src/react/pages/settings/general/SQLEditorSection.tsx+++ b/frontend/src/react/pages/settings/general/SQLEditorSection.tsx@@ -112,13 +112,14 @@ export const SQLEditorSection = forwardRef<const [state, setState] = useState<LocalState>(getInitialState);// Re-sync state when underlying data loads (e.g. after policy fetch on mount).- const prevPolicyRef = useRef(policyPayload);+ const prevInitialRef = useRef<LocalState>(getInitialState());useEffect(() => {- if (prevPolicyRef.current !== policyPayload) {- prevPolicyRef.current = policyPayload;- setState(getInitialState());+ const next = getInitialState();+ if (!isEqual(prevInitialRef.current, next)) {+ prevInitialRef.current = next;+ setState(next);}- }, [policyPayload, getInitialState]);+ }, [getInitialState]);const isDirty = useCallback(() => !isEqual(state, getInitialState()),