credit

package
v1.46.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package credit provides reusable starter credit logic. Shared between api/billing handlers and middleware/iammiddleware.

Index

Constants

View Source
const (
	StarterCreditCents = 10000 // $100.00 USD
	StarterCreditDays  = 365   // expires in 365 days
	StarterCreditTag   = "starter-credit"
)

Starter credit constants.

Variables

This section is empty.

Functions

func GrantIfEligible

func GrantIfEligible(db *datastore.Datastore, userId, trigger string)

GrantIfEligible checks if the user already has a starter credit and grants one if not. It is idempotent: duplicate calls for the same user are safe. The trigger parameter records what initiated the grant (e.g. "payment-method-added", "org-created").

This function is intended to be called from a goroutine; it only logs on failure and never panics. For the synchronous form that reports whether a grant was created, use GrantIfEligibleNow.

func GrantIfEligibleNow added in v1.42.38

func GrantIfEligibleNow(db *datastore.Datastore, userId, trigger string) (granted bool, err error)

GrantIfEligibleNow is the synchronous form of GrantIfEligible. It ensures the subject (userId — an IAM "owner/name" per-user key, or an org slug for pooled billing) has received exactly one starter-credit Deposit, and reports whether THIS call created it (false = one already existed).

Idempotent + race-safe: the check-and-create runs inside a datastore transaction so concurrent callers can never double-grant (no bleed). The dedupe key is (DestinationId=userId, Tags=StarterCreditTag).

The grant is a real Deposit transaction (tag "starter-credit", $5, 30-day expiry) — so it nets into GET /v1/billing/balance, which is the account the cloud gateway's balance gate reads and its usage debit withdraws from. (It is deliberately NOT a credit-grant record; those live in a separate ledger the balance endpoint does not read.)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL