Skip to content

Commit 22cfd49

Browse files
[ACL-229] Add support for mandates in HppLinkBuilder (#228)
1 parent ffe1832 commit 22cfd49

File tree

3 files changed

+140
-13
lines changed

3 files changed

+140
-13
lines changed
Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,70 @@
11
using System;
2+
using TrueLayer.Common;
3+
using TrueLayer.Payments.Model;
24

35
namespace TrueLayer.Payments
46
{
57
internal sealed class HppLinkBuilder
68
{
7-
internal const string SandboxUrl = "https://payment.truelayer-sandbox.com/";
8-
internal const string ProdUrl = "https://payment.truelayer.com/";
9-
109
private readonly Uri _baseUri;
1110

1211
public HppLinkBuilder(Uri? baseUri = null, bool useSandbox = true)
1312
{
14-
_baseUri = baseUri ??
15-
new Uri((useSandbox) ? SandboxUrl : ProdUrl);
13+
_baseUri = baseUri ?? (useSandbox ? TrueLayerBaseUris.SandboxHppBaseUri : TrueLayerBaseUris.ProdHppBaseUri);
1614
}
1715

18-
public string Build(string paymentId, string paymentToken, Uri returnUri)
16+
public string Build(
17+
string id,
18+
string token,
19+
Uri returnUri,
20+
ResourceType resourceType = ResourceType.Payment,
21+
int? maxWaitForResultSeconds = null,
22+
bool? signup = null)
1923
{
20-
paymentId.NotNullOrWhiteSpace(nameof(paymentId));
21-
paymentToken.NotNullOrWhiteSpace(nameof(paymentToken));
24+
id.NotNullOrWhiteSpace(nameof(id));
25+
token.NotNullOrWhiteSpace(nameof(token));
2226
returnUri.NotNull(nameof(returnUri));
2327

24-
var fragment = $"payment_id={paymentId}&resource_token={paymentToken}&return_uri={returnUri.AbsoluteUri}";
25-
26-
var builder = new UriBuilder(_baseUri);
27-
builder.Path = "payments";
28-
builder.Fragment = fragment;
28+
var builder = new UriBuilder(_baseUri)
29+
{
30+
Path = ToResourcePath(resourceType),
31+
Fragment = ToFragment(id, token, returnUri, resourceType, maxWaitForResultSeconds, signup)
32+
};
2933

3034
return builder.Uri.AbsoluteUri;
3135
}
36+
37+
private static string ToFragment(string id, string token, Uri returnUri, ResourceType resourceType, int? maxWaitForResultSeconds, bool? signup)
38+
{
39+
var fragment = $"{ToResourceFieldId(resourceType)}={id}&resource_token={token}&return_uri={returnUri.AbsoluteUri}";
40+
41+
if (maxWaitForResultSeconds is not null)
42+
{
43+
fragment += $"&max_wait_seconds={maxWaitForResultSeconds}";
44+
}
45+
46+
if (signup is not null)
47+
{
48+
fragment += $"&signup={signup.Value.ToString().ToLowerInvariant()}";
49+
}
50+
51+
return fragment;
52+
}
53+
54+
private static string ToResourceFieldId(ResourceType resourceType) =>
55+
resourceType switch
56+
{
57+
ResourceType.Payment => "payment_id",
58+
ResourceType.Mandate => "mandate_id",
59+
_ => throw new ArgumentOutOfRangeException(nameof(resourceType), resourceType, null)
60+
};
61+
62+
private static string ToResourcePath(ResourceType resourceType) =>
63+
resourceType switch
64+
{
65+
ResourceType.Payment => "payments",
66+
ResourceType.Mandate => "mandates",
67+
_ => throw new ArgumentOutOfRangeException(nameof(resourceType), resourceType, null)
68+
};
3269
}
3370
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TrueLayer.Payments.Model;
2+
3+
public enum ResourceType
4+
{
5+
Payment,
6+
Mandate
7+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using FluentAssertions;
3+
using TrueLayer.Payments;
4+
using TrueLayer.Payments.Model;
5+
using Xunit;
6+
7+
namespace TrueLayer.Tests.Payments;
8+
9+
public class HppLinkBuilderTests
10+
{
11+
[Fact]
12+
public void Build_Payment_Sandbox_ReturnsCorrectUri()
13+
{
14+
var paymentId = new Guid().ToString();
15+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
16+
var sut = new HppLinkBuilder(null, true);
17+
18+
var result = sut.Build(paymentId, token, new Uri("https://return.client.com/"), ResourceType.Payment);
19+
20+
result.Should().Be($"https://payment.truelayer-sandbox.com/payments#payment_id={paymentId}&resource_token={token}&return_uri=https://return.client.com/");
21+
}
22+
23+
[Fact]
24+
public void Build_Payment_Production_ReturnsCorrectUri()
25+
{
26+
var paymentId = new Guid().ToString();
27+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
28+
var sut = new HppLinkBuilder(null, false);
29+
30+
var result = sut.Build(paymentId, token, new Uri("https://return.client.com/"), ResourceType.Payment);
31+
32+
result.Should().Be($"https://payment.truelayer.com/payments#payment_id={paymentId}&resource_token={token}&return_uri=https://return.client.com/");
33+
}
34+
35+
[Fact]
36+
public void Build_CustomUri_ReturnsCorrectUri()
37+
{
38+
var uri = new Uri("https://api.custom.com/");
39+
var paymentId = new Guid().ToString();
40+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
41+
var sut = new HppLinkBuilder(uri, false);
42+
43+
var result = sut.Build(paymentId, token, new Uri("https://return.client.com/"), ResourceType.Payment);
44+
45+
result.Should().Be($"https://api.custom.com/payments#payment_id={paymentId}&resource_token={token}&return_uri=https://return.client.com/");
46+
}
47+
48+
[Fact]
49+
public void Build_Payment_Sandbox_MaxWait_Signup_ReturnsCorrectUri()
50+
{
51+
var paymentId = new Guid().ToString();
52+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
53+
var sut = new HppLinkBuilder(null, true);
54+
55+
var result = sut.Build(paymentId, token, new Uri("https://return.client.com/"), ResourceType.Payment, 60, true);
56+
57+
result.Should().Be($"https://payment.truelayer-sandbox.com/payments#payment_id={paymentId}&resource_token={token}&return_uri=https://return.client.com/&max_wait_seconds=60&signup=true");
58+
}
59+
60+
[Fact]
61+
public void Build_Mandate_Sandbox_ReturnsCorrectUri()
62+
{
63+
var mandateId = new Guid().ToString();
64+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
65+
var sut = new HppLinkBuilder(null, true);
66+
67+
var result = sut.Build(mandateId, token, new Uri("https://return.client.com/"), ResourceType.Mandate);
68+
69+
result.Should().Be($"https://payment.truelayer-sandbox.com/mandates#mandate_id={mandateId}&resource_token={token}&return_uri=https://return.client.com/");
70+
}
71+
72+
[Fact]
73+
public void Build_Mandate_Production_ReturnsCorrectUri()
74+
{
75+
var mandateId = new Guid().ToString();
76+
const string token = "eyJhbGciOiJSUzUxMiIsImtpZCI6IlBDNHJFVHZycGZTV1dqYW91R2dIbmJHNTBBR184SFBHXzBuU0";
77+
var sut = new HppLinkBuilder(null, false);
78+
79+
var result = sut.Build(mandateId, token, new Uri("https://return.client.com/"), ResourceType.Mandate);
80+
81+
result.Should().Be($"https://payment.truelayer.com/mandates#mandate_id={mandateId}&resource_token={token}&return_uri=https://return.client.com/");
82+
}
83+
}

0 commit comments

Comments
 (0)