billingtest

package
v1.21.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: GPL-3.0 Imports: 50 Imported by: 0

Documentation

Overview

Package billingtest provides the billing module's behaviour-test harness: the real repository store over a private SQLite database and a miniredis instance, with seeding helpers for the rows order flows read. Only tests import it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlipayConfig

func AlipayConfig(t testing.TB, appID, gatewayURL string) string

AlipayConfig is a face-to-face payment configuration of appID whose sandbox gateway is gatewayURL.

func AlipayKey

func AlipayKey(t testing.TB) *rsa.PrivateKey

AlipayKey is the RSA key pair every Alipay test shares as both the merchant and the gateway key; it only exists so the SDK's signing and verification succeed.

func AlipayKeys

func AlipayKeys(t testing.TB) (private, public string)

AlipayKeys returns AlipayKey encoded as the payment configuration stores it: the PKCS#1 private key and the PKIX public key, base64.

func AlipaySign

func AlipaySign(t testing.TB, content string) string

AlipaySign signs content as the gateway does: RSA-SHA256, base64.

func UserContext

func UserContext(u *user.User) context.Context

UserContext returns a request context authenticated as u.

Types

type AlipayAnswer

type AlipayAnswer struct {
	Biz    string
	Signed bool
}

AlipayAnswer is the gateway's answer to one call: the business JSON, and whether it carries the gateway signature. Unsigned answers mimic gateway business failures.

type EPayV2 added in v1.21.1

type EPayV2 struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

EPayV2 is a local HTTPS peer with independent merchant/platform RSA keys. Requests and replies use a small independent signer to catch SDK drift.

func NewEPayV2 added in v1.21.1

func NewEPayV2(t testing.TB, handler http.HandlerFunc) *EPayV2

func (*EPayV2) Config added in v1.21.1

func (p *EPayV2) Config(mode, paymentType string) string

func (*EPayV2) Reply added in v1.21.1

func (p *EPayV2) Reply(w http.ResponseWriter, fields map[string]any)

func (*EPayV2) Sign added in v1.21.1

func (p *EPayV2) Sign(params map[string]string) map[string]string

Sign does not overwrite caller-provided pid/timestamp, allowing mismatch tests.

type FakeAlipay

type FakeAlipay struct {

	// URL is the gateway address to configure.
	URL string
	// contains filtered or unexported fields
}

FakeAlipay impersonates the Alipay OpenAPI gateway. Respond receives the method, its per-method call number and the request's business content.

func NewFakeAlipay

func NewFakeAlipay(t testing.TB, respond func(method string, call int, biz map[string]any) AlipayAnswer) *FakeAlipay

NewFakeAlipay starts a fake gateway for the test.

func (*FakeAlipay) Calls

func (g *FakeAlipay) Calls(method string) int

Calls counts the calls of method.

func (*FakeAlipay) LastBiz

func (g *FakeAlipay) LastBiz(method string) map[string]any

LastBiz is the business content of the last call of method.

func (*FakeAlipay) ServeHTTP

func (g *FakeAlipay) ServeHTTP(w http.ResponseWriter, r *http.Request)

type FakeStripe

type FakeStripe struct {

	// Backends routes a Stripe client to the fake.
	Backends *stripe.Backends
	// contains filtered or unexported fields
}

FakeStripe is an in-process Stripe API answering the customer, ephemeral key, PaymentIntent and webhook endpoint calls billing makes. It records which secret key created each intent, so tests can prove that clients never borrow each other's key.

func NewFakeStripe

func NewFakeStripe(t testing.TB) *FakeStripe

NewFakeStripe starts a fake Stripe API for the test.

func (*FakeStripe) Canceled

func (f *FakeStripe) Canceled() []string

Canceled lists the cancelled intents in order.

func (*FakeStripe) Created

func (f *FakeStripe) Created() int

Created counts the intents created through the API.

func (*FakeStripe) Customers

func (f *FakeStripe) Customers() int

Customers counts the customers created.

func (*FakeStripe) Keys

func (f *FakeStripe) Keys() map[string]string

Keys maps each order number to the secret key that created its intent.

func (*FakeStripe) Seed

func (f *FakeStripe) Seed(id string, amount int64, currency, status, orderNo, method string)

Seed stores an existing PaymentIntent of orderNo.

