99using Microsoft . Extensions . DependencyInjection ;
1010using Microsoft . Extensions . Options ;
1111using OneOf ;
12+ using TrueLayer . AcceptanceTests . Helpers ;
13+ using TrueLayer . AcceptanceTests . MockBank ;
1214using TrueLayer . Common ;
1315using TrueLayer . Payments . Model ;
1416using TrueLayer . Payments . Model . AuthorizationFlow ;
@@ -46,6 +48,8 @@ public partial class PaymentTests : IClassFixture<ApiTestFixture>
4648 private readonly TrueLayerOptions _configuration ;
4749 private readonly string _gbpMerchantAccountId ;
4850 private readonly string _eurMerchantSecretKey ;
51+ private readonly MockBankClient _mockBankClient ;
52+ private readonly PayApiClient _payApiClient ;
4953
5054 public PaymentTests ( ApiTestFixture fixture )
5155 {
@@ -54,11 +58,19 @@ public PaymentTests(ApiTestFixture fixture)
5458 ( string gbpMerchantAccountId , string eurMerchantAccountId ) = GetMerchantBeneficiaryAccountsAsync ( ) . Result ;
5559 _gbpMerchantAccountId = gbpMerchantAccountId ;
5660 _eurMerchantSecretKey = eurMerchantAccountId ;
61+ _mockBankClient = new MockBankClient ( new HttpClient
62+ {
63+ BaseAddress = new Uri ( "https://pay-mock-connect.truelayer-sandbox.com/" )
64+ } ) ;
65+ _payApiClient = new PayApiClient ( new HttpClient
66+ {
67+ BaseAddress = new Uri ( "https://pay-api.truelayer-sandbox.com" )
68+ } ) ;
5769 }
5870
5971 [ Theory ]
6072 [ MemberData ( nameof ( ExternalAccountPaymentRequests ) ) ]
61- public async Task can_create_external_account_payment ( CreatePaymentRequest paymentRequest )
73+ public async Task Can_Create_External_Account_Payment ( CreatePaymentRequest paymentRequest )
6274 {
6375 var response = await _fixture . Client . Payments . CreatePayment (
6476 paymentRequest , idempotencyKey : Guid . NewGuid ( ) . ToString ( ) ) ;
@@ -77,7 +89,7 @@ public async Task can_create_external_account_payment(CreatePaymentRequest payme
7789 }
7890
7991 [ Fact ]
80- public async Task can_create_merchant_account_gbp_Payment ( )
92+ public async Task Can_Create_Merchant_Account_Gbp_Payment ( )
8193 {
8294 var paymentRequest = CreateTestPaymentRequest (
8395 new Provider . UserSelected
@@ -139,7 +151,7 @@ public async Task can_create_merchant_account_gbp_verification_Payment()
139151 }
140152
141153 [ Fact ]
142- public async Task can_create_merchant_account_eur_Payment ( )
154+ public async Task Can_Create_Merchant_Account_Eur_Payment ( )
143155 {
144156 var paymentRequest = CreateTestPaymentRequest (
145157 new Provider . Preselected ( "mock-payments-fr-redirect" ,
@@ -169,7 +181,7 @@ public async Task can_create_merchant_account_eur_Payment()
169181 }
170182
171183 [ Fact ]
172- public async Task Can_create_payment_with_auth_flow ( )
184+ public async Task Can_Create_Payment_With_Auth_Flow ( )
173185 {
174186 var sortCodeAccountNumber = new AccountIdentifier . SortCodeAccountNumber ( "567890" , "12345678" ) ;
175187 var providerSelection = new Provider . Preselected ( "mock-payments-gb-redirect" , "faster_payments_service" )
@@ -202,9 +214,42 @@ public async Task Can_create_payment_with_auth_flow()
202214 hppUri . Should ( ) . NotBeNullOrWhiteSpace ( ) ;
203215 }
204216
217+ [ Fact ]
218+ public async Task GetPayment_Should_Return_Settled_Payment ( )
219+ {
220+ var providerSelection = new Provider . Preselected ( "mock-payments-gb-redirect" , "faster_payments_service" ) ;
221+
222+ var paymentRequest = CreateTestPaymentRequest (
223+ beneficiary : new Beneficiary . MerchantAccount ( _gbpMerchantAccountId ) ,
224+ providerSelection : providerSelection ,
225+ initAuthorizationFlow : true ) ;
226+
227+ var response = await _fixture . Client . Payments . CreatePayment ( paymentRequest , idempotencyKey : Guid . NewGuid ( ) . ToString ( ) ) ;
228+
229+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . Created ) ;
230+ var authorizing = response . Data . AsT3 ;
231+ var paymentId = authorizing . Id ;
232+
233+ var providerReturnUri = await _mockBankClient . AuthorisePaymentAsync (
234+ authorizing . AuthorizationFlow ! . Actions . Next . AsT2 . Uri ,
235+ MockBankAction . Execute ) ;
236+
237+ await _payApiClient . SubmitProviderReturnParametersAsync ( providerReturnUri . Query , providerReturnUri . Fragment ) ;
238+
239+ var getPaymentResponse = await PollPaymentForTerminalStatusAsync ( paymentId , typeof ( GetPaymentResponse . Settled ) ) ;
240+
241+ var executed = getPaymentResponse . AsT4 ;
242+ executed . AmountInMinor . Should ( ) . Be ( paymentRequest . AmountInMinor ) ;
243+ executed . Currency . Should ( ) . Be ( paymentRequest . Currency ) ;
244+ executed . Id . Should ( ) . NotBeNullOrWhiteSpace ( ) ;
245+ executed . CreatedAt . Should ( ) . NotBe ( default ) ;
246+ executed . PaymentMethod . AsT0 . Should ( ) . NotBeNull ( ) ;
247+ executed . CreditableAt . Should ( ) . NotBeNull ( ) ;
248+ }
249+
205250 [ Theory ]
206251 [ MemberData ( nameof ( ExternalAccountPaymentRequests ) ) ]
207- public async Task Can_get_authorization_required_payment ( CreatePaymentRequest paymentRequest )
252+ public async Task Can_Get_Authorization_Required_Payment ( CreatePaymentRequest paymentRequest )
208253 {
209254 var response = await _fixture . Client . Payments . CreatePayment (
210255 paymentRequest , idempotencyKey : Guid . NewGuid ( ) . ToString ( ) ) ;
@@ -250,7 +295,7 @@ var getPaymentResponse
250295 }
251296
252297 [ Fact ]
253- public async Task Can_create_payment_with_retry_option_and_get_attemptFailed_error ( )
298+ public async Task Can_Create_Payment_With_Retry_Option_And_Get_AttemptFailed_Error ( )
254299 {
255300 // Arrange
256301 var paymentRequest = CreateTestPaymentRequest (
@@ -702,4 +747,16 @@ private GetPaymentResponse.PaymentDetails GetPaymentDetailsAndWaitForItToBeDone(
702747
703748 return ( payment . Value as GetPaymentResponse . PaymentDetails ) ! ;
704749 }
750+
751+ private async Task < GetPaymentUnion > PollPaymentForTerminalStatusAsync (
752+ string paymentId ,
753+ Type expectedStatus )
754+ {
755+ var getPaymentResponseBody = await Waiter . WaitAsync (
756+ ( ) => _fixture . Client . Payments . GetPayment ( paymentId ) ,
757+ r => r . Data . GetType ( ) == expectedStatus ) ;
758+
759+ getPaymentResponseBody . IsSuccessful . Should ( ) . BeTrue ( ) ;
760+ return getPaymentResponseBody . Data ;
761+ }
705762}
0 commit comments