Skip to content

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com.

License

Notifications You must be signed in to change notification settings

stripe/stripe-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stripe
Build status
ready

Stripe Services

3D Secure (needs documentation)
Accounts
Application Fees
Application Fee Refunds (needs documentation)
Balance
Bank Accounts
Cards
Charges
Connect
Country Specs
Coupons
Customers
Discounts (needs documentation)
Disputes
File Uploads
Invoices
Invoice Items
OAuth (see stripe connect)
Payouts
Plans
Radar
Refunds
Sources
Subscriptions
Subscription Items
Tokens
Transfers

Features

Expandable Properties
StripeDateFilter
StripeListOptions
StripeResponse
StripeRequestOptions

Infrastructure

Errors
Events
Metadata

Support

Search issues and pull requests to see if your issue has already been reported. If it has, please leave a comment or a reaction. Create a new issue if your question is still not answered. :)

Quick Start

Install Stripe.net via NuGet nuget install Stripe.net

Provide Stripe.net with your api key - there are a couple of ways to do this:

a) In your application initialization, call this function (you only have to do it once during startup):

	StripeConfiguration.SetApiKey("[your api key here]");

b) pass the api key to any of the services when you create them:

	var planService = new StripePlanService("[your api key here]");

c) pass the api key to a StripeRequestOptions:

	var planService = new StripePlanService();
	planService.Get(*planId*, new StripeRequestOptions() { ApiKey = "[your api key here]" });

** If you are using Xamarin/Mono, you may want to provide your own HttpMessageHandler. You can do so by passing an instance to StripeConfiguration.HttpMessageHandler on your application's startup. See this thread for details.

Additional Learning

There is a course made by Craig McKeachie - find it here:

Training

Stripe Response

The StripeResponse object is a property (with the same name) attached to all entities in Stripe.net when they are returned from a service call.

e.g. var charge = new StripeChargeService().Create(...) - when you access charge.StripeResponse, this object will be available. The object is documented below with useful tips on each property.

	public class StripeResponse
	{
		// ResponseJson will always tell you the complete json Stripe returned to Stripe.net.
		// this will be the same as the ObjectJson when you execute a create/get/delete call. 
		// however, if you execute a List() method, the ResponseJson will have the full api result 
		// from Stripe (a charge list with 10 charges, for example).
		public string ResponseJson { get; set; }
		
		// when you call a List() method, the object json is the object in the response array that represents
		// the entity. The ResponseJson will be the full array returned from Stripe on every entity, however, 
		// since that was the full response from Stripe. ObjectJson is always the same as ResponseJson when 
		// you are doing a regular create/get/delete, because you are dealing with a single object.
		public string ObjectJson { get; set; }

		// this is the request id of the call, as seen in the Stripe dashboard. I would recommend logging 
		// this and/or saving it to your database. this is very useful to help you find your request 
		// in the dashboard, or ask Stripe a question about your api call
		public string RequestId { get; set; }

		// this is the request date and time of the call. I would also recommend logging this and/or 
		// saving it to your database, as it tells you when Stripe processed the request.
		public DateTime RequestDate { get; set; }
	}

Accounts

Creating an account

When creating an account, you can create a standalone or managed account. Standalone accounts are managed by Stripe and the account owner directly. Managed accounts are handled by your platform. See the Stripe documentation for more information.

Since Stripe returns ExternalAccounts as a single array (contains StripeCard's and/or StripeBankAccount's), that type is a dynamic StripeList. These are split up as ExternalCards and ExternalBankAccounts for your convenience.

	var account = new StripeAccountCreateOptions();
	account.Email = "jayme@yoyoyo.com"  // this is required if it is not a managed account. the user is emailed on standalone accounts,
	                                    // it's only used for reference on managed accounts
	account.Managed = false;            // set this to true if you want a managed account (email is not required if this is set to true)

	// a few optional settings
	account.Country = "US"                                 // defaults to your country
	account.BusinessName = "Jayme Davis' GitHub, Inc";
	account.BusinessUrl = "http://github.com/jaymedavis";

	var accountService = new StripeAccountService();
	StripeAccount response = accountService.Create(account);

Retrieving an account

	var accountService = new StripeAccountService();
	StripeAccount response = accountService.Get(*accountId*);

Updating an account

Updating an account has almost all the same available properties as creating an account.

	var myAccount = new StripeAccountUpdateOptions();
	account.BusinessUrl = "http://twitter.com/jaymed";

	var accountService = new StripeAccountService();
	StripeAccount response = accountService.Update(*accountId*, myAccount);

Deleting an account

	var accountService = new StripeAccountService();
	accountService.Delete(*accountId*);

List all accounts

	var accountService = new StripeAccountService();
	IEnumerable<StripeAccount> response = accountService.List(); // optional StripeListOptions

StripeListOptions for paging

Application Fees

If you do not specify an amount, the entire application fee is refunded.

