payments

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package payments is the worked example from the post: an idempotent payment that writes an outbox event, and a service that never holds a transaction across the network call to the payment rail.

Index

Constants

This section is empty.

Variables

View Source
var Schema string

Schema is the DDL for the example tables.

Functions

This section is empty.

Types

type Payment

type Payment struct {
	ID        string
	AccountID string
	Amount    int64
	Status    Status
	Reference string
}

Payment is the domain entity; it owns the translation from the sqlc record.

func NewPayment

func NewPayment(id, accountID string, amount int64) Payment

NewPayment returns a pending Payment.

type PaymentRepo

type PaymentRepo struct {
	// contains filtered or unexported fields
}

PaymentRepo owns its transactions. Each method opens one, does only database work, and returns; the transaction never leaves the method.

func NewPaymentRepo

func NewPaymentRepo(tx *quartermaster.Transactor[*db.Queries]) *PaymentRepo

NewPaymentRepo returns a PaymentRepo that runs its transactions on tx.

func (*PaymentRepo) CreatePending

func (r *PaymentRepo) CreatePending(ctx context.Context, p Payment, idemKey string) (Payment, error)

CreatePending claims the idempotency key, inserts the payment and writes the outbox event in one transaction. A retry with the same key returns the payment the first call created. A failed call leaves nothing behind, not even the key, so the client can retry with the same key.

func (*PaymentRepo) RecordResult

func (r *PaymentRepo) RecordResult(ctx context.Context, id, reference string, submitErr error) (Payment, error)

RecordResult stores what the rail said. submitErr is the error from the rail, if any, and decides between succeeded and failed.

type Rail

type Rail interface {
	Submit(ctx context.Context, paymentID, idemKey string) (reference string, err error)
}

Rail is the external payment provider. Calls to it are slow and fallible.

type Request

type Request struct {
	ID             string
	AccountID      string
	Amount         int64
	IdempotencyKey string
}

Request is a caller's intent to pay. IdempotencyKey identifies retries of the same request, so a retry returns the first payment instead of paying twice.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service pays by orchestrating the repository and the rail. It owns the order of operations; the repository owns the transactions.

func NewService

func NewService(payments *PaymentRepo, rail Rail) *Service

NewService returns a Service that stores payments in payments and submits them to rail.

func (*Service) Pay

func (s *Service) Pay(ctx context.Context, req Request) (Payment, error)

Pay stores the payment as pending, calls the rail, then stores the result: two short transactions and no connection held during the network call. If the process dies after Submit the payment stays pending; this example has no reconciler, so something else would have to find and finish it.

type Status

type Status string

Status is the lifecycle state of a Payment.

const (
	StatusPending   Status = "pending"
	StatusSucceeded Status = "succeeded"
	StatusFailed    Status = "failed"
)

The states a Payment moves through. It starts pending and ends succeeded or failed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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