holds.go201 lines, 5.9 KB
| 1 | package wallet |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 9 | "github.com/fiston-user/payments/internal/wallet/walletdb" |
| 10 | "github.com/fiston-user/payments/internal/wallet/walletevents" |
| 11 | "github.com/google/uuid" |
| 12 | "github.com/jackc/pgx/v5" |
| 13 | ) |
| 14 | |
| 15 | // HoldsAccount is the system account where held money waits. |
| 16 | const HoldsAccount = "holds" |
| 17 | |
| 18 | type ( |
| 19 | Hold = walletdb.Hold |
| 20 | HoldStatus = walletdb.HoldStatus |
| 21 | ) |
| 22 | |
| 23 | const ( |
| 24 | HoldHeld = walletdb.HoldStatusHeld |
| 25 | HoldCaptured = walletdb.HoldStatusCaptured |
| 26 | HoldReleased = walletdb.HoldStatusReleased |
| 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | ErrWalletFrozen = apperr.NewRule("wallet_frozen", "wallet is frozen") |
| 31 | ErrCurrencyMismatch = apperr.NewRule("currency_mismatch", "wallets have different currencies") |
| 32 | ErrHoldSettled = apperr.NewRule("hold_settled", "hold was already captured or released") |
| 33 | ErrHoldNotFound = fmt.Errorf("hold %w", apperr.ErrNotFound) |
| 34 | ) |
| 35 | |
| 36 | // Hold sets money aside from a wallet, as hold id. Calling it again is safe. |
| 37 | func (s *Service) Hold(ctx context.Context, id, walletID uuid.UUID, amount Money) (Hold, error) { |
| 38 | var v apperr.ValidationError |
| 39 | checkAmount(&v, amount.Amount) |
| 40 | w, err := s.Wallet(ctx, walletID) |
| 41 | if err != nil { |
| 42 | return Hold{}, err |
| 43 | } |
| 44 | if amount.Currency != w.Currency { |
| 45 | v.Add("currency", "must match the wallet's currency, "+w.Currency) |
| 46 | } |
| 47 | if err := v.Err(); err != nil { |
| 48 | return Hold{}, err |
| 49 | } |
| 50 | if w.Frozen { |
| 51 | return Hold{}, fmt.Errorf("wallet %s: %w", w.ID, ErrWalletFrozen) |
| 52 | } |
| 53 | return s.store.PlaceHold(ctx, id, w, amount.Amount) |
| 54 | } |
| 55 | |
| 56 | // Capture pays hold id into anyone's wallet. |
| 57 | func (s *Service) Capture(ctx context.Context, id, to uuid.UUID) (Hold, error) { |
| 58 | dst, err := (&Service{store: s.store}).Wallet(ctx, to) |
| 59 | if err != nil { |
| 60 | return Hold{}, err |
| 61 | } |
| 62 | return s.store.SettleHold(ctx, id, &dst) |
| 63 | } |
| 64 | |
| 65 | // Release gives hold id back to the wallet it came from. |
| 66 | func (s *Service) Release(ctx context.Context, id uuid.UUID) (Hold, error) { |
| 67 | return s.store.SettleHold(ctx, id, nil) |
| 68 | } |
| 69 | |
| 70 | // PlaceHold moves amount from w into the holds account, as hold id, once. |
| 71 | func (s *Store) PlaceHold(ctx context.Context, id uuid.UUID, w Account, amount int64) (Hold, error) { |
| 72 | var h Hold |
| 73 | err := s.inTx(ctx, func(q *walletdb.Queries) error { |
| 74 | if _, err := q.LockAccount(ctx, w.ID); err != nil { |
| 75 | return fmt.Errorf("lock account %s: %w", w.ID, err) |
| 76 | } |
| 77 | var err error |
| 78 | h, err = q.GetHold(ctx, id) |
| 79 | if err == nil { |
| 80 | if h.WalletID != w.ID || h.Amount != amount { |
| 81 | return fmt.Errorf("%w: hold %s has different terms", apperr.ErrConflict, id) |
| 82 | } |
| 83 | return nil // placed before: nothing to do |
| 84 | } |
| 85 | if !errors.Is(err, pgx.ErrNoRows) { |
| 86 | return fmt.Errorf("get hold %s: %w", id, err) |
| 87 | } |
| 88 | b, err := q.GetAccountBalance(ctx, w.ID) |
| 89 | if err != nil { |
| 90 | return fmt.Errorf("get balance %s: %w", w.ID, err) |
| 91 | } |
| 92 | if b.Balance < amount { |
| 93 | return fmt.Errorf("wallet %s: %w", w.ID, ErrInsufficientFunds) |
| 94 | } |
| 95 | |
| 96 | holds, err := holdsAccount(ctx, q, w.Currency) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | posted, err := insertTransaction(ctx, q, NewTransaction{ |
| 101 | Description: "hold", |
| 102 | Postings: []Posting{ |
| 103 | {AccountID: w.ID, Direction: Debit, Amount: amount, Currency: w.Currency}, |
| 104 | {AccountID: holds.ID, Direction: Credit, Amount: amount, Currency: w.Currency}, |
| 105 | }, |
| 106 | }) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | h, err = q.CreateHold(ctx, walletdb.CreateHoldParams{ |
| 111 | ID: id, WalletID: w.ID, Amount: amount, Currency: w.Currency, HoldTxID: posted.ID, |
| 112 | }) |
| 113 | return err |
| 114 | }) |
| 115 | if err != nil { |
| 116 | return Hold{}, fmt.Errorf("place hold: %w", err) |
| 117 | } |
| 118 | return h, nil |
| 119 | } |
| 120 | |
| 121 | // SettleHold pays hold id into wallet to, or back to its own if to is nil. |
| 122 | func (s *Store) SettleHold(ctx context.Context, id uuid.UUID, to *Account) (Hold, error) { |
| 123 | status := HoldReleased |
| 124 | if to != nil { |
| 125 | status = HoldCaptured |
| 126 | } |
| 127 | var h Hold |
| 128 | err := s.inTx(ctx, func(q *walletdb.Queries) error { |
| 129 | var err error |
| 130 | h, err = q.LockHold(ctx, id) |
| 131 | if errors.Is(err, pgx.ErrNoRows) { |
| 132 | return ErrHoldNotFound |
| 133 | } |
| 134 | if err != nil { |
| 135 | return fmt.Errorf("lock hold %s: %w", id, err) |
| 136 | } |
| 137 | if h.Status == status && (to == nil || *h.SettledTo == to.ID) { |
| 138 | return nil // settled before, the same way |
| 139 | } |
| 140 | if h.Status != HoldHeld { |
| 141 | return fmt.Errorf("hold %s is %s: %w", id, h.Status, ErrHoldSettled) |
| 142 | } |
| 143 | |
| 144 | src, err := q.GetAccount(ctx, h.WalletID) |
| 145 | if err != nil { |
| 146 | return fmt.Errorf("get account %s: %w", h.WalletID, err) |
| 147 | } |
| 148 | dst := src // a release always works, even into a frozen wallet |
| 149 | if to != nil { |
| 150 | dst = *to |
| 151 | switch { |
| 152 | case dst.Currency != h.Currency: |
| 153 | return fmt.Errorf("wallet %s: %w", dst.ID, ErrCurrencyMismatch) |
| 154 | case dst.Frozen: |
| 155 | return fmt.Errorf("wallet %s: %w", dst.ID, ErrWalletFrozen) |
| 156 | } |
| 157 | } |
| 158 | holds, err := holdsAccount(ctx, q, h.Currency) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | posted, err := insertTransaction(ctx, q, NewTransaction{ |
| 163 | Description: "hold " + string(status), |
| 164 | Postings: []Posting{ |
| 165 | {AccountID: holds.ID, Direction: Debit, Amount: h.Amount, Currency: h.Currency}, |
| 166 | {AccountID: dst.ID, Direction: Credit, Amount: h.Amount, Currency: h.Currency}, |
| 167 | }, |
| 168 | }) |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | h, err = q.SettleHold(ctx, walletdb.SettleHoldParams{ |
| 173 | ID: id, Status: status, SettledTo: &dst.ID, SettleTxID: &posted.ID, |
| 174 | }) |
| 175 | if err != nil || status != HoldCaptured { |
| 176 | return err |
| 177 | } |
| 178 | return enqueue(ctx, q, walletevents.TransferCompleted, src.ID, walletevents.Transfer{ |
| 179 | TransactionID: posted.ID, |
| 180 | From: eventWallet(src), |
| 181 | To: eventWallet(dst), |
| 182 | Amount: h.Amount, |
| 183 | }) |
| 184 | }) |
| 185 | if err != nil { |
| 186 | return Hold{}, fmt.Errorf("settle hold: %w", err) |
| 187 | } |
| 188 | return h, nil |
| 189 | } |
| 190 | |
| 191 | // holdsAccount returns the system account where held money waits. |
| 192 | func holdsAccount(ctx context.Context, q *walletdb.Queries, currency string) (Account, error) { |
| 193 | acc, err := q.GetSystemAccount(ctx, walletdb.GetSystemAccountParams{ |
| 194 | Name: HoldsAccount, |
| 195 | Currency: currency, |
| 196 | }) |
| 197 | if err != nil { |
| 198 | return Account{}, fmt.Errorf("holds account %s: %w", currency, err) |
| 199 | } |
| 200 | return acc, nil |
| 201 | } |