Retrieving an application fee

	var feeService = new StripeApplicationFeeService();
	StripeApplicationFee stripeApplicationFee = feeService.Get(*applicationFeeId*);

Refunding an application fee

	var feeService = new StripeApplicationFeeService();
	StripeApplicationFee stripeApplicationFee = feeService.Refund(*applicationFeeId*, *amount*);

Balance

Retrieving your account balance

	var balanceService = new StripeBalanceService();
	StripeBalance response = balanceService.Get();

Retrieving a specific balance transaction

	var balanceService = new StripeBalanceService();
	StripeBalanceTransaction transaction = balanceService.Get(*balanceTransactionId*);

Listing balance transactions

	var balanceService = new StripeBalanceService();
	IEnumerable<StripeBalanceTransaction> balanceTransactions = balanceService.List(); // optional StripeBalanceTransactionListOptions

StripeBalanceTransactionListOptions supports filtering by a StripeDateFilter for date created, a StripeDateFilter for date available, currency, source, transfer, type, and supports StripeListOptions for paging

Bank Accounts

Creating a bank account

When creating a bank account you can use either bank account details or a token (ONE OR THE OTHER, NOT BOTH)

With a token:

	var myBankAccount = new BankAccountCreateOptions();

	myBankAccount.SourceToken = *tokenId*;

	var bankAccountService = new BankAccountService();
	CustomerBankAccount bankAccount = bankAccountService.Create(*customerId*, myBankAccount);

With a bank account:

	var myBankAccount = new BankAccountCreateOptions
	{
		SourceBankAccount = new SourceBankAccount()
		{
			AccountNumber = "000123456789",
			Country = "US",
			Currency = "usd",
			AccountHolderName = "Frank",
			AccountHolderType = BankAccountHolderType.Company,
			RoutingNumber = "110000000",
			Metadata = new Dictionary<string, string>
			{
				{ "Name", "Ray Barone" },
				{ "OftenSays", "Thatttttt's right" }
			}
		}
	};

	var bankAccountService = new BankAccountService();
	CustomerBankAccount bankAccount = bankAccountService.Create(*customerId*, myBankAccount);

Retrieving a bank account

	var bankAccountService = new BankAccountService();
	CustomerBankAccount bankAccount = bankAccountService.Get(*customerId*, *bankAccountId*);

Updating a bank account

	var myBankAccount = new BankAccountUpdateOptions()
	{
		AccountHolderName = "Robert",
		AccountHolderType = BankAccountHolderType.Individual,
		Metadata = new Dictionary<string, string>()
		{
			{ "Name", "Frank Barone" },
			{ "OftenSays", "Holy Crap" }
		}
	};

	var bankAccountService = new BankAccountService();
	CustomerBankAccount bankAccount = bankAccountService.Update(*customerId*, *bankAccountId*, myBankAccount);

Deleting a bank account

	var bankAccountService = new BankAccountService();
	bankAccountService.Delete(*customerId*, *bankAccountId*);

List all bank account

	var bankAccountService = new BankAccountService();
	IEnumerable<CustomerBankAccount> response = bankAccountService.List(*customerId*); // optional StripeListOptions

StripeListOptions for paging

Verify a bank account

The Verify function is also available. You pass it to the two values sent to the user's account.

Cards

Creating a card

When creating a card you can use either a card or a token

With a token:

	var myCard = new StripeCardCreateOptions();

	myCard.SourceToken = *tokenId*;

	var cardService = new StripeCardService();
	StripeCard stripeCard = cardService.Create(*customerId*, myCard); // optional isRecipient

With a card:

	var myCard = new StripeCardCreateOptions();

	// setting up the card
	myCard.SourceCard = new SourceCard()
	{
		Number = "4242424242424242",
		ExpirationYear = "2022",
		ExpirationMonth = "10",
		AddressCountry = "US",                // optional
		AddressLine1 = "24 Beef Flank St",    // optional
		AddressLine2 = "Apt 24",              // optional
		AddressCity = "Biggie Smalls",        // optional
		AddressState = "NC",                  // optional
		AddressZip = "27617",                 // optional
		Name = "Joe Meatballs",               // optional
		Cvc = "1223"                          // optional
	};

	var cardService = new StripeCardService();
	StripeCard stripeCard = cardService.Create(*customerId*, myCard); // optional isRecipient

Retrieving a card

	var cardService = new StripeCardService();
	StripeCard stripeCard = cardService.Get(*customerId*, *cardId*); // optional isRecipient

Updating a card

	var myCard = new StripeCardUpdateOptions();

	myCard.Name = "Cardy MyCardson"
	myCard.ExpirationYear = "2016";
	myCard.ExpirationMonth = "10";
	myCard.AddressCountry = "US";
	myCard.AddressLine1 = "1234 ComeOnBabySayYouLoveMe St";
	myCard.AddressLine2 = "";
	myCard.AddressState = "NC";
	myCard.AddressCity = "Raleigh"
	myCard.AddressZip = "27617";

	var cardService = new StripeCardService();
	StripeCard stripeCard = cardService.Update(*customerId*, *cardId*, myCard); // optional isRecipient

