feat/Update_Controllers_to_Allow_for_Premium_Membership
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Security.Claims;
|
||||
using JobTrackerApi.Models;
|
||||
using JobTrackerApi.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -16,17 +17,20 @@ public sealed class BillingController : ControllerBase
|
||||
private readonly UserManager<ApplicationUser> _users;
|
||||
private readonly RoleManager<IdentityRole> _roles;
|
||||
private readonly ILogger<BillingController> _logger;
|
||||
private readonly ExternalOrigin _externalOrigin;
|
||||
|
||||
public BillingController(
|
||||
IConfiguration configuration,
|
||||
UserManager<ApplicationUser> users,
|
||||
RoleManager<IdentityRole> roles,
|
||||
ILogger<BillingController> logger)
|
||||
ILogger<BillingController> logger,
|
||||
ExternalOrigin? externalOrigin = null)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_users = users;
|
||||
_roles = roles;
|
||||
_logger = logger;
|
||||
_externalOrigin = externalOrigin ?? ExternalOrigin.FromConfiguration(configuration);
|
||||
}
|
||||
|
||||
public sealed record BillingRedirectDto(string Url);
|
||||
@@ -44,7 +48,7 @@ public sealed class BillingController : ControllerBase
|
||||
var entitlements = AccountPlans.ForRoles(await _users.GetRolesAsync(user));
|
||||
return Ok(new BillingStatusDto(
|
||||
enabled,
|
||||
enabled && !entitlements.AdvancedAi,
|
||||
enabled && !entitlements.Ai,
|
||||
enabled && !string.IsNullOrWhiteSpace(user.StripeCustomerId)));
|
||||
}
|
||||
|
||||
@@ -60,8 +64,8 @@ public sealed class BillingController : ControllerBase
|
||||
if (user is null) return Unauthorized();
|
||||
|
||||
var currentRoles = await _users.GetRolesAsync(user);
|
||||
if (AccountPlans.ForRoles(currentRoles).AdvancedAi)
|
||||
return Conflict("This account already has Premium access.");
|
||||
if (AccountPlans.ForRoles(currentRoles).Ai)
|
||||
return Conflict("This account already has Pro access.");
|
||||
|
||||
var metadata = new Dictionary<string, string> { [UserMetadataKey] = user.Id };
|
||||
var options = new Stripe.Checkout.SessionCreateOptions
|
||||
@@ -224,9 +228,7 @@ public sealed class BillingController : ControllerBase
|
||||
secretKey = (_configuration["Stripe:SecretKey"] ?? string.Empty).Trim();
|
||||
premiumPrice = (_configuration["Stripe:PricePremium"] ?? string.Empty).Trim();
|
||||
webhookSecret = (_configuration["Stripe:WebhookSecret"] ?? string.Empty).Trim();
|
||||
publicBaseUrl = (_configuration["App:PublicBaseUrl"] ?? string.Empty).Trim().TrimEnd('/');
|
||||
return secretKey.Length > 0 && premiumPrice.Length > 0 && webhookSecret.Length > 0
|
||||
&& Uri.TryCreate(publicBaseUrl, UriKind.Absolute, out var uri)
|
||||
&& uri.Scheme is "http" or "https";
|
||||
publicBaseUrl = _externalOrigin.BaseUrl;
|
||||
return secretKey.Length > 0 && premiumPrice.Length > 0 && webhookSecret.Length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user