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 ¶
var Schema string
Schema is the DDL for the example tables.
Functions ¶
This section is empty.
Types ¶
type Payment ¶
Payment is the domain entity; it owns the translation from the sqlc record.
func NewPayment ¶
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 ¶
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 ¶
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.