1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-29 15:24:51 -05:00

Consistency on TaxInfo use in service params

This commit is contained in:
Chad Scharf 2020-06-18 10:41:55 -04:00
parent 1b027cab59
commit 0f28ac45f9
5 changed files with 46 additions and 35 deletions

View File

@ -467,7 +467,8 @@ namespace Bit.Api.Controllers
{ {
license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License); license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
} }
else if (!valid && !_globalSettings.SelfHosted)
if (!valid && !_globalSettings.SelfHosted && string.IsNullOrWhiteSpace(model.Country))
{ {
throw new BadRequestException("Country is required."); throw new BadRequestException("Country is required.");
} }
@ -479,7 +480,11 @@ namespace Bit.Api.Controllers
var result = await _userService.SignUpPremiumAsync(user, model.PaymentToken, var result = await _userService.SignUpPremiumAsync(user, model.PaymentToken,
model.PaymentMethodType.Value, model.AdditionalStorageGb.GetValueOrDefault(0), license, model.PaymentMethodType.Value, model.AdditionalStorageGb.GetValueOrDefault(0), license,
model.Country, model.PostalCode); new TaxInfo
{
BillingAddressCountry = model.Country,
BillingAddressPostalCode = model.PostalCode,
});
var profile = new ProfileResponseModel(user, null, await _userService.TwoFactorIsEnabledAsync(user)); var profile = new ProfileResponseModel(user, null, await _userService.TwoFactorIsEnabledAsync(user));
return new PaymentResponseModel return new PaymentResponseModel
{ {
@ -539,12 +544,12 @@ namespace Bit.Api.Controllers
throw new UnauthorizedAccessException(); throw new UnauthorizedAccessException();
} }
await _paymentService.SaveTaxInfoAsync(user, new TaxInfo await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType.Value,
{ new TaxInfo
BillingAddressCountry = model.Country, {
BillingAddressPostalCode = model.PostalCode, BillingAddressCountry = model.Country,
}); BillingAddressPostalCode = model.PostalCode,
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType.Value); });
} }
[HttpPost("storage")] [HttpPost("storage")]

View File

@ -14,13 +14,13 @@ namespace Bit.Core.Services
Task<string> UpgradeFreeOrganizationAsync(Organization org, Models.StaticStore.Plan plan, Task<string> UpgradeFreeOrganizationAsync(Organization org, Models.StaticStore.Plan plan,
short additionalStorageGb, short additionalSeats, bool premiumAccessAddon); short additionalStorageGb, short additionalSeats, bool premiumAccessAddon);
Task<string> PurchasePremiumAsync(User user, PaymentMethodType paymentMethodType, string paymentToken, Task<string> PurchasePremiumAsync(User user, PaymentMethodType paymentMethodType, string paymentToken,
short additionalStorageGb, string country, string postalCode); short additionalStorageGb, TaxInfo taxInfo);
Task<string> AdjustStorageAsync(IStorableSubscriber storableSubscriber, int additionalStorage, string storagePlanId); Task<string> AdjustStorageAsync(IStorableSubscriber storableSubscriber, int additionalStorage, string storagePlanId);
Task CancelSubscriptionAsync(ISubscriber subscriber, bool endOfPeriod = false, Task CancelSubscriptionAsync(ISubscriber subscriber, bool endOfPeriod = false,
bool skipInAppPurchaseCheck = false); bool skipInAppPurchaseCheck = false);
Task ReinstateSubscriptionAsync(ISubscriber subscriber); Task ReinstateSubscriptionAsync(ISubscriber subscriber);
Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, PaymentMethodType paymentMethodType, Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, PaymentMethodType paymentMethodType,
string paymentToken, bool allowInAppPurchases = false); string paymentToken, bool allowInAppPurchases = false, TaxInfo taxInfo = null);
Task<bool> CreditAccountAsync(ISubscriber subscriber, decimal creditAmount); Task<bool> CreditAccountAsync(ISubscriber subscriber, decimal creditAmount);
Task<BillingInfo> GetBillingAsync(ISubscriber subscriber); Task<BillingInfo> GetBillingAsync(ISubscriber subscriber);
Task<SubscriptionInfo> GetSubscriptionAsync(ISubscriber subscriber); Task<SubscriptionInfo> GetSubscriptionAsync(ISubscriber subscriber);

View File

@ -47,11 +47,11 @@ namespace Bit.Core.Services
Task SendDeleteConfirmationAsync(string email); Task SendDeleteConfirmationAsync(string email);
Task<Tuple<bool, string>> SignUpPremiumAsync(User user, string paymentToken, Task<Tuple<bool, string>> SignUpPremiumAsync(User user, string paymentToken,
PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license, PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license,
string country, string postalCode); TaxInfo taxInfo);
Task IapCheckAsync(User user, PaymentMethodType paymentMethodType); Task IapCheckAsync(User user, PaymentMethodType paymentMethodType);
Task UpdateLicenseAsync(User user, UserLicense license); Task UpdateLicenseAsync(User user, UserLicense license);
Task<string> AdjustStorageAsync(User user, short storageAdjustmentGb); Task<string> AdjustStorageAsync(User user, short storageAdjustmentGb);
Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType); Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType, TaxInfo taxInfo);
Task CancelPremiumAsync(User user, bool? endOfPeriod = null, bool accountDelete = false); Task CancelPremiumAsync(User user, bool? endOfPeriod = null, bool accountDelete = false);
Task ReinstatePremiumAsync(User user); Task ReinstatePremiumAsync(User user);
Task EnablePremiumAsync(Guid userId, DateTime? expirationDate); Task EnablePremiumAsync(Guid userId, DateTime? expirationDate);

