Skip to main content

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.

Words used in this doc​

WordMeaning
Product domainA domain that sells a service that an employer pays for with credits, such as Ads, Careers or Gig.
DeliverableThe product record that one spend pays for, such as one Ads::CampaignPlacement.
Own accountThe Billing::Account whose billing_accounts.org_company_id is the company.
Public classA class that code in another domain may call.
Private classA class that only code inside its own domain may call.
Deliverable routeA 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.

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::SpendService and Billing::SpendValidator read the own account as org_company.billing_account.
    • When org_company.billing_account is nil, both raise Errors::InternalServerError, because every company is created with its own account.
  • Billing::SpendService writes the description of the product on each ledger entry.
  • The Team Portal campaign page reads its Billing data from Billing endpoints, so Ads::Campaigns::TeamShowManager calls 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.
  • ApplicationController turns every Billing error into the standard error object, with no rescue in Ads.
  • Billing::InsufficientCreditsError is a subclass of Billing::Error, and its code is not_enough_credits.
    • For now Billing::Error is a subclass of Errors::UnprocessableContentError, so the request ends with a 422.

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 StandardError for 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.

MoveMethodWho calls itentry_type
Available to reservedreserveThe Ads submit managers:reserve
Reserved to consumedconsume_reservedAds::CampaignPlacements::ConsumeReservedCreditsJob, every hour:consume
Reserved to availablereleaseThe Ads cancel managers and Ads::Campaigns::RejectService:release
Available to consumedconsume_nowCareers job posts, after this work:consume
Available to expiredNo method of Billing::SpendService, because Billing expires credits itselfrelease, for the credits it returns to an expired EntitlementLot
Billing::ExpireEntitlementLotsJob, every hour
:expire
  • A released credit goes back to available first, and expires at once when its EntitlementLot has expired.
    • So that cancel writes two ledger entries: one release entry, then one expire entry.
  • This work builds consume_now and 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 release never touches placement 78.
  • The ledger row shows billing_ledger_entries.description as 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 workNot in this work
The public classes Billing::SpendService and Billing::SpendValidatorCareers job posts, which build on top of this work
The error classes Billing::Error and Billing::InsufficientCreditsErrorGig shifts and outlet budgets, which a later phase builds
The column billing_ledger_entries.description, which is never emptyA seat check for Workforce, because how Workforce reads its seats is not designed yet
Every Ads caller uses the public classesThe statement of account that an employer downloads in the Employer Dashboard
The daily consume job of Ads campaigns lives in AdsSeparate 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​

Pages in this folder​

  1. Billing Spend Interface — Design Plan: the rules, the design, the code and the rollout.
  2. Billing Spend Interface — Use Cases: every flow step by step, with an Acme worked example.
  3. Billing Spend Interface — Implementation Plan: the issues for jodapp-api and jodapp-web, in build order, and a manual test plan.