Deleting a card

	var cardService = new StripeCardService();
	cardService.Delete(*customerId*, *cardId*); // optional isRecipient

List all cards

	var cardService = new StripeCardService();
	IEnumerable<StripeCard> response = cardService.List(*customerId*); // optional StripeListOptions and isRecipient

StripeListOptions for paging

Charges

Creating a charge

When creating a charge you can use either a card, customer, or a token/existing source. Only one is allowed.

With a token (or an existing source):

	var myCharge = new StripeChargeCreateOptions();

	// always set these properties
	myCharge.Amount = 5153;
	myCharge.Currency = "usd";

	// set this if you want to
	myCharge.Description = "Charge it like it's hot";

	myCharge.SourceTokenOrExistingSourceId = *tokenId or existingSourceId*;

	// set this property if using a customer - this MUST be set if you are using an existing source!
	myCharge.CustomerId = *customerId*;

	// set this if you have your own application fees (you must have your application configured first within Stripe)
	myCharge.ApplicationFee = 25;

	// (not required) set this to false if you don't want to capture the charge yet - requires you call capture later
	myCharge.Capture = true;

	var chargeService = new StripeChargeService();
	StripeCharge stripeCharge = chargeService.Create(myCharge);

With a card:

	// setting up the card
	var myCharge = new StripeChargeCreateOptions();

	// always set these properties
	myCharge.Amount = 5153;
	myCharge.Currency = "usd";

	// set this if you want to
	myCharge.Description = "Charge it like it's hot";

	myCharge.SourceCard = new SourceCard()
	{
		Number = "4242424242424242",
		ExpirationYear = "2022",
		ExpirationMonth = "10",
		AddressCountry = "US",                // optional
		AddressLine1 = "24 Beef Flank St",    // optional
		AddressLine2 = "Apt 24",              // optional
		AddressCity = "Biggie Smalls",        // optional
		AddressState = "NC",                  // optional
		AddressZip = "27617",                 // optional
		Name = "Joe Meatballs",               // optional
		Cvc = "1223"                          // optional
	};

	// set this property if using a customer
	myCharge.CustomerId = *customerId*;

	// set this if you have your own application fees (you must have your application configured first within Stripe)
	myCharge.ApplicationFee = 25;

	// (not required) set this to false if you don't want to capture the charge yet - requires you call capture later
	myCharge.Capture = true;

	var chargeService = new StripeChargeService();
	StripeCharge stripeCharge = chargeService.Create(myCharge);

Retrieving a charge

	var chargeService = new StripeChargeService();
	StripeCharge stripeCharge = chargeService.Get(*chargeId*);

Capturing a charge

If you set a charge to capture = false, you use this to capture the charge later. amount and applicationFee are not required.

	var chargeService = new StripeChargeService();
	StripeCharge stripeCharge = chargeService.Capture(*chargeId*, *amount*, *applicationFee*);

List all charges

	var chargeService = new StripeChargeService();
	IEnumerable<StripeCharge> response = chargeService.List(); // optional StripeChargeListOptions

StripeChargeListOptions supports a CustomerId, StripeListOptions for paging, and a StripeDateFilter for date filtering

Stripe Connect

The Stripe Connect documentation can be a little intimidating, so I am going to try to break it down a little. Stripe Connect gives you the ability to accept money on behalf of other accounts, and access or modify connected accounts depending on permissions.

  1. The first thing you need to do is register your platform with Stripe Connect.

  2. The next thing to do, is have another party connect to your site. To do this, put a link on your site which will start the authorization process, or you can use a Stripe Connect Button. Your link will need to contain some querystring parameters:

    response_type: code client_id: your client id from the stripe connect dashboard scope: read_only (default), or read_write (lets you modify their data as well) // this is optional and defaults to read_only redirect_uri: this is optional, and will return the user to this page when the connection is complete other options are available and you can learn more about them with the Connect OAuth Reference

  3. When the user clicks the link on your site, they will be prompted to authorize the connection. At this point, they can create a new Stripe account or setup the connection with an existing account.

Your link will look something like this:

https://connect.stripe.com/oauth/authorize?response_type=code&client_id=*your_client_id_from_the_stripe_connect_dashboard&scope=read_write
  1. The link above will return a code when the setup is complete (and also return back to your redirect_uri if specified). With this code, you can make a request to Stripe to get the StripeUserId for accessing their account.

In Stripe.net, you can accomplish this with the following code:

	var stripeOAuthTokenService = new StripeOAuthTokenService();
	var _stripeOAuthTokenCreateOptions = new StripeOAuthTokenCreateOptions()
	{
		ClientSecret = ConfigurationManager.AppSettings["StripeApiKey"],
		Code = *the code returned from above*,
		GrantType = "authorization_code"
	};

	StripeOAuthToken stripeOAuthToken = stripeOAuthTokenService.Create(_stripeOAuthTokenCreateOptions);
  1. You're done! Whenever you need to access the connected account, you simply need the StripeUserId from the StripeOAuthToken to be passed as part of the StripeRequestOptions which all service calls now support as an optional parameter.

