Skip to content

Commit 345830e

Browse files
authored
Support for StripeAccount in StripeClient and StripeContext in StripeClientOptions (#3058)
1 parent 0204e61 commit 345830e

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

src/Stripe.net/Infrastructure/Public/StripeClientOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public class StripeClientOptions
3636
/// <value>The base URL for Stripe's Meter Events API.</value>
3737
public string MeterEventsBase { get; set; }
3838

39+
/// <summary>Gets and sets the Stripe-Account header value for requests made from this client.</summary>
40+
public string StripeAccount { get; set; }
41+
42+
/// <summary>Gets and sets the Stripe-Context header value for requests made from this client.</summary>
43+
public string StripeContext { get; set; }
44+
3945
internal StripeClientOptions Clone()
4046
{
4147
return (StripeClientOptions)this.MemberwiseClone();

src/Stripe.net/Services/_common/RequestOptions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ internal static T WithClientOptions<T>(this T requestOptions, StripeClientOption
7171
clone.ApiKey = clientOptions.ApiKey;
7272
}
7373

74+
if (string.IsNullOrEmpty(clone.StripeContext))
75+
{
76+
clone.StripeContext = clientOptions.StripeContext;
77+
}
78+
79+
if (string.IsNullOrEmpty(clone.StripeAccount))
80+
{
81+
clone.StripeAccount = clientOptions.StripeAccount;
82+
}
83+
7484
return clone;
7585
}
7686

src/StripeTests/Infrastructure/Public/LiveApiRequestorTest.cs

+49
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,55 @@ public void Deserialize()
641641
Assert.Equal(typeof(Foo), deserialized.GetType());
642642
}
643643

644+
[Fact]
645+
public async Task StripeContextHeaderSet()
646+
{
647+
var apiRequestorWithContext = new LiveApiRequestor(new StripeClientOptions
648+
{
649+
ApiKey = "sk_test_123",
650+
HttpClient = this.httpClient,
651+
StripeContext = "ctx_1234",
652+
});
653+
var response = new StripeResponse(HttpStatusCode.OK, null, "{}");
654+
this.httpClient.Response = response;
655+
656+
var service = new CustomerService(apiRequestorWithContext);
657+
658+
await service.CreateAsync(new CustomerCreateOptions() { });
659+
var lastRequest = this.httpClient.LastRequest;
660+
Assert.Equal("ctx_1234", lastRequest.StripeHeaders["Stripe-Context"]);
661+
662+
// If its set in the request options, that takes precendence
663+
await service.CreateAsync(new CustomerCreateOptions() { }, new RequestOptions { StripeContext = "ctx_2345" });
664+
lastRequest = this.httpClient.LastRequest;
665+
Assert.Equal("ctx_2345", lastRequest.StripeHeaders["Stripe-Context"]);
666+
}
667+
668+
[Fact]
669+
public async Task StripeAccountHeaderSet()
670+
{
671+
var apiRequestorWithContext = new LiveApiRequestor(new StripeClientOptions
672+
{
673+
ApiKey = "sk_test_123",
674+
HttpClient = this.httpClient,
675+
StripeAccount = "acct_1234",
676+
});
677+
678+
var response = new StripeResponse(HttpStatusCode.OK, null, "{}");
679+
this.httpClient.Response = response;
680+
681+
var service = new CustomerService(apiRequestorWithContext);
682+
683+
await service.CreateAsync(new CustomerCreateOptions() { });
684+
var lastRequest = this.httpClient.LastRequest;
685+
Assert.Equal("acct_1234", lastRequest.StripeHeaders["Stripe-Account"]);
686+
687+
// If its set in the request options, that takes precendence
688+
await service.CreateAsync(new CustomerCreateOptions() { }, new RequestOptions { StripeAccount = "acct_2345" });
689+
lastRequest = this.httpClient.LastRequest;
690+
Assert.Equal("acct_2345", lastRequest.StripeHeaders["Stripe-Account"]);
691+
}
692+
644693
private class Foo : StripeEntity<Foo>
645694
{
646695
[JsonProperty("bar")]

src/StripeTests/Infrastructure/Public/StripeClientTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public void Ctr_StripeClientOptions()
3333
MeterEventsBase = "localhost:6502",
3434
FilesBase = "localhost:555",
3535
HttpClient = new TestHttpClient(),
36+
StripeAccount = "acct",
37+
StripeContext = "ctx",
3638
};
3739

3840
// This test is designed to ensure StripeClient properly consumes all options. If any are
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace StripeTests
2+
{
3+
using System;
4+
using Stripe;
5+
using Stripe.Infrastructure.FormEncoding;
6+
using Xunit;
7+
8+
public class RequestOptionsTest : BaseStripeTest
9+
{
10+
[Fact]
11+
public void WithClientOptionsSetsApiKey()
12+
{
13+
var clientOptions = new StripeClientOptions
14+
{
15+
ApiKey = "12345",
16+
};
17+
18+
// Should copy when request option is empty
19+
var options = new RequestOptions();
20+
var newOptions = options.WithClientOptions(clientOptions);
21+
Assert.Equal(clientOptions.ApiKey, newOptions.ApiKey);
22+
23+
// Should NOT copy when request option is set
24+
options = new RequestOptions() { ApiKey = "23456" };
25+
newOptions = options.WithClientOptions(clientOptions);
26+
Assert.Equal(options.ApiKey, newOptions.ApiKey);
27+
}
28+
29+
[Fact]
30+
31+
public void WithClientOptionsSetsStripeContext()
32+
{
33+
var clientOptions = new StripeClientOptions
34+
{
35+
StripeContext = "ctx_12345",
36+
};
37+
38+
// Should copy when request option is empty
39+
var options = new RequestOptions();
40+
var newOptions = options.WithClientOptions(clientOptions);
41+
Assert.Equal(clientOptions.StripeContext, newOptions.StripeContext);
42+
43+
// Should NOT copy when request option is set
44+
options = new RequestOptions() { StripeContext = "ctx_23456" };
45+
newOptions = options.WithClientOptions(clientOptions);
46+
Assert.Equal(options.StripeContext, newOptions.StripeContext);
47+
}
48+
49+
[Fact]
50+
public void WithClientOptionsSetsStripeAccount()
51+
{
52+
var clientOptions = new StripeClientOptions
53+
{
54+
StripeAccount = "acct_12345",
55+
};
56+
57+
// Should copy when request option is empty
58+
var options = new RequestOptions();
59+
var newOptions = options.WithClientOptions(clientOptions);
60+
Assert.Equal(clientOptions.StripeAccount, newOptions.StripeAccount);
61+
62+
// Should NOT copy when request option is set
63+
options = new RequestOptions() { StripeAccount = "acct_23456" };
64+
newOptions = options.WithClientOptions(clientOptions);
65+
Assert.Equal(options.StripeAccount, newOptions.StripeAccount);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)