Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,32 @@ how to use the library.

## Usage

### Using StripeClient
Comment thread
ramya-stripe marked this conversation as resolved.

Stripe .NET SDK offers a more flexible approach of working with the Stripe API through the `StripeClient` class.
StripeClient pattern has several advantages over the traditional approach of instantiating services with a global configuration:
- Client based configuration: Allows you to instantiate several clients with different configurations(API key, network retries etc.).
- Single Root Entry Point: The StripeClient provides a centralized entry point to all available Stripe services. This improves discoverability through IDE auto-completion and creates a more intuitive developer experience for you.
Comment thread
ramya-stripe marked this conversation as resolved.
Outdated

```C#
// StripeClient pattern (Recommended)
var client = new StripeClient("sk_test_...");
Customer customer = client.V1.Customers.Get("cus_1234");

// Global Configuration pattern (Legacy)
StripeConfiguration.ApiKey = "sk_test_...";
var service = new CustomerService();
Customer customer = service.Get("cus_1234");
```

The traditional Global Configuration pattern will continue to work for everyone.
Comment thread
ramya-stripe marked this conversation as resolved.
Outdated

### Authentication

Stripe authenticates API requests using your account’s secret key, which you can find in the Stripe Dashboard. By default, secret keys can be used to perform any API request without restriction.

Use `StripeConfiguration.ApiKey` property to set the secret key.

```C#
StripeConfiguration.ApiKey = "sk_test_...";
var client = new StripeClient("sk_test_...");
```

### Creating a resource
Expand All @@ -63,8 +81,8 @@ var options = new CustomerCreateOptions
Email = "customer@example.com"
};

var service = new CustomerService();
Customer customer = service.Create(options);
var client = new StripeClient("sk_test_...");
Customer customer = client.V1.Customers.Create(options);

// Newly created customer is returned
Console.WriteLine(customer.Email);
Expand All @@ -75,8 +93,8 @@ Console.WriteLine(customer.Email);
The `Retrieve` method of the service class can be used to retrieve a resource:

```C#
var service = new CustomerService();
Customer customer = service.Get("cus_1234");
var client = new StripeClient("sk_test_...");
Customer customer = client.V1.Customers.Get("cus_1234");

Console.WriteLine(customer.Email);
```
Expand All @@ -91,8 +109,8 @@ var options = new CustomerUpdateOptions
Email = "updated-email@example.com"
};

var service = new CustomerService();
Customer customer = service.Update("cus_123", options);
var client = new StripeClient("sk_test_...");
Customer customer = client.V1.Customers.Update("cus_123", options);

// The updated customer is returned
Console.WriteLine(customer.Email);
Expand All @@ -103,8 +121,8 @@ Console.WriteLine(customer.Email);
The `Delete` method of the service class can be used to delete a resource:

```C#
var service = new CustomerService();
Customer customer = service.Delete("cus_123", options);
var client = new StripeClient("sk_test_...");
Customer customer = client.V1.Customers.Delete("cus_123", options);
```

### Listing a resource
Expand All @@ -115,8 +133,8 @@ The `List` method on the service class can be used to list resources page-by-pag
> The `List` method returns only a single page, you have to manually continue the iteration using the `StartingAfter` parameter.

```C#
var service = new CustomerService();
var customers = service.List();
var client = new StripeClient("sk_test_...");
var customers = client.V1.Customers.List();

string lastId = null;

Expand Down Expand Up @@ -145,8 +163,8 @@ foreach (Customer customer in customers)
The `ListAutoPaging` method on the service class can be used to automatically iterate over all pages.

```C#
var service = new CustomerService();
var customers = service.ListAutoPaging();
var client = new StripeClient("sk_test_...");
var customers = client.V1.Customers.ListAutoPaging();

// Enumerate all pages of the list
foreach (Customer customer in customers)
Expand All @@ -157,7 +175,7 @@ foreach (Customer customer in customers)

### Per-request configuration

All of the service methods accept an optional `RequestOptions` object. This is
All the service methods accept an optional `RequestOptions` object. This is
used if you want to set an [idempotency key][idempotency-keys], if you are
using [Stripe Connect][connect-auth], or if you want to pass the secret API
key on each method.
Expand Down Expand Up @@ -216,22 +234,21 @@ options.AddExtraParam("secret_feature_enabled", "true");
options.AddExtraParam("secret_parameter[primary]", "primary value");
options.AddExtraParam("secret_parameter[secondary]", "secondary value");

var service = new CustomerService();
var customer = service.Create(options);
var client = new StripeClient("sk_test_...");
var customer = client.V1.Customers.Create(options);
```

#### Properties

To retrieve undocumented properties from Stripe using C# you can use an option in the library to return the raw JSON object and return the property. An example of this is shown below:

```c#
var service = new CustomerService();
var customer = service.Get("cus_1234");
var client = new StripeClient("sk_test_...");
var customer = client.V1.Customers.Get("cus_1234");

customer.RawJObject["secret_feature_enabled"];
customer.RawJObject["secret_parameter"]["primary"];
customer.RawJObject["secret_parameter"]["secondary"];

```

### Writing a plugin
Expand Down
Loading