If you plan on building a customer dashboard for your application, it’s often a good idea to integrate it with your payment processor. One of the best tools to use is the Stripe API. It supports over 26 different countries and offers an extensive set of features for building out an e-commerce application. In this tutorial, we will go over managing customer information using the Stripe Customer API and ASP.NET Core. Review some of our videos courses or check out our GitHub account if you want to learn more.

Stripe API Setup

The first step is to add the Stripe package to your ASP.NET Core project. You can start off by adding the Stripe.net package to your project.

dotnet add package Stripe.net

You’ll then want to add your keys to your settings file.

"Stripe": {
    "SecretKey": "sk_test_.....",
    "PublishableKey": "pk_test_......"
  },

You can then add the Stripe configuration to your Startup.cs file. This will allow for your Stripe configuration to be globally accessible in your application.

public Startup(IConfiguration configuration)
{
            Configuration = configuration;
            StripeConfiguration.SetApiKey(Configuration.GetSection("Stripe")["SecretKey"]);
}

Stripe API Customer Model

You will want to create a model that represents the Customer object that you want to map to your Stripe Model. This will be used to set the parameters for creating a Stripe Customer. We will have an email, description, and MetaData for the customer. This can contain information like firstName and lastName.

using System.Collections.Generic;

namespace StripeCustomers.Models
{
    public class Customer
    {
        public string Email { get; set; }
        public string Description { get; set; }
        public Dictionary<string, string> Metadata { get; set; }
    }
}

Customer Controller

Now that we have our Model and Configuration set up, we can create our Controller for performing CRUD operations on our Customers. The first thing we’ll want to do is create an instance of our CustomerService for our controller.

private readonly StripeCustomerService customerService;
public CustomersController()
{
            customerService = new StripeCustomerService();
}

After that, we’ll be able to create out CRUD methods.

[Route("api/customers")]
    public class CustomersController : Controller
    {
        private readonly StripeCustomerService customerService;
        public CustomersController()
        {
            customerService = new StripeCustomerService();
        }
        // GET
        [HttpGet]
        public IEnumerable<StripeCustomer> Index()
        {
            StripeList<StripeCustomer> customerItems = customerService.List(
                new StripeCustomerListOptions() {
                    Limit = 3
                }
            );

            return customerItems;
        }

        [HttpGet("{id}")]
        public StripeCustomer GetCustomer(string id)
        {
            var customer = customerService.Get(id);
            return customer;
        }

        [HttpPost]
        public StripeCustomer CreateCustomer([FromBody] Customer customerOptions)
        {
            var customerData = new StripeCustomerCreateOptions
            {
                Description = customerOptions.Description,
                Email = customerOptions.Email,
                Metadata = customerOptions.Metadata
            };
            var customer = customerService.Create(customerData);
            return customer;
        }

        [HttpPut("{id}")]
        public StripeCustomer UpdateCustomer([FromRoute] string id, [FromBody] Customer customerOptions)
        {
            var customerData = new StripeCustomerUpdateOptions
            {
                Description = customerOptions.Description,
                Email = customerOptions.Email,
                Metadata = customerOptions.Metadata
            };
            var customer = customerService.Update(id, customerData);
            return customer;
        }

        [HttpDelete("{id}")]
        public StripeDeleted DeleteCustomer(string id)
        {
            var result = customerService.Delete(id);
            return result;
        }
    }

As you can see here, we use our Custom customer model map to the Customer Options that we want to modify for our customer.

Once that’s done, you should now be able to go to your API to create and view customers.

Stripe Get All Customers

Stripe Get All Customers

Stripe Create Customer

Stripe Create Customer

Conclusion

Platforms like Stripe allow for you to easily allow payment processing and customer management to your ASP.NET Core application. The platform is well documented and constantly maintained. If you’re interested in learning more, please review our video courses or check out our GitHub. We also have an article similar article using the Stripe API and Express.js.

Codebrains Newsletter

Get weekly dev news and tutorials.

Powered by ConvertKit