Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
// Command sync-stripe-packs ensures each DefaultCreditPack exists in Stripe as a
|
|
// Product + one-time Price, then prints Price IDs (and optionally writes them to
|
|
// platform settings when -write-settings is set).
|
|
//
|
|
// go run ./cmd/sync-stripe-packs
|
|
// go run ./cmd/sync-stripe-packs -write-settings
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/billing"
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/platformsettings"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func main() {
|
|
pg := flag.String("postgres", os.Getenv("DATABASE_URL"), "Postgres DATABASE_URL")
|
|
writeSettings := flag.Bool("write-settings", false, "Upsert stripe.price.pack.* into platform settings")
|
|
flag.Parse()
|
|
|
|
secret := strings.TrimSpace(os.Getenv("STRIPE_SECRET_KEY"))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
|
|
var pool *pgxpool.Pool
|
|
var plat *platformsettings.Service
|
|
if strings.TrimSpace(*pg) != "" {
|
|
var err error
|
|
pool, err = pgxpool.New(ctx, *pg)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "connect: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer pool.Close()
|
|
plat = &platformsettings.Service{Pool: pool}
|
|
if resolved, err := plat.ResolveStripe(ctx, billing.StripeConfig{SecretKey: secret}); err == nil {
|
|
if strings.TrimSpace(resolved.SecretKey) != "" {
|
|
secret = resolved.SecretKey
|
|
}
|
|
}
|
|
}
|
|
if secret == "" {
|
|
fmt.Fprintln(os.Stderr, "STRIPE_SECRET_KEY (or stripe.secret_key in admin settings) required")
|
|
os.Exit(1)
|
|
}
|
|
|
|
svc := &billing.StripeService{
|
|
Pool: pool,
|
|
Cfg: billing.StripeConfig{SecretKey: secret},
|
|
}
|
|
results, err := svc.SyncCreditPackProducts(ctx)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "sync: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println("Stripe credit packs (one-time products):")
|
|
for _, r := range results {
|
|
flag := "ok"
|
|
if r.Created {
|
|
flag = "created/updated"
|
|
}
|
|
fmt.Printf(" %-8s $%-5d credits=%-6d price=%s product=%s (%s)\n",
|
|
r.PackID, r.PriceUSD, r.Credits, r.PriceID, r.ProductID, flag)
|
|
if *writeSettings {
|
|
if plat == nil {
|
|
fmt.Fprintln(os.Stderr, "-write-settings requires DATABASE_URL")
|
|
os.Exit(1)
|
|
}
|
|
key := billing.CreditPackSettingsKey(r.PackID)
|
|
if err := plat.SetKV(ctx, key, r.PriceID); err != nil {
|
|
fmt.Fprintf(os.Stderr, "write %s: %v\n", key, err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf(" wrote %s\n", key)
|
|
} else {
|
|
fmt.Printf(" settings key: %s\n", billing.CreditPackSettingsKey(r.PackID))
|
|
fmt.Printf(" env fallback: %s=%s\n", billing.CreditPackEnvVar(r.PackID), r.PriceID)
|
|
}
|
|
}
|
|
if !*writeSettings {
|
|
fmt.Println("\nRe-run with -write-settings to store Price IDs in platform settings.")
|
|
}
|
|
}
|