From 506fe28ee7c17da309f4460960d1d7dafd98baa1 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 26 Feb 2019 12:45:34 -0500 Subject: [PATCH] make payment method type required --- src/Api/Controllers/AccountsController.cs | 4 +- .../Controllers/OrganizationsController.cs | 2 +- .../Request/Accounts/PremiumRequestModel.cs | 2 +- .../OrganizationCreateRequestModel.cs | 6 ++- .../Models/Api/Request/PaymentRequestModel.cs | 2 +- src/Core/Services/IOrganizationService.cs | 2 +- src/Core/Services/IUserService.cs | 4 +- .../Implementations/OrganizationService.cs | 36 +--------------- .../Services/Implementations/UserService.cs | 42 ++----------------- 9 files changed, 19 insertions(+), 81 deletions(-) diff --git a/src/Api/Controllers/AccountsController.cs b/src/Api/Controllers/AccountsController.cs index b0985d6feb..04a6b62306 100644 --- a/src/Api/Controllers/AccountsController.cs +++ b/src/Api/Controllers/AccountsController.cs @@ -463,7 +463,7 @@ namespace Bit.Api.Controllers throw new BadRequestException("Invalid license."); } - await _userService.SignUpPremiumAsync(user, model.PaymentToken, model.PaymentMethodType, + await _userService.SignUpPremiumAsync(user, model.PaymentToken, model.PaymentMethodType.Value, model.AdditionalStorageGb.GetValueOrDefault(0), license); return new ProfileResponseModel(user, null, await _userService.TwoFactorIsEnabledAsync(user)); } @@ -518,7 +518,7 @@ namespace Bit.Api.Controllers throw new UnauthorizedAccessException(); } - await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType); + await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType.Value); } [HttpPost("storage")] diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index 8ab111aabe..ccc7d396ab 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -210,7 +210,7 @@ namespace Bit.Api.Controllers } await _organizationService.ReplacePaymentMethodAsync(orgIdGuid, model.PaymentToken, - model.PaymentMethodType); + model.PaymentMethodType.Value); } [HttpPost("{id}/upgrade")] diff --git a/src/Core/Models/Api/Request/Accounts/PremiumRequestModel.cs b/src/Core/Models/Api/Request/Accounts/PremiumRequestModel.cs index fcf89672f6..0a755383ac 100644 --- a/src/Core/Models/Api/Request/Accounts/PremiumRequestModel.cs +++ b/src/Core/Models/Api/Request/Accounts/PremiumRequestModel.cs @@ -7,7 +7,7 @@ namespace Bit.Core.Models.Api { public class PremiumRequestModel : IValidatableObject { - // TODO: Required in future + [Required] public PaymentMethodType? PaymentMethodType { get; set; } public string PaymentToken { get; set; } [Range(0, 99)] diff --git a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs index 2fb261df24..798ab19dcc 100644 --- a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs @@ -21,7 +21,6 @@ namespace Bit.Core.Models.Api public PlanType PlanType { get; set; } [Required] public string Key { get; set; } - // TODO: Required in future if not free plan public PaymentMethodType? PaymentMethodType { get; set; } public string PaymentToken { get; set; } [Range(0, double.MaxValue)] @@ -58,6 +57,11 @@ namespace Bit.Core.Models.Api { yield return new ValidationResult("Payment required.", new string[] { nameof(PaymentToken) }); } + if(PlanType != PlanType.Free && !PaymentMethodType.HasValue) + { + yield return new ValidationResult("Payment method type required.", + new string[] { nameof(PaymentMethodType) }); + } } } } diff --git a/src/Core/Models/Api/Request/PaymentRequestModel.cs b/src/Core/Models/Api/Request/PaymentRequestModel.cs index e345d3ae13..7834805779 100644 --- a/src/Core/Models/Api/Request/PaymentRequestModel.cs +++ b/src/Core/Models/Api/Request/PaymentRequestModel.cs @@ -5,7 +5,7 @@ namespace Bit.Core.Models.Api { public class PaymentRequestModel { - // TODO: Required in future + [Required] public PaymentMethodType? PaymentMethodType { get; set; } [Required] public string PaymentToken { get; set; } diff --git a/src/Core/Services/IOrganizationService.cs b/src/Core/Services/IOrganizationService.cs index 06893f3c48..ea1012ae84 100644 --- a/src/Core/Services/IOrganizationService.cs +++ b/src/Core/Services/IOrganizationService.cs @@ -10,7 +10,7 @@ namespace Bit.Core.Services { public interface IOrganizationService { - Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken, PaymentMethodType? paymentMethodType); + Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken, PaymentMethodType paymentMethodType); Task CancelSubscriptionAsync(Guid organizationId, bool? endOfPeriod = null); Task ReinstateSubscriptionAsync(Guid organizationId); Task UpgradePlanAsync(Guid organizationId, PlanType plan, int additionalSeats); diff --git a/src/Core/Services/IUserService.cs b/src/Core/Services/IUserService.cs index 233d5c3857..a585374c3f 100644 --- a/src/Core/Services/IUserService.cs +++ b/src/Core/Services/IUserService.cs @@ -43,11 +43,11 @@ namespace Bit.Core.Services Task DeleteAsync(User user); Task DeleteAsync(User user, string token); Task SendDeleteConfirmationAsync(string email); - Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType? paymentMethodType, + Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license); Task UpdateLicenseAsync(User user, UserLicense license); Task AdjustStorageAsync(User user, short storageAdjustmentGb); - Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType? paymentMethodType); + Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType); Task CancelPremiumAsync(User user, bool? endOfPeriod = null); Task ReinstatePremiumAsync(User user); Task DisablePremiumAsync(Guid userId, DateTime? expirationDate); diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 007b932d46..e8d21ded55 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -72,7 +72,7 @@ namespace Bit.Core.Services } public async Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken, - PaymentMethodType? paymentMethodType) + PaymentMethodType paymentMethodType) { var organization = await GetOrgById(organizationId); if(organization == null) @@ -80,24 +80,8 @@ namespace Bit.Core.Services throw new NotFoundException(); } - if(!paymentMethodType.HasValue) - { - if(paymentToken.StartsWith("tok_")) - { - paymentMethodType = PaymentMethodType.Card; - } - else if(paymentToken.StartsWith("btok_")) - { - paymentMethodType = PaymentMethodType.BankAccount; - } - else - { - paymentMethodType = PaymentMethodType.PayPal; - } - } - var updated = await _paymentService.UpdatePaymentMethodAsync(organization, - paymentMethodType.Value, paymentToken); + paymentMethodType, paymentToken); if(updated) { await ReplaceAndUpdateCache(organization); @@ -550,22 +534,6 @@ namespace Bit.Core.Services } else { - if(!signup.PaymentMethodType.HasValue && !string.IsNullOrWhiteSpace(signup.PaymentToken)) - { - if(signup.PaymentToken.StartsWith("btok_")) - { - signup.PaymentMethodType = PaymentMethodType.BankAccount; - } - else if(signup.PaymentToken.StartsWith("tok_")) - { - signup.PaymentMethodType = PaymentMethodType.Card; - } - else - { - signup.PaymentMethodType = PaymentMethodType.PayPal; - } - } - await _paymentService.PurchaseOrganizationAsync(organization, signup.PaymentMethodType.Value, signup.PaymentToken, plan, signup.AdditionalStorageGb, signup.AdditionalSeats, signup.PremiumAccessAddon); diff --git a/src/Core/Services/Implementations/UserService.cs b/src/Core/Services/Implementations/UserService.cs index 031d2581cf..8cc3bcac25 100644 --- a/src/Core/Services/Implementations/UserService.cs +++ b/src/Core/Services/Implementations/UserService.cs @@ -678,7 +678,7 @@ namespace Bit.Core.Services return true; } - public async Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType? paymentMethodType, + public async Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license) { if(user.Premium) @@ -710,24 +710,7 @@ namespace Bit.Core.Services } else { - if(!paymentMethodType.HasValue) - { - if(paymentToken.StartsWith("tok_")) - { - paymentMethodType = PaymentMethodType.Card; - } - else if(paymentToken.StartsWith("btok_")) - { - paymentMethodType = PaymentMethodType.BankAccount; - } - else - { - paymentMethodType = PaymentMethodType.PayPal; - } - } - - await _paymentService.PurchasePremiumAsync(user, paymentMethodType.Value, - paymentToken, additionalStorageGb); + await _paymentService.PurchasePremiumAsync(user, paymentMethodType, paymentToken, additionalStorageGb); } user.Premium = true; @@ -802,31 +785,14 @@ namespace Bit.Core.Services await SaveUserAsync(user); } - public async Task ReplacePaymentMethodAsync(User user, string paymentToken, - PaymentMethodType? paymentMethodType) + public async Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType) { if(paymentToken.StartsWith("btok_")) { throw new BadRequestException("Invalid token."); } - if(!paymentMethodType.HasValue) - { - if(paymentToken.StartsWith("tok_")) - { - paymentMethodType = PaymentMethodType.Card; - } - else if(paymentToken.StartsWith("btok_")) - { - paymentMethodType = PaymentMethodType.BankAccount; - } - else - { - paymentMethodType = PaymentMethodType.PayPal; - } - } - - var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType.Value, paymentToken); + var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken); if(updated) { await SaveUserAsync(user);