agentleFS
aideploytutoraideploytutor/skill-deploy-share/skills/pay-webhook/SKILL.md

pay-webhook

Skill2 starsChanged 35 days agoMITView on GitHub

Make Creem payments on an app-foundation site fully automatic and complete — every paid order unlocks access (pay_grants), refunds and chargebacks take it away, each notification is processed exactly once, failures return 500 so Creem retries, and an hourly catch-up asks Creem about anything still pending. Use when the user says "handle payments automatically", "unlock content after payment", "what if a webhook is missed", "handle refunds", "paid users only page", or a later skill (subscriptions, receipts, orders) requires pay-webhook.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    No new secrets or packages.

    line 33
  2. Distinctive:

    Never mark anything paid from the browser's word: payments come from a verified webhook or from Creem's API.

    line 35

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

pay-webhook

Goal: the owner never touches an order. Pay → access unlocks by itself; refund or chargeback → access goes away by itself; a notification that never arrives is caught within the hour. Follow ../../STACK.md.

Why (say this to the owner)

  • Creem retries a failed notification only a few times over several hours, then stops (https://docs.creem.io/code/webhooks). A site that's down or mid-deploy at the wrong moment loses the order: money in, nothing delivered.
  • The same notification can arrive twice. Without a record, a buyer gets two of everything.
  • pay-checkout (010) records payments only. Refunds and chargebacks were ignored, so a refunded buyer kept access.

What it does

  • Exactly once: every notification is written to pay_webhook_events first, keyed by event type + Creem object id (refund.created:<refund id>), not by the delivery's event id: Creem's Resend gives the same refund a new event id (seen in testing). A repeat answers 200 duplicate and does nothing; a full refund or dispute that's already recorded is never applied twice. A failure is saved with the error and answers 500, so Creem retries; the retry is processed again.
  • Unlocks: checkout.completed → payment paid → a pay_grants row → pay.granted. hasAccess(userId, productId?) gates any page; /members is a ready example.
  • Takes back: refund.created (full) → payment refunded, grant revoked, pay.refunded. A partial refund only emits pay.refunded with full: false. dispute.created → disputed, revoked, pay.disputed.
  • Ignores other sites' events: a notification whose payment isn't in this site's pay_payments is marked ignored (one Creem store can serve several sites).
  • Catch-up: POST /api/pay/reconcile asks Creem about this mode's pending payments from the last 7 days (older than 2 minutes, 25 at most) and grants any paid payment that has no grant. It needs no secret, it's safe to call by anyone, and it runs at most once every 5 minutes. A GitHub Actions workflow calls it every hour.
  • Belt and braces: /members calls ensureGrants() so a buyer who arrives before the webhook still sees Unlocked.

Rules

  • No new secrets or packages. Never read or type CREEM_* values.
  • Refunds are the owner's action in Creem. The agent may click Refund on a test-mode payment only with the owner's OK, never in live mode.
  • Never mark anything paid from the browser's word: payments come from a verified webhook or from Creem's API.

Steps

1. Add the code

node <skill-dir>/scripts/apply.mjs --dir "<project>" --url "https://<site>" --json

Adds the two tables, src/lib/pay-webhook.ts, replaces pay-checkout's /api/pay/webhook (only if unedited), adds /api/pay/reconcile, /members, .github/workflows/pay-reconcile.yml (hourly, calls --url), patches /pricing (shows Refunded/Disputed) and /pay/success (Open members area), stack.json, a git save point, and the migration if node_modules exists (else npm run db:generate -- --name pay_webhook). Push; the migration runs in the build.

2. Owner: tick the events (a phone works)

Creem → Developers → Webhooks → the /api/pay/webhook endpoint → make sure refund.created and dispute.created are sent (or all events). Same secret, nothing to change in the host.

3. Verify

  1. Signed in, /members → Locked (or Unlocked if this account already paid: ensureGrants backfills).
  2. Owner buys on /pricing with the test card → Open members area → Unlocked.
  3. Owner refunds that test payment in Creem → /members Locked ("Access was removed after a refund"), /pricing ↩️ Refunded, core_events has pay.refunded.
  4. GitHub → Actions → Payment catch-up → Run workflow → green; summary {"ok":true,"checked":…,"recovered":…,"repaired":…}.

Extending it (1.1.0)

  • Later skills add handlers: handleCreemEvent(event, [myHandler]); a CreemHandler runs after the built-in handling and may return its own Result.
  • Recurring (monthly/yearly) products never get a lifetime grant: grantFor skips them, pay-subscription (013) gives time-limited access instead.
  • Subscription events are keyed by subscription id + transaction (renewals) or update time, so month 2 isn't mistaken for a repeat of month 1.

Using it in code

import { hasAccess } from "@/lib/pay-webhook";
if (!(await hasAccess(user.id, process.env.CREEM_PRODUCT_ID))) redirect("/pricing");

Other systems listen in core_events for pay.granted, pay.refunded (full, amount, reason) and pay.disputed.

Troubleshooting

See references/troubleshooting.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.