func (*FakeStripe) ServeHTTP

func (f *FakeStripe) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*FakeStripe) SetStatus

func (f *FakeStripe) SetStatus(id, status string)

SetStatus changes the status of intent id.

func (*FakeStripe) Status

func (f *FakeStripe) Status(id string) string

Status reports the status of intent id.

func (*FakeStripe) Webhooks

func (f *FakeStripe) Webhooks() map[string]string

Webhooks maps each registered endpoint id to its URL.

type Harness

type Harness struct {
	DB    *gorm.DB
	Redis *redis.Client
	Store *repository.GormStore
	// contains filtered or unexported fields
}

Harness is a real repository store over a private in-memory database.

func New

func New(t testing.TB) *Harness

New opens a fresh database with the billing tables and the subscription (plans, user subscriptions, entitlement periods), identity and platform tables the order flows touch.

func (*Harness) BalanceLogs

func (h *Harness) BalanceLogs(userID int64) []logEntity.Balance

BalanceLogs decodes the balance ledger of userID.

func (*Harness) Coupon

func (h *Harness) Coupon(code string, adjust ...func(*coupon.Coupon)) *coupon.Coupon

Coupon seeds an enabled coupon valid for the next hour.

func (*Harness) Events

func (h *Harness) Events(orderNo string) []order.Event

Events lists the order events of orderNo.

func (*Harness) GiftLogs

func (h *Harness) GiftLogs(userID int64) []logEntity.Gift

GiftLogs decodes the gift ledger of userID.

func (*Harness) Logs

func (h *Harness) Logs(kind logEntity.Type, objectID int64) []*logEntity.SystemLog

Logs returns the audit records of kind for objectID in insertion order.

func (*Harness) Order

func (h *Harness) Order(o *order.Order) *order.Order

Order seeds an order row directly, without its creation event.

func (*Harness) Orders

func (h *Harness) Orders(userID int64) []*order.Order

Orders counts the orders of userID.

func (*Harness) Payment

func (h *Harness) Payment(platform, config string, adjust ...func(*payment.Payment)) *payment.Payment

Payment seeds an enabled payment method of platform with a configuration.

func (*Harness) Plan

func (h *Harness) Plan(unitPrice int64, adjust ...func(*subscribe.Subscribe)) *subscribe.Subscribe

Plan seeds a plan on sale with unlimited stock.

func (*Harness) ReloadCoupon

func (h *Harness) ReloadCoupon(code string) *coupon.Coupon

ReloadCoupon reads the coupon back.

func (*Harness) ReloadOrder

func (h *Harness) ReloadOrder(orderNo string) *order.Order

ReloadOrder reads the order back from the database.

func (*Harness) ReloadPlan

func (h *Harness) ReloadPlan(id int64) *subscribe.Subscribe

ReloadPlan reads the plan back.

func (*Harness) ReloadWallet

func (h *Harness) ReloadWallet(userID int64) wallet.Wallet

ReloadWallet reads the wallet of userID; a missing row reads as zero.

func (*Harness) User

func (h *Harness) User(adjust ...func(*user.User)) *user.User

User seeds an account.

func (*Harness) UserSubscription

func (h *Harness) UserSubscription(userID int64, plan *subscribe.Subscribe, adjust ...func(*usersub.Subscribe)) *usersub.Subscribe

UserSubscription seeds a subscription of userID to plan.

func (*Harness) Wallet

func (h *Harness) Wallet(userID, balance, gift int64)

Wallet seeds the wallet of userID.

type Queue

type Queue struct {
	Activations    []string
	DeferredCloses []string
	ActivationErr  error
}

Queue records the order lifecycle tasks the flows schedule.

func (*Queue) EnqueueActivation

func (q *Queue) EnqueueActivation(_ context.Context, orderNo string) error

func (*Queue) EnqueueDeferredClose

func (q *Queue) EnqueueDeferredClose(_ context.Context, orderNo string) error

type UserCache

type UserCache struct {
	Cleared []int64
}

UserCache stands in for the identity module's user cache: it records the users whose cached projection a flow dropped.

func (*UserCache) ClearUserCache

func (c *UserCache) ClearUserCache(_ context.Context, userIDs ...int64) error

Jump to

Keyboard shortcuts

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