humanblnk tutorial

Chapter 14 · Running it

Cheat sheet

Everything on one page. Import github.com/otyang/humanblnk; every type below is humanblnk.X. Build one *humanblnk.Client at start-up with humanblnk.New(client, humanblnk.WithAccountLedger(id), humanblnk.WithCustomerLedgers(ids...)) (chapter 1).

Services

ServiceMethods
hb.IdentitiesCreate(ctx, meta) (string, error), Get(ctx, id), UpdateMeta(ctx, id, meta), FindByCustomer(ctx, customerID)
hb.AccountsOpenForCustomer(ctx, CustomerAccount), CreateInternal(ctx, NewAccount), Get(ctx, id), System(ctx, humanblnk.FeeIncome, usd), List(ctx, AccountQuery) ([]Account, total, error)
hb.TransactionsRecord(ctx, Transaction), RecordAll(ctx, []Transaction), Preview(ctx, Transaction), Reverse(ctx, Reversal), Get(ctx, id), GetByReference(ctx, ref), List(ctx, EntryQuery) ([]Entry, total, error)
hb.HoldsPlace(ctx, Transaction, expires) (HoldID, error), Capture(ctx, HoldID, amount), Cancel(ctx, HoldID)
hb.WatchesCreate(ctx, Watch), Get(ctx, WatchID), List(ctx, account), Change(ctx, WatchID, Watch), Delete(ctx, WatchID)
hb.ReconciliationAddRule, ChangeRule, RemoveRule, Upload(ctx, source, filename, r), Run(ctx, StatementID, Run), RunLines(ctx, []StatementLine, Run), Result(ctx, RunID)
hb.LedgersCreate(ctx, name, meta), Get(ctx, id), List(ctx, limit, offset), Rename(ctx, id, name)
package functionhumanblnk.VerifyWebhook(secret, r, tolerance) (Event, error)

Naming accounts

HelperGivesFor
humanblnk.AccountRef("bln_…")that accounta customer or internal account, by id
humanblnk.World@Worldmoney outside your books: deposits come from it, payouts go to it
humanblnk.FeeIncome@FeeIncomefees you earned
humanblnk.Adjustments@Adjustmentsstaff corrections
humanblnk.ClearingAccount("paystack")@PaystackClearingmoney a provider owes you, or you owe it
humanblnk.SystemAccount("FXDesk")@FXDeskany other system account; keep it in a variable

Building requests

Helper or type
humanblnk.Reference("payout", "po_123")"payout:po_123": build it from your own ids
humanblnk.Single(account, amount)one posting
humanblnk.Many(humanblnk.Posting{…}, …)several postings, for one side of a split
TransactionRef, From, To, Description, Meta
ReversalRef, Entry (an entry's id, or a captured hold's), Amount, Reason
CustomerAccountOwner, Currency, Ledger
NewAccountLedger, Owner, Currency, Meta
WatchAccount, Field, Op, Threshold, Description

What comes back

TypeFields
AccountID, Ledger, Owner, Currency, Total, Available, OutgoingHeld, IncomingHeld, Meta, CreatedAt
EntryID, Ref, Status, Amount, From, To, Description, Meta, CreatedAt
PreviewWouldApply, Rejection, Changes []AccountChange, Notes
IdentityID, Meta, CreatedAt
LedgerID, Name, Meta, CreatedAt
Watchwhat you sent, plus ID, CreatedAt
MatchRulewhat you sent, plus ID, CreatedAt
RunResultID, Statement, Status, Matched, Unmatched, StartedAt, CompletedAt
EventKind, Entry, Watch, Account, Data

Entry statuses: StatusApplied (money moved), StatusHeld (a hold), StatusVoid (a released hold), StatusRejected (refused or rolled back), StatusQueued.

Ids

HoldID, WatchID, StatementID, RuleID and RunID are strings; convert an id you stored, as in humanblnk.HoldID(s). Owner (idt_…) and ledger (ldg_…) ids are plain strings.

Money

Call
currency.Get(code), currency.Lookup(code), currency.MustGet(code)a currency: typed, stored, constant
money.Parse(s, c)an exact amount
m.Add, m.Sub, m.Cmp, m.Equalone currency only
m.Percent(p, mode), money.Round(d, c, mode)with money.Up, money.Down or money.HalfEven
m.Minor(), money.FromMinor(n, c)to and from the smallest unit

Testing

Import github.com/otyang/humanblnk/humanblnktest (chapter 13).

Call
hb := humanblnktest.New(t, opts...)*Books, embedding a *humanblnk.Client (hb.Client) on a fresh in-memory Blnk, closed when t ends
hb.Customer(t, "USD")an owner and their wallet: its AccountRef
hb.Fund(t, account, "100.00")a deposit from @World
hb.Available(t, account)what it can spend now, such as "70.00"
humanblnktest.Money(t, "30.00", "USD")an amount, failing the test if it can't parse