1
0
mirror of https://github.com/bitwarden/server synced 2025-12-25 20:53:16 +00:00
Files
server/src/Api/Models/Request/Accounts/PremiumRequestModel.cs
2021-12-14 16:05:07 +01:00

45 lines
1.6 KiB
C#

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Bit.Core.Settings;
using Microsoft.AspNetCore.Http;
using Enums = Bit.Core.Enums;
namespace Bit.Api.Models.Request.Accounts
{
public class PremiumRequestModel : IValidatableObject
{
[Required]
public Enums.PaymentMethodType? PaymentMethodType { get; set; }
public string PaymentToken { get; set; }
[Range(0, 99)]
public short? AdditionalStorageGb { get; set; }
public IFormFile License { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
public bool Validate(GlobalSettings globalSettings)
{
if (!(License == null && !globalSettings.SelfHosted) ||
(License != null && globalSettings.SelfHosted))
{
return false;
}
return globalSettings.SelfHosted || !string.IsNullOrWhiteSpace(Country);
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var creditType = PaymentMethodType.HasValue && PaymentMethodType.Value == Enums.PaymentMethodType.Credit;
if (string.IsNullOrWhiteSpace(PaymentToken) && !creditType && License == null)
{
yield return new ValidationResult("Payment token or license is required.");
}
if (Country == "US" && string.IsNullOrWhiteSpace(PostalCode))
{
yield return new ValidationResult("Zip / postal code is required.",
new string[] { nameof(PostalCode) });
}
}
}
}