For example, to get the plans on the connected account, you could run the following code:

	var planService = new StripePlanService();
	StripePlan response = planService.List(null /* StripeListOptions */, new StripeRequestOptions() { StripeConnectAccountId = *the StripeUserId on the StripeOAuthToken above* });

Depending on if your permissions are read_write or read_only, you can do anything on the connected account you can do on your own account just by passing the StripeUserId as part of StripeRequestOptions.

Country Specs

Each account's country can have different rules for required fields, payment methods, and currencies. The CountrySpecService allows you to receive this metadata.

Retrieving a CountrySpec

	var countrySpecService = new CountrySpecService();
	CountrySpec spec = countrySpecService.Get("US");

List all CountrySpec's

	var countrySpecService = new CountrySpecService();
	CountrySpec spec = countrySpecService.List();

StripeListOptions for paging

Coupons

Creating a coupon

	var myCoupon = new StripeCouponCreateOptions();
	myCoupon.Id = "HOLIDAY10OFF";
	myCoupon.PercentOff = "10";
	myCoupon.Duration = "repeating";    // "forever", "once", or "repeating"
	myCoupon.DurationInMonths = 3;      // valid when "repeating" only

	// set these if you want to
	myCoupon.MaxRedemptions = 100;
	myCoupon.RedeemBy = '12/31/2012';

	var couponService = new StripeCouponService();
	StripeCoupon response = couponService.Create(myCoupon);

Retrieving a coupon

	var couponService = new StripeCouponService();
	StripeCoupon response = couponService.Get(*couponId*);

Deleting a coupon

	var couponService = new StripeCouponService();
	couponService.Delete(*couponId*);

List all coupons

	var couponService = new StripeCouponService();
	IEnumerable<StripeCoupon> response = couponService.List();  // optional StripeListOptions

StripeListOptions for paging

Customers

Creating a customer

When creating a customer, you can specify any plan they are on, any coupons that will apply, a credit card or token, and various meta data.

With a token:

	var myCustomer = new StripeCustomerCreateOptions();
	myCustomer.Email = "pork@email.com";
	myCustomer.Description = "Johnny Tenderloin (pork@email.com)";

    myCustomer.SourceToken = *token*;

	myCustomer.PlanId = *planId*;                          // only if you have a plan
	myCustomer.TaxPercent = 20;                            // only if you are passing a plan, this tax percent will be added to the price.
	myCustomer.Coupon = *couponId*;                        // only if you have a coupon
	myCustomer.TrialEnd = DateTime.UtcNow.AddMonths(1);    // when the customers trial ends (overrides the plan if applicable)
	myCustomer.Quantity = 1;                               // optional, defaults to 1

	var customerService = new StripeCustomerService();
	StripeCustomer stripeCustomer = customerService.Create(myCustomer);

With a card:

	var myCustomer = new StripeCustomerCreateOptions();
	myCustomer.Email = "pork@email.com";
	myCustomer.Description = "Johnny Tenderloin (pork@email.com)";

	// setting up the card
	myCustomer.SourceCard = new SourceCard()
	{
		Number = "4242424242424242",
		ExpirationYear = "2022",
		ExpirationMonth = "10",
		AddressCountry = "US",                // optional
		AddressLine1 = "24 Beef Flank St",    // optional
		AddressLine2 = "Apt 24",              // optional
		AddressCity = "Biggie Smalls",        // optional
		AddressState = "NC",                  // optional
		AddressZip = "27617",                 // optional
		Name = "Joe Meatballs",               // optional
		Cvc = "1223"                          // optional
	};

	myCustomer.PlanId = *planId*;                          // only if you have a plan
	myCustomer.TaxPercent = 20;                            // only if you are passing a plan, this tax percent will be added to the price.
	myCustomer.Coupon = *couponId*;                        // only if you have a coupon
	myCustomer.TrialEnd = DateTime.UtcNow.AddMonths(1);    // when the customers trial ends (overrides the plan if applicable)
	myCustomer.Quantity = 1;                               // optional, defaults to 1

	var customerService = new StripeCustomerService();
	StripeCustomer stripeCustomer = customerService.Create(myCustomer);

Don't let this be intimidating - all of these fields are optional. You could just create a customer with an email if you wanted.

Updating a customer

With a token:

	var myCustomer = new StripeCustomerUpdateOptions();
	myCustomer.Email = "pork@email.com";
	myCustomer.Description = "Johnny Tenderloin (pork@email.com)";

    myCustomer.SourceToken = *token*;

	myCustomer.Coupon = *couponId*;    // only if you have a coupon

	var customerService = new StripeCustomerService();
	StripeCustomer stripeCustomer = customerService.Update(*customerId*, myCustomer);

