Skip to content

Commit

Permalink
chore(webhooks): add webhooks methods with signatureValidation method
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-facturapi committed Oct 17, 2024
1 parent dccfa6b commit 155bf53
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Constants/WebhookEvents.cs
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";
}
}
21 changes: 21 additions & 0 deletions Models/Webhook.cs
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; }
}
}
20 changes: 20 additions & 0 deletions Models/WebhookValidateSignature.cs
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; }

}
}
40 changes: 40 additions & 0 deletions Router/WebhookRouter.cs
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";
}


}
}
98 changes: 98 additions & 0 deletions Wrappers/WebhookWrapper.cs
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;
}

}
}

0 comments on commit 155bf53

Please sign in to comment.