Billing Spend Interface — Overview
This work gives every product domain one public way to spend credits, through Billing::SpendService and Billing::SpendValidator.
- Ads calls only the two public classes, and Billing reads no product table.
- Careers job posts and Gig shifts use the same two classes from their first pull request.
- A new product spends credits through the same two classes, with no change to Billing code.
The rules of this work are in Billing D18 — Product domains spend credits only through SpendService.
- The interface itself is on Billing interface for product domains.
- This folder is the work record for building it.
Words used in this doc
| Word | Meaning |
|---|---|
| Product domain | A domain that sells a service that an employer pays for with credits, such as Ads, Careers or Gig. |
| Deliverable | The product record that one spend pays for, such as one Ads::CampaignPlacement. |
| Own account | The Billing::Account whose billing_accounts.org_company_id is the company. |
| Public class | A class that code in another domain may call. |
| Private class | A class that only code inside its own domain may call. |
| Deliverable route | A Team Portal web route that loads one deliverable and redirects to the page that shows it, such as /team/ads/campaign-placements/:id. |
Problem
A product that sells for credits must not learn the private classes of Billing.
- Billing must not learn each product either.
- Careers charges placement credits for a job post, and Gig shifts spend gig credits after that.
- Without one public way in, each product copies the Ads code and adds product code inside Billing.
- So a product spends credits through two Billing classes, and Billing does not change.
User stories
- US-1: As a Careers engineer, I want one Billing class to call, so I never touch lots or accounts.
- US-2: As the CTO, I want Billing to read no product table, so an Ads change cannot break Billing.
- US-3: As an HQ manager, I want all form errors in one response, so I fix them at once.
- US-4: As Finance, I want each spend to say what it bought, so a customer can check their statement.
- US-5: As Sales, I want each ledger spend to link to its campaign or job, so I answer customers.
- US-6: As a code reviewer, I want one search to find a boundary break, so I catch it in review.
The work at a glance
The diagrams below show the whole design, so a reader can regain the context without the other pages.
- Each diagram follows Acme Pte Ltd and its campaign "Hiring baristas", campaign 42.
- The rules and the code behind each diagram are on Billing interface for product domains.
Who calls whom
Every product calls only the two public classes, and Billing reads no product table.
Legend: yellow = the public Billing classes, lavender = private to Billing, grey = product domains.
- The dashed box holds the only Billing classes that product code may call to spend credits.
Billing::SpendServiceandBilling::SpendValidatorread the own account asorg_company.billing_account.- When
org_company.billing_accountisnil, both raiseErrors::InternalServerError, because every company is created with its own account.
- When
Billing::SpendServicewrites the description of the product on each ledger entry.- The Team Portal campaign page reads its Billing data from Billing endpoints, so
Ads::Campaigns::TeamShowManagercalls no Billing class.
One spend, and where its error goes
An API request gets its credit error back as a 422, from the validator or from the service.
- The validator reads the balance with no lock, so the employer gets every form error in one 422.
- The service reads the balance again under the lock, so a race can never spend more credits than Acme has.
ApplicationControllerturns every Billing error into the standard error object, with no rescue in Ads.Billing::InsufficientCreditsErroris a subclass ofBilling::Error, and itscodeisnot_enough_credits.- For now
Billing::Erroris a subclass ofErrors::UnprocessableContentError, so the request ends with a 422.
- For now
A job or a scheduler has no controller, so it catches the error itself and reports it to Sentry.
- One failed record rolls back alone, and the job moves to the next record.
- The job rescues
StandardErrorfor each record, and reports it with the record id. - The service check protects the job, even when the job runs no
Billing::SpendValidator.
The life of the credits of one deliverable
Each credit of one deliverable moves between four states, and each move writes one ledger entry.
| Move | Method | Who calls it | entry_type |
|---|---|---|---|
| Available to reserved | reserve | The Ads submit managers | :reserve |
| Reserved to consumed | consume_reserved | Ads::CampaignPlacements::ConsumeReservedCreditsJob, every hour | :consume |
| Reserved to available | release | The Ads cancel managers and Ads::Campaigns::RejectService | :release |
| Available to consumed | consume_now | Careers job posts, after this work | :consume |
| Available to expired | No method of Billing::SpendService, because Billing expires credits itself | release, for the credits it returns to an expired EntitlementLotBilling::ExpireEntitlementLotsJob, every hour | :expire |
- A released credit goes back to available first, and expires at once when its
EntitlementLothas expired.- So that cancel writes two ledger entries: one
releaseentry, then oneexpireentry.
- So that cancel writes two ledger entries: one
- This work builds
consume_nowand its tests in jodapp-api#2216, and Careers is its first caller. - A grant comes from an invoice or a credit action, and never from a product.
The source of one ledger entry, and the link that Sales clicks
Each spend ledger entry names its deliverable as its source, and the Team Portal ledger links it to a page.
Legend: solid arrow = data, dotted arrow = a click, lavender = ledger entries, grey = Ads and Team Portal.
- Billing finds the credits of placement 77 by its source, so
releasenever touches placement 78. - The ledger row shows
billing_ledger_entries.descriptionas the link text, and links to the deliverable route of placement 77. - The route loads placement 77 from
GET /team/ads/campaign_placements/77, and redirects to the page of campaign 42. - Billing stores no page record, because which page shows a deliverable is a web decision.
Scope
| In this work | Not in this work |
|---|---|
The public classes Billing::SpendService and Billing::SpendValidator | Careers job posts, which build on top of this work |
The error classes Billing::Error and Billing::InsufficientCreditsError | Gig shifts and outlet budgets, which a later phase builds |
The column billing_ledger_entries.description, which is never empty | A seat check for Workforce, because how Workforce reads its seats is not designed yet |
| Every Ads caller uses the public classes | The statement of account that an employer downloads in the Employer Dashboard |
| The daily consume job of Ads campaigns lives in Ads | Separate Xero revenue lines per product, which Finance does not need |
| The Team Portal campaign page reads its Billing data from Billing endpoints | |
| The Team Portal ledger links each spend to its deliverable route, with the description as the link text | |
A search that finds a boundary break, and the rule in app/domains/AGENTS.md |
What does not change
- The lots and the balances keep every column they have, and the ledger gains only
billing_ledger_entries.description. - The two spending patterns of Billing D5 — Spending mechanics: consume now, or reserve first stay.
- The product's write and the spend run in one database transaction, which the product opens.
- The rule of Billing D16 — A company spends only its own billing account stays.
- The Ads daily formula stays in Ads, and Billing subtracts the units already consumed.
- The employer balance endpoint
GET /employers/billing/entitlement-balancesstays the same.
Pages in this folder
- Billing Spend Interface — Design Plan: the rules, the design, the code and the rollout.
- Billing Spend Interface — Use Cases: every flow step by step, with an Acme worked example.
- Billing Spend Interface — Implementation Plan: the issues for
jodapp-apiandjodapp-web, in build order, and a manual test plan.