With a card:

	var myCustomer = new StripeCustomerUpdateOptions();
	myCustomer.Email = "pork@email.com";
	myCustomer.Description = "Johnny Tenderloin (pork@email.com)";

	// setting up the card
	myCustomer.Source = new SourceCard()
	{
		// set these properties if passing full card details (do not
		// set these properties if you set TokenId)
		Object = "card",
		Number = "4242424242424242",
		ExpirationYear = "2022",
		ExpirationMonth = "10",
		AddressCountry = "US",                // optional
		AddressLine1 = "24 Beef Flank St",    // optional
		AddressLine2 = "Apt 24",              // optional
		AddressCity = "Biggie Smalls",        // optional
		AddressState = "NC",                  // optional
		AddressZip = "27617",                 // optional
		Name = "Joe Meatballs",               // optional
		Cvc = "1223"                          // optional
	};

	myCustomer.Coupon = *couponId*;    // only if you have a coupon

	var customerService = new StripeCustomerService();
	StripeCustomer stripeCustomer = customerService.Update(*customerId*, myCustomer);

If you want to set the default source, just add:

	myCustomer.DefaultSource = *sourceId*;

Retrieving a customer

	var customerService = new StripeCustomerService();
	StripeCustomer stripeCustomer = customerService.Get(*customerId*);

Deleting a customer

See Stripe's documentation on deleting a customer for more information.
	var customerService = new StripeCustomerService();
	customerService.Delete(*customerId*);

List all customers

	var customerService = new StripeCustomerService();
	IEnumerable<StripeCustomer> response = customerService.List(); // optional StripeCustomerListOptions

StripeCustomerListOptions supports StripeListOptions for paging, and a StripeDateFilter for date filtering

Disputes

Updating a dispute

	var disputeService = new StripeDisputeService();

	// providing the dispute reason is optional
	StripeDispute stripeDispute = disputeService.Update(*chargeId*, "customer ate the donut before I charged them, so they said it was free");

Events

Stripe sends Events (webhooks) whenever an associated action occurs. The list of events Stripe sends is documented here: https://stripe.com/docs/api#event_types.

Configure your webhook urls first, so Stripe knows where to send the data.

Receiving events from Stripe

a) Use a Controller (recommended)

See here

b) Create a handler that looks something like the below:

	namespace TheBestApplicationEverCreated
	{
		public class StripeHandler : IHttpHandler
		{
			public bool IsReusable
			{
				get { return true; }
			}

			public void ProcessRequest(HttpContext context)
			{
				var json = new StreamReader(context.Request.InputStream).ReadToEnd();

				var stripeEvent = StripeEventUtility.ParseEvent(json);

				switch (stripeEvent.Type)
				{
					case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
						var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
						break;
				}
			}
		}
	}

c) Create a StripeHandler.ashx in the root of your website (or wherever) that looks like this:

	<%@ WebHandler Language="C#" Class="StripeHandler" CodeBehind="StripeHandler.cs" %>

Whenever an Event is received, StripeEventUtility.ParseEvent(json) will convert the response into a StripeEvent object.

Retrieving an event

If you have the id and you want to retrieve the event

	var eventService = new StripeEventService();
	StripeEvent response = eventService.Get(*eventId*)

List all events

You can list events in the same way everything else works in Stripe.net.

	var eventService = new StripeEventService();
	IEnumerable<StripeEvent> response = eventService.List(); // optional StripeEventListOptions

StripeEventListOptions supports a type, StripeListOptions for paging, and a StripeDateFilter for date filtering

Errors

Any errors that occur on any of the services will throw a StripeException with the message returned from Stripe. It is a good idea to run your service calls in a try and catch StripeException.

The StripeException contains and HttpStatusCode and a StripeError entity. The StripeError entity contains the type, message, code and param. For more infomation, review the Errors section of stripe here: https://stripe.com/docs/api#errors

Expandable Properties

Many of the services support expandable properties. Setting an expandable property to true means you want the entire object back instead of just the id.

For example:

	var chargeService = new StripeChargeService();
	chargeService.ExpandBalanceTransaction = true;
	chargeService.ExpandCustomer = true;
	chargeService.ExpandInvoice = true;

	StripeCharge stripeCharge = chargeService.Get(*chargeId*);

When the StripeCharge is returned, the Customer, BalanceTransaction, and Invoice properties will be hydrated objects.

File Uploads

Creating a file upload

When setting the Purpose of a file, all available options are listed as constants in StripeFilePurpose.

	var fileService = new StripeFileUploadService();

	StripeFileUpload file = _fileService.Create("logo.png", *fileStream*, StripeFilePurpose.BusinessLogo);

In order to determine the mime type, your filename must end with one of the following extensions: jpeg, jpg, pdf, or png.

Retrieving a file upload

	var fileService = new StripeFileUploadService();
	StripeRefund refund = fileService.Get(*fileUploadId*);

