Note to self: It's pretty easy to get a Stripe CheckoutSession in .NET.
I talked about writing a Chrome extension and integrating payment through Stripe a few months ago. Unfortunately the "what appears to be an amazingly well-written howto" wasn't, quite.
So here's a quick bit of play with the Stripe API using .NET. This looks up information according to a "Checkout Session" id, which is what you'll get sent if you set up a "After Payment" processor URL, which is what I'm doing -- for now in an Azure Function, just to be kewl.
(No, you shouldn't use hard-coded paths in your code. No, you shouldn't pass around anonymous classed entities in C#. You're right, there's one instance of insanely not-DRY-ness in this code that almost bugs me. And I hate kludging !
after a nullable and would refactor that. This is pretty serious "proof of concept" territory here.)
Steps:
- Create a .NET console app.
- For bonus points, use the newer template without a
Main
method version, I guess. I got this and was too lazy to start over.
- Add stripe.net to your project via your preferred NuGet manager.
- Get your Stripe secret key and put it in
C:\temp\stripe.key
- Paste in the below junk, wiping out the
Main
method that's in there by default.
- Get a
CheckoutSession
id from, idk, say a Payment Link "After Payment" URL, and insert it into the code.
- Profit?
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
StripeConfiguration.ApiKey = File.ReadAllText(@"C:\temp\stripe.key").Trim();
Service = new Stripe.Checkout.SessionService();
var csId = "cs_test_a1RRCS1VWdOwOwvpJfdZCtjb9jSVOxp9ZhQibAI1Hv3dsvi6BfCPIWwdZP";
var info = LookupStripeCheckoutSession(csId);
Console.WriteLine(JsonConvert.SerializeObject(info, Formatting.Indented));
File.WriteAllText(
@"C:\temp\importantStuff.txt",
JsonConvert.SerializeObject(info, Formatting.Indented)
);
}
// Stripe Fees:
// https://stripe.com/pricing
// "Starts at 2.9% + 30¢ [per successful transaction for domestic cards]"
// Fudge.
public static object LookupStripeCheckoutSession(string checkoutSessionId)
{
Session session = Service!.Get(checkoutSessionId);
if (!session.Metadata.TryGetValue("appId", out var appId))
{
throw new MissingFieldException("Checkout session does not contain a product id");
}
System.IO.File.WriteAllText(
@"C:\temp\session.txt",
JsonConvert.SerializeObject(session, Formatting.Indented)
);
return new {
CheckoutSessionId = session.Id,
RufworkAppId = appId,
session.AmountTotal,
session.Created,
session.PaymentStatus, // One of: <c>no_payment_required</c>, <c>paid</c>, or <c>unpaid</c>.
// Note that TotalDetails (as with any child object brought over)
// has its own json settings, so it'll, eg, serialize to snake case
// if you don't munge it.
session.TotalDetails,
session.CustomerDetails.Email,
};
}
I'd been thinking about having a $3/year [sic] subscription for an extension. After a test, I learned what the comments, above, mention: That'll cost me ($3 x .029 + 30¢ somehow equals...) 41¢ [???] per transaction.
Okay, apparently there's a "Usage fee" in addition to the charge?
Amount
|
Fees
|
Total
|
Type
|
Description
|
Created
|
Available on
|
-$0.02
|
—
|
-$0.02
|
Stripe fee
|
Billing - Usage Fee (2025-05-26)
|
May 27
|
May 27
|
$3.00
|
-$0.39
|
$2.61
|
Charge
|
Subscription creation
|
May 26
|
Jun 2
|
From togai.com:
Stripe allows you to perform end-to-end financial transactions with its suite of integrated offerings:
Stripe Billing - This lets you create recurring subscriptions and invoices. Pricing starts at 2.9% + 30¢for every successful transaction.
Usage pricing for pay-as-you-go is 0.7% of billing volume.
If you need a Custom billing domain it is $10 per month.
Stripe Invoicing - Allows you to invoice upto 25 customers for free every month. Thereafter an overage of 0.4% is applied per invoice paid.
Stripe Tax - Offers you tools for tax calculation priced at 0.5% for each transaction.
Stripe Atlas - Assists you with setting up a company at a one-time setup fee of $500.
Stripe Sigma - Offers reporting and analytics using SQL. Pricing starts at 2¢per transaction and includes a $10 fee towards infrastructure. (emphasis mine -mfn)
From bossrevolution.com:
- Billing. For businesses with a subscription-based or usage-based model, Stripe enables automated billing on a pay-as-you-go or monthly payment basis. Fees start at 0.7% of the billing volume or $620 per month for a one-year contract.
Cool cool. 13 2/3% of $3. Just below Apple's small dev 15%.
Good times. Least the API is simple. More to come.
From stripe.com:
Debit card purchases in the US are more extreme: there are no returns on interchange fees on any US debit card transaction.
If your business often processes refunds shortly after a payment, you can combat these potential lost fees by leaving the transaction authorization open rather than settling the sale right away. This is possible because you only pay interchange fees once a transaction is settled. If you leave the authorization open and a customer makes a return, you can simply reverse the authorization and avoid losing any extra interchange fees (since you never paid those fees to begin with).
For example, if you captured and settled a $100 debit card transaction and a customer requested a return, you could lose $0.42. However, if you had left the authorization open, you could only lose slightly less than $0.04.
You can generally leave an authorization open for up to two days before you pay additional fees, so this approach is most relevant for industries with the immediate delivery of goods (such as food delivery services).
What you can do: Configure the Stripe Payment Intents API to separate authorization and capture.
good heavens.
Labels: .NET, c#, example, noteToSelf, stripe