Skip to content

Commit

Permalink
Add fake payment processor for use in automated GUI tests. (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorntore authored Jul 4, 2024
1 parent 2b0f85a commit 4f3d27c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<IPaymentService, PaymentService>();
services.AddTransient<IProcessTask, PaymentProcessTask>();
services.AddTransient<IUserAction, PaymentUserAction>();

// Fake Payment Processor used for automatic frontend tests
if (!env.IsProduction())
{
services.AddTransient<IPaymentProcessor, FakePaymentProcessor>();
}

// Nets Easy
IConfigurationSection configurationSection = configuration.GetSection("NetsPaymentSettings");
if (configurationSection.Exists())
{
services.Configure<NetsPaymentSettings>(configurationSection);
services.AddHttpClient<INetsClient, NetsClient>();
services.AddTransient<IPaymentProcessor, NetsPaymentProcessor>();
}

services.AddTransient<IPaymentService, PaymentService>();
services.AddTransient<IProcessTask, PaymentProcessTask>();
services.AddTransient<IUserAction, PaymentUserAction>();
}

private static void AddSignatureServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// This class is a fake payment processor that can be used for testing purposes.
/// </summary>
internal sealed class FakePaymentProcessor : IPaymentProcessor
{
private readonly GeneralSettings _generalSettings;
public string PaymentProcessorId => "Fake Payment Processor";

public FakePaymentProcessor(IOptions<GeneralSettings> generalSettings)
{
_generalSettings = generalSettings.Value;
}

public Task<PaymentDetails> StartPayment(Instance instance, OrderDetails orderDetails, string? language)
{
return Task.FromResult(
new PaymentDetails { PaymentId = "fake-payment-id", RedirectUrl = GetAltinnAppUrl(instance) }
);
}

public Task<bool> 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 = "[email protected]",
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;
}
}

0 comments on commit 4f3d27c

Please sign in to comment.