-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPaymentTests.cs
More file actions
813 lines (723 loc) · 34.4 KB
/
PaymentTests.cs
File metadata and controls
813 lines (723 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using OneOf;
using TrueLayer.AcceptanceTests.Clients;
using TrueLayer.AcceptanceTests.Helpers;
using TrueLayer.Common;
using TrueLayer.Payments.Model;
using TrueLayer.Payments.Model.AuthorizationFlow;
using Xunit;
namespace TrueLayer.AcceptanceTests;
using AccountIdentifierUnion = OneOf<
AccountIdentifier.SortCodeAccountNumber,
AccountIdentifier.Iban,
AccountIdentifier.Bban,
AccountIdentifier.Nrb>;
using PaymentsSchemeSelectionUnion = OneOf<
SchemeSelection.InstantOnly,
SchemeSelection.InstantPreferred,
SchemeSelection.Preselected,
SchemeSelection.UserSelected>;
using ProviderUnion = OneOf<Provider.UserSelected, Provider.Preselected>;
using BeneficiaryUnion = OneOf<Beneficiary.MerchantAccount, Beneficiary.ExternalAccount>;
using GetPaymentUnion = OneOf<
GetPaymentResponse.AuthorizationRequired,
GetPaymentResponse.Authorizing,
GetPaymentResponse.Authorized,
GetPaymentResponse.Executed,
GetPaymentResponse.Settled,
GetPaymentResponse.Failed,
GetPaymentResponse.AttemptFailed
>;
public partial class PaymentTests : IClassFixture<ApiTestFixture>
{
private readonly ApiTestFixture _fixture;
public PaymentTests(ApiTestFixture fixture)
{
_fixture = fixture;
}
[Theory]
[MemberData(nameof(ExternalAccountPaymentRequests))]
public async Task Can_Create_External_Account_Payment(CreatePaymentRequest paymentRequest)
{
foreach (var client in _fixture.TlClients)
{
var response = await client.Payments.CreatePayment(paymentRequest);
response.StatusCode.Should().Be(HttpStatusCode.Created);
var authorizationRequired = response.Data.AsT0;
authorizationRequired.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.ResourceToken.Should().NotBeNullOrWhiteSpace();
authorizationRequired.User.Should().NotBeNull();
authorizationRequired.User.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.Status.Should().Be("authorization_required");
string hppUri = client.Payments.CreateHostedPaymentPageLink(
authorizationRequired.Id, authorizationRequired.ResourceToken,
new Uri("https://redirect.mydomain.com"));
hppUri.Should().NotBeNullOrWhiteSpace();
}
}
[Fact]
public async Task Can_Create_Merchant_Account_Gbp_Payment()
{
var paymentRequest = CreateTestPaymentRequest(
new Provider.UserSelected
{
Filter = new ProviderFilter { ProviderIds = ["mock-payments-gb-redirect"] },
SchemeSelection = new SchemeSelection.InstantOnly { AllowRemitterFee = true },
},
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId)
{
AccountHolderName = "account holder name",
StatementReference = "statement-ref"
});
var response = await _fixture.TlClients[0].Payments.CreatePayment(
paymentRequest, idempotencyKey: Guid.NewGuid().ToString());
response.StatusCode.Should().Be(HttpStatusCode.Created);
var authorizationRequired = response.Data.AsT0;
authorizationRequired.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.ResourceToken.Should().NotBeNullOrWhiteSpace();
authorizationRequired.User.Should().NotBeNull();
authorizationRequired.User.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.Status.Should().Be("authorization_required");
string hppUri = _fixture.TlClients[0].Payments.CreateHostedPaymentPageLink(
authorizationRequired.Id, authorizationRequired.ResourceToken, new Uri("https://redirect.mydomain.com"));
hppUri.Should().NotBeNullOrWhiteSpace();
}
[Fact]
public async Task Can_Create_Merchant_Account_Gbp_Verification_Payment()
{
var paymentRequest = CreateTestPaymentRequest(
new Provider.UserSelected
{
Filter = new ProviderFilter { ProviderIds = ["mock-payments-gb-redirect"] },
SchemeSelection = new SchemeSelection.InstantOnly { AllowRemitterFee = true },
},
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId)
{
Verification = new Verification.Automated { RemitterName = true }
});
var response = await _fixture.TlClients[0].Payments.CreatePayment(paymentRequest);
response.StatusCode.Should().Be(HttpStatusCode.Created);
var authorizationRequired = response.Data.AsT0;
authorizationRequired.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.ResourceToken.Should().NotBeNullOrWhiteSpace();
authorizationRequired.User.Should().NotBeNull();
authorizationRequired.User.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.Status.Should().Be("authorization_required");
string hppUri = _fixture.TlClients[0].Payments.CreateHostedPaymentPageLink(
authorizationRequired.Id, authorizationRequired.ResourceToken, new Uri("https://redirect.mydomain.com"));
hppUri.Should().NotBeNullOrWhiteSpace();
}
[Fact]
public async Task Can_Create_Merchant_Account_Eur_Payment()
{
var paymentRequest = CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-fr-redirect",
schemeSelection: new SchemeSelection.Preselected { SchemeId = "sepa_credit_transfer_instant" })
{
Remitter = new RemitterAccount("John Doe", new AccountIdentifier.Iban("FR1420041010050500013M02606"))
},
null,
Currencies.EUR,
new RelatedProducts(new SignupPlus()),
new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].EurMerchantAccountId));
var response = await _fixture.TlClients[0].Payments.CreatePayment(paymentRequest);
response.StatusCode.Should().Be(HttpStatusCode.Created);
var authorizationRequired = response.Data.AsT0;
authorizationRequired.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.ResourceToken.Should().NotBeNullOrWhiteSpace();
authorizationRequired.User.Should().NotBeNull();
authorizationRequired.User.Id.Should().NotBeNullOrWhiteSpace();
authorizationRequired.Status.Should().Be("authorization_required");
string hppUri = _fixture.TlClients[0].Payments.CreateHostedPaymentPageLink(
authorizationRequired.Id, authorizationRequired.ResourceToken, new Uri("https://redirect.mydomain.com"));
hppUri.Should().NotBeNullOrWhiteSpace();
}
[Fact]
public async Task Can_Create_Payment_With_Auth_Flow()
{
var sortCodeAccountNumber = new AccountIdentifier.SortCodeAccountNumber("567890", "12345678");
var providerSelection = new Provider.Preselected("mock-payments-gb-redirect", "faster_payments_service")
{
Remitter = new RemitterAccount("John Doe", sortCodeAccountNumber),
};
var paymentRequest = CreateTestPaymentRequest(
providerSelection,
sortCodeAccountNumber,
initAuthorizationFlow: true);
var response = await _fixture.TlClients[0].Payments.CreatePayment(paymentRequest);
response.StatusCode.Should().Be(HttpStatusCode.Created);
response.Data.IsT3.Should().BeTrue();
var authorizing = response.Data.AsT3;
authorizing.Id.Should().NotBeNullOrWhiteSpace();
authorizing.ResourceToken.Should().NotBeNullOrWhiteSpace();
authorizing.User.Should().NotBeNull();
authorizing.User.Id.Should().NotBeNullOrWhiteSpace();
authorizing.Status.Should().Be("authorizing");
authorizing.AuthorizationFlow.Should().NotBeNull();
// The next action is a redirect
authorizing.AuthorizationFlow!.Actions.Next.IsT2.Should().BeTrue();
authorizing.AuthorizationFlow.Actions.Next.AsT2.Uri.Should().NotBeNull();
string hppUri = _fixture.TlClients[0].Payments.CreateHostedPaymentPageLink(
authorizing.Id, authorizing.ResourceToken, new Uri("https://redirect.mydomain.com"));
hppUri.Should().NotBeNullOrWhiteSpace();
}
[Fact]
public async Task GetPayment_Should_Return_Settled_Payment()
{
var client = _fixture.TlClients[0];
var providerSelection = new Provider.Preselected("mock-payments-gb-redirect", "faster_payments_service");
var paymentRequest = CreateTestPaymentRequest(
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId),
providerSelection: providerSelection,
initAuthorizationFlow: true);
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(client, paymentRequest, MockBankPaymentAction.Execute, typeof(GetPaymentResponse.Settled));
var settled = payment.AsT4;
settled.AmountInMinor.Should().Be(paymentRequest.AmountInMinor);
settled.Currency.Should().Be(paymentRequest.Currency);
settled.Id.Should().NotBeNullOrWhiteSpace();
settled.CreatedAt.Should().NotBe(default);
settled.PaymentMethod.AsT0.Should().NotBeNull();
settled.CreditableAt.Should().NotBeNull();
}
[Theory]
[MemberData(nameof(ExternalAccountPaymentRequests))]
public async Task Can_Get_Authorization_Required_Payment(CreatePaymentRequest paymentRequest)
{
var response = await _fixture.TlClients[1].Payments.CreatePayment(
paymentRequest, idempotencyKey: Guid.NewGuid().ToString());
response.IsSuccessful.Should().BeTrue();
response.Data.IsT0.Should().BeTrue();
var authorizationRequiredResponse = response.Data.AsT0;
var getPaymentResponse = await _fixture.TlClients[1].Payments.GetPayment(authorizationRequiredResponse.Id);
getPaymentResponse.IsSuccessful.Should().BeTrue();
getPaymentResponse.Data.TryPickT0(out var payment, out _).Should().BeTrue();
payment.Status.Should().Be("authorization_required");
payment.AmountInMinor.Should().Be(paymentRequest.AmountInMinor);
payment.Currency.Should().Be(paymentRequest.Currency);
payment.Id.Should().NotBeNullOrWhiteSpace();
payment.CreatedAt.Should().NotBe(default);
var bankTransfer = payment.PaymentMethod.AsT0;
bankTransfer.ProviderSelection.Switch(
userSelected =>
{
Provider.UserSelected providerSelectionReq = bankTransfer.ProviderSelection.AsT0;
userSelected.Filter.Should().BeEquivalentTo(providerSelectionReq.Filter);
// Provider selection hasn't happened yet
userSelected.ProviderId.Should().BeNullOrEmpty();
userSelected.SchemeId.Should().BeNullOrEmpty();
},
preselected =>
{
Provider.Preselected providerSelectionReq = bankTransfer.ProviderSelection.AsT1;
AssertSchemeSelection(preselected.SchemeSelection, providerSelectionReq.SchemeSelection, preselected.SchemeId, providerSelectionReq.SchemeId);
preselected.ProviderId.Should().Be(providerSelectionReq.ProviderId);
preselected.Remitter.Should().Be(providerSelectionReq.Remitter);
});
bankTransfer.Beneficiary.TryPickT1(out var externalAccount, out _).Should().BeTrue();
bankTransfer.Beneficiary.Should().Be(bankTransfer.Beneficiary);
payment.User.Should().NotBeNull();
payment.User.Id.Should().Be(authorizationRequiredResponse.User.Id);
}
[Fact]
public async Task Can_Create_Payment_With_Retry_Option_And_Get_AttemptFailed_Error()
{
// Arrange
var client = _fixture.TlClients[0];
var paymentRequest = CreateTestPaymentRequest(
initAuthorizationFlow: true,
retry: new Retry.BaseRetry());
// Act && Assert
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(
client,
paymentRequest,
MockBankPaymentAction.RejectAuthorisation,
typeof(GetPaymentResponse.AttemptFailed));
// Assert
payment.AsT6.Id.Should().NotBeNullOrWhiteSpace();
payment.AsT6.Status.Should().Be("attempt_failed");
}
[Fact]
public async Task Can_create_and_get_payment_refund()
{
// Arrange
var client = _fixture.TlClients[0];
var paymentRequest = CreateTestPaymentRequest(
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId),
initAuthorizationFlow: true);
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(client, paymentRequest, MockBankPaymentAction.Execute, typeof(GetPaymentResponse.Settled));
var paymentId = payment.AsT4.Id;
// Act && assert
var createRefundResponse = await client.Payments.CreatePaymentRefund(
paymentId: paymentId,
idempotencyKey: Guid.NewGuid().ToString(),
new CreatePaymentRefundRequest(Reference: "a-reference"));
createRefundResponse.IsSuccessful.Should().BeTrue();
createRefundResponse.Data!.Id.Should().NotBeNullOrWhiteSpace();
var getPaymentRefundResponse = await client.Payments.GetPaymentRefund(
paymentId: paymentId,
refundId: createRefundResponse.Data!.Id);
// Assert
createRefundResponse.IsSuccessful.Should().BeTrue();
createRefundResponse.Data!.Id.Should().NotBeNullOrWhiteSpace();
getPaymentRefundResponse.IsSuccessful.Should().BeTrue();
}
[Fact]
public async Task Can_Create_And_List_Payment_Refunds()
{
// Arrange
var client = _fixture.TlClients[0];
var paymentRequest = CreateTestPaymentRequest(
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId),
initAuthorizationFlow: true);
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(client, paymentRequest, MockBankPaymentAction.Execute, typeof(GetPaymentResponse.Settled));
var paymentId = payment.AsT4.Id;
// Act && assert
var createRefundResponse = await client.Payments.CreatePaymentRefund(
paymentId: paymentId,
idempotencyKey: Guid.NewGuid().ToString(),
new CreatePaymentRefundRequest(Reference: "a-reference"));
createRefundResponse.IsSuccessful.Should().BeTrue();
createRefundResponse.Data!.Id.Should().NotBeNullOrWhiteSpace();
// Act
var listPaymentRefundsResponse = await client.Payments.ListPaymentRefunds(paymentId);
// Assert
createRefundResponse.IsSuccessful.Should().BeTrue();
createRefundResponse.Data!.Id.Should().NotBeNullOrWhiteSpace();
listPaymentRefundsResponse.IsSuccessful.Should().BeTrue();
listPaymentRefundsResponse.Data!.Items.Count.Should().Be(1);
}
[Fact]
public async Task Can_List_Payment_Refunds_With_RefundExecuted_Status()
{
// Arrange
var client = _fixture.TlClients[0];
var paymentRequest = CreateTestPaymentRequest(
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId),
initAuthorizationFlow: true);
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(client, paymentRequest, MockBankPaymentAction.Execute, typeof(GetPaymentResponse.Settled));
var paymentId = payment.AsT4.Id;
// Create refund and wait for it to be executed
var createRefundResponse = await client.Payments.CreatePaymentRefund(
paymentId: paymentId,
idempotencyKey: Guid.NewGuid().ToString(),
new CreatePaymentRefundRequest(Reference: "executed-refund"));
createRefundResponse.IsSuccessful.Should().BeTrue();
// Act - List refunds (may include RefundExecuted status)
var listPaymentRefundsResponse = await client.Payments.ListPaymentRefunds(paymentId);
// Assert
listPaymentRefundsResponse.IsSuccessful.Should().BeTrue();
listPaymentRefundsResponse.Data!.Items.Should().NotBeEmpty();
// Note: RefundExecuted status depends on actual payment processing state
}
[Fact]
public async Task Can_List_Payment_Refunds_With_RefundFailed_Status()
{
// Arrange
var client = _fixture.TlClients[0];
var paymentRequest = CreateTestPaymentRequest(
beneficiary: new Beneficiary.MerchantAccount(_fixture.ClientMerchantAccounts[0].GbpMerchantAccountId),
initAuthorizationFlow: true);
var payment = await CreatePaymentAndSetAuthorisationStatusAsync(client, paymentRequest, MockBankPaymentAction.Execute, typeof(GetPaymentResponse.Settled));
var paymentId = payment.AsT4.Id;
// Create refund with specific reference that may trigger failure
var createRefundResponse = await client.Payments.CreatePaymentRefund(
paymentId: paymentId,
idempotencyKey: Guid.NewGuid().ToString(),
new CreatePaymentRefundRequest(Reference: "TUOYAP"));
createRefundResponse.IsSuccessful.Should().BeTrue();
// Act - List refunds (may include RefundFailed status)
var listPaymentRefundsResponse = await client.Payments.ListPaymentRefunds(paymentId);
// Assert
listPaymentRefundsResponse.IsSuccessful.Should().BeTrue();
listPaymentRefundsResponse.Data!.Items.Should().NotBeEmpty();
// Note: RefundFailed status depends on actual payment processing state
}
[Fact]
public async Task Can_Cancel_Payment()
{
// arrange
var paymentRequest = CreateTestPaymentRequest();
var payment = await _fixture.TlClients[0].Payments.CreatePayment(
paymentRequest, idempotencyKey: Guid.NewGuid().ToString());
payment.IsSuccessful.Should().BeTrue();
var paymentId = payment.Data.AsT0.Id;
// act
var cancelPaymentResponse = await _fixture.TlClients[0].Payments.CancelPayment(
paymentId,
idempotencyKey: Guid.NewGuid().ToString());
var getPaymentResponse = await _fixture.TlClients[0].Payments.GetPayment(paymentId);
// assert
cancelPaymentResponse.IsSuccessful.Should().BeTrue();
cancelPaymentResponse.StatusCode.Should().Be(HttpStatusCode.Accepted);
getPaymentResponse.IsSuccessful.Should().BeTrue();
getPaymentResponse.Data.AsT5.Should().NotBeNull();
getPaymentResponse.Data.AsT5.FailureReason.Should().Be("canceled");
}
private static void AssertSchemeSelection(
PaymentsSchemeSelectionUnion? actualSchemeSelection,
PaymentsSchemeSelectionUnion? expectedSchemeSelection,
string? actualSchemeId,
string? expectedSchemeId)
{
if (actualSchemeSelection is null && expectedSchemeSelection is null)
{
actualSchemeId.Should().BeEquivalentTo(expectedSchemeId);
return;
}
actualSchemeSelection.Should().NotBeNull();
expectedSchemeSelection.Should().NotBeNull();
var expectedSelection = expectedSchemeSelection!.Value;
actualSchemeSelection!.Value.Switch(
instantOnly => { instantOnly.AllowRemitterFee.Should().Be(expectedSelection.AsT0.AllowRemitterFee); },
instantPreferred => { instantPreferred.AllowRemitterFee.Should().Be(expectedSelection.AsT1.AllowRemitterFee); },
preselectedSchemeSelection => { preselectedSchemeSelection.SchemeId.Should().Be(expectedSelection.AsT2.SchemeId); },
userSelected => { expectedSelection.IsT3.Should().Be(true); });
}
[Fact]
public async Task GetPayment_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() => client.Payments.GetPayment(paymentId));
result.Message.Should().Be("Value is malformed (Parameter 'id')");
}
[Fact]
public async Task StartAuthFlow_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.StartAuthorizationFlow(paymentId, Guid.NewGuid().ToString(),
new StartAuthorizationFlowRequest()));
result.Message.Should().Be("Value is malformed (Parameter 'paymentId')");
}
[Fact]
public async Task CreateRefund_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.CreatePaymentRefund(paymentId, Guid.NewGuid().ToString(),
new CreatePaymentRefundRequest("reference")));
result.Message.Should().Be("Value is malformed (Parameter 'paymentId')");
}
[Fact]
public async Task ListPaymentRefunds_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.ListPaymentRefunds(paymentId));
result.Message.Should().Be("Value is malformed (Parameter 'paymentId')");
}
[Fact]
public async Task GetPaymentRefund_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.GetPaymentRefund(paymentId, Guid.NewGuid().ToString()));
result.Message.Should().Be("Value is malformed (Parameter 'paymentId')");
}
[Fact]
public async Task GetPaymentRefund_Url_As_RefundId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string refundId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.GetPaymentRefund(Guid.NewGuid().ToString(), refundId));
result.Message.Should().Be("Value is malformed (Parameter 'refundId')");
}
[Fact]
public async Task CancelPayment_Url_As_PaymentId_Should_Throw_Exception()
{
var client = _fixture.TlClients[0];
const string paymentId = "https://test.com";
var result = await Assert.ThrowsAsync<ArgumentException>(() =>
client.Payments.CancelPayment(paymentId, Guid.NewGuid().ToString()));
result.Message.Should().Be("Value is malformed (Parameter 'paymentId')");
}
private static CreatePaymentRequest CreateTestPaymentRequest(
ProviderUnion? providerSelection = null,
AccountIdentifierUnion? accountIdentifier = null,
string currency = Currencies.GBP,
RelatedProducts? relatedProducts = null,
BeneficiaryUnion? beneficiary = null,
Retry.BaseRetry? retry = null,
bool initAuthorizationFlow = false)
{
accountIdentifier ??= new AccountIdentifier.SortCodeAccountNumber("567890", "12345678");
providerSelection ??= new Provider.Preselected("mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.Preselected { SchemeId = "faster_payments_service" });
beneficiary ??= new Beneficiary.ExternalAccount(
"TrueLayer",
"truelayer-dotnet",
accountIdentifier.Value);
var authorizationFlow = initAuthorizationFlow
? new StartAuthorizationFlowRequest(
ProviderSelection: providerSelection,
Redirect: new Redirect(
new Uri("http://localhost:3000/callback"),
new Uri("http://localhost:3000/callback")),
Retry: retry)
: null;
return new CreatePaymentRequest(
100,
currency,
new PaymentMethod.BankTransfer(
providerSelection.Value,
beneficiary.Value,
retry),
new PaymentUserRequest(
name: "Jane Doe",
email: "jane.doe@example.com",
phone: "+442079460087",
dateOfBirth: new DateTime(1999, 1, 1),
address: new Address("London", "England", "EC1R 4RB", "GB", "1 Hardwick St")),
relatedProducts,
authorizationFlow,
metadata: new Dictionary<string, string>
{
["test-key-1"] = "test-value-1",
["test-key-2"] = "test-value-2",
},
riskAssessment: new RiskAssessment("test")
);
}
public static IEnumerable<object[]> ExternalAccountPaymentRequests()
{
var sortCodeAccountNumber = new AccountIdentifier.SortCodeAccountNumber("567890", "12345678");
var providerFilterMockGbRedirect = new ProviderFilter { ProviderIds = ["mock-payments-gb-redirect"] };
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.InstantOnly { AllowRemitterFee = true },
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.InstantOnly
{
AllowRemitterFee = false,
InstantOverrideProviderIds = ["mock-payments-gb-redirect"],
NonInstantOverrideProviderIds = ["mock-payments-de-redirect"]
},
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.InstantPreferred
{
AllowRemitterFee = true,
InstantOverrideProviderIds = ["mock-payments-de-embedded"],
NonInstantOverrideProviderIds = ["mock-payments-de-redirect"]
},
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.InstantPreferred { AllowRemitterFee = false },
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.UserSelected(),
},
sortCodeAccountNumber)
];
var remitterSortAccountNumber = new RemitterAccount("John Doe", sortCodeAccountNumber);
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-gb-redirect", "faster_payments_service")
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.Preselected { SchemeId = "faster_payments_service"})
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-gb-redirect", schemeSelection: new SchemeSelection.UserSelected())
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.InstantOnly { AllowRemitterFee = true })
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.InstantOnly { AllowRemitterFee = false })
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.InstantPreferred { AllowRemitterFee = true })
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.InstantPreferred { AllowRemitterFee = false })
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-fr-redirect", schemeSelection: new SchemeSelection.Preselected { SchemeId = "sepa_credit_transfer_instant" })
{
Remitter = new RemitterAccount("John Doe", new AccountIdentifier.Iban("FR1420041010050500013M02606")),
},
new AccountIdentifier.Iban("IT60X0542811101000000123456"),
Currencies.EUR,
new RelatedProducts(new SignupPlus()))
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-gb-redirect", schemeSelection: new SchemeSelection.Preselected { SchemeId = "faster_payments_service" }),
sortCodeAccountNumber)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-fr-redirect", schemeSelection: new SchemeSelection.Preselected { SchemeId = "sepa_credit_transfer_instant" }),
new AccountIdentifier.Iban("IT60X0542811101000000123456"),
Currencies.EUR)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-pl-redirect", schemeSelection: new SchemeSelection.Preselected { SchemeId = "polish_domestic_standard" })
{
Remitter = new RemitterAccount(
"John Doe", new AccountIdentifier.Nrb("12345678901234567890123456"))
},
new AccountIdentifier.Nrb("12345678901234567890123456"),
Currencies.PLN)
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected("mock-payments-no-redirect", schemeSelection:new SchemeSelection.Preselected { SchemeId = "norwegian_domestic_credit_transfer" })
{
Remitter = new RemitterAccount(
"John Doe", new AccountIdentifier.Bban("12345678901234567890123456"))
},
new AccountIdentifier.Bban("IT60X0542811101000000123456"),
Currencies.NOK)
];
// Create a payment with retry
yield return
[
CreateTestPaymentRequest(new Provider.UserSelected
{
Filter = providerFilterMockGbRedirect,
SchemeSelection = new SchemeSelection.InstantOnly { AllowRemitterFee = true },
},
sortCodeAccountNumber,
retry: new Retry.BaseRetry())
];
yield return
[
CreateTestPaymentRequest(
new Provider.Preselected(
"mock-payments-gb-redirect",
schemeSelection: new SchemeSelection.InstantPreferred { AllowRemitterFee = false })
{
Remitter = remitterSortAccountNumber,
},
sortCodeAccountNumber,
retry: new Retry.BaseRetry())
];
}
private async Task<GetPaymentUnion> CreatePaymentAndSetAuthorisationStatusAsync(
ITrueLayerClient trueLayerClient,
CreatePaymentRequest paymentRequest,
MockBankPaymentAction mockBankPaymentAction,
Type expectedPaymentStatus)
{
var response = await trueLayerClient.Payments.CreatePayment(paymentRequest, idempotencyKey: Guid.NewGuid().ToString());
response.StatusCode.Should().Be(HttpStatusCode.Created);
var authorizing = response.Data.AsT3;
var paymentId = authorizing.Id;
var providerReturnUri = await _fixture.MockBankClient.AuthorisePaymentAsync(
authorizing.AuthorizationFlow!.Actions.Next.AsT2.Uri,
mockBankPaymentAction);
await _fixture.ApiClient.SubmitPaymentsProviderReturnAsync(providerReturnUri.Query, providerReturnUri.Fragment);
return await PollPaymentForTerminalStatusAsync(trueLayerClient, paymentId, expectedPaymentStatus);
}
private static async Task<GetPaymentUnion> PollPaymentForTerminalStatusAsync(
ITrueLayerClient trueLayerClient,
string paymentId,
Type expectedStatus)
{
var getPaymentResponse = await Waiter.WaitAsync(
() => trueLayerClient.Payments.GetPayment(paymentId),
r => r.Data.GetType() == expectedStatus);
getPaymentResponse.IsSuccessful.Should().BeTrue();
return getPaymentResponse.Data;
}
}