fakebilling

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package fakebilling is an in-memory billing server implementing the mcp-billing/v1 protocol (see docs/COMMERCE.md) for tests and local e2e runs: signature verification (same scheme as the real servers), Idempotency-Key replay on mutations, and failure injection.

srv := fakebilling.New("client-1", "secret")
defer srv.Close()
srv.FailNext("POST", "/accounts", 503, 2) // next two creates fail

Index

Constants

View Source
const MaxSkew = 5 * time.Minute

MaxSkew is the accepted clock difference for X-MCBB-Timestamp.

View Source
const Prefix = "/mcp-billing/v1"

Prefix is the protocol path prefix.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	ExternalID string   `json:"external_id"`
	PlanCode   string   `json:"plan_code"`
	Status     string   `json:"status"`
	PeriodEnd  string   `json:"period_end"`
	Orders     []string `json:"orders"`
}

Account is a provisioned account, keyed by external id.

type Call

type Call struct {
	Method, Path, IdempotencyKey string
	Body                         map[string]any
	RawBody                      string // exactly as received (signed bytes)
	Replayed                     bool   // answered from the idempotency store
	Failed                       int    // rejected (injected failure or missing key): the status, else 0
}

Call records one request that passed signature verification.

type Plan

type Plan struct {
	Code   string `json:"code"`
	Name   string `json:"name"`
	NameAr string `json:"name_ar,omitempty"`
}

Plan is a catalog plan.

type Server

type Server struct {
	*httptest.Server
	ClientID, Secret string
	// contains filtered or unexported fields
}

Server is the fake billing server. All methods are safe for concurrent use.

func New

func New(clientID, secret string) *Server

New starts a fake billing server accepting clientID/secret.

func (*Server) Account

func (s *Server) Account(id string) (Account, bool)

Account returns a copy of the account with external id id.

func (*Server) CallCount

func (s *Server) CallCount(method, path string) int

CallCount counts verified, non-replayed, non-failed requests for method path.

func (*Server) Calls

func (s *Server) Calls() []Call

Calls returns the verified requests so far.

func (*Server) ClearFailures

func (s *Server) ClearFailures()

ClearFailures drops every pending injected failure.

func (*Server) FailNext

func (s *Server) FailNext(method, path string, status, times int)

FailNext makes the next times requests matching method and path (relative to the prefix; a trailing "*" matches a prefix) answer status.

func (*Server) SetAccountStatus

func (s *Server) SetAccountStatus(id, status string)

SetAccountStatus changes an account's status (e.g. to test reconcile).

func (*Server) SetPeriodEndAnswer

func (s *Server) SetPeriodEndAnswer(v any)

SetPeriodEndAnswer makes POST /accounts answer period_end v (e.g. an epoch number or an unparseable string) instead of the stored value; nil restores the default.

func (*Server) SetPlans

func (s *Server) SetPlans(p ...Plan)

SetPlans replaces the catalog plans.

Jump to

Keyboard shortcuts

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