Store a card's intro APR expiry and step-up rate

JA
2d ago
+792-23

Fixes jaysbeekay/sure#904
Upstream issue: we-promise/sure#5445
Upstream PR: https://github.com/we-promise/sure/pull/5993 (we-promise/sure#5993, draft, position 8 of the native stack https://github.com/we-promise/sure/stack/5995); same branch, same commits.

Stack: base up/card-minimum-percent (jaysbeekay/sure#1024) ← jaysbeekay/sure#1023 ← jaysbeekay/sure#1022 ← jaysbeekay/sure#1021 ← jaysbeekay/sure#1020 ← jaysbeekay/sure#1006. The daily-rate slice (jaysbeekay/sure#1017) now sits below this one. Its clash with this PR is resolved in 0908daf88: interest is charged per day at that day's effective_apr, and daily_rate(on:) is effective_apr(on:) over the basis days, so the Overview's daily rate follows the intro step.

Summary

A card can now record an introductory rate as one dated step. The apr column holds the intro rate, labelled Intro rate on the form while an end date is stored. Intro rate ends on is when it stops, and Rate after the intro applies from that date. Every APR reader uses the rate in force on its own date, so interest no longer runs on at the teaser rate past the expiry. Clearing the end date makes the rate after the intro the card's APR. Per the revised plan, the chart step moved to the payoff projection (jaysbeekay/sure#903).

What changed

  • CreditCard#effective_apr(on:): apr before intro_apr_expires_on, after_intro_apr from that date itself.
  • Validation both ways, with named messages (config/locales/models/credit_card/en.yml): an expiry needs a step-up rate, a step-up rate needs an expiry, and an intro needs an apr; the step-up rate is ≥ 0.
  • before_validation :end_intro_rate: a save that clears intro_apr_expires_on while a rate after the intro is set moves that rate into apr and clears after_intro_apr, so an ended intro can be removed without falling back to the intro rate. Every other save stores the rates as typed. The intro days of an open cycle keep the intro rate after the expiry (a new rate after the intro saved on 1 Oct still gives 7.40 for the 11 Sep–10 Oct cycle; re-pricing those days would give 14.79).
  • APR readers (grep of apr in app/ — every one is listed):
    • cycle_interest_money (#1006): each day charged at that day's effective_apr (cycle_balance_days → cycle_rated_balance_days), still summed then rounded once. #1006's comment "the current APR applies to the whole cycle" is updated.

    • CreditCard#daily_rate(on:) (from jaysbeekay/sure#1017) and StatementSummary#daily_rate: effective_apr(on:) over the basis days, so the Overview's daily rate is the one charged on as_of.

    • Overview APR tile: effective_apr(on: today) and "Until (n days), then " while the intro runs; show_apr still keys on apr.

    • GetAccounts#credit_card_terms: apr is the rate on as_of_date; after_intro_apr and intro_apr_expires_on added.

    • PlaidAccount::Liabilities::CreditProcessor#write_apr, through enrich_attribute(..., source: "plaid"):

      • while an intro runs, Plaid's APR is skipped, so the intro rate stays;
      • once the intro has ended, Plaid's APR is written to after_intro_apr, so the days before the expiry keep the intro rate;
      • with no intro, it overwrites apr (ignore_locks: true), as main does for every Plaid card field;
      • the APR is the aprs entry whose apr_type is purchase_apr, or the first entry when none is;
      • a nil APR changes nothing;
      • the card is restored and its errors are cleared before the write, so a refused minimum or cycle update cannot stop it, and an APR the model refuses is logged against the Plaid connection ("Plaid card APR refused by the model").

      Plaid reports no expiry, so the expiry has no provider writer.

    • Left alone: RedbarkAccount::CreditCardDetailsProcessor (already fills only a blank, unlocked APR); the form/controller/importer (they carry the column).

  • Form: the end date and "Rate after the intro", with a hint that ends "Clear the end date to make the rate after the intro your APR.", before the interest basis. The apr field is labelled "Intro rate" when an end date is stored (it follows the stored date, so a refused submit does not relabel it) and "APR" otherwise. Every field stays editable. The fields are permitted and carried by the importer.
  • GetAccounts description: apr is the rate in force on the date asked, and it says what after_intro_apr and intro_apr_expires_on mean.

Tests (red on #1024: 9 errors unknown attribute 'after_intro_apr' / undefined method 'effective_apr', 1 failure no form input)

test/models/credit_card_test.rb

  • expiry 5 Jan 2027: 4 Jan → 0, 5 Jan → 22.9, 6 Jan → 22.9; no intro → 18.99 on any date
  • expiry without rate and rate without expiry refused with the named messages; both set valid
  • 1,000 owed over 11 Sep–10 Oct, 0% until 26 Sep then 18% → 7.40 (all-0% gives 0, all-18% 14.79)
  • summary daily rate 0 on 20 Sep, 18/365 on 1 Oct

accounts_controller_test.rb: on 6 Dec the tile shows 0.000% and "Until January 05, 2027 (30 days), then 22.900%"; on 5 Jan it shows 22.900% and no intro line.
get_accounts_test.rb: apr 0 with after_intro_apr 22.9 and the date; on the expiry date apr 22.9.
plaid_account/liabilities/credit_processor_test.rb: "keeps the intro APR while the intro rate runs" (0 survives a sync reporting 15; minimum still updated).
credit_cards_controller_test.rb, data_importer_test.rb: form saves both; round trip.

Added after the Gatekeeper review (each failed first as noted, or is a guard caught by a mutation):

  • credit_card_test.rb:
    • "an intro rate needs an APR";
    • "the step-up rate cannot be negative" (−0.01 refused, 0 valid);
    • "clearing the intro's end date makes the rate after the intro the APR" (before: RecordInvalid) and its guard "clearing the intro's end date and its rate after keeps the APR";
    • "after the intro has ended, a new APR is saved as the intro rate" (before: 0.0, expected 1.9);
    • "a new APR while the intro runs keeps the intro rate", "after the intro has ended, a save that leaves the APR alone keeps the intro rate", "an APR saved with its intro fields keeps them after the expiry";
    • "a new rate after the intro saved after the expiry keeps the cycle's intro days at the intro rate" (7.40).
  • credit_cards_controller_test.rb:
    • "the edit form offers the intro rate and saves it" now sends apr: 0 and asserts the APR is locked, which ties a form save to the lock;
    • "clearing the intro's end date in the form makes the rate after the intro the APR" (before: 422);
    • "after the intro has ended, the edit form still edits the intro rate and the rate after it";
    • "an intro rate entered after the intro has ended is saved as given";
    • "a card without an intro rate edits its APR";
    • "a refused first intro rate re-renders the form with the APR label".
  • credit_processor_test.rb:
    • "keeps the APR when Plaid sends none";
    • "after the intro has ended, takes Plaid's APR as the rate after the intro" (before: 0.0, expected 24.9);
    • "overwrites a locked APR when no intro rate runs, as for the other Plaid card fields" (before: 15, expected 27.9);
    • "takes the purchase APR from Plaid's list" and "takes Plaid's first APR when none is a purchase APR";
    • "an APR the model refuses is logged against the Plaid connection";
    • "an unchanged APR is not logged as refused after another attribute failed";
    • "Plaid's APR saves on its own after the other card fields are refused" (before: a numeric overflow raised out of the sync).
  • get_accounts_test.rb: "describes a card's APR in force and its intro rate keys".

Mutations (each alone)

#MutationCaught by
M1step-up the day after expiryeffective APR, cycle interest
M2cycle interest at the column APRcycle interest 7.40
M3daily rate from the columnsummary daily rate
M4expiry without rate acceptedvalidation
M5rate without expiry acceptedvalidation
M6tile reads the columnOverview
M7intro line after expiryOverview
M8GetAccounts reads the columnGetAccounts
M9Plaid writes the APR while the intro runs"keeps the intro APR while the intro rate runs"
M10Plaid APR not written2 Plaid tests
M11not permittedform test
M12dropped from importerround trip
M13unless apr.nil? removedkeeps-when-none
M14accountable.lock_saved_attributes! droppedform lock assertion
M15step-up >= 0 droppedcannot-be-negative
M16apr not required with an introintro-needs-an-APR
M17end_intro_rate without the nil guard, or without moving/clearing the ratethe clearing tests
M18end_intro_rate fires on any expiry change, or whenever the expiry is nilform intro test and 7 others; validation and Plaid log tests
M19Plaid always writes aprafter-the-intro Plaid test
M20restore_attributes dropped before the writesaves-on-its-own test
M21label always "APR", or from the submitted datethe two label tests

Verification

  • Full unit suite (beastpc) on fb5418e78 (262d65aae with the updated lower slices merged): 19642 runs, 0 failures, 8 errors (8 ReleaseNotesWorkflowTest errors, no node). Targeted card, Plaid, importer, assistant and Redbark files: 652 runs, 0 failures, 0 errors.
  • System accounts_test (it fills "APR" on card creation): 12 runs, 0 failures.
  • bin/rubocop: no offenses. erb_lint (2 files): no errors. bin/brakeman: 0 warnings.

Screenshots

Same seeded card with APR 0% (the intro rate). Before (#1024): nothing says the 0% ends. After: 24.9% from 40 days out, shown on the APR tile. Before = #1024.

ViewBeforeAfter
Overview — light, desktopbeforeafter
Overview — light, phonebeforeafter
Overview — dark, desktopbeforeafter
Overview — dark, phonebeforeafter
Edit form — light, desktopbeforeafter
Edit form — light, phonebeforeafter
Edit form — dark, desktopbeforeafter
Edit form — dark, phonebeforeafter

After the Gatekeeper review fixes (262d65aae): the edit form for a card whose intro ended 20 days ago (intro 0%, rate after 24.9%). Both rates are labelled and editable, and the hint says how to clear the intro. On the reviewed head (d2e59ca39) the same two fields were labelled "APR" and "APR after the intro rate".

ViewAfter
Edit form, intro ended — light, desktopafter
Edit form, intro ended — light, phoneafter
Edit form, intro ended — dark, desktopafter
Edit form, intro ended — dark, phoneafter

Migration notes

20261128120400_add_intro_apr_to_credit_cards: intro_apr_expires_on date and after_intro_apr decimal(10,2), both nullable; existing rows NULL (no intro, effective_apr = apr).

Blast radius

  • cycle_interest_money → statement_summary.estimated_interest and the helper's wording: a card without intro fields multiplies by the same apr every day, so the sum is unchanged (all #1006 interest tests unchanged).
  • Plaid: a card with no intro is written exactly as before, a hand-edited APR included (main's adapter path ignores locks for every card field). The changes are only for intro cards: the APR is skipped while the intro runs, and after it ends Plaid's APR becomes the rate after the intro. Separately, the purchase APR is picked by type, and a refused APR is logged.
  • end_intro_rate fires only on a save that clears the end date. Family::DataImporter builds new cards only, where the expiry goes nil → nil, so it never fires there.
  • Not changed here: Enrichable#enrich_attributes reads locked? before its transaction, so a lock saved mid-sync can still lose to that sync. The code is shared by every enrich_attribute caller and is left alone here. Plaid's APR path reads no lock: it skips the APR while an intro runs and passes ignore_locks: true otherwise.