From 4f3d27c5ae61cfc3b8f0bffbb724a8354b5804d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Tore=20Gjerde?= Date: Thu, 4 Jul 2024 11:38:24 +0200 Subject: [PATCH] Add fake payment processor for use in automated GUI tests. (#705) --- .../Extensions/ServiceCollectionExtensions.cs | 25 ++++-- .../FakePaymentProcessor.cs | 80 +++++++++++++++++++ 2 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/Altinn.App.Core/Features/Payment/Processors/FakePaymentProcessor/FakePaymentProcessor.cs diff --git a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs index 73cd00bc0..d79058bcd 100644 --- a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs @@ -9,6 +9,7 @@ using Altinn.App.Core.Features.Options; using Altinn.App.Core.Features.PageOrder; using Altinn.App.Core.Features.Payment.Processors; +using Altinn.App.Core.Features.Payment.Processors.FakePaymentProcessor; using Altinn.App.Core.Features.Payment.Processors.Nets; using Altinn.App.Core.Features.Payment.Services; using Altinn.App.Core.Features.Pdf; @@ -178,7 +179,7 @@ IWebHostEnvironment env AddAppOptions(services); AddActionServices(services); AddPdfServices(services); - AddNetsPaymentServices(services, configuration); + AddPaymentServices(services, configuration, env); AddSignatureServices(services); AddEventServices(services); AddNotificationServices(services); @@ -263,20 +264,30 @@ private static void AddPdfServices(IServiceCollection services) #pragma warning restore CS0618 // Type or member is obsolete } - private static void AddNetsPaymentServices(this IServiceCollection services, IConfiguration configuration) + private static void AddPaymentServices( + this IServiceCollection services, + IConfiguration configuration, + IHostEnvironment env + ) { - IConfigurationSection configurationSection = configuration.GetSection("NetsPaymentSettings"); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + // Fake Payment Processor used for automatic frontend tests + if (!env.IsProduction()) + { + services.AddTransient(); + } + + // Nets Easy + IConfigurationSection configurationSection = configuration.GetSection("NetsPaymentSettings"); if (configurationSection.Exists()) { services.Configure(configurationSection); services.AddHttpClient(); services.AddTransient(); } - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); } private static void AddSignatureServices(IServiceCollection services) diff --git a/src/Altinn.App.Core/Features/Payment/Processors/FakePaymentProcessor/FakePaymentProcessor.cs b/src/Altinn.App.Core/Features/Payment/Processors/FakePaymentProcessor/FakePaymentProcessor.cs new file mode 100644 index 000000000..552c02719 --- /dev/null +++ b/src/Altinn.App.Core/Features/Payment/Processors/FakePaymentProcessor/FakePaymentProcessor.cs @@ -0,0 +1,80 @@ +using Altinn.App.Core.Configuration; +using Altinn.App.Core.Features.Payment.Models; +using Altinn.App.Core.Models; +using Altinn.Platform.Storage.Interface.Models; +using Microsoft.Extensions.Options; + +namespace Altinn.App.Core.Features.Payment.Processors.FakePaymentProcessor; + +/// +/// This class is a fake payment processor that can be used for testing purposes. +/// +internal sealed class FakePaymentProcessor : IPaymentProcessor +{ + private readonly GeneralSettings _generalSettings; + public string PaymentProcessorId => "Fake Payment Processor"; + + public FakePaymentProcessor(IOptions generalSettings) + { + _generalSettings = generalSettings.Value; + } + + public Task StartPayment(Instance instance, OrderDetails orderDetails, string? language) + { + return Task.FromResult( + new PaymentDetails { PaymentId = "fake-payment-id", RedirectUrl = GetAltinnAppUrl(instance) } + ); + } + + public Task TerminatePayment(Instance instance, PaymentInformation paymentInformation) + { + return Task.FromResult(true); + } + + public Task<(PaymentStatus status, PaymentDetails paymentDetails)> GetPaymentStatus( + Instance instance, + string paymentId, + decimal expectedTotalIncVat, + string? language + ) + { + return Task.FromResult( + ( + PaymentStatus.Paid, + new PaymentDetails + { + PaymentId = paymentId, + RedirectUrl = GetAltinnAppUrl(instance), + Payer = new Payer + { + PrivatePerson = new PayerPrivatePerson + { + FirstName = "Test", + LastName = "Testersen", + Email = "test@test.no", + PhoneNumber = new PhoneNumber { Prefix = "+47", Number = "12345678" } + } + }, + PaymentType = "CARD", + PaymentMethod = "MasterCard", + CreatedDate = new DateTime().ToLongDateString(), + ChargedDate = new DateTime().ToLongDateString(), + InvoiceDetails = null, + CardDetails = new CardDetails + { + ExpiryDate = new DateTime().AddYears(2).ToLongDateString(), + MaskedPan = "1234********1234", + } + } + ) + ); + } + + private string GetAltinnAppUrl(Instance instance) + { + var instanceIdentifier = new InstanceIdentifier(instance); + string baseUrl = _generalSettings.FormattedExternalAppBaseUrl(new AppIdentifier(instance)); + var altinnAppUrl = $"{baseUrl}#/instance/{instanceIdentifier}"; + return altinnAppUrl; + } +}