How to Integrate with Stripe ASP.NET Core 2.2

The Stripe API is fabulous at documentation and giving examples of how to do all sorts of things to allow your application to take credit cards.

This example will describe how to create a simple charge using ASP.NET Core 2.2

First we will need to install the Stripe.Net nuget package.

Then we will need to create the StripeChargeCreateOptions model and fill in some of the properties.

var chargeOptions = new StripeChargeCreateOptions()
{
  Amount = 200,
  Currency = "usd",
  Description = $"Charge to {customer_email} for {product_name}",
  Capture = true,
  SourceTokenOrExistingSourceId = "tokenId_from_stripe_libraries"
};

The “tokenId_stripe_libraries” is going to be the id returned from Stripe when using the Stripe libraries and validating your request with your public Stripe API key.

We then just need to create the StripeChargeService and have it create a charge.

var chargeService = new StripeChargeService();
try
{
  await chargeService.CreateAsync(chargeOptions);
}
catch (StripeException ex) 
{
  Console.WriteLine(ex.StripeError.Message);
}

The Stripe API has many more features you can incorporate into your application, I just wanted to give you a brief view of what it would look like to use .NET Core to charge Stripe.

2 thoughts on “How to Integrate with Stripe ASP.NET Core 2.2

Leave a Reply

Your email address will not be published. Required fields are marked *