List all file uploads

	var fileService = new StripeFileUploadService();
	IEnumerable<StripeFileUpload> response = fileService.List(); // optional StripeFileUploadListOptions

StripeFileUploadListOptions supports a Purpose, StripeListOptions for paging, and a StripeDateFilter for date filtering

Invoices

Retrieving an invoice

	var invoiceService = new StripeInvoiceService();
	StripeInvoice response = invoiceService.Get(*invoiceId*);

Retrieving an upcoming invoice (for a single customer)

	var invoiceService = new StripeInvoiceService();
	StripeInvoice response = invoiceService.Upcoming(*customerId*);

Create a customer invoice

	var invoiceService = new StripeInvoiceService();
	StripeInvoice response = invoiceService.Create(*customerId*); // optional StripeInvoiceCreateOptions

Updating a customer invoice

	var stripeInvoiceUpdateOptions = new StripeInvoiceUpdateOptions();
	stripeInvoiceUpdateOptions.Closed = true;

	var invoiceService = new StripeInvoiceService();
	StripeInvoice response = invoiceService.Update(*invoiceId*, stripeInvoiceUpdateOptions);

Paying an invoice

	var invoiceService = new StripeInvoiceService();
	StripeInvoice response = invoiceService.Pay(*invoiceId*);

List all invoices

	var invoiceService = new StripeInvoiceService();
	IEnumerable<StripeInvoice> response = invoiceService.List(); // optional StripeInvoiceListOptions

StripeInvoiceListOptions supports a CustomerId, StripeListOptions for paging, and a StripeDateFilter for date filtering

Invoice Items

Creating an invoice item

Any invoice items you create for a customer will be added to their bill.

	var myItem = new StripeInvoiceItemCreateOptions();
	myItem.Amount = 1000;
	myItem.Currency = "usd";            // "usd" only supported right now
	myItem.CustomerId = *customerId*;
	myItem.Description = "na";          // not required

	var invoiceItemService = new StripeInvoiceItemService();
	StripeInvoiceItem response = invoiceItemService.Create(myItem);

Retrieving an invoice item

	var invoiceItemService = new StripeInvoiceItemService();
	StripeInvoiceItem response = invoiceItemService.Get(*invoiceItemId*);

Updating an invoice item

	var myUpdatedItem = new StripeInvoiceItemUpdateOptions();
	myUpdatedItem.Amount = 1010;
	myUpdatedItem.Currency = "usd";        // "usd" only supported right now
	myUpdatedItem.Description = "test";    // not required

	var invoiceItemService = new StripeInvoiceItemService();
	StripeInvoiceItem response = invoiceItemService.Update(*invoiceItemId*, myUpdatedItem);

Deleting an invoice item

	var invoiceItemService = new StripeInvoiceItemService();
	invoiceItemService.Delete(*invoiceItemId*);

List all invoice items

	var invoiceItemService = new StripeInvoiceItemService();
	IEnumerable<StripeInvoiceItem> response = invoiceItemService.List(); // optional StripeInvoiceItemListOptions

StripeInvoiceItemListOptions supports a CustomerId, StripeListOptions for paging, and a StripeDateFilter for date filtering

Metadata

Various calls allow attaching metadata to them such as Creating a Customer and Creating a Charge.

To delete metadata you can call the update operation and pass an empty string (String.Empty) or null for each value you want to delete.

Payouts

Creating a payout

This will create a payout to the account you setup when activating your Stripe account.

	var payout = new StripePayoutService(*apikey*).Create(
		new StripePayoutCreateOptions
		{
			Amount = 1000,
			Currency = "usd"
		}
	);

Retrieving a payout

	var payout = new StripePayoutService(*apiKey*).Get(*payoutId*);

List all payouts

	var payouts = new StripePayoutService(*apiKey*).List(
		new StripePayoutListOptions
		{
			Created = DateTime.SomeDay,
			ArrivalDate = DateTime.SomeDay
		};
	);

StripeListOptions for paging, and a StripeDateFilter for date filtering (on both the created and date fields)

Plans

Creating a plan

If your site has multiple offerings, plans are perfect. You can create as many plans as you want and then just assign customers to those plans later on.

	var myPlan = new StripePlanCreateOptions();
	myPlan.Id = "hi, im unique!";
	myPlan.Amount = 1000;           // all amounts on Stripe are in cents, pence, etc
	myPlan.Currency = "usd";        // "usd" only supported right now
	myPlan.Interval = "month";      // "month" or "year"
	myPlan.IntervalCount = 1;       // optional
	myPlan.Name = "Bronze";
	myPlan.TrialPeriodDays = 30;    // amount of time that will lapse before the customer is billed

	var planService = new StripePlanService();
	StripePlan response = planService.Create(myPlan);

The returned StripePlan entity above will have a unique Id. You will want to persist this for later. When you create a customer you will be able to assign them to a plan id (or not)

