allowances: recognise YEARLY contributions so a yearly payer isn't read as unpaid

_payment_verdict treated MONTHLY as the only recurring frequency, so a live
ACTIVE YEARLY contribution — one charge, current for twelve cycles — fell
through to "no recurring (monthly) contribution" and would have been reminded
and (with deactivation on) deactivated for eleven of those months.

Make the verdict frequency-aware: only a MONTHLY or YEARLY order can cover an
allowance cycle; an ACTIVE YEARLY covers while its window (last charge + 1yr,
or nextChargeDate) includes now; PROCESSING stays paid-pending; a ONE-TIME
payment is credit, never an allowance period and never a mask. A failed
monthly charge still takes the remind path.

Dormant on today's data: inference-cooperative has 0 yearly orders (104 orders:
88 MONTHLY, 16 ONETIME), verified per member ACCOUNT because a member's own
yearly order does not appear in the collective-level order list. This changes
no current member's decision — the report is identical before and after.

Adds tests/test_payment_verdict.py (yearly branch + monthly/processing/onetime/
error/cancelled cases), runnable in the app container.
This commit is contained in:
inference-bot committed 2026-11-04 17:29:47 +00:00
1 parent 86e77a0ad4
commit a984a443e1
2 files changed
+157 -12

No files matched your search

+63 -12
View File
@@ -4176,30 +4176,81 @@ async def fetch_member_orders() -> dict[str, list[dict]]:
return by_slug
def _payment_verdict(orders: list[dict], cycle_start: datetime) -> tuple[bool, str]:
def _payment_verdict(orders: list[dict], cycle_start: datetime,
now: datetime | None = None) -> tuple[bool, str]:
"""Did this member's recurring contribution pay for the current cycle?
Returns (paid, evidence). A PROCESSING order counts as PAID (a payment in
flight is not a missed payment). A live ACTIVE recurring contribution is
treated as paid even if its next charge is still ahead — otherwise a member
whose charge falls due tomorrow reads as unpaid on every sweep.
Frequency-aware. Only a RECURRING contribution (MONTHLY or YEARLY) can cover
an allowance cycle; a ONE-TIME payment is credit, not an allowance period, so
it is never treated as covering a month and never masks an unpaid member.
Returns (paid, evidence).
Rules, in order:
* PROCESSING — a payment is in flight; treated as PAID (a payment mid-flight
is not a failure), so a PROCESSING order never triggers a reminder.
* ACTIVE MONTHLY — a live subscription; treated as paid even if its next
charge is still ahead, so a member whose charge falls due tomorrow does
not read as unpaid on every sweep.
* ACTIVE YEARLY — paid only while its coverage window contains the current
cycle: a year from its last charge, or until its nextChargeDate. A yearly
contributor paid once and is current for twelve cycles; a flat monthly
test would read them as unpaid (and, with deactivation on, deactivate
them) for eleven of those months.
* otherwise — judge on the most recent RECURRING charge: within the cycle
(7-day slack) is paid, before it is the remind path.
DORMANT ON TODAY'S DATA: as of 2026-11-04 the collective has no yearly orders
(104 orders: 88 MONTHLY, 16 ONETIME, 0 YEARLY — verified per member account,
since a member's own yearly order does not appear in the collective-level
order list). The yearly branch is defensive robustness for a tier we may add
later; it is not a fix for any current member. `now` is injectable for tests.
"""
monthly = [o for o in orders if (o.get("frequency") or "").upper() == "MONTHLY"]
if not monthly:
now = now or datetime.now(timezone.utc)
def _freq(o: dict) -> str:
return (o.get("frequency") or "").upper()
def _status(o: dict) -> str:
return (o.get("status") or "").upper()
recurring = [o for o in orders if _freq(o) in ("MONTHLY", "YEARLY")]
if not recurring:
return False, "no recurring (monthly) contribution on Open Collective"
live = [o for o in monthly if (o.get("status") or "").upper() in ("ACTIVE", "PROCESSING")]
if live:
charges = [c for c in (_parse_oc_dt(o.get("lastChargedAt")) for o in live) if c]
live = [o for o in recurring if _status(o) in ("ACTIVE", "PROCESSING")]
# An ACTIVE monthly subscription, or any PROCESSING order, is a live
# contribution that covers the cycle (evidence string kept stable so a
# report is comparable run to run).
live_monthly = [o for o in live if _freq(o) == "MONTHLY" or _status(o) == "PROCESSING"]
if live_monthly:
charges = [c for c in (_parse_oc_dt(o.get("lastChargedAt")) for o in live_monthly) if c]
last = max(charges).date().isoformat() if charges else "unknown"
return True, f"live recurring contribution (last charged {last})"
charged = [(_parse_oc_dt(o.get("lastChargedAt")), o.get("status")) for o in monthly]
# An ACTIVE yearly subscription covers the cycle only while its coverage
# window (last charge + 1 year, or up to nextChargeDate) still includes now.
for o in live:
if _freq(o) != "YEARLY":
continue
lc = _parse_oc_dt(o.get("lastChargedAt"))
nc = _parse_oc_dt(o.get("nextChargeDate"))
# Coverage runs to the later of (next charge date) and (last charge + 1yr).
covered_until = nc
if lc:
year_end = lc + timedelta(days=365)
if covered_until is None or year_end > covered_until:
covered_until = year_end
if covered_until and now < covered_until:
return True, (f"live yearly contribution covers this cycle "
f"(last charged {lc.date().isoformat() if lc else 'unknown'}, "
f"covers until {covered_until.date().isoformat()})")
charged = [(_parse_oc_dt(o.get("lastChargedAt")), _status(o)) for o in recurring]
charged = [(c, s) for c, s in charged if c]
if charged:
last_dt, _ = max(charged, key=lambda t: t[0])
if last_dt >= cycle_start - timedelta(days=7):
return True, f"charged {last_dt.date().isoformat()} within the current cycle"
return False, f"last charge {last_dt.date().isoformat()} is before this cycle"
statuses = ",".join(sorted({(o.get("status") or "?").upper() for o in monthly}))
statuses = ",".join(sorted({_status(o) or "?" for o in recurring}))
return False, f"no live recurring contribution (order status: {statuses})"