diff --git a/app/main.py b/app/main.py index 5aecb69..b86a4de 100644 --- a/app/main.py +++ b/app/main.py @@ -4191,6 +4191,30 @@ async def _process_opencollective_webhook(request: Request, token: str): # calendar (all members landing on Oct 1 otherwise). members = await fetch_members() member = next((m for m in members if m["slug"] == slug), None) + if not member and slug: + # A FIRST-TIME contributor's OC profile is usually not in the + # collective's members list yet when their first order lands (OC + # reports the payer as guest-…/incognito-…). The slug lookup then + # finds nothing, and this branch used to fall straight through — + # SILENTLY. The webhook answered 200, nobody was provisioned, + # nothing was credited and nothing was logged. On 2026-11-13 that + # cost conor.canaday@gmail.com his account and his welcome email + # on a live $5/month membership; only the sweep's + # unmapped-backers check noticed, hours later. + payer = await fetch_account_by_slug(slug) + email = (payer or {}).get("email") + if email: + member = {"email": email, + "name": (payer or {}).get("name") or email.split("@")[0], + "slug": slug} + logger.warning( + "Webhook: membership order from %s is not in the OC members " + "list yet; resolved it to %s by account email", slug, email) + else: + logger.error( + "Webhook: membership order from %s could not be resolved to an " + "email (incognito profile); NOBODY was provisioned or credited — " + "the sweep's unmapped-backers check must report this", slug) if member: # A contribution may arrive under a DIFFERENT email than the # portal account (a invited friend who joined under another OC @@ -7369,6 +7393,42 @@ def payment_reminder_email(name: str, contribute_url: str) -> tuple[str, str, st return subject, text, html +async def fetch_account_by_slug(slug: str) -> dict | None: + """The Open Collective account behind a payer slug, with its email if OC gives it. + + Orders report their payer as `fromAccount { slug }`. For a FIRST-TIME + contributor that account is often not in the collective's members list yet + (OC reports the payer as `guest-…`/`incognito-…`), which is exactly the case + this exists for: it is the only way to learn who paid, and therefore the only + way to provision them. + + OC exposes `email` on the Individual fragment — visible for an ordinary guest + checkout, null for an incognito profile (the contributor hid it on purpose). + Returns None when the account cannot be read; NEVER raises, because its + caller must be able to say "could not resolve" rather than die mid-webhook. + """ + slug = (slug or "").strip() + if not slug or not OC_PERSONAL_TOKEN: + return None + query = ("query($slug: String!) { account(slug: $slug) { __typename id name slug " + "... on Individual { email isIncognito } } }") + try: + async with httpx.AsyncClient(timeout=30.0) as client: + data = await _oc_graphql_post(client, {"query": query, "variables": {"slug": slug}}) + except Exception as e: # read failure: report, never raise + logger.warning("OC account lookup failed for slug %s: %s", slug, e) + return None + acct = ((data or {}).get("data") or {}).get("account") or None + if not acct: + return None + if acct.get("isIncognito"): + # Hidden on purpose: there is no email to provision from, and inventing + # one from the slug would be worse than saying so. + return {**acct, "email": None} + acct["email"] = (acct.get("email") or "").strip().lower() or None + return acct + + async def fetch_member_index() -> dict[str, dict]: """email -> {slug, joined, role, tier} from the live OC member list. diff --git a/tests/test_resolve_payer.py b/tests/test_resolve_payer.py new file mode 100644 index 0000000..363e82e --- /dev/null +++ b/tests/test_resolve_payer.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +"""Regression test: a first-time contributor's order must not be dropped in silence. + +WHY THIS EXISTS + On 2026-11-13 21:59:43 OC delivered `order.processed` for a live $5/month + membership (Conor W, conor.canaday@gmail.com, order or_eU8gRkrPCvFsaYHrgiCfz). + The portal answered 200 and did nothing: the membership branch resolves the + payer with + + members = await fetch_members() + member = next((m for m in members if m["slug"] == slug), None) + + and for a FIRST-TIME contributor the payer's OC account is not in the + collective's members list yet — OC reports them as guest-…/incognito-…. So + `member` was None, the whole branch fell through, and because the payload had + arrived successfully there was no error to record either. No account, no + welcome email, no credit, no log line. The sweep's unmapped-backers check + found it hours later, which is the wrong place to find it. + + The fix resolves the payer from the order's account slug (`Individual.email`, + which OC exposes for an ordinary guest checkout), and when even that yields no + email it says so at ERROR level. A payment we cannot attribute is news, never + a shrug. + + The "silent no-op" shape is the actual defect here: an `if` with no `else` on a + money path, where the absence of an action leaves no trace. +""" +import asyncio +import os +import pathlib +import sys +import tempfile + +ROOT = pathlib.Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT)) +os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "payer-fixture.db") + +import app.main as m # noqa: E402 + +m.DB_PATH = os.environ["DB_PATH"] +m.OC_PERSONAL_TOKEN = "test-token" + +FAILS = [] +def check(cond, msg): + print((" ok " if cond else " FAIL ") + msg) + if not cond: + FAILS.append(msg) + +REAL_POST = m._oc_graphql_post + + +def fake_post(payload): + """Stand in for OC's GraphQL endpoint.""" + async def _post(client, json_body, headers=None, attempts=2): + return payload + return _post + + +print("1. an ordinary guest checkout resolves to the payer's email") +m._oc_graphql_post = fake_post({"data": {"account": { + "__typename": "Individual", "id": "acc_x", "name": "Conor W", + "slug": "guest-71e9d110", "email": "Conor.Canaday@Gmail.com", "isIncognito": False}}}) +acct = asyncio.run(m.fetch_account_by_slug("guest-71e9d110")) +check(acct and acct["email"] == "conor.canaday@gmail.com", + f"the email is returned and normalised (got {acct and acct.get('email')!r})") +check(acct and acct["name"] == "Conor W", "the name comes back too, for the welcome email") + +print("2. an incognito profile yields NO email — and says so rather than guessing") +m._oc_graphql_post = fake_post({"data": {"account": { + "__typename": "Individual", "id": "acc_y", "name": "Incognito", + "slug": "incognito-5ad60182", "email": None, "isIncognito": True}}}) +acct = asyncio.run(m.fetch_account_by_slug("incognito-5ad60182")) +check(acct is not None and acct.get("email") is None, + "incognito: the account is returned, the email is None (a slug is not an address)") + +print("3. failures and edge cases never raise — the caller must be able to report") +m._oc_graphql_post = fake_post({"data": {"account": None}}) +check(asyncio.run(m.fetch_account_by_slug("guest-nope")) is None, + "an account OC does not know returns None") +m._oc_graphql_post = fake_post({}) +check(asyncio.run(m.fetch_account_by_slug("guest-nope")) is None, + "an empty payload returns None") + +def boom(client, json_body, headers=None, attempts=2): + raise RuntimeError("OC read timed out") +m._oc_graphql_post = boom +check(asyncio.run(m.fetch_account_by_slug("guest-x")) is None, + "a failing lookup returns None instead of dying mid-webhook") +check(asyncio.run(m.fetch_account_by_slug("")) is None, "an empty slug is not looked up") +m.OC_PERSONAL_TOKEN = "" +check(asyncio.run(m.fetch_account_by_slug("guest-x")) is None, + "with no OC token configured there is no lookup to make") +m.OC_PERSONAL_TOKEN = "test-token" + +print("4. the webhook branch: the fix is present, and its silent-fallthrough shape is gone") +src = (ROOT / "app" / "main.py").read_text() +branch = src[src.index('if slug and "membership" in tier_name.lower()'):] +branch = branch[:branch.index("elif slug and first_payment")] +check("fetch_account_by_slug(slug)" in branch, + "the branch resolves an unknown payer by account email") +check("logger.error(" in branch and "NOBODY was provisioned or credited" in branch, + "an unresolvable payer is logged at ERROR — not swallowed") +check(branch.count("if member:") == 1 and "logger.warning(" in branch, + "a resolved-by-email payer is announced in the log") + +m._oc_graphql_post = REAL_POST +print() +if FAILS: + print(f"resolve payer: {len(FAILS)} FAILURE(S)") + sys.exit(1) +print("resolve payer: OK — first-time payers resolve, unresolvable ones are loud")