View File

@ -345,7 +345,7 @@ namespace Bit.Core.Services
} }
public async Task<string> PurchasePremiumAsync(User user, PaymentMethodType paymentMethodType, public async Task<string> PurchasePremiumAsync(User user, PaymentMethodType paymentMethodType,
string paymentToken, short additionalStorageGb, string country, string postalCode) string paymentToken, short additionalStorageGb, TaxInfo taxInfo)
{ {
if (paymentMethodType != PaymentMethodType.Credit && string.IsNullOrWhiteSpace(paymentToken)) if (paymentMethodType != PaymentMethodType.Credit && string.IsNullOrWhiteSpace(paymentToken))
{ {
@ -393,7 +393,7 @@ namespace Bit.Core.Services
{ {
try try
{ {
await UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken, true); await UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken, true, taxInfo);
} }
catch (Exception e) catch (Exception e)
{ {
@ -467,24 +467,12 @@ namespace Bit.Core.Services
Address = new AddressOptions Address = new AddressOptions
{ {
Line1 = string.Empty, Line1 = string.Empty,
Country = country, Country = taxInfo.BillingAddressCountry,
PostalCode = postalCode, PostalCode = taxInfo.BillingAddressPostalCode,
}, },
}); });
createdStripeCustomer = true; createdStripeCustomer = true;
} }
else if (customer != null)
{
await customerService.UpdateAsync(customer.Id, new CustomerUpdateOptions
{
Address = new AddressOptions
{
Line1 = string.Empty,
Country = country,
PostalCode = postalCode,
}
});
}
if (customer == null) if (customer == null)
{ {
@ -1116,7 +1104,7 @@ namespace Bit.Core.Services
} }
public async Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, PaymentMethodType paymentMethodType, public async Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, PaymentMethodType paymentMethodType,
string paymentToken, bool allowInAppPurchases = false) string paymentToken, bool allowInAppPurchases = false, TaxInfo taxInfo = null)
{ {
if (subscriber == null) if (subscriber == null)
{ {
@ -1304,7 +1292,16 @@ namespace Bit.Core.Services
InvoiceSettings = new CustomerInvoiceSettingsOptions InvoiceSettings = new CustomerInvoiceSettingsOptions
{ {
DefaultPaymentMethod = stipeCustomerPaymentMethodId DefaultPaymentMethod = stipeCustomerPaymentMethodId
} },
Address = taxInfo == null ? null : new AddressOptions
{
Country = taxInfo.BillingAddressCountry,
PostalCode = taxInfo.BillingAddressPostalCode,
Line1 = taxInfo.BillingAddressLine1 ?? string.Empty,
Line2 = taxInfo.BillingAddressLine2,
City = taxInfo.BillingAddressCity,
State = taxInfo.BillingAddressState,
},
}); });
subscriber.Gateway = GatewayType.Stripe; subscriber.Gateway = GatewayType.Stripe;
@ -1363,7 +1360,16 @@ namespace Bit.Core.Services
InvoiceSettings = new CustomerInvoiceSettingsOptions InvoiceSettings = new CustomerInvoiceSettingsOptions
{ {
DefaultPaymentMethod = defaultPaymentMethodId DefaultPaymentMethod = defaultPaymentMethodId
} },
Address = taxInfo == null ? null : new AddressOptions
{
Country = taxInfo.BillingAddressCountry,
PostalCode = taxInfo.BillingAddressPostalCode,
Line1 = taxInfo.BillingAddressLine1 ?? string.Empty,
Line2 = taxInfo.BillingAddressLine2,
City = taxInfo.BillingAddressCity,
State = taxInfo.BillingAddressState,
},
}); });
} }
} }

View File

@ -704,7 +704,7 @@ namespace Bit.Core.Services
public async Task<Tuple<bool, string>> SignUpPremiumAsync(User user, string paymentToken, public async Task<Tuple<bool, string>> SignUpPremiumAsync(User user, string paymentToken,
PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license, PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license,
string country, string postalCode) TaxInfo taxInfo)
{ {
if (user.Premium) if (user.Premium)
{ {
@ -743,7 +743,7 @@ namespace Bit.Core.Services
else else
{ {
paymentIntentClientSecret = await _paymentService.PurchasePremiumAsync(user, paymentMethodType, paymentIntentClientSecret = await _paymentService.PurchasePremiumAsync(user, paymentMethodType,
paymentToken, additionalStorageGb, country, postalCode); paymentToken, additionalStorageGb, taxInfo);
} }
user.Premium = true; user.Premium = true;
@ -845,14 +845,14 @@ namespace Bit.Core.Services
return secret; return secret;
} }
public async Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType) public async Task ReplacePaymentMethodAsync(User user, string paymentToken, PaymentMethodType paymentMethodType, TaxInfo taxInfo)
{ {
if (paymentToken.StartsWith("btok_")) if (paymentToken.StartsWith("btok_"))
{ {
throw new BadRequestException("Invalid token."); throw new BadRequestException("Invalid token.");
} }
var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken); var updated = await _paymentService.UpdatePaymentMethodAsync(user, paymentMethodType, paymentToken, taxInfo: taxInfo);
if (updated) if (updated)
{ {
await SaveUserAsync(user); await SaveUserAsync(user);