Open checkout and detect a new contribution

Send supporters to GitHub Sponsors or Open Collective, then find out when their new contribution appears.

On this page

Sponsorkit never handles money. Supporters pay on GitHub Sponsors or Open Collective, and Sponsorkit can't see the payment happen. openCheckout() bridges that gap: it sends the supporter to the right provider, then checks their contributions until something new shows up.

A sponsor command#

sponsor.ts
import { Sponsorkit, SponsorkitError } from "@sponsorkit/sdk/node";

const sk = new Sponsorkit({ github: "octocat", openCollective: "my-project", appName: "my-cli" });

try {
  const result = await sk.openCheckout({ frequency: "monthly", amount: 500 });
  if (result.status === "contributed") {
    const { amount } = result.contribution;
    console.log(`Thank you for your ${amount.value / 100} ${amount.currency} contribution!`);
  } else {
    console.log("We couldn't confirm a contribution yet. If you contributed, thank you! It will show up soon.");
  }
} catch (e) {
  // Only before polling starts: from the sign-in phase (cancelled, timeout, access_denied, network_error, ...) or disabled.
  if (!(e instanceof SponsorkitError)) throw e;
  console.error(e.code === "cancelled" || e.code === "timeout" ? "Sign-in was cancelled or timed out." : `Couldn't start checkout: ${e.message}`);
  process.exitCode = 1;
}

In the browser, call openCheckout() from a click handler, because it opens a popup. See Add a sponsor button to a web app, or use <sponsorkit-button>, which does this for you.

What happens#

  1. Sign in, if needed. Sponsorkit can only detect a new contribution for someone it knows, so a signed-out supporter signs in first (same flows as sign-in). In the browser this happens in the same popup; in native apps the browser continues to the checkout page after the loopback sign-in.
  2. Choose a provider. The checkout page offers the providers that fit the requested amount, currency and frequency (rules below). If only one fits, it goes straight there; if both do, the supporter picks.
  3. Pay. The supporter contributes on GitHub Sponsors or Open Collective.
  4. Detect it. The SDK re-checks the supporter's contributions every 60 seconds, skipping the cache. In the browser it starts once the popup closes; in native apps, right after opening the page. It stops when a contribution appears that's new, or changed since the check started (for example a tier upgrade), or when the timeout passes.

The 60-second interval isn't arbitrary: the API allows one refresh per minute per session, so polling faster would only hit the rate limit. Change it with checkoutPollInterval (or pollInterval per call) if you have a reason to, for example in tests against a fake server.

What the result means#

ResultMeaning
{ status: "contributed", contribution }A new or changed contribution was found. It's already in the cached result.
{ status: "unknown" }Nothing new was found before the timeout, or you cancelled the checkout through your signal or token.

unknown doesn't mean the supporter didn't pay. They may have closed the tab, or the provider may still be processing the payment. Thank them anyway, and let the next refresh pick it up.

The names follow each language: CheckoutStatus.Contributed in .NET, sponsorkit.CheckoutContributed in Go, CheckoutOutcome::Contributed { contribution } in Rust (with an outcome.contribution() helper).

Options#

OptionDefaultMeaning
frequencynoneone_time, monthly or yearly.
amountnonePositive integer in minor units: 500 is $5.00.
currencynoneISO 4217 code such as USD or EUR. Python requires it in uppercase.
providernonegithub or open-collective, to preselect a provider. It must be one of your targets.
timeout10 minutesHow long to poll for the new contribution. It starts after sign-in; sign-in has its own signInTimeout.
pollIntervalcheckoutPollInterval (60 s)Seconds between checks.
cancellationnonesignal (JS), CancellationToken (.NET, Rust), cancel (Python), ctx (Go).

All options are optional. Without any, the supporter picks the provider and the amount on the provider's page.

Timeouts and cancellation#

  • Cancelling through your signal or token resolves unknown in either the sign-in or the polling phase, rather than failing.
  • If the user closes the sign-in window or enters no code, openCheckout() fails with cancelled, as signIn() does.
  • If the sign-in phase exceeds signInTimeout, openCheckout() fails with timeout.
  • Other sign-in failures (access_denied, network_error and so on) fail as they would in signIn().
  • In the browser, openCheckout() opens its popup first, so it can fail with popup_blocked (when no returnTo fallback is set) even when the supporter is already signed in.
  • With SPONSORKIT_DISABLE set, openCheckout() fails with disabled. In mock mode it resolves contributed immediately with a simulated contribution.

Choosing a provider#

The checkout page offers a provider only if all of these hold:

  • The account exists and can receive contributions. On GitHub that means it has a Sponsors listing.
  • The frequency is supported. GitHub supports monthly and one_time. Open Collective supports one_time, monthly and yearly.
  • The currency matches exactly. GitHub is always USD. Open Collective uses the collective's currency. Sponsorkit never converts currencies.
  • GitHub only: a tier fits. The listing needs at least one tier for the frequency (monthly when none is given). The cheapest tier at or above the amount is preselected, otherwise the highest tier.

If you preselect a provider that doesn't fit while another one does, the page shows the chooser with the reason; it never switches provider silently. Each disabled option explains why it's unavailable.

On Open Collective, the amount and interval are prefilled on the contribution page, and the supporter returns to a Sponsorkit "thank you" page afterwards.

checkoutUrl() builds a link to the same checkout page, synchronously and offline, without signing anyone in. It suits README badges, --help output, nag messages and emails:

link.ts
import { Sponsorkit } from "@sponsorkit/sdk";

const sk = new Sponsorkit({ github: "octocat" });
console.log(sk.checkoutUrl({ frequency: "monthly", amount: 500 }));
// https://ffmathy.github.io/sponsorkit/checkout/?targets=github%3Aoctocat&amount=500&frequency=monthly

Invalid options (an unconfigured provider, an unknown frequency, a non-positive amount, a malformed currency) fail with invalid_config. A contribution made through this link isn't detected right away, because nobody is waiting for it; it shows up the next time the supporter is signed in and the cache refreshes.