-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the client link api (#325)
* Create boilerplate code for the ClientLink API * Add unit test for create client link endpoint * Add method to generate a client link URL with parameters * Add documentation on ClientLinkClient * Fix spacing issue in documentation links * Increase version number to 3.2.0.0 --------- Co-authored-by: Vincent Kok <vincent@vinkosolutions.nl>
- Loading branch information
1 parent
e24fb58
commit e00ec77
Showing
10 changed files
with
279 additions
and
4 deletions.
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,18 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Mollie.Api.Models.ClientLink.Request; | ||
using Mollie.Api.Models.ClientLink.Response; | ||
|
||
namespace Mollie.Api.Client.Abstract | ||
{ | ||
public interface IClientLinkClient | ||
{ | ||
Task<ClientLinkResponse> CreateClientLinkAsync(ClientLinkRequest request); | ||
|
||
string GenerateClientLinkWithParameters( | ||
string clientLinkUrl, | ||
string state, | ||
List<string> scopes, | ||
bool forceApprovalPrompt = false); | ||
} | ||
} |
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,42 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Mollie.Api.Client.Abstract; | ||
using Mollie.Api.Extensions; | ||
using Mollie.Api.Models.ClientLink.Request; | ||
using Mollie.Api.Models.ClientLink.Response; | ||
|
||
namespace Mollie.Api.Client { | ||
public class ClientLinkClient : OauthBaseMollieClient, IClientLinkClient | ||
{ | ||
private readonly string _clientId; | ||
|
||
public ClientLinkClient(string clientId, string oauthAccessToken, HttpClient httpClient = null) | ||
: base(oauthAccessToken, httpClient) | ||
{ | ||
this._clientId = clientId; | ||
} | ||
|
||
public async Task<ClientLinkResponse> CreateClientLinkAsync(ClientLinkRequest request) | ||
{ | ||
return await this.PostAsync<ClientLinkResponse>($"client-links", request) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
public string GenerateClientLinkWithParameters( | ||
string clientLinkUrl, | ||
string state, | ||
List<string> scopes, | ||
bool forceApprovalPrompt = false) | ||
{ | ||
var parameters = new Dictionary<string, string> { | ||
{"client_id", _clientId}, | ||
{"state", state}, | ||
{"scope", string.Join(" ", scopes)}, | ||
{"approval_prompt", forceApprovalPrompt ? "force" : "auto"} | ||
}; | ||
|
||
return clientLinkUrl + parameters.ToQueryString(); | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/Mollie.Api/Models/ClientLink/Request/ClientLinkOwner.cs
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,28 @@ | ||
namespace Mollie.Api.Models.ClientLink.Request | ||
{ | ||
public class ClientLinkOwner | ||
{ | ||
/// <summary> | ||
/// The email address of your customer. | ||
/// </summary> | ||
public string Email { get; set; } | ||
|
||
/// <summary> | ||
/// The given name (first name) of your customer. | ||
/// </summary> | ||
public string GivenName { get; set; } | ||
|
||
/// <summary> | ||
/// The family name (surname) of your customer. | ||
/// </summary> | ||
public string FamilyName { get; set; } | ||
|
||
/// <summary> | ||
/// Allows you to preset the language to be used in the login / authorize flow. When this parameter is | ||
/// omitted, the browser language will be used instead. You can provide any xx_XX format ISO 15897 locale, | ||
/// but the authorize flow currently only supports the following languages: | ||
/// en_US nl_NL nl_BE fr_FR fr_BE de_DE es_ES it_IT | ||
/// </summary> | ||
public string Locale { get; set; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Mollie.Api/Models/ClientLink/Request/ClientLinkRequest.cs
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,33 @@ | ||
namespace Mollie.Api.Models.ClientLink.Request | ||
{ | ||
public class ClientLinkRequest | ||
{ | ||
/// <summary> | ||
/// Personal data of your customer which is required for this endpoint. | ||
/// </summary> | ||
public ClientLinkOwner Owner { get; set; } | ||
|
||
/// <summary> | ||
/// Name of the organization. | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Address of the organization. Note that the country parameter | ||
/// must always be provided. | ||
/// </summary> | ||
public AddressObject Address { get; set; } | ||
|
||
/// <summary> | ||
/// The Chamber of Commerce (or local equivalent) registration number | ||
/// of the organization. | ||
/// </summary> | ||
public string RegistrationNumber { get; set; } | ||
|
||
/// <summary> | ||
/// The VAT number of the organization, if based in the European Union | ||
/// or the United Kingdom. | ||
/// </summary> | ||
public string VatNumber { get; set; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Mollie.Api/Models/ClientLink/Response/ClientLinkResponse.cs
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,14 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Mollie.Api.Models.ClientLink.Response | ||
{ | ||
public class ClientLinkResponse | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Resource { get; set; } | ||
|
||
[JsonProperty("_links")] | ||
public ClientLinkResponseLinks Links { get; set; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Mollie.Api/Models/ClientLink/Response/ClientLinkResponseLinks.cs
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,10 @@ | ||
using Mollie.Api.Models.Url; | ||
|
||
namespace Mollie.Api.Models.ClientLink.Response | ||
{ | ||
public class ClientLinkResponseLinks | ||
{ | ||
public UrlLink ClientLink { get; set; } | ||
public UrlLink Documentation { 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
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,89 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Mollie.Api.Client; | ||
using Mollie.Api.Models.Chargeback; | ||
using Mollie.Api.Models.ClientLink.Request; | ||
using Mollie.Api.Models.ClientLink.Response; | ||
using RichardSzalay.MockHttp; | ||
using Xunit; | ||
|
||
namespace Mollie.Tests.Unit.Client; | ||
|
||
public class ClientLinkClientTests : BaseClientTests | ||
{ | ||
[Fact] | ||
public async Task CreateClientLinkAsync_ResponseIsDeserializedInExpectedFormat() | ||
{ | ||
// Given: We create a payment link | ||
const string clientLinkId = "csr_vZCnNQsV2UtfXxYifWKWH"; | ||
const string clientLinkUrl = "myurl"; | ||
string clientLinkResponseJson = CreateClientLinkResponseJson(clientLinkId, clientLinkUrl); | ||
var mockHttp = new MockHttpMessageHandler(); | ||
mockHttp.When( HttpMethod.Post, $"{BaseMollieClient.ApiEndPoint}client-links") | ||
.Respond("application/json", clientLinkResponseJson); | ||
HttpClient httpClient = mockHttp.ToHttpClient(); | ||
ClientLinkClient clientLinkClient = new ClientLinkClient("clientId", "access_1234", httpClient); | ||
|
||
// When: We send the request | ||
ClientLinkResponse response = await clientLinkClient.CreateClientLinkAsync(new ClientLinkRequest()); | ||
|
||
// Then | ||
response.Id.Should().Be(clientLinkId); | ||
response.Links.ClientLink.Href.Should().Be(clientLinkUrl); | ||
mockHttp.VerifyNoOutstandingRequest(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void GenerateClientLinkWithParameters_GeneratesExpectedUrl(bool forceApprovalPrompt) | ||
{ | ||
// Arrange | ||
const string clientId = "app_j9Pakf56Ajta6Y65AkdTtAv"; | ||
const string clientLinkUrl = "https://my.mollie.com/dashboard/client-link/csr_vZCnNQsV2UtfXxYifWKWH"; | ||
const string state = "decafbad"; | ||
var scopes = new List<string>() | ||
{ | ||
"onboarding.read", | ||
"organizations.read", | ||
"payments.write", | ||
"payments.read", | ||
"profiles.write", | ||
}; | ||
ClientLinkClient clientLinkClient = new ClientLinkClient( | ||
clientId, "access_1234", new MockHttpMessageHandler().ToHttpClient()); | ||
|
||
// Act | ||
string result = clientLinkClient.GenerateClientLinkWithParameters( | ||
clientLinkUrl, state, scopes, forceApprovalPrompt); | ||
|
||
// Assert | ||
string expectedApprovalPrompt = forceApprovalPrompt ? "force" : "auto"; | ||
result.Should() | ||
.Be("https://my.mollie.com/dashboard/client-link/csr_vZCnNQsV2UtfXxYifWKWH" + | ||
$"?client_id={clientId}" + | ||
$"&state={state}" + | ||
"&scope=onboarding.read+organizations.read+payments.write+payments.read+profiles.write" + | ||
$"&approval_prompt={expectedApprovalPrompt}"); | ||
} | ||
|
||
private string CreateClientLinkResponseJson(string id, string clientLinkUrl) | ||
{ | ||
return $@"{{ | ||
""id"": ""{id}"", | ||
""resource"": ""client-link"", | ||
""_links"": {{ | ||
""clientLink"": {{ | ||
""href"": ""{clientLinkUrl}"", | ||
""type"": ""text/html"" | ||
}}, | ||
""documentation"": {{ | ||
""href"": ""https://docs.mollie.com/reference/v2/clients-api/create-client-link"", | ||
""type"": ""text/html"" | ||
}} | ||
}} | ||
}}"; | ||
} | ||
} |