Implement billing functionality and enhance role management
- Introduced billing capabilities, allowing users to request payments for subscription periods (3/6/12 months) with admin approval via Telegram. - Updated role management to include a `BillingEnabled` property, preventing billing for admin roles. - Enhanced the `CreateRoleCommand` and `UpdateRoleCommand` to accept billing parameters, ensuring proper handling during role creation and updates. - Added new endpoints for billing management and integrated billing checks into VPN config creation to enforce payment requirements. - Updated related services, models, and tests to support the new billing features, ensuring comprehensive coverage and functionality. - Enhanced documentation to reflect the new billing processes and role management changes.
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
using PnvPanel.Application.Billing;
|
||||
using PnvPanel.Application.Billing.CancelPaymentRequest;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Billing;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Billing.CancelPaymentRequest;
|
||||
|
||||
public class CancelPaymentRequestCommandHandlerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Handle_WhenAwaitingPayment_Cancels()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000);
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new CancelPaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CancelPaymentRequestCommand(request.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(PaymentRequestStatus.Cancelled, request.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenAwaitingConfirmation_ReturnsNotCancellable()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000);
|
||||
request.MarkPaymentSent();
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new CancelPaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CancelPaymentRequestCommand(request.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.RequestNotCancellable, result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenOwnedByAnotherUser_ReturnsNotFound()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var request = PaymentRequest.Create(Guid.NewGuid(), PaymentPeriod.Quarter, 1000);
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new CancelPaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
FakeCurrentUser.Authenticated(Guid.NewGuid())
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CancelPaymentRequestCommand(request.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.RequestNotFound, result.Error);
|
||||
}
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Billing;
|
||||
using PnvPanel.Application.Billing.CreatePaymentRequest;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Billing;
|
||||
using PnvPanel.Domain.Pricing;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Billing.CreatePaymentRequest;
|
||||
|
||||
public class CreatePaymentRequestCommandHandlerTests
|
||||
{
|
||||
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
||||
|
||||
private static CurrentUserProfile Profile(
|
||||
Guid userId,
|
||||
bool billingEnabled = true,
|
||||
int maxConfigs = 5,
|
||||
DateTimeOffset? paidUntil = null
|
||||
) =>
|
||||
new(
|
||||
userId,
|
||||
"alice",
|
||||
Guid.NewGuid(),
|
||||
"premium",
|
||||
IsActivated: true,
|
||||
IsBlocked: false,
|
||||
MaxConfigs: maxConfigs,
|
||||
MaxIpLimit: 3,
|
||||
SubscriptionToken: "sub-token",
|
||||
BillingEnabled: billingEnabled,
|
||||
BillingPaidUntil: paidUntil,
|
||||
BillingSuspended: false
|
||||
);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithConfiguredPricing_ComputesAmountAndCreatesRequest()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var pricing = PricingSettings.CreateDefault();
|
||||
pricing.Update(500, 450, 400);
|
||||
dbContext.PricingSettings.Add(pricing);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, maxConfigs: 5));
|
||||
|
||||
var handler = new CreatePaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(500 * 5 * 3, result.Value.AmountSnapshot);
|
||||
Assert.Equal(PaymentRequestStatus.AwaitingPayment, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenBillingNotEnabled_ReturnsNotEnabled()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, billingEnabled: false));
|
||||
|
||||
var handler = new CreatePaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.NotEnabled, result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenRoleHasUnlimitedConfigs_ReturnsUnlimitedRoleNotSupported()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, maxConfigs: RoleQuota.Unlimited));
|
||||
|
||||
var handler = new CreatePaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.UnlimitedRoleNotSupported, result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenActiveRequestExists_ReturnsActiveRequestExists()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
dbContext.PaymentRequests.Add(PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1000));
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId));
|
||||
|
||||
var handler = new CreatePaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.ActiveRequestExists, result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenPricingNotConfigured_ReturnsPricingNotConfigured()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId));
|
||||
|
||||
var handler = new CreatePaymentRequestCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new CreatePaymentRequestCommand(PaymentPeriod.Quarter),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.PricingNotConfigured, result.Error);
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Billing.GetMyBillingStatus;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Billing;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Billing.GetMyBillingStatus;
|
||||
|
||||
public class GetMyBillingStatusQueryHandlerTests
|
||||
{
|
||||
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
||||
|
||||
private static CurrentUserProfile Profile(
|
||||
Guid userId,
|
||||
bool billingEnabled,
|
||||
DateTimeOffset? paidUntil = null,
|
||||
bool suspended = false
|
||||
) =>
|
||||
new(
|
||||
userId,
|
||||
"alice",
|
||||
Guid.NewGuid(),
|
||||
"premium",
|
||||
IsActivated: true,
|
||||
IsBlocked: false,
|
||||
MaxConfigs: 5,
|
||||
MaxIpLimit: 3,
|
||||
SubscriptionToken: "sub-token",
|
||||
BillingEnabled: billingEnabled,
|
||||
BillingPaidUntil: paidUntil,
|
||||
BillingSuspended: suspended
|
||||
);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenBillingNotEnabled_ReturnsDisabledStatusWithoutQueryingRequests()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, billingEnabled: false));
|
||||
|
||||
var handler = new GetMyBillingStatusQueryHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(new GetMyBillingStatusQuery(), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.False(result.Value.BillingEnabled);
|
||||
Assert.Null(result.Value.ActiveRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenActiveRequestExists_IncludesItInStatus()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var paidUntil = DateTimeOffset.UtcNow.AddDays(10);
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1500);
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_identityService
|
||||
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
|
||||
.Returns(Profile(userId, billingEnabled: true, paidUntil: paidUntil));
|
||||
|
||||
var handler = new GetMyBillingStatusQueryHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(new GetMyBillingStatusQuery(), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.True(result.Value.BillingEnabled);
|
||||
Assert.Equal(paidUntil, result.Value.PaidUntil);
|
||||
Assert.NotNull(result.Value.ActiveRequest);
|
||||
Assert.Equal(request.Id, result.Value.ActiveRequest!.Id);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Billing;
|
||||
using PnvPanel.Application.Billing.MarkPaymentSent;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Billing;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Billing.MarkPaymentSent;
|
||||
|
||||
public class MarkPaymentSentCommandHandlerTests
|
||||
{
|
||||
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
||||
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenAwaitingPayment_MovesToAwaitingConfirmationAndNotifiesAdmins()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Year, 6000);
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new MarkPaymentSentCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
_telegramNotifier,
|
||||
FakeCurrentUser.Authenticated(userId, "alice")
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new MarkPaymentSentCommand(request.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(PaymentRequestStatus.AwaitingConfirmation, request.Status);
|
||||
await _telegramNotifier
|
||||
.Received(1)
|
||||
.NotifyAdminsPaymentRequestedAsync(
|
||||
request.Id,
|
||||
Arg.Any<string>(),
|
||||
PaymentPeriod.Year,
|
||||
6000,
|
||||
Arg.Any<CancellationToken>()
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenAlreadyAwaitingConfirmation_ReturnsNotAwaitingPayment()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var userId = Guid.NewGuid();
|
||||
var request = PaymentRequest.Create(userId, PaymentPeriod.Year, 6000);
|
||||
request.MarkPaymentSent();
|
||||
dbContext.PaymentRequests.Add(request);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new MarkPaymentSentCommandHandler(
|
||||
dbContext,
|
||||
_identityService,
|
||||
_telegramNotifier,
|
||||
FakeCurrentUser.Authenticated(userId)
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new MarkPaymentSentCommand(request.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(BillingErrors.RequestNotAwaitingPayment, result.Error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user