Getting Started with Stripe .NET example


If you are billing a paid app when you probably need a way to collect a payment or more likely a subscription payment. Stripe developer centric API helps you get get started quickly.

However, it isn’t really clear how some of their API’s and UI Controls should work together.

Strip Basic Workflow for Subscriptions

Let say you have a subscription based Saas you are offering. You will need to collect payment info and charge them on a recurring basis. Assuming you have already signed yourself up for a subscription you will need to define your plans and the pricing using their online portal.

Your workflow with Stripe will look like this for a newly acquired user:

  1. Customer Selects a Product/Plan
  2. Collect Credit Card Info & returns a Token.Id that will reference the Credit Card info submitted. (Client side using “Checkout” Stripe Control)
  3. Create a “Stripe” Customer after the first Credit Card is submitted. (Server Side Stripe System)
  4. Save that CustomerId on your system to use as a cross-reference between your system and Stripes API (Server side Your System – for changing or cancelling subscriptions later)
  5. Associate Customer to the Selected Plan using the Plan Id you setup using the Stripe Portal.

A Mix of Server Side vs Client Side Stripe .NET Example

To get started quickly, you will want to use all available resources Stripe provides. So you will want to uses there Checkout UI control to collect, validate and rely credit card information to Stripe Servers. This way you don’t have to build a custom form (which Stripe Supports) and Stripe handles the sensitive CC info, not you. This is the only client side feature I used get get started quickly.

For the Server-side work, I elected to use their recommended SDK writen by Jayme Davis and recommended by Stripe. After Stripe’s “Checkout” Control collected the Credit Card information the only thing I need to supply the server side was the token that was returned from the Stripe server. This token can only be used once. I used this token to create a customer instead because I need to assign them to recurring payments.

Let me go through each of the steps and the code used to implement that feature:

1. Customer Selects a Product/Plan

You display your available plans on your website and the user selects one. No coding for that one, simple enough.

2. Collect Credit Card Info & returns a Token.Id

This is the only client side code required for this demonstration:

<script src=""></script>
<script type="text/javascript">
$(function () {
    var renderPaymentMethod = function () {
                var handler = StripeCheckout.configure({
                    key: 'pk_XXXXX_XXXXX', //Your publishable key from the Stripe Portal
                    image: "",  //customize the Checkout popup with your logo
                    email: '',
                    token: function (token, args) {
                        //However you want to get the Token back to the server


                //Stripe's  checkout.js will display a iframe modal to collect CC info
                    name: 'My Awesome Business',
                    panelLabel: 'Add Payment Method',
                    description: '',
                    allowRememberMe: false

    $("#btnSubscribePayment").click(function () {


3. Create a “Stripe” Customer

Now we have passed the token to the server. Since this can only be used once we will use it to create a customer (rather then charge them). This is done by assigning the credit card’s token to the customer’s Card property. After you assign the credit card token to the new customer object then you can create the customer.

public void CreateStripeCustomer(string token)
    var myCustomer = new StripeCustomerCreateOptions();

    // set these properties if it makes you happy
    myCustomer.Email = "";
    myCustomer.Description = "Customer Name here";
    myCustomer.Card = new StripeCreditCardOptions() 
            TokenId = token 
    var customerService = new StripeCustomerService();
    StripeCustomer stripeCustomer = customerService.Create(myCustomer);
    var StripeCustomerId = stripeCustomer.Id;


Fyi, for the .NET Stripe library to work you need to pass your private key in to the server constructors or just set it up as as appSettings:

    <add key="StripeApiKey" value="api key here" />

4. Save that CustomerId on your system

Save that id for use later (unsubscribing or changing plans). This also important if you would like to display transaction history or hashed payment info to the user.


5. Associate Customer to the Selected Plan

Here we are going to grab the presisted CustomerId from our Local DB. We will use it to query Stripe for that Customer Object. Then we will associate that Customer with the Plan ID we created in the portal called “BASIC”. This creates what Stripe calls a “Subscription” (plan + customer = subscription).

Just make sure you

public void SubscribeCustomerToBasicPlan()
    var StripeCustomerId = DB.GetStripeCustomerId();  //Grab stripe customerId from local DB
    var customerService = new StripeCustomerService();
    StripeCustomer stripeCustomer = customerService.Get(StripeCustomerId);
    StripeSubscriptionService subscriptionSvc = new StripeSubscriptionService();
    subscriptionSvc.Create(clientInfo.StripeBillingCustomerId, "BASIC"); //use the PlanId you configured in the Stripe Portal to create a subscription
    //Do something here to give your customer what they are paying for

Comment if you have any questions or feedback.


Posted in .NET and tagged , , , , , .