Skip to content

Commit 11507b1

Browse files
authored
Merge pull request #1342 from stripe/remi-add-webhook-endpoint
Add support for the Webhook Endpoint resource
2 parents 2668d6b + 449ba6e commit 11507b1

File tree

8 files changed

+329
-0
lines changed

8 files changed

+329
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace Stripe
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
using Stripe.Infrastructure;
7+
8+
public class WebhookEndpoint : StripeEntity, IHasId, IHasObject
9+
{
10+
[JsonProperty("id")]
11+
public string Id { get; set; }
12+
13+
[JsonProperty("object")]
14+
public string Object { get; set; }
15+
16+
[JsonProperty("application")]
17+
public string ApplicationId { get; set; }
18+
19+
[JsonProperty("connect")]
20+
public bool Connect { get; set; }
21+
22+
[JsonProperty("created")]
23+
public DateTime Created { get; set; }
24+
25+
[JsonProperty("deleted")]
26+
public bool Deleted { get; set; }
27+
28+
[JsonProperty("enabled_events")]
29+
public string[] EnabledEvents { get; set; }
30+
31+
[JsonProperty("livemode")]
32+
public bool Livemode { get; set; }
33+
34+
[JsonProperty("secret")]
35+
public string Secret { get; set; }
36+
37+
[JsonProperty("status")]
38+
public string Status { get; set; }
39+
40+
[JsonProperty("url")]
41+
public string Url { get; set; }
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Stripe
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
using Stripe.Infrastructure;
7+
8+
public class WebhookEndpointCreateOptions : WebhookEndpointSharedOptions
9+
{
10+
[JsonProperty("connect")]
11+
public bool? Connect { get; set; }
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Stripe
2+
{
3+
using Newtonsoft.Json;
4+
5+
public class WebhookEndpointListOptions : ListOptionsWithCreated
6+
{
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace Stripe
2+
{
3+
using System.Collections.Generic;
4+
using System.Net;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Stripe.Infrastructure;
8+
9+
public class WebhookEndpointService : Service<WebhookEndpoint>,
10+
ICreatable<WebhookEndpoint, WebhookEndpointCreateOptions>,
11+
IDeletable<WebhookEndpoint>,
12+
IListable<WebhookEndpoint, WebhookEndpointListOptions>,
13+
IRetrievable<WebhookEndpoint>,
14+
IUpdatable<WebhookEndpoint, WebhookEndpointUpdateOptions>
15+
{
16+
public WebhookEndpointService()
17+
: base(null)
18+
{
19+
}
20+
21+
public WebhookEndpointService(string apiKey)
22+
: base(apiKey)
23+
{
24+
}
25+
26+
public override string BasePath => "/webhook_endpoints";
27+
28+
public virtual WebhookEndpoint Create(WebhookEndpointCreateOptions options, RequestOptions requestOptions = null)
29+
{
30+
return this.CreateEntity(options, requestOptions);
31+
}
32+
33+
public virtual Task<WebhookEndpoint> CreateAsync(WebhookEndpointCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
34+
{
35+
return this.CreateEntityAsync(options, requestOptions, cancellationToken);
36+
}
37+
38+
public virtual WebhookEndpoint Delete(string endpointId, RequestOptions requestOptions = null)
39+
{
40+
return this.DeleteEntity(endpointId, null, requestOptions);
41+
}
42+
43+
public virtual Task<WebhookEndpoint> DeleteAsync(string endpointId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
44+
{
45+
return this.DeleteEntityAsync(endpointId, null, requestOptions, cancellationToken);
46+
}
47+
48+
public virtual WebhookEndpoint Get(string endpointId, RequestOptions requestOptions = null)
49+
{
50+
return this.GetEntity(endpointId, null, requestOptions);
51+
}
52+
53+
public virtual Task<WebhookEndpoint> GetAsync(string endpointId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
54+
{
55+
return this.GetEntityAsync(endpointId, null, requestOptions, cancellationToken);
56+
}
57+
58+
public virtual StripeList<WebhookEndpoint> List(WebhookEndpointListOptions options = null, RequestOptions requestOptions = null)
59+
{
60+
return this.ListEntities(options, requestOptions);
61+
}
62+
63+
public virtual Task<StripeList<WebhookEndpoint>> ListAsync(WebhookEndpointListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
64+
{
65+
return this.ListEntitiesAsync(options, requestOptions, cancellationToken);
66+
}
67+
68+
public virtual WebhookEndpoint Update(string endpointId, WebhookEndpointUpdateOptions options, RequestOptions requestOptions = null)
69+
{
70+
return this.UpdateEntity(endpointId, options, requestOptions);
71+
}
72+
73+
public virtual Task<WebhookEndpoint> UpdateAsync(string endpointId, WebhookEndpointUpdateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
74+
{
75+
return this.UpdateEntityAsync(endpointId, options, requestOptions, cancellationToken);
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Stripe
2+
{
3+
using System.Collections.Generic;
4+
using Newtonsoft.Json;
5+
6+
public abstract class WebhookEndpointSharedOptions : BaseOptions
7+
{
8+
[JsonProperty("enabled_events")]
9+
public string[] EnabledEvents { get; set; }
10+
11+
[JsonProperty("url")]
12+
public string Url { get; set; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Stripe
2+
{
3+
using System.Collections.Generic;
4+
using Newtonsoft.Json;
5+
6+
public class WebhookEndpointUpdateOptions : WebhookEndpointSharedOptions
7+
{
8+
[JsonProperty("disabled")]
9+
public bool? Disabled { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace StripeTests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
using Newtonsoft.Json;
8+
using Stripe;
9+
using Xunit;
10+
11+
public class WebhookEndpointTest : BaseStripeTest
12+
{
13+
[Fact]
14+
public void Deserialize()
15+
{
16+
string json = GetFixture("/v1/webhook_endpoints/we_123");
17+
var endpoint = Mapper<WebhookEndpoint>.MapFromJson(json);
18+
Assert.NotNull(endpoint);
19+
Assert.IsType<WebhookEndpoint>(endpoint);
20+
Assert.NotNull(endpoint.Id);
21+
Assert.Equal("webhook_endpoint", endpoint.Object);
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
namespace StripeTests
2+
{
3+
using System.Collections.Generic;
4+
using System.Net.Http;
5+
using System.Threading.Tasks;
6+
7+
using Stripe;
8+
using Xunit;
9+
10+
public class WebhookEndpointServiceTest : BaseStripeTest
11+
{
12+
private const string WebhookEndpointId = "we_123";
13+
14+
private WebhookEndpointService service;
15+
private WebhookEndpointCreateOptions createOptions;
16+
private WebhookEndpointUpdateOptions updateOptions;
17+
private WebhookEndpointListOptions listOptions;
18+
19+
public WebhookEndpointServiceTest()
20+
{
21+
this.service = new WebhookEndpointService();
22+
23+
this.createOptions = new WebhookEndpointCreateOptions()
24+
{
25+
EnabledEvents = new string[]
26+
{
27+
"charge.succeeded",
28+
},
29+
Url = "https://stripe.com",
30+
};
31+
32+
this.updateOptions = new WebhookEndpointUpdateOptions()
33+
{
34+
EnabledEvents = new string[]
35+
{
36+
"charge.succeeded",
37+
},
38+
};
39+
40+
this.listOptions = new WebhookEndpointListOptions()
41+
{
42+
Limit = 1,
43+
};
44+
}
45+
46+
[Fact]
47+
public void Create()
48+
{
49+
var endpoint = this.service.Create(this.createOptions);
50+
this.AssertRequest(HttpMethod.Post, "/v1/webhook_endpoints");
51+
Assert.NotNull(endpoint);
52+
Assert.Equal("webhook_endpoint", endpoint.Object);
53+
}
54+
55+
[Fact]
56+
public async Task CreateAsync()
57+
{
58+
var endpoint = await this.service.CreateAsync(this.createOptions);
59+
this.AssertRequest(HttpMethod.Post, "/v1/webhook_endpoints");
60+
Assert.NotNull(endpoint);
61+
Assert.Equal("webhook_endpoint", endpoint.Object);
62+
}
63+
64+
[Fact]
65+
public void Delete()
66+
{
67+
var deleted = this.service.Delete(WebhookEndpointId);
68+
this.AssertRequest(HttpMethod.Delete, "/v1/webhook_endpoints/we_123");
69+
Assert.NotNull(deleted);
70+
}
71+
72+
[Fact]
73+
public async Task DeleteAsync()
74+
{
75+
var deleted = await this.service.DeleteAsync(WebhookEndpointId);
76+
this.AssertRequest(HttpMethod.Delete, "/v1/webhook_endpoints/we_123");
77+
Assert.NotNull(deleted);
78+
}
79+
80+
[Fact]
81+
public void Get()
82+
{
83+
var endpoint = this.service.Get(WebhookEndpointId);
84+
this.AssertRequest(HttpMethod.Get, "/v1/webhook_endpoints/we_123");
85+
Assert.NotNull(endpoint);
86+
Assert.Equal("webhook_endpoint", endpoint.Object);
87+
}
88+
89+
[Fact]
90+
public async Task GetAsync()
91+
{
92+
var endpoint = await this.service.GetAsync(WebhookEndpointId);
93+
this.AssertRequest(HttpMethod.Get, "/v1/webhook_endpoints/we_123");
94+
Assert.NotNull(endpoint);
95+
Assert.Equal("webhook_endpoint", endpoint.Object);
96+
}
97+
98+
[Fact]
99+
public void List()
100+
{
101+
var endpoints = this.service.List(this.listOptions);
102+
this.AssertRequest(HttpMethod.Get, "/v1/webhook_endpoints");
103+
Assert.NotNull(endpoints);
104+
Assert.Equal("list", endpoints.Object);
105+
Assert.Single(endpoints.Data);
106+
Assert.Equal("webhook_endpoint", endpoints.Data[0].Object);
107+
}
108+
109+
[Fact]
110+
public async Task ListAsync()
111+
{
112+
var endpoints = await this.service.ListAsync(this.listOptions);
113+
this.AssertRequest(HttpMethod.Get, "/v1/webhook_endpoints");
114+
Assert.NotNull(endpoints);
115+
Assert.Equal("list", endpoints.Object);
116+
Assert.Single(endpoints.Data);
117+
Assert.Equal("webhook_endpoint", endpoints.Data[0].Object);
118+
}
119+
120+
[Fact]
121+
public void Update()
122+
{
123+
var endpoint = this.service.Update(WebhookEndpointId, this.updateOptions);
124+
this.AssertRequest(HttpMethod.Post, "/v1/webhook_endpoints/we_123");
125+
Assert.NotNull(endpoint);
126+
Assert.Equal("webhook_endpoint", endpoint.Object);
127+
}
128+
129+
[Fact]
130+
public async Task UpdateAsync()
131+
{
132+
var endpoint = await this.service.UpdateAsync(WebhookEndpointId, this.updateOptions);
133+
this.AssertRequest(HttpMethod.Post, "/v1/webhook_endpoints/we_123");
134+
Assert.NotNull(endpoint);
135+
Assert.Equal("webhook_endpoint", endpoint.Object);
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)