Checkout in .NET

Send users to contribute with CheckoutUrl for links and OpenCheckoutAsync to open the checkout and wait for the new contribution.

On this page

There are two ways to send someone to contribute:

CheckoutUrl(options)OpenCheckoutAsync(options, ct)
ReturnsA string URLA Task<CheckoutResult>
Signs inNoYes, first, if the user isn't signed in
Network or browserNone: synchronous and offlineOpens the browser, then polls Sponsorkit
Use it forNags, --help text, READMEs, "Sponsor" links in a UIA sponsor command or button that should thank the user when they're done

Both send the user to Sponsorkit's checkout page, which forwards them to GitHub Sponsors or Open Collective. Payment always happens on the provider's own site; Sponsorkit never handles money.

Options#

CheckoutUrlOptions holds the preferences; CheckoutOptions extends it with the polling settings.

PropertyTypeDefaultMeaning
Providerstring?the page asksgithub or open-collective. Must be one of your targets.
Amountlong?noneSuggested amount in minor units (500 is $5.00). Must be positive.
Currencystring?noneThree-letter ISO 4217 code such as USD. The checkout URL uppercases it; in mock mode the simulated contribution keeps it as you passed it.
Frequencystring?noneone_time, monthly or yearly, from Frequencies.
TimeoutTimeSpan?10 minutesCheckoutOptions only: how long to poll for the new contribution.
PollIntervalTimeSpan?CheckoutPollInterval (60 s)CheckoutOptions only: time between polls.
SignInMethodSignInMethod?the client'sCheckoutOptions only: how to sign in if needed.

Invalid values (an unknown or unconfigured provider, a frequency other than the three above, an amount of zero or less, or a currency that isn't three letters) throw SponsorkitError("invalid_config") before anything happens. Amounts and frequencies are suggestions: the provider's page decides what's possible, and checkout describes how each provider treats them.

Program.cs
using Sponsorkit;

var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", OpenCollective = "my-collective" });

Console.WriteLine(sk.CheckoutUrl());
// https://ffmathy.github.io/sponsorkit/checkout/?targets=github%3Aoctocat%2Copen-collective%3Amy-collective

Console.WriteLine(sk.CheckoutUrl(new CheckoutUrlOptions
{
    Provider = Providers.OpenCollective,
    Amount = 1000,
    Currency = "EUR",
    Frequency = Frequencies.Yearly,
}));

CheckoutUrl needs no sign-in and makes no request, so it's safe anywhere, even with SPONSORKIT_DISABLE set (the link still points at your checkout page).

Open and wait: OpenCheckoutAsync#

Program.cs
using Sponsorkit;

var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "my-cli" });

using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) => { e.Cancel = true; cts.Cancel(); };

try
{
    var result = await sk.OpenCheckoutAsync(
        new CheckoutOptions { Frequency = Frequencies.Monthly, Amount = 500, Timeout = TimeSpan.FromMinutes(15) },
        cts.Token);

    if (result.Status == CheckoutStatus.Contributed)
        Console.WriteLine($"Thank you for your {result.Contribution!.Frequency} contribution!");
    else
        Console.WriteLine("No new contribution yet. If you completed the payment, it will show up shortly.");
}
catch (SponsorkitError e)
{
    Console.Error.WriteLine($"Could not open the checkout ({e.Code}): {e.Message}");
}

What happens, step by step:

  1. Signed out: the checkout page asks the user to sign in first and then continues to the payment, in one flow, using the client's sign-in method (or CheckoutOptions.SignInMethod). The sign-in phase is bounded by SignInTimeout and throws its usual errors, such as timeout or access_denied.
  2. Signed in: the SDK records the current contributions and opens the checkout page. If the browser can't be opened, it prints the URL to stderr instead.
  3. Polling: every PollInterval it fetches the contributions, skipping the cache, until one is new or has changed (for example a cancelled sponsorship that became active again), then returns CheckoutStatus.Contributed with that contribution.

The result is CheckoutStatus.Unknown with Contribution == null when Timeout runs out, when the token is cancelled in either phase, or when the session stops being valid while polling. Unknown doesn't mean the user didn't pay: providers sometimes take a few minutes to report a new contribution. The next GetFinancialContributionAsync after the cache expires will pick it up.

OpenCheckoutAsync throws disabled when SPONSORKIT_DISABLE is set, invalid_config for bad options, and the sign-in errors from step 1. Cancelling never throws; see errors & cancellation.

In mock mode#

With Mock or SPONSORKIT_MOCK set, OpenCheckoutAsync returns Contributed immediately with a simulated contribution built from your options (amount 500 USD and monthly unless you say otherwise; Currency is kept exactly as you pass it, not uppercased), signs the instance in and adds the contribution to later results. See testing.