purchase

package module
v0.0.0-...-ba45184 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2021 License: MIT Imports: 4 Imported by: 0

README

A Simple Purchase Engine

This is a package I am building for practice between contracts. The goal is to make a simple, flexible, and correct purchasing system that can be used with any combination of payment gateway, distribution service, product catalogue.

A secondary goal is to demonstrate how executable specifications can be developed and run against either a package, backend service, or UI. The specifications are written in Gherkin and located in the features directory. Implementations for the test are located in purchase_test.go and main_test.go. These can be run with the go test . command or godog.

Rough idea of how it works:

mind_map

Note: the customer holding the money bag is essential to this process!

Documentation

Index

Constants

View Source
const (
	OrderStatusPaidInFull     = "paid-in-full"
	OrderStatusPendingPayment = "pending-payment"
	OrderStatusPaidInPart     = "paid-in-part"
	OrderStatusOverPaid       = "over-paid"
)

Variables

View Source
var (
	ErrDoubleSpend           = errors.New("double spend")
	ErrInsufficientPayment   = errors.New("insufficent payment")
	ErrOrderNotFound         = errors.New("order not found")
	ErrPaymentNotFound       = errors.New("payment not found")
	ErrNetworkError          = errors.New("network error")
	ErrPaymentFound          = errors.New("payment found")
	ErrCreateOrderValidation = errors.New("invalid order")
)

Functions

This section is empty.

Types

type Currency

type Currency = string

type CurrencyBalance

type CurrencyBalance struct{}

type Fee

type Fee struct {
	Amount int
}

type NewOrder

type NewOrder struct {
	Fees      []Fee
	Products  []Product
	Currency  Currency
	Purchaser Purchaser
	Recipient Receipt
}

type NewOrderPolicy

type NewOrderPolicy func(ctx context.Context, no NewOrder) (bool, error)

type Order

type Order struct {
	ID              OrderID
	Status          string
	UserID          UserID
	AmountRemaining int
	TotalAmount     int
}

type OrderID

type OrderID = string

type Payment

type Payment struct {
	ID                   PaymentID
	GatewayID            string
	GatewayTransactionID string
	Amount               int
	Currency             Currency
	UserID               UserID
	Consumed             bool
	ConsumedBy           OrderID
}

type PaymentID

type PaymentID = string

type PaymentRegistry

type PaymentRegistry interface {
	StoreOrder(ctx context.Context, order Order) error
	StorePayment(ctx context.Context, payment Payment) error
	FindPayment(ctx context.Context, ids PaymentID) (Payment, error)
	FindOrder(ctx context.Context, id OrderID) (Order, error)
	OrdersByUser(ctx context.Context, id UserID) ([]Order, error)
	PaymentsByUser(ctx context.Context, id UserID) ([]Payment, error)
}

type Product

type Product struct {
	ID        string
	Quantity  int
	UnitPrice int
}

type PurchaseSystem

type PurchaseSystem interface {
	CreateOrder(ctx context.Context, no NewOrder) (OrderID, error)
	ReceivePayment(ctx context.Context, paymentEvet Payment) (PaymentID, error)
	UserHistory(ctx context.Context, userID UserID) ([]Payment, []Order, error)
	Pay(ctx context.Context, orderID OrderID, paymentIDs PaymentID) error
	UpdateTrackingInfo(ctx context.Context, orderID OrderID, update TrackingInfoUpdate) error
	ViewOrder(ctx context.Context, orderID OrderID) (Order, error)
	ViewReceipt(ctx context.Context, orderID OrderID) (Receipt, error)
}

func New

func New(registry PaymentRegistry, newOrderPolicy NewOrderPolicy) PurchaseSystem

type Purchaser

type Purchaser struct {
	ID UserID
}

type Receipt

type Receipt struct{}

type Recipient

type Recipient struct{}

type TrackingInfoUpdate

type TrackingInfoUpdate struct{}

type UserID

type UserID = string

Directories

Path Synopsis
purchase_registries

Jump to

Keyboard shortcuts

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