Updating a plan

	var myPlan = new StripePlanUpdateOptions();

	myPlan.Name = "NEW Plan YO!";

	var planService = new StripePlanService();
	StripePlan response = planService.Update(*planId*, myPlan);

Retrieving a plan

	var planService = new StripePlanService();
	StripePlan response = planService.Get(*planId*);

Deleting a plan

	var planService = new StripePlanService();
	planService.Delete(*planId*);

List all plans

	var planService = new StripePlanService();
	IEnumerable<StripePlan> response = planService.List(); // optional StripeListOptions

StripeListOptions for paging

Radar

Radar is a suite of products from Stripe that learns (and improves) from their network of businesses to identify and prevent fraud. I recommend reading more about Radar, and checking out how it works. The Stripe dashboard allows you to add rules for when to block or review payments. You can also enable or disable these rules as needed.

In Stripe.net, once a payment has been processed via Radar, it adds the Review property on StripeCharge. In a nutshell, if your review's Open property is set to true, you will need to take action.

Some example code (in test mode) that will create and expand a review:

	// a token from expiration month "06", expiration year "2020", and card "4000000000009235"
	var token = "tkn_12345"; 
	
	var service = new StripeChargeService();
	
	// Review is an expandable property. If you don't expand it on your service, it will only populate ReviewId!
	service.ExpandReview = true;
	
	var charge = service.Create(new StripeChargeCreateOptions { SourceTokenOrExistingSourceId = token });
	Console.WriteLine(_charge.Review);

Read the docs for the radar review object to see the reasons a review is open or closed here

Refunds

Creating a refund

If you do not specify an amount to refund, the entire amount will be refunded.

	var refundService = new StripeRefundService();

	StripeRefund refund = refundService.Create(*chargeId*, new StripeRefundCreateOptions()
	{
		Amount = 300,
		Reason = StripeRefundReasons.Fradulent
	});

You can also specify Metadata, RefundApplicationFee (Connect Only), and ReverseTransfer (Connect Only)

Retrieving a refund

	var refundService = new StripeRefundService();
	StripeRefund refund = refundService.Get(*refundId*);

Updating a refund

	var refundService = new StripeRefundService();
	StripeRefund refund =
		refundService.Update(*refundId*, new StripeRefundUpdateOptions()
			{
				Metadata = new Dictionary<string, string>() {{ "SomeKey", "SomeValue" }}
			});

List all refunds

	var refundService = new StripeRefundService();
	IEnumerable<StripeRefund> response = refundService.List(); // optional StripeRefundListOptions

StripeRefundListOptions supports ChargeId and StripeListOptions for paging

Sources

Before working with sources, I would recommend you read Getting Started with Sources. Sources allow you to use a common integration for multiple payment types.

Creating a source

var source = new StripeSourceService().Create(
	new StripeSourceCreateOptions
	{
		Type = StripeSourceType.Bitcoin,
		Amount = 1,
		Currency = "usd",
		Owner = new StripeSourceOwner
		{
			Email = "jimmy@hendrix.com",
			CityOrTown = "Mayberry",
			State = "NC"
		}
	}
);

Updating a source

var source = new StripeSourceService().Update(*sourceId*,
	new StripeSourceUpdateOptions
	{
		Owner = new StripeSourceOwner
		{
			Email = "hendrix@hjimmy.com"
		}
	}
);

Retrieving a source

var source = new StripeSourceService().Get(*sourceId*);

Subscriptions

Creating a subscription

	var subscriptionService = new StripeSubscriptionService();
	StripeSubscription stripeSubscription = subscriptionService.Create(*customerId*, *planId*); // optional StripeSubscriptionCreateOptions

Updating a subscription

	var subscriptionService = new StripeSubscriptionService();
	StripeSubscription stripeSubscription = subscriptionService.Update(*subscriptionId*); // optional StripeSubscriptionUpdateOptions

Retrieving a subscription

	var subscriptionService = new StripeSubscriptionService();
	StripeSubscription stripeSubscription = subscriptionService.Get(*subscriptionId*);

Canceling a subscription

	var subscriptionService = new StripeSubscriptionService();
	subscriptionService.Cancel(*subscriptionId*); // optional cancelAtPeriodEnd flag

List all subscriptions

	var subscriptionService = new StripeSubscriptionService();
	IEnumerable<StripeSubscription> response = subscriptionService.List(); // optional StripeSubscriptionListOptions

StripeListOptions for paging

Subscription Items

Creating a subscription item

	var subscriptionItem = new StripeSubscriptionItemService().Create(
		new StripeSubscriptionItemCreateOptions
		{
			SubscriptionId = *subscriptionId*,  // required!
			PlanId = *planId*,                  // required!
			Quantity = 1
		};
	);

Updating a subscription item

	var subscriptionItem = new StripeSubscriptionItemService().Update(
		*subscriptionItemId*,
		new StripeSubscriptionItemUpdateOptions
		{
			Quantity = 2
		};
	);

Retrieving a subscription item

