Skip to content

Commit

Permalink
Merge branch 'main' into feat/392-metadata-fields-on-data-element
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorntore committed Jul 5, 2024
2 parents 985b0d5 + 4f3d27c commit 5b27af2
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Altinn.App.Api/Altinn.App.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.1" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.3.0" />
<PackageReference Include="Azure.Identity" Version="1.12.0"/>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Altinn.App.Core\Altinn.App.Core.csproj" />
Expand Down
44 changes: 44 additions & 0 deletions src/Altinn.App.Api/Extensions/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Altinn.App.Core.Internal.App;
using Azure.Identity;

namespace Altinn.App.Api.Extensions;

/// <summary>
/// Class for defining extensions to IHostBuilder for AltinnApps
/// </summary>
public static class HostBuilderExtensions
{
/// <summary>
/// Add KeyVault as a configuration provider. Requires that the kvSetting section is present in the configuration and throws an exception if not. See documentation for secret handling in Altinn apps.
/// </summary>
/// <remarks>Use </remarks>
/// <param name="builder"></param>
/// <exception cref="ApplicationConfigException"></exception>
public static void AddAzureKeyVaultAsConfigProvider(this IHostApplicationBuilder builder)
{
IConfigurationManager configuration = builder.Configuration;
IConfigurationSection section = configuration.GetSection("kvSetting");

var keyVaultUri = section.GetValue<string>("SecretUri");
var clientId = section.GetValue<string>("ClientId");
var clientSecret = section.GetValue<string>("ClientSecret");
var tenantId = section.GetValue<string>("TenantId");

if (
string.IsNullOrWhiteSpace(keyVaultUri)
|| string.IsNullOrWhiteSpace(clientId)
|| string.IsNullOrWhiteSpace(clientSecret)
|| string.IsNullOrWhiteSpace(tenantId)
)
{
throw new ApplicationConfigException(
"Attempted to add KeyVault as a configuration provider, but the required settings for authenticating with KeyVault are missing. Please check the configuration."
);
}

builder.Configuration.AddAzureKeyVault(
new Uri(keyVaultUri),
new ClientSecretCredential(tenantId, clientId, clientSecret)
);
}
}
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 5b27af2

Please sign in to comment.