mirror of
https://github.com/bitwarden/server.git
synced 2024-11-22 12:15:36 +01:00
add support for passing payment method type
This commit is contained in:
parent
de85dbc67c
commit
a9336a0e02
@ -463,7 +463,7 @@ namespace Bit.Api.Controllers
|
||||
throw new BadRequestException("Invalid license.");
|
||||
}
|
||||
|
||||
await _userService.SignUpPremiumAsync(user, model.PaymentToken,
|
||||
await _userService.SignUpPremiumAsync(user, model.PaymentToken, model.PaymentMethodType,
|
||||
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);
|
||||
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType);
|
||||
}
|
||||
|
||||
[HttpPost("storage")]
|
||||
|
@ -209,7 +209,8 @@ namespace Bit.Api.Controllers
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _organizationService.ReplacePaymentMethodAsync(orgIdGuid, model.PaymentToken);
|
||||
await _organizationService.ReplacePaymentMethodAsync(orgIdGuid, model.PaymentToken,
|
||||
model.PaymentMethodType);
|
||||
}
|
||||
|
||||
[HttpPost("{id}/upgrade")]
|
||||
|
@ -5,6 +5,6 @@
|
||||
Card = 0,
|
||||
BankAccount = 1,
|
||||
PayPal = 2,
|
||||
Bitcoin = 3
|
||||
BitPay = 3,
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class PremiumRequestModel : IValidatableObject
|
||||
{
|
||||
// TODO: Required in future
|
||||
public PaymentMethodType? PaymentMethodType { get; set; }
|
||||
public string PaymentToken { get; set; }
|
||||
[Range(0, 99)]
|
||||
public short? AdditionalStorageGb { get; set; }
|
||||
|
@ -21,6 +21,8 @@ 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)]
|
||||
public short AdditionalSeats { get; set; }
|
||||
@ -39,6 +41,7 @@ namespace Bit.Core.Models.Api
|
||||
OwnerKey = Key,
|
||||
Name = Name,
|
||||
Plan = PlanType,
|
||||
PaymentMethodType = PaymentMethodType,
|
||||
PaymentToken = PaymentToken,
|
||||
AdditionalSeats = AdditionalSeats,
|
||||
AdditionalStorageGb = AdditionalStorageGb.GetValueOrDefault(0),
|
||||
|
@ -1,9 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class PaymentRequestModel
|
||||
{
|
||||
// TODO: Required in future
|
||||
public PaymentMethodType? PaymentMethodType { get; set; }
|
||||
[Required]
|
||||
public string PaymentToken { get; set; }
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
|
||||
namespace Bit.Core.Models.Business
|
||||
{
|
||||
@ -9,10 +10,11 @@ namespace Bit.Core.Models.Business
|
||||
public string BillingEmail { get; set; }
|
||||
public User Owner { get; set; }
|
||||
public string OwnerKey { get; set; }
|
||||
public Enums.PlanType Plan { get; set; }
|
||||
public PlanType Plan { get; set; }
|
||||
public short AdditionalSeats { get; set; }
|
||||
public short AdditionalStorageGb { get; set; }
|
||||
public bool PremiumAccessAddon { get; set; }
|
||||
public PaymentMethodType? PaymentMethodType { get; set; }
|
||||
public string PaymentToken { get; set; }
|
||||
public string CollectionName { get; set; }
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace Bit.Core.Services
|
||||
{
|
||||
public interface IOrganizationService
|
||||
{
|
||||
Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken);
|
||||
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);
|
||||
|
@ -43,10 +43,11 @@ namespace Bit.Core.Services
|
||||
Task<IdentityResult> DeleteAsync(User user);
|
||||
Task<IdentityResult> DeleteAsync(User user, string token);
|
||||
Task SendDeleteConfirmationAsync(string email);
|
||||
Task SignUpPremiumAsync(User user, string paymentToken, short additionalStorageGb, UserLicense license);
|
||||
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);
|
||||
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);
|
||||
|
@ -71,7 +71,8 @@ namespace Bit.Core.Services
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
|
||||
public async Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken)
|
||||
public async Task ReplacePaymentMethodAsync(Guid organizationId, string paymentToken,
|
||||
PaymentMethodType? paymentMethodType)
|
||||
{
|
||||
var organization = await GetOrgById(organizationId);
|
||||
if(organization == null)
|
||||
@ -79,22 +80,24 @@ namespace Bit.Core.Services
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
PaymentMethodType paymentMethodType;
|
||||
if(paymentToken.StartsWith("btok_"))
|
||||
if(!paymentMethodType.HasValue)
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.BankAccount;
|
||||
}
|
||||
else if(paymentToken.StartsWith("tok_"))
|
||||
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, paymentToken);
|
||||
paymentMethodType.Value, paymentToken);
|
||||
if(updated)
|
||||
{
|
||||
await ReplaceAndUpdateCache(organization);
|
||||
@ -547,21 +550,23 @@ namespace Bit.Core.Services
|
||||
}
|
||||
else
|
||||
{
|
||||
PaymentMethodType paymentMethodType;
|
||||
if(!signup.PaymentMethodType.HasValue)
|
||||
{
|
||||
if(signup.PaymentToken.StartsWith("btok_"))
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.BankAccount;
|
||||
signup.PaymentMethodType = PaymentMethodType.BankAccount;
|
||||
}
|
||||
else if(signup.PaymentToken.StartsWith("tok_"))
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.Card;
|
||||
signup.PaymentMethodType = PaymentMethodType.Card;
|
||||
}
|
||||
else
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.PayPal;
|
||||
signup.PaymentMethodType = PaymentMethodType.PayPal;
|
||||
}
|
||||
}
|
||||
|
||||
await _paymentService.PurchaseOrganizationAsync(organization, paymentMethodType,
|
||||
await _paymentService.PurchaseOrganizationAsync(organization, signup.PaymentMethodType.Value,
|
||||
signup.PaymentToken, plan, signup.AdditionalStorageGb, signup.AdditionalSeats,
|
||||
signup.PremiumAccessAddon);
|
||||
}
|
||||
|
@ -171,6 +171,11 @@ namespace Bit.Core.Services
|
||||
var stripePaymentMethod = paymentMethodType == PaymentMethodType.Card ||
|
||||
paymentMethodType == PaymentMethodType.BankAccount;
|
||||
|
||||
if(paymentMethodType == PaymentMethodType.BankAccount)
|
||||
{
|
||||
throw new GatewayException("Bank account payment method is not supported at this time.");
|
||||
}
|
||||
|
||||
if(user.Gateway == GatewayType.Stripe && !string.IsNullOrWhiteSpace(user.GatewayCustomerId))
|
||||
{
|
||||
try
|
||||
|
@ -678,7 +678,8 @@ namespace Bit.Core.Services
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task SignUpPremiumAsync(User user, string paymentToken, short additionalStorageGb, UserLicense license)
|
||||
public async Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType? paymentMethodType,
|
||||
short additionalStorageGb, UserLicense license)
|
||||
{
|
||||
if(user.Premium)
|
||||
{
|
||||
@ -709,18 +710,23 @@ namespace Bit.Core.Services
|
||||
}
|
||||
else if(!string.IsNullOrWhiteSpace(paymentToken))
|
||||
{
|
||||
if(paymentToken.StartsWith("btok_"))
|
||||
if(!paymentMethodType.HasValue)
|
||||
{
|
||||
throw new BadRequestException("Invalid token.");
|
||||
if(paymentToken.StartsWith("tok_"))
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.Card;
|
||||
}
|
||||
|
||||
var paymentMethodType = PaymentMethodType.Card;
|
||||
if(!paymentToken.StartsWith("tok_"))
|
||||
else if(paymentToken.StartsWith("btok_"))
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.BankAccount;
|
||||
}
|
||||
else
|
||||
{
|
||||
paymentMethodType = PaymentMethodType.PayPal;
|
||||
}
|
||||
}
|
||||
|
||||
await _paymentService.PurchasePremiumAsync(user, paymentMethodType,
|
||||
await _paymentService.PurchasePremiumAsync(user, paymentMethodType.Value,
|
||||
paymentToken, additionalStorageGb);
|
||||
}
|
||||
else
|
||||
@ -800,24 +806,31 @@ namespace Bit.Core.Services
|
||||
await SaveUserAsync(user);
|
||||
}
|
||||
|
||||
public async Task ReplacePaymentMethodAsync(User user, string paymentToken)
|
||||
public async Task ReplacePaymentMethodAsync(User user, string paymentToken,
|
||||
PaymentMethodType? paymentMethodType)
|
||||
{
|
||||
if(paymentToken.StartsWith("btok_"))
|
||||
{
|
||||
throw new BadRequestException("Invalid token.");
|
||||
}
|
||||
|
||||
PaymentMethodType paymentMethodType;
|
||||
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, paymentToken);
|
||||
var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType.Value, paymentToken);
|
||||
if(updated)
|
||||
{
|
||||
await SaveUserAsync(user);
|
||||
|
Loading…
Reference in New Issue
Block a user