Variable-amount credit pack: grant tip-free contribution amount via public OC order API

This commit is contained in:
inference-bot committed 2026-09-27 22:17:48 -06:00
1 parent 315247e5db
commit 9b1d9edde4
1 file changed
+38 -40
+38 -40
View File
@@ -85,40 +85,40 @@ WEBHOOK_TOKEN = os.environ.get("WEBHOOK_TOKEN", "")
OC_GRAPHQL_URL = "https://opencollective.com/api/graphql/v2" OC_GRAPHQL_URL = "https://opencollective.com/api/graphql/v2"
OC_COLLECTIVE_SLUG = os.environ.get("OC_COLLECTIVE_SLUG", "inference-cooperative") OC_COLLECTIVE_SLUG = os.environ.get("OC_COLLECTIVE_SLUG", "inference-cooperative")
# Cache of TierId -> (face_value_cents, name), fetched from the OC GraphQL # Cache of order idV2 -> tip-free contribution amount in cents, resolved via
# API (tiers scope is allowed on our personal token; orders is not). # OC's PUBLIC GraphQL API (no token needed; our personal token lacks the
_tier_face_cache: dict[int, tuple[int, str]] = {} # orders scope but unauthenticated reads of public orders work).
_order_amount_cache: dict[str, int] = {}
async def get_tier_face(tier_id: int | None) -> tuple[int | None, str]: async def get_order_amount(order_id_v2: str | None) -> int | None:
"""Look up a tier's face value and name by numeric legacyId. """Look up an order's contribution amount (EXCLUDING the platform tip).
Returns (face_cents, name); (None, "") if unknown. The webhook payload The webhook's `totalAmount` includes the buyer's optional OC tip, which
carries only TierId — no name, no face amount — so we resolve via the the co-op never receives — credits must be granted on the tip-free
collective's tiers query and cache the mapping. amount. Returns cents, or None if unresolvable.
""" """
if not tier_id: if not order_id_v2:
return None, "" return None
if tier_id in _tier_face_cache: if order_id_v2 in _order_amount_cache:
return _tier_face_cache[tier_id] return _order_amount_cache[order_id_v2]
try: try:
query = ('{ collective(slug: "%s") { tiers { nodes { legacyId name ' query = ('{ order(order: {id: "%s"}) { amount { valueInCents } '
"amount { valueInCents } } } } }" % OC_COLLECTIVE_SLUG) 'platformTipAmount { valueInCents } } }' % order_id_v2)
async with httpx.AsyncClient(timeout=15.0) as client: async with httpx.AsyncClient(timeout=15.0) as client:
r = await client.post(OC_GRAPHQL_URL, r = await client.post(OC_GRAPHQL_URL,
headers={"Personal-Token": OC_PERSONAL_TOKEN, headers={"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"}, "User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
json={"query": query}) json={"query": query})
for t in r.json().get("data", {}).get("collective", {}).get("tiers", {}).get("nodes", []): o = r.json().get("data", {}).get("order") or {}
lid = t.get("legacyId") cents = (o.get("amount") or {}).get("valueInCents")
if lid is not None: if cents is not None:
_tier_face_cache[int(lid)] = ( _order_amount_cache[order_id_v2] = int(cents)
(t.get("amount") or {}).get("valueInCents") or 0, return int(cents)
t.get("name") or "",
)
except Exception as e: except Exception as e:
logger.warning("Tier face lookup failed for %s: %s", tier_id, e) logger.warning("Order amount lookup failed for %s: %s", order_id_v2, e)
return _tier_face_cache.get(tier_id, (None, "")) return None
# Personal token for the "Inference Co-op Bot" account, which is an admin of # Personal token for the "Inference Co-op Bot" account, which is an admin of
@@ -1334,16 +1334,12 @@ async def opencollective_webhook(request: Request, token: str):
# OC but is NOT a membership — don't provision it. # OC but is NOT a membership — don't provision it.
# #
# Tier detection: the webhook payload carries only `data.order.TierId` # Tier detection: the webhook payload carries only `data.order.TierId`
# (numeric) — NO tier name or face amount. Resolve both via the # (numeric) — NO tier name. The order description embeds it
# collective's tiers query (cached); the order description is the # ("Financial contribution to Inference Cooperative (Credit pack)").
# fallback for the name ("... (Credit pack)").
order = data.get("order") or {} order = data.get("order") or {}
tier_id = order.get("TierId") order_desc = order.get("description") or ""
face_cents, tier_name = await get_tier_face(tier_id) m = re.search(r"\(([^)]+)\)\s*$", order_desc.strip())
if not tier_name: tier_name = m.group(1).strip() if m else ""
order_desc = order.get("description") or ""
m = re.search(r"\(([^)]+)\)\s*$", order_desc.strip())
tier_name = m.group(1).strip() if m else ""
amount_cents = data.get("amount") or 0 amount_cents = data.get("amount") or 0
if not amount_cents: if not amount_cents:
amount_cents = order.get("totalAmount") or 0 amount_cents = order.get("totalAmount") or 0
@@ -1356,18 +1352,20 @@ async def opencollective_webhook(request: Request, token: str):
if "credit" in tier_name.lower() and slug and event_type == "order.processed": if "credit" in tier_name.lower() and slug and event_type == "order.processed":
members = await fetch_members() members = await fetch_members()
email = next((m["email"] for m in members if m["slug"] == slug), None) email = next((m["email"] for m in members if m["slug"] == slug), None)
# Credits are granted at the tier's FACE VALUE (what the pack # The tier is VARIABLE-amount — no fixed face value. Credits are
# buys), not the total charged — the total can include a platform # granted on the contribution amount EXCLUDING the OC platform
# tip the co-op never receives. Fall back to the charged amount # tip (totalAmount - tip = amount), which we resolve via OC's
# if the face value is unavailable. # public order API. Fallback: webhook totalAmount (slightly
if face_cents: # overgrants if a tip was included; logged).
amount = round(face_cents / 100.0, 2) tip_free_cents = await get_order_amount(order.get("idV2"))
if tip_free_cents is not None:
amount = round(tip_free_cents / 100.0, 2)
else: else:
amount = round(float(amount_cents) / 100.0, 2) amount = round(float(amount_cents) / 100.0, 2)
charged = round(float(amount_cents) / 100.0, 2) charged = round(float(amount_cents) / 100.0, 2)
if charged > amount: if charged > amount:
logger.info( logger.info(
"Credit pack: charged $%.2f but face value $%.2f (tip/fees); granting face value", "Credit pack: charged $%.2f including tip; granting tip-free $%.2f",
charged, amount, charged, amount,
) )
oc_ref = f"oc:{slug}:{data.get('id', '')}" oc_ref = f"oc:{slug}:{data.get('id', '')}"