-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(webhooks): add webhooks methods with signatureValidation method
- Loading branch information
1 parent
9226b9a
commit 7c1cfc7
Showing
6 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Facturapi | ||
{ | ||
public static class WebhooksEvents | ||
{ | ||
public const string INVOICE_CREATED = "invoice.global_invoice_created"; | ||
public const string STATUS_UPDATED = "invoice.status_updated"; | ||
public const string CANCELLATION_STATUS_UPDATED = "invoice.cancellation_status_updated"; | ||
public const string SELF_INVOICE_COMPLETE = "receipt.self_invoice_complete"; | ||
public const string RECEIPT_STATUS_UPDATED = "receipt.status_updated"; | ||
public const string RECEIPT_CANCELLATION_STATUS_UPDATED = "receipt.cancellation_status_updated"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace Facturapi | ||
{ | ||
public class Webhook | ||
|
||
{ | ||
public string Id { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
|
||
public string[] EnabledEvents { get; set; } | ||
|
||
public bool Livemode { get; set; } | ||
|
||
public Organization Organization { get; set; } | ||
|
||
public string Url { get; set; } | ||
|
||
public string Status { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Facturapi | ||
{ | ||
public class WebhookValidateSignature | ||
|
||
{ | ||
public string Id { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
|
||
public bool Livemode { get; set; } | ||
|
||
public string Organization { get; set; } | ||
|
||
public string Type { get; set; } | ||
|
||
public object Data { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Facturapi | ||
{ | ||
internal static partial class Router | ||
{ | ||
public static string ListWebhooks(Dictionary<string, object> query = null) | ||
{ | ||
return UriWithQuery("webhooks", query); | ||
} | ||
|
||
public static string RetrieveWebhook(string id) | ||
{ | ||
return $"webhooks/{id}"; | ||
} | ||
|
||
public static string CreateWebhook() | ||
{ | ||
return "webhooks"; | ||
} | ||
|
||
public static string UpdateWebhook(string id) | ||
{ | ||
return RetrieveWebhook(id); | ||
} | ||
|
||
public static string DeleteWebhook(string id) | ||
{ | ||
return RetrieveWebhook(id); | ||
} | ||
|
||
public static string ValidateSignature() | ||
{ | ||
return "webhooks/validate-signature"; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Facturapi.Wrappers | ||
{ | ||
public class WebhookWrapper : BaseWrapper | ||
{ | ||
public WebhookWrapper(string apiKey, string apiVersion = "v2") : base(apiKey, apiVersion) | ||
{ | ||
} | ||
|
||
public async Task<SearchResult<Webhook>> ListAsync(Dictionary<string, object> query = null) | ||
{ | ||
var response = await client.GetAsync(Router.ListWebhooks(query)); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
|
||
var searchResult = JsonConvert.DeserializeObject<SearchResult<Webhook>>(resultString, this.jsonSettings); | ||
return searchResult; | ||
} | ||
|
||
public async Task<Webhook> CreateAsync(Dictionary<string, object> data) | ||
{ | ||
var response = await client.PostAsync(Router.CreateWebhook(), new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
var webhook = JsonConvert.DeserializeObject<Webhook>(resultString, this.jsonSettings); | ||
return webhook; | ||
} | ||
|
||
public async Task<Webhook> RetrieveAsync(string id) | ||
{ | ||
var response = await client.GetAsync(Router.RetrieveWebhook(id)); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
var webhook = JsonConvert.DeserializeObject<Webhook>(resultString, this.jsonSettings); | ||
return webhook; | ||
} | ||
|
||
public async Task<Webhook> UpdateAsync(string id, Dictionary<string, object> data) | ||
{ | ||
var response = await client.PutAsync(Router.UpdateWebhook(id), new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
var webhook = JsonConvert.DeserializeObject<Webhook>(resultString, this.jsonSettings); | ||
return webhook; | ||
} | ||
|
||
public async Task<Webhook> DeleteAsync(string id) | ||
{ | ||
var response = await client.DeleteAsync(Router.DeleteWebhook(id)); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
var webhook = JsonConvert.DeserializeObject<Webhook>(resultString, this.jsonSettings); | ||
return webhook; | ||
} | ||
|
||
public async Task<Webhook> ValidateSignatureAsync(Dictionary<string, object> data) | ||
{ | ||
var response = await client.PostAsync(Router.ValidateSignature(),new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); | ||
var resultString = await response.Content.ReadAsStringAsync(); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var error = JsonConvert.DeserializeObject<JObject>(resultString); | ||
throw new FacturapiException(error["message"].ToString()); | ||
} | ||
var webhook = JsonConvert.DeserializeObject<Webhook>(resultString, this.jsonSettings); | ||
return webhook; | ||
} | ||
|
||
} | ||
} |