Compare commits
121
Commits
99c0508f6f
...
1.121.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5ad8e2d50 | ||
|
|
8e2942487d | ||
|
|
fa79a58004 | ||
|
|
9997d4f0ed | ||
|
|
33edae9eff | ||
|
|
c1e52ea908 | ||
|
|
0d5702f0fe | ||
|
|
e4c3779092 | ||
|
|
da5f233c3b | ||
|
|
02d89eec4f | ||
|
|
95dc2e2da2 | ||
|
|
59fc0432b4 | ||
|
|
157f097dfb | ||
|
|
6248d03ead | ||
|
|
3deae15f5a | ||
|
|
d9f2d32c76 | ||
|
|
1ace61baa5 | ||
|
|
2ed15b548f | ||
|
|
ef2428f8e3 | ||
|
|
b424b24c2e | ||
|
|
cadc5888cc | ||
|
|
92abf6c5be | ||
|
|
37e0d3b93f | ||
|
|
b42c0fcc4f | ||
|
|
9418c77f0e | ||
|
|
2bb1c8c3bc | ||
|
|
388a5f4c87 | ||
|
|
c31dd308a4 | ||
|
|
787507bed9 | ||
|
|
5140da2c6c | ||
|
|
0ad410c64e | ||
|
|
6e580ecdf6 | ||
|
|
4c194c1141 | ||
|
|
41eb4daeb4 | ||
|
|
7fe5f7aef3 | ||
|
|
31254932ae | ||
|
|
5a0aae8182 | ||
|
|
8d8ebffabf | ||
|
|
e2d29261da | ||
|
|
f001b02633 | ||
|
|
de955a96a8 | ||
|
|
c4d3bb4cdf | ||
|
|
cdc80db214 | ||
|
|
3847927ace | ||
|
|
c27aba1954 | ||
|
|
1c946dab26 | ||
|
|
9977cf27b9 | ||
|
|
8e9ac1e1ad | ||
|
|
16dae7c9fb | ||
|
|
f8153e86b4 | ||
|
|
84d33d3607 | ||
|
|
f51cc03327 | ||
|
|
aff6fcabf4 | ||
|
|
8140b5fe65 | ||
|
|
9285cedfa9 | ||
|
|
343874551a | ||
|
|
0eac9d533f | ||
|
|
24ba609e0c | ||
|
|
29f6d66c44 | ||
|
|
bcfc9ef962 | ||
|
|
b4e967acc9 | ||
|
|
205bbb9f3f | ||
|
|
73ef4b04a9 | ||
|
|
780415b6d4 | ||
|
|
879094073a | ||
|
|
b369dad452 | ||
|
|
c938bfec09 | ||
|
|
adc14038db | ||
|
|
61cb6c2228 | ||
|
|
e7a798b5e9 | ||
|
|
41b6b71b31 | ||
|
|
ee6beef603 | ||
|
|
1c3f3eaf0d | ||
|
|
2e77666d9e | ||
|
|
1977b6b301 | ||
|
|
0ab14d8b63 | ||
|
|
466458e230 | ||
|
|
141d32f591 | ||
|
|
d9e7f225ae | ||
|
|
6ae63e2ad1 | ||
|
|
f5efdde37c | ||
|
|
1592d5dc8f | ||
|
|
50a8a59d92 | ||
|
|
b70d9559b0 | ||
|
|
81d5e8f07c | ||
|
|
20a53942b5 | ||
|
|
9edb2aa4aa | ||
|
|
6ed023f2cf | ||
|
|
2c9f5a846c | ||
|
|
41f7c05be3 | ||
|
|
1a03355e84 | ||
|
|
7743c3178e | ||
|
|
ab3d8e6e9a | ||
|
|
db4c348288 | ||
|
|
6683234642 | ||
|
|
6ddbb9479a | ||
|
|
6c7349a0f8 | ||
|
|
e97fd6cd3f | ||
|
|
184c7c252a | ||
|
|
bfe8c458d6 | ||
|
|
e6e0475db1 | ||
|
|
5090c60797 | ||
|
|
9432252e15 | ||
|
|
47111a1a3a | ||
|
|
6eb3d50375 | ||
|
|
4deb732804 | ||
|
|
20d9387d0b | ||
|
|
9f6d0ccaa0 | ||
|
|
1acbc4d213 | ||
|
|
8c99668fac | ||
|
|
ad44f46204 | ||
|
|
49d999c1e3 | ||
|
|
9ed4777a18 | ||
|
|
0cf44f68cc | ||
|
|
41ed5a4288 | ||
|
|
bbcba5e06c | ||
|
|
502cc326dd | ||
|
|
4675d4c5fc | ||
|
|
f80bb2fff9 | ||
|
|
6767906b0d | ||
|
|
a344af4498 |
@@ -11,7 +11,7 @@
|
||||
<!-- Quartz Scheduler-->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Bogus" Version="35.6.5" />
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.103">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
@@ -116,8 +116,8 @@
|
||||
|
||||
<!-- Amazon S3 SDK -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.6" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.3" />
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.7" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.4" />
|
||||
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
|
||||
|
||||
<!-- global Usings -->
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
<!-- Quartz Scheduler-->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Humanizer" Version="3.0.10" />
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.103">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
@@ -136,8 +136,8 @@
|
||||
|
||||
<!-- Amazon S3 SDK -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.6" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.3" />
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.7" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.4" />
|
||||
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
|
||||
|
||||
<!-- global Usings -->
|
||||
|
||||
+25
-85
@@ -1,17 +1,13 @@
|
||||
using LiteCharms.Features.Api.Configuration;
|
||||
using LiteCharms.Features.Hasher;
|
||||
using LiteCharms.Features.Hasher;
|
||||
using LiteCharms.Features.Mediator;
|
||||
using LiteCharms.Features.MidrandBooks.Orders;
|
||||
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Payments.Events.Handlers;
|
||||
|
||||
public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvider services,
|
||||
IOptions<PayfastSettings> payfastOptions, ILogger<PayfastPaymentConfirmationReceivedEvent> logger) :
|
||||
public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvider services, ILogger<PayfastPaymentConfirmationReceivedEvent> logger) :
|
||||
INotificationHandler<PayfastPaymentConfirmationReceivedEvent>
|
||||
{
|
||||
private readonly PayfastSettings pasfastSettings = payfastOptions.Value;
|
||||
|
||||
public async ValueTask Handle(PayfastPaymentConfirmationReceivedEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
using var activity = MediatorTelemetry.Source.StartActivity($"Quartz: {typeof(PayfastPaymentConfirmationReceivedEvent).Name}");
|
||||
@@ -23,83 +19,34 @@ public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvi
|
||||
var paymentService = scope.ServiceProvider.GetRequiredService<PaymentService>();
|
||||
var payfastService = scope.ServiceProvider.GetRequiredService<PayfastService>();
|
||||
|
||||
var payload = notification.Payload ?? throw new Exception("Payload metadata context context is null.");
|
||||
var payload = notification.Payload ?? throw new Exception("Payload metadata context is null.");
|
||||
|
||||
var dict = payload.ToParamDictionary();
|
||||
var localSignature = PayfastService.GenerateSignature(dict, pasfastSettings.Passphrase);
|
||||
var hashResult = hashService.DecodeLongIdHash(payload.MerchantPaymentId!);
|
||||
if (hashResult.IsFailed) throw new Exception("Failed to decode application tracking hash key identifier.");
|
||||
|
||||
if (localSignature.IsFailed)
|
||||
throw new Exception("Failed to generate local signature for incoming webhook payload.");
|
||||
var orderResult = await orderService.GetOrderAsync(hashResult.Value, cancellationToken);
|
||||
if (orderResult.IsFailed) throw new Exception("Target system order entity context cannot be traced.");
|
||||
|
||||
if (!string.Equals(localSignature.Value, payload.Signature, StringComparison.OrdinalIgnoreCase))
|
||||
var paymentResult = await paymentService.GetOrderPaymentAsync(orderResult.Value.Id, cancellationToken);
|
||||
if (paymentResult.IsFailed) throw new Exception("Target payment ledger entity cannot be resolved.");
|
||||
|
||||
var isAlreadyProcessed = await paymentService.HasLedgerEntryAsync(orderResult.Value.Id, paymentResult.Value.Id, cancellationToken);
|
||||
if (isAlreadyProcessed.Value)
|
||||
{
|
||||
logger.LogCritical("Incoming webhook signature verification failed. Possible payload tampering.");
|
||||
logger.LogWarning("Webhook reference token '{Ref}' already verified. Skipping processing routines.", payload.MerchantPaymentId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var hashResult = hashService.DecodeLongIdHash(payload.MerchantPaymentId!);
|
||||
|
||||
if (hashResult.IsFailed) throw new Exception("Failed to decode application tracking hash key identifier.");
|
||||
|
||||
var orderResult = await orderService.GetOrderAsync(hashResult.Value, cancellationToken);
|
||||
|
||||
if (orderResult.IsFailed) throw new Exception("Target system order entity context cannot be traced.");
|
||||
|
||||
var paymentResult = await paymentService.GetOrderPaymentAsync(orderResult.Value.Id, cancellationToken);
|
||||
|
||||
if (paymentResult.IsFailed) throw new Exception("Target payment ledger entity cannot be resolved.");
|
||||
var isAmountValid = payfastService.ValidatePaymentAmount(orderResult.Value.Total, payload.AmountGross);
|
||||
if (!isAmountValid.Value)
|
||||
throw new Exception("Security validation exception: Transaction cost variance bounds breached (Price Tampering Detected).");
|
||||
|
||||
decimal.TryParse(payload.AmountGross, CultureInfo.InvariantCulture, out var gross);
|
||||
decimal.TryParse(payload.AmountFee, CultureInfo.InvariantCulture, out var fee);
|
||||
decimal.TryParse(payload.AmountNet, CultureInfo.InvariantCulture, out var net);
|
||||
string status = payload.PaymentStatus ?? "UNKNOWN";
|
||||
|
||||
var isAlreadyProcessed = await paymentService.HasLedgerEntryAsync(orderResult.Value.Id, paymentResult.Value.Id, cancellationToken);
|
||||
|
||||
if (isAlreadyProcessed.Value)
|
||||
{
|
||||
logger.LogWarning("Webhook reference token '{Ref}' already verified. Skipping validation routines.", payload.MerchantPaymentId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (notification.PerformBackgroundChecks)
|
||||
{
|
||||
var isHostValid = await payfastService.ValidateReferrerIpAsync(notification.RemoteIpAddress!, notification.AllowLoopback, cancellationToken);
|
||||
|
||||
if (isHostValid.IsFailed)
|
||||
throw new Exception("Security validation exception: Webhook packet source address failed cluster validation checks.");
|
||||
|
||||
if (!isHostValid.Value)
|
||||
throw new Exception("Security validation exception: Webhook packet source address failed cluster validation checks.");
|
||||
|
||||
var isAmountValid = payfastService.ValidatePaymentAmount(orderResult.Value.Total, payload.AmountGross);
|
||||
|
||||
if (!isAmountValid.Value)
|
||||
throw new Exception("Security validation exception: Transaction cost variance bounds breached.");
|
||||
|
||||
var paramList = new List<string>();
|
||||
|
||||
foreach (var kvp in dict)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(kvp.Value))
|
||||
{
|
||||
string encoded = HttpUtility.UrlEncode(kvp.Value.Trim());
|
||||
|
||||
string safeValue = PayfastService.PercentEncodingRegex.Replace(encoded, m => m.Value.ToLowerInvariant());
|
||||
paramList.Add($"{kvp.Key}={safeValue}");
|
||||
}
|
||||
}
|
||||
|
||||
string rawParamString = string.Join("&", paramList);
|
||||
|
||||
var serverConfirmation = await payfastService.ValidateServerConfirmationAsync(rawParamString, isSandbox: true, cancellationToken);
|
||||
|
||||
if (serverConfirmation.IsFailed)
|
||||
throw new Exception("Security validation exception: Payfast central handshake server rejected payload legitimacy.");
|
||||
}
|
||||
|
||||
await payfastService.WriteLedgerEntryAsync(new CreateGatewayLedgerEntry
|
||||
{
|
||||
OrderId = orderResult.Value.Id,
|
||||
@@ -124,31 +71,23 @@ public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvi
|
||||
CustomerId = orderResult.Value.CustomerId,
|
||||
}, cancellationToken);
|
||||
|
||||
if (ledgerWriteResult.IsFailed)
|
||||
throw new Exception("Failed to write ledger entry for payment confirmation.");
|
||||
if (ledgerWriteResult.IsFailed) throw new Exception("Failed to write ledger entry for payment confirmation.");
|
||||
|
||||
var completePaymentResult = await paymentService.CompletePaymentAsync(paymentResult.Value.Id, PaymentStatuses.Paid, cancellationToken);
|
||||
|
||||
if (completePaymentResult.IsFailed)
|
||||
throw new Exception("Failed to update payment status to 'Paid' for payment confirmation.");
|
||||
if (completePaymentResult.IsFailed) throw new Exception("Failed to update payment status to 'Paid'.");
|
||||
|
||||
var updateOrderResult = await orderService.UpdateOrderStatusAsync(orderResult.Value.Id, OrderStatus.Completed, cancellationToken);
|
||||
|
||||
if (updateOrderResult.IsFailed)
|
||||
throw new Exception("Failed to update order status to 'Completed' for payment confirmation.");
|
||||
if (updateOrderResult.IsFailed) throw new Exception("Failed to update order status to 'Completed'.");
|
||||
|
||||
logger.LogInformation("Order payment verified secure and cleared successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
LedgerStatuses ledgerStatus;
|
||||
LedgerStatuses ledgerStatus = status.Equals("CANCELLED", StringComparison.OrdinalIgnoreCase)
|
||||
? LedgerStatuses.Cancelled
|
||||
: LedgerStatuses.Failed;
|
||||
|
||||
if (status.Equals("CANCELLED", StringComparison.OrdinalIgnoreCase))
|
||||
ledgerStatus = LedgerStatuses.Cancelled;
|
||||
else
|
||||
ledgerStatus = LedgerStatuses.Failed;
|
||||
|
||||
var ledgerWriteResult = await paymentService.WriteLedgerEntryAsync(new CreateLedgerEntry
|
||||
await paymentService.WriteLedgerEntryAsync(new CreateLedgerEntry
|
||||
{
|
||||
OrderId = orderResult.Value.Id,
|
||||
PaymentId = paymentResult.Value.Id,
|
||||
@@ -157,8 +96,9 @@ public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvi
|
||||
CustomerId = orderResult.Value.CustomerId,
|
||||
}, cancellationToken);
|
||||
|
||||
logger.LogInformation("Webhook validation pipeline passed checks successfully, logged entry to ledger with status: {Status}", status);
|
||||
logger.LogInformation("Webhook pipeline logged non-success entry to ledger with status: {Status}", status);
|
||||
}
|
||||
|
||||
activity?.SetStatus(ActivityStatusCode.Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using LiteCharms.Features.Api.Configuration;
|
||||
using LiteCharms.Features.Hasher;
|
||||
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace LiteCharms.Features.MidrandBooks.Payments;
|
||||
|
||||
@@ -49,32 +48,36 @@ public sealed partial class PayfastService(IDbContextFactory<MidrandBooksDbConte
|
||||
}
|
||||
}
|
||||
|
||||
public static bool VerifyIncomingSignature(HttpRequest request, string passphrase)
|
||||
public static bool VerifyIncomingSignatureFromForm(IFormCollection formCollection, string passphrase)
|
||||
{
|
||||
var formFields = request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
|
||||
var sortedFields = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
|
||||
if (!formFields.TryGetValue("signature", out string? incomingSignature))
|
||||
return false;
|
||||
foreach (var field in formCollection)
|
||||
{
|
||||
sortedFields.Add(field.Key, field.Value.ToString());
|
||||
}
|
||||
|
||||
if (!sortedFields.TryGetValue("signature", out var incomingSignature)) return false;
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
|
||||
foreach (var key in formFields.Keys)
|
||||
foreach (var key in sortedFields.Keys)
|
||||
{
|
||||
if (key.Equals("signature", StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
if (key.Equals("signature", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
|
||||
string encodedVal = HttpUtility.UrlEncode(formFields[key].Trim());
|
||||
string cleanVal = PercentEncodingRegex.Replace(encodedVal, m => m.Value.ToUpperInvariant());
|
||||
string encodedVal = HttpUtility.UrlEncode(sortedFields[key].Trim());
|
||||
string cleanVal = PercentEncodingRegex.Replace(encodedVal, m => m.Value.ToUpperInvariant());
|
||||
|
||||
stringBuilder.Append($"{key}={cleanVal}&");
|
||||
}
|
||||
|
||||
string encodedPassphrase = HttpUtility.UrlEncode(passphrase.Trim());
|
||||
string safePassphrase = PercentEncodingRegex.Replace(encodedPassphrase, m => m.Value.ToUpperInvariant());
|
||||
string safePassphrase = PercentEncodingRegex.Replace(encodedPassphrase, m => m.Value.ToUpperInvariant());
|
||||
|
||||
stringBuilder.Append($"passphrase={safePassphrase}");
|
||||
|
||||
string generatedSignature = HashService.ToMd5Hash(stringBuilder.ToString()).Value;
|
||||
|
||||
return incomingSignature.Equals(generatedSignature, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
|
||||
@@ -162,6 +162,7 @@ public sealed class PaymentService(IDbContextFactory<MidrandBooksDbContext> cont
|
||||
CustomerId = request.CustomerId,
|
||||
OrderId = request.OrderId,
|
||||
PaymentId = request.PaymentId,
|
||||
MerchantPaymentId = request.PaymentGatewayReference,
|
||||
Status = request.Status,
|
||||
});
|
||||
|
||||
|
||||
@@ -136,8 +136,8 @@
|
||||
|
||||
<!-- Amazon S3 SDK -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.6" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.3" />
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.7" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.4" />
|
||||
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
|
||||
|
||||
<!-- global Usings -->
|
||||
|
||||
@@ -79,7 +79,7 @@
|
||||
<!-- Quartz Scheduler-->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hashids.net" Version="1.7.0" />
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
|
||||
<PackageReference Include="Meziantou.Analyzer" Version="3.0.103">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
@@ -183,8 +183,8 @@
|
||||
|
||||
<!-- Amazon S3 SDK -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.6" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.3" />
|
||||
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.7" />
|
||||
<PackageReference Include="AWSSDK.S3" Version="4.0.24.4" />
|
||||
|
||||
<!-- global Usings -->
|
||||
<Using Include="Amazon.S3" />
|
||||
|
||||
Reference in New Issue
Block a user