var subscriptionItem = new StripeSubscriptionItemService().Get(*subscriptionItemId*);

List all subscription items

	var subscriptionItems = new StripeSubscriptionItemService().List(
		new StripeSubscriptionItemListOptions
		{ 
			SubscriptionId = *subscriptionId* 
		}
	);

StripeListOptions for paging

Tokens

Creating a token

A token can be used anywhere on Stripe where you would normally pass a card. Once it's created, it can be used on a customer or a charge, but only used once.

For production usage, you'll almost always want to create tokens with either stripe.js or Checkout, but it can be useful to create tokens with Stripe.net for testing.

You generally wouldn't want to use stripe.net to create tokens in production, since creating tokens with your server offers almost no security or compliance benefits - it still involves passing raw card data through your server. If you're OK with the additional compliance burden, it's usually still simpler to pass card data directly to the API. However, there are occasionally situations where it would make sense to create tokens on your server.

	var myToken = new StripeTokenCreateOptions();

	// if you need this...
	myToken.Card = new StripeCreditCardOptions()
	{
		// set these properties if passing full card details (do not
		// set these properties if you set TokenId)
		Number = "4242424242424242",
		ExpirationYear = "2022",
		ExpirationMonth = "10",
		AddressCountry = "US",                // optional
		AddressLine1 = "24 Beef Flank St",    // optional
		AddressLine2 = "Apt 24",              // optional
		AddressCity = "Biggie Smalls",        // optional
		AddressState = "NC",                  // optional
		AddressZip = "27617",                 // optional
		Name = "Joe Meatballs",               // optional
		Cvc = "1223"                          // optional
	};

	// set this property if using a customer (stripe connect only)
	myToken.CustomerId = *customerId*;

	var tokenService = new StripeTokenService();
	StripeToken stripeToken = tokenService.Create(myToken);

Retrieving a token

	var tokenService = new StripeTokenService();
	StripeToken stripeToken = tokenService.Get(*tokenId*);

Transfers

Transfers are for sending funds to another account inside stripe.

Creating a transfer

	var myTransfer = new StripeTransferCreateOptions();
	myTransfer.Amount = 100;
	myTransfer.Currency = "usd";
	myTransfer.Destination = *someAccountId*;

	var transferService = new StripeTransferService();
	StripeTransfer stripeTransfer = transferService.Create(myTransfer);

Retrieving a transfer

	var transferService = new StripeTransferService();
	StripeTransfer stripeTransfer = transferService.Get(*transferId*);

Cancel a transfer

	var transferService = new StripeTransferService();
	StripeTransfer stripeTransfer = transferService.Cancel(*transferId*);

List all transfers

	var transferService = new StripeTransferService();
	IEnumerable<StripeTransfer> response = transferService.List(); // optional StripeTransferListOptions

StripeListOptions for paging, and a StripeDateFilter for date filtering (on both the created and date fields)

StripeDateFilter (date filtering)

Many of the List methods support parameters to filter by date. To use this, use the StripeDateFilter class. You can combine the filters to make complex queries. Some examples are:

	var chargeService = new StripeChargeService();

	var chargesToday = chargeService.List(new StripeChargeListOptions {
		Created = new StripeDateFilter { GreaterThanOrEqual = DateTime.UtcNow.Date }
	});

	var chargesYesterday = chargeService.List(new StripeChargeListOptions {
		Created = new StripeDateFilter {
			GreaterThanOrEqual = DateTime.Now.AddDays(-1).Date,
			LessThan = DateTime.Now.Date
		}
	});

StripeListOptions (paging)

All Stripe List methods support paging, using limit, starting_after and ending_before properties. If you do not specify any options, limit will default to 10. Some examples of retrieving paged data from the StripeChargeService:

	var chargeService = new StripeChargeService();

	// get the first five results
	IEnumerable<StripeCharge> firstPage = chargeService.List(new StripeChargeListOptions {
		Limit = 5
	});

	// get the next five results
	IEnumerable<StripeCharge> nextPage = chargeService.List(new StripeChargeListOptions {
		Limit = 5,
		StartingAfter = firstPage.Last().Id
	});

	// get the previous five results again
	IEnumerable<StripeCharge> previousPage = chargeService.List(new StripeChargeListOptions {
		Limit = 5,
		EndingBefore = nextPage.First().Id
	});

StripeRequestOptions

All of the service methods accept an optional StripeRequestOptions object. This is used if you need an Idempotency Key, if you are using Stripe Connect, or if you want to pass the ApiKey on each method.

	var requestOptions = new StripeRequestOptions();
	requestOptions.ApiKey = *optional*;              // this is not required unless you choose to pass the apikey on every service call
	requestOptions.IdempotencyKey = "some string";   // this is for Idempotent Requests - https://stripe.com/docs/api?lang=curl#idempotent_requests
	requestOptions.StripeConnectAccountId = "acct_*" // if you are using Stripe Connect and want to issue a request on the connected account

About

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages