70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using LiteCharms.Features;
|
|
using LiteCharms.Features.Hasher;
|
|
using LiteCharms.Features.MidrandBooks.Customers;
|
|
using LiteCharms.Features.MidrandBooks.Payments;
|
|
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
|
|
|
namespace MidrandBookshop.Components.Pages;
|
|
|
|
public partial class PaymentSuccess
|
|
{
|
|
[Inject] private CartService CartService { get; set; } = default!;
|
|
[Inject] private CustomerService CustomerService { get; set; } = default!;
|
|
[Inject] private PaymentService PaymentService { get; set; } = default!;
|
|
[Inject] private HashService HashService { get; set; } = default!;
|
|
[Inject] private AuthenticationStateProvider AuthStateProvider { get; set; } = default!;
|
|
[Inject] private CancellationToken CancellationToken { get; set; } = default!;
|
|
|
|
[Parameter]
|
|
[SupplyParameterFromQuery(Name = "reference")]
|
|
public string? PaymentReference { get; set; }
|
|
|
|
private ClaimsPrincipal? User { get; set; }
|
|
|
|
private string? CustomerEmail { get; set; }
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
User = authState!.User;
|
|
|
|
if (User?.Identity?.IsAuthenticated == false) Navigation.NavigateTo("/login");
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (!firstRender) return;
|
|
|
|
long orderId = HashService.DecodeLongIdHash(PaymentReference!).Value;
|
|
string orderHash = HashService.HashEncodeLongId(orderId).Value;
|
|
|
|
CustomerEmail = User!.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)!.Value!;
|
|
|
|
var customerFetch = await CustomerService.GetCustomerAsync(CustomerEmail, CancellationToken);
|
|
|
|
if (customerFetch.IsFailed) return;
|
|
|
|
long customerId = customerFetch.Value.Id;
|
|
|
|
var paymentIdFetch = await PaymentService.GetOrderPaymentAsync(orderId, CancellationToken);
|
|
|
|
if (paymentIdFetch.IsFailed) return;
|
|
|
|
await PaymentService.WriteLedgerEntryAsync(new CreateLedgerEntry
|
|
{
|
|
CustomerId = customerId,
|
|
OrderId = orderId,
|
|
PaymentGatewayId = 1,
|
|
PaymentGatewayReference = orderHash,
|
|
PaymentId = paymentIdFetch.Value.Id,
|
|
Status = LedgerStatuses.Changed
|
|
|
|
}, CancellationToken);
|
|
|
|
CartService.Clear();
|
|
CartService.ShoppingCart.OrderId = null;
|
|
await CartService.SaveCartToStorageAsync();
|
|
CartService.NotifyStateChanged();
|
|
}
|
|
}
|