-
Notifications
You must be signed in to change notification settings - Fork 566
/
StripeClient.cs
220 lines (189 loc) · 8.56 KB
/
StripeClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
namespace Stripe
{
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Stripe.Infrastructure;
/// <summary>
/// A Stripe client, used to issue requests to Stripe's API and deserialize responses.
/// </summary>
public class StripeClient : IStripeClient
{
/// <summary>Initializes a new instance of the <see cref="StripeClient"/> class.</summary>
/// <param name="apiKey">The API key used by the client to make requests.</param>
/// <param name="clientId">The client ID used by the client in OAuth requests.</param>
/// <param name="httpClient">
/// The <see cref="IHttpClient"/> client to use. If <c>null</c>, an HTTP client will be
/// created with default parameters.
/// </param>
/// <param name="apiBase">
/// The base URL for Stripe's API. Defaults to <see cref="DefaultApiBase"/>.
/// </param>
/// <param name="connectBase">
/// The base URL for Stripe's OAuth API. Defaults to <see cref="DefaultConnectBase"/>.
/// </param>
/// <param name="filesBase">
/// The base URL for Stripe's Files API. Defaults to <see cref="DefaultFilesBase"/>.
/// </param>
/// <exception cref="ArgumentNullException">if <c>apiKey</c> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">
/// if <c>apiKey</c> is empty or contains whitespace.
/// </exception>
public StripeClient(
string apiKey = null,
string clientId = null,
IHttpClient httpClient = null,
string apiBase = null,
string connectBase = null,
string filesBase = null)
{
if (apiKey != null && apiKey.Length == 0)
{
throw new ArgumentException("API key cannot be the empty string.", nameof(apiKey));
}
if (apiKey != null && StringUtils.ContainsWhitespace(apiKey))
{
throw new ArgumentException("API key cannot contain whitespace.", nameof(apiKey));
}
this.ApiKey = apiKey;
this.ClientId = clientId;
this.HttpClient = httpClient ?? BuildDefaultHttpClient();
this.ApiBase = apiBase ?? DefaultApiBase;
this.ConnectBase = connectBase ?? DefaultConnectBase;
this.FilesBase = filesBase ?? DefaultFilesBase;
}
/// <summary>Default base URL for Stripe's API.</summary>
public static string DefaultApiBase => "https://api.stripe.com";
/// <summary>Default base URL for Stripe's OAuth API.</summary>
public static string DefaultConnectBase => "https://connect.stripe.com";
/// <summary>Default base URL for Stripe's Files API.</summary>
public static string DefaultFilesBase => "https://files.stripe.com";
/// <summary>Gets the base URL for Stripe's API.</summary>
/// <value>The base URL for Stripe's API.</value>
public string ApiBase { get; }
/// <summary>Gets the API key used by the client to send requests.</summary>
/// <value>The API key used by the client to send requests.</value>
public string ApiKey { get; }
/// <summary>Gets the client ID used by the client in OAuth requests.</summary>
/// <value>The client ID used by the client in OAuth requests.</value>
public string ClientId { get; }
/// <summary>Gets the base URL for Stripe's OAuth API.</summary>
/// <value>The base URL for Stripe's OAuth API.</value>
public string ConnectBase { get; }
/// <summary>Gets the base URL for Stripe's Files API.</summary>
/// <value>The base URL for Stripe's Files API.</value>
public string FilesBase { get; }
/// <summary>Gets the <see cref="IHttpClient"/> used to send HTTP requests.</summary>
/// <value>The <see cref="IHttpClient"/> used to send HTTP requests.</value>
public IHttpClient HttpClient { get; }
/// <summary>Sends a request to Stripe's API as an asynchronous operation.</summary>
/// <typeparam name="T">Type of the Stripe entity returned by the API.</typeparam>
/// <param name="method">The HTTP method.</param>
/// <param name="path">The path of the request.</param>
/// <param name="options">The parameters of the request.</param>
/// <param name="requestOptions">The special modifiers of the request.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
/// <exception cref="StripeException">Thrown if the request fails.</exception>
public async Task<T> RequestAsync<T>(
HttpMethod method,
string path,
BaseOptions options,
RequestOptions requestOptions,
CancellationToken cancellationToken = default)
where T : IStripeEntity
{
var request = new StripeRequest(this, method, path, options, requestOptions);
var response = await this.HttpClient.MakeRequestAsync(request, cancellationToken)
.ConfigureAwait(false);
return ProcessResponse<T>(response);
}
/// <inheritdoc/>
public async Task<Stream> RequestStreamingAsync(
HttpMethod method,
string path,
BaseOptions options,
RequestOptions requestOptions,
CancellationToken cancellationToken = default)
{
var request = new StripeRequest(this, method, path, options, requestOptions);
var response = await this.HttpClient.MakeStreamingRequestAsync(request, cancellationToken)
.ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK)
{
return response.Body;
}
var readResponse = await response.ToStripeResponseAsync().ConfigureAwait(false);
throw BuildStripeException(readResponse);
}
private static IHttpClient BuildDefaultHttpClient()
{
return new SystemNetHttpClient();
}
private static T ProcessResponse<T>(StripeResponse response)
where T : IStripeEntity
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw BuildStripeException(response);
}
T obj;
try
{
obj = StripeEntity.FromJson<T>(response.Content);
}
catch (Newtonsoft.Json.JsonException)
{
throw BuildInvalidResponseException(response);
}
obj.StripeResponse = response;
return obj;
}
private static StripeException BuildStripeException(StripeResponse response)
{
JObject jObject = null;
try
{
jObject = JObject.Parse(response.Content);
}
catch (Newtonsoft.Json.JsonException)
{
return BuildInvalidResponseException(response);
}
// If the value of the `error` key is a string, then the error is an OAuth error
// and we instantiate the StripeError object with the entire JSON.
// Otherwise, it's a regular API error and we instantiate the StripeError object
// with just the nested hash contained in the `error` key.
var errorToken = jObject["error"];
if (errorToken == null)
{
return BuildInvalidResponseException(response);
}
var stripeError = errorToken.Type == JTokenType.String
? StripeError.FromJson(response.Content)
: StripeError.FromJson(errorToken.ToString());
stripeError.StripeResponse = response;
return new StripeException(
response.StatusCode,
stripeError,
stripeError.Message ?? stripeError.ErrorDescription)
{
StripeResponse = response,
};
}
private static StripeException BuildInvalidResponseException(StripeResponse response)
{
return new StripeException(
response.StatusCode,
null,
$"Invalid response object from API: \"{response.Content}\"")
{
StripeResponse = response,
};
}
}
}