Compare commits

..

6 Commits

Author SHA1 Message Date
khwezi 157f097dfb Merge pull request 'Catering for service registration of non-UI apps' (#109) from payments into master
Reviewed-on: #109
2026-06-13 10:46:11 +02:00
Khwezi Mngoma 630e74814b Catering for service registration of non-UI apps
continuous-integration/drone/pr Build is passing
2026-06-13 10:45:31 +02:00
khwezi 6248d03ead Merge pull request 'Removed automatic service registration for the CartService' (#108) from payments into master
Reviewed-on: #108
2026-06-13 10:22:52 +02:00
Khwezi Mngoma 9b474a398b Removed automatic service registration for the CartService
continuous-integration/drone/pr Build is passing
2026-06-13 10:22:24 +02:00
khwezi 3deae15f5a Merge pull request 'Removed automatic LocalStorageService registration' (#107) from payments into master
Reviewed-on: #107
2026-06-13 10:19:13 +02:00
Khwezi Mngoma 8e1df7938b Removed automatic LocalStorageService registration
continuous-integration/drone/pr Build is passing
2026-06-13 10:18:42 +02:00
6 changed files with 43 additions and 45 deletions
@@ -1,24 +1,28 @@
using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Browser;
using LiteCharms.Features.MidrandBooks.Abstractions;
namespace LiteCharms.Features.MidrandBooks.Extensions;
public static class Shop
{
public static IServiceCollection AddShopServices(this IServiceCollection services)
public static IServiceCollection AddShopServices(this IServiceCollection services, bool includeLocalStorage = false)
{
var serviceType = typeof(IService);
var sharedImplementations = typeof(IFeatures).Assembly.GetTypes()
.Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
foreach (var sharedImplementation in sharedImplementations) services.AddTransient(sharedImplementation);
foreach (var sharedImplementation in sharedImplementations) services.AddScoped(sharedImplementation);
var coreImplementations = typeof(IMidrandBooks).Assembly.GetTypes()
.Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
foreach (var coreImplementation in coreImplementations) services.AddScoped(coreImplementation);
if (includeLocalStorage)
services.AddScoped<LocalStorageService>();
return services;
}
}
@@ -1,5 +1,4 @@
using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Browser;
using LiteCharms.Features.Browser;
using LiteCharms.Features.Hasher;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Payments.Models;
@@ -7,7 +6,7 @@ using LiteCharms.Features.MidrandBooks.Products.Models;
namespace LiteCharms.Features.MidrandBooks.Payments;
public sealed class CartService(LocalStorageService localStorage) : IService
public sealed class CartService(LocalStorageService localStorage)
{
private readonly string CartStorageKey = HashService.ToMd5Hash(nameof(Cart)).Value;
@@ -147,8 +147,35 @@ public sealed partial class PayfastService(IDbContextFactory<MidrandBooksDbConte
{
var pfOutput = new StringBuilder();
// Define the exact structural sequence mandated by Payfast's documentation
string[] mandatorySequence =
var mandatorySequence = GetPayfastMandatoryFieldSequence();
foreach (string key in mandatorySequence)
{
if (data.TryGetValue(key, out string? rawValue) && !string.IsNullOrEmpty(rawValue))
{
string encodedVal = HttpUtility.UrlEncode(rawValue.Trim());
string val = PercentEncodingRegex.Replace(encodedVal, m => m.Value.ToUpperInvariant());
pfOutput.Append($"{key}={val}&");
}
}
var getString = pfOutput.Length > 0
? pfOutput.ToString()[..^1]
: string.Empty;
if (!string.IsNullOrWhiteSpace(passPhrase))
{
string encodedPassphrase = HttpUtility.UrlEncode(passPhrase.Trim());
string safePassphrase = PercentEncodingRegex.Replace(encodedPassphrase, m => m.Value.ToUpperInvariant());
getString += $"&passphrase={safePassphrase}";
}
return HashService.ToMd5Hash(getString);
}
private static string[] GetPayfastMandatoryFieldSequence() =>
[
"merchant_id",
"merchant_key",
@@ -182,35 +209,4 @@ public sealed partial class PayfastService(IDbContextFactory<MidrandBooksDbConte
"frequency",
"cycles"
];
// 1. Iterate explicitly by the mandatory positional array sequence instead of the dictionary's internal order
foreach (string key in mandatorySequence)
{
// Only append if the key exists in your source dictionary and contains data
if (data.TryGetValue(key, out string? rawValue) && !string.IsNullOrEmpty(rawValue))
{
// Payfast requires spaces to be '+' signs. HttpUtility does this natively.
string encodedVal = HttpUtility.UrlEncode(rawValue.Trim());
// Payfast requires all OTHER percent-encoded hex arrays to be UPPERCASE (e.g., %3A instead of %3a)
string val = Regex.Replace(encodedVal, "%[0-9A-Fa-f]{2}", m => m.Value.ToUpperInvariant());
pfOutput.Append($"{key}={val}&");
}
}
string getString = pfOutput.Length > 0
? pfOutput.ToString()[..^1]
: string.Empty;
if (!string.IsNullOrWhiteSpace(passPhrase))
{
string encodedPassphrase = HttpUtility.UrlEncode(passPhrase.Trim());
string safePassphrase = Regex.Replace(encodedPassphrase, "%[0-9A-Fa-f]{2}", m => m.Value.ToUpperInvariant());
getString += $"&passphrase={safePassphrase}";
}
return HashService.ToMd5Hash(getString);
}
}
+2 -3
View File
@@ -1,11 +1,10 @@
using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Api.Configuration;
using LiteCharms.Features.Api.Configuration;
using LiteCharms.Features.Api.Models;
using LiteCharms.Features.Api.Sdk;
namespace LiteCharms.Features.Api;
public sealed class TokenService(IConnectApi connectApi, IOptions<LiteCharmsClientSettings> clientOptions) : IService
public sealed class TokenService(IConnectApi connectApi, IOptions<LiteCharmsClientSettings> clientOptions)
{
private readonly LiteCharmsClientSettings clientSettings = clientOptions.Value;
@@ -1,8 +1,6 @@
using LiteCharms.Features.Abstractions;
namespace LiteCharms.Features.Browser;
namespace LiteCharms.Features.Browser;
public sealed class LocalStorageService(ProtectedLocalStorage storage) : IService
public sealed class LocalStorageService(ProtectedLocalStorage storage)
{
public async ValueTask<Result> DeleteAsync(string key)
{
+2
View File
@@ -46,6 +46,8 @@ public static class Api
options.Retry.BackoffType = Polly.DelayBackoffType.Exponential;
});
services.AddScoped<TokenService>();
return services;
}