Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions tests/PartnerGatewayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace tests
{
[TestClass]
public class PartnerGatewayTest
{
[TestMethod]
public void DedicatedMethodsCallDocumentedPartnerEndpoints()
{
var body = new { enabled = true };

AssertRequest("GET", "/v1/fees", null, partner => partner.GetFees());
AssertRequest("GET", "/v1/fees?currency=USD", null, partner => partner.GetFees("USD"));
AssertRequest("PATCH", "/v1/fees", "{\"enabled\":true}", partner => partner.UpdateFees(body));
AssertRequest("GET", "/v1/payout-methods", null, partner => partner.ListPayoutMethods());
AssertRequest("GET", "/v1/payout-methods/bank-transfer", null, partner => partner.GetPayoutMethod("bank-transfer"));
AssertRequest("PATCH", "/v1/payout-methods/bank-transfer", "{\"enabled\":true}", partner => partner.UpdatePayoutMethod("bank-transfer", body));
AssertRequest("GET", "/v1/processing-settings", null, partner => partner.GetProcessingSettings());
AssertRequest("PATCH", "/v1/processing-settings", "{\"enabled\":true}", partner => partner.UpdateProcessingSettings(body));
AssertRequest("GET", "/v1/white-label/dns-records", null, partner => partner.GetWhiteLabelDnsRecords());
AssertRequest("POST", "/v1/white-label/dns-records/verify", "{}", partner => partner.VerifyWhiteLabelDnsRecords());
AssertRequest("DELETE", "/v1/white-label/email", null, partner => partner.DeleteWhiteLabelEmail());
AssertRequest("PATCH", "/v1/white-label/icon", "{\"enabled\":true}", partner => partner.UpdateWhiteLabelIcon(body));
AssertRequest("GET", "/v1/white-label", null, partner => partner.GetWhiteLabelSettings());
AssertRequest("PATCH", "/v1/white-label", "{\"enabled\":true}", partner => partner.UpdateWhiteLabelSettings(body));
AssertRequest("GET", "/v1/iframe/config", null, partner => partner.GetWidgetConfiguration());
AssertRequest("POST", "/v1/iframe/config", "{\"enabled\":true}", partner => partner.UpdateWidgetConfiguration(body));
}

private static void AssertRequest(
string expectedMethod,
string expectedPath,
string expectedBody,
Func<Trolley.PartnerGateway, string> request)
{
string method = null;
string path = null;
string body = null;

WithServer(async context =>
{
method = context.Request.HttpMethod;
path = context.Request.Url.PathAndQuery;
using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
body = await reader.ReadToEndAsync();
}

byte[] response = Encoding.UTF8.GetBytes("{\"ok\":true}");
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
context.Response.OutputStream.Write(response, 0, response.Length);
context.Response.Close();
}, baseUrl =>
{
var partner = new Trolley.Gateway("access", "secret", baseUrl).partner;
Assert.AreEqual("{\"ok\":true}", request(partner));
});

Assert.AreEqual(expectedMethod, method);
Assert.AreEqual(expectedPath, path);
Assert.AreEqual(expectedBody ?? "", body);
}

private static void WithServer(Func<HttpListenerContext, Task> handler, Action<string> test)
{
int port = GetAvailablePort();
string baseUrl = $"http://localhost:{port}";

using (var listener = new HttpListener())
{
listener.Prefixes.Add(baseUrl + "/");
listener.Start();

Task server = Task.Run(async () =>
{
HttpListenerContext context = await listener.GetContextAsync();
await handler(context);
});

test(baseUrl);
server.GetAwaiter().GetResult();
}
}

private static int GetAvailablePort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
}
2 changes: 2 additions & 0 deletions trolley/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Gateway
public InvoicePaymentGateway invoicePayment;
public VerificationGateway verification;
public VerificationGateway trust;
public PartnerGateway partner;

public string apiKey;
public string apiSecret;
Expand All @@ -35,6 +36,7 @@ public Gateway(Configuration config)
this.invoicePayment = new InvoicePaymentGateway(this);
this.verification = new VerificationGateway(this);
this.trust = this.verification;
this.partner = new PartnerGateway(this);
}

public Gateway(string apiKey, string apiSecret, string apiBase = "production"):this(new Configuration(apiKey, apiSecret, apiBase))
Expand Down
107 changes: 107 additions & 0 deletions trolley/PartnerGateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using Newtonsoft.Json;

namespace Trolley
{
public class PartnerGateway
{
private readonly Gateway gateway;

public PartnerGateway(Gateway gateway)
{
this.gateway = gateway;
}

public string GetFees(string currency = null)
{
string endPoint = "/v1/fees";
if (!string.IsNullOrEmpty(currency))
{
endPoint += $"?currency={Uri.EscapeDataString(currency)}";
}
return this.gateway.client.Get(endPoint);
}

public string UpdateFees(object body)
{
return this.gateway.client.Patch("/v1/fees", Json(body));
}

public string ListPayoutMethods()
{
return this.gateway.client.Get("/v1/payout-methods");
}

public string GetPayoutMethod(string payoutMethod)
{
return this.gateway.client.Get($"/v1/payout-methods/{Uri.EscapeDataString(payoutMethod)}");
}

public string UpdatePayoutMethod(string payoutMethod, object body)
{
return this.gateway.client.Patch(
$"/v1/payout-methods/{Uri.EscapeDataString(payoutMethod)}",
Json(body)
);
}

public string GetProcessingSettings()
{
return this.gateway.client.Get("/v1/processing-settings");
}

public string UpdateProcessingSettings(object body)
{
return this.gateway.client.Patch("/v1/processing-settings", Json(body));
}

public string GetWhiteLabelDnsRecords()
{
return this.gateway.client.Get("/v1/white-label/dns-records");
}

public string VerifyWhiteLabelDnsRecords()
{
return this.gateway.client.Post("/v1/white-label/dns-records/verify", "{}");
}

public string DeleteWhiteLabelEmail()
{
return this.gateway.client.Delete("/v1/white-label/email");
}

public string UpdateWhiteLabelIcon(object body)
{
return this.gateway.client.Patch("/v1/white-label/icon", Json(body));
}

public string GetWhiteLabelSettings()
{
return this.gateway.client.Get("/v1/white-label");
}

public string UpdateWhiteLabelSettings(object body)
{
return this.gateway.client.Patch("/v1/white-label", Json(body));
}

public string GetWidgetConfiguration()
{
return this.gateway.client.Get("/v1/iframe/config");
}

public string UpdateWidgetConfiguration(object body = null)
{
return this.gateway.client.Post("/v1/iframe/config", Json(body));
}

private static string Json(object body)
{
if (body is string)
{
return (string)body;
}
return JsonConvert.SerializeObject(body ?? new object());
}
}
}
Loading