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 9226b9a commit 7c1cfc7
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.6.0] - 2024-23-09
## [4.6.0] - 2024-17-10

### Added

- Add List of Live Api Keys `Organizations.ListAsyncLiveApiKey`
- Add Delete of a Live Api Key `Organization.DeleteAsyncLiveApiKey`
- Add Create Webhook `Webhooks.CreateAsync`
- Add Update Webhook `Webhooks.UpdateAsync`
- Add Retrieve Webhook `Webhooks.RetrieveAsync`
- Add Delete Webhook `Webhooks.DeleteAsync`
- Add List Webhooks `Webhooks.ListAsync`
- Add Validate Signature Webhook `Webhooks.ValidateSignatureAsync`


## [4.5.0] - 2024-06-05

Expand Down
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 7c1cfc7

Please sign in to comment.