public async Task ReplacePaymentMethodAsync(User user, string paymentToken) { if (paymentToken.StartsWith("btok_")) { throw new BadRequestException("Invalid token."); } IPaymentService paymentService = null; if (paymentToken.StartsWith("tok_")) { paymentService = new StripePaymentService(); } else { paymentService = new BraintreePaymentService(_globalSettings); } var updated = await paymentService.UpdatePaymentMethodAsync(user, paymentToken); if (updated) { await SaveUserAsync(user); } }
public async Task SignUpPremiumAsync(User user, string paymentToken, short additionalStorageGb, UserLicense license) { if (user.Premium) { throw new BadRequestException("Already a premium user."); } IPaymentService paymentService = null; if (_globalSettings.SelfHosted) { if (license == null || !_licenseService.VerifyLicense(license)) { throw new BadRequestException("Invalid license."); } if (!license.CanUse(user)) { throw new BadRequestException("This license is not valid for this user."); } var dir = $"{_globalSettings.LicenseDirectory}/user"; Directory.CreateDirectory(dir); File.WriteAllText($"{dir}/{user.Id}.json", JsonConvert.SerializeObject(license, Formatting.Indented)); } else if (!string.IsNullOrWhiteSpace(paymentToken)) { if (paymentToken.StartsWith("btok_")) { throw new BadRequestException("Invalid token."); } if (paymentToken.StartsWith("tok_")) { paymentService = new StripePaymentService(); } else { paymentService = new BraintreePaymentService(_globalSettings); } await paymentService.PurchasePremiumAsync(user, paymentToken, additionalStorageGb); } else { throw new InvalidOperationException("License or payment token is required."); } user.Premium = true; user.RevisionDate = DateTime.UtcNow; if (_globalSettings.SelfHosted) { user.MaxStorageGb = 10240; // 10 TB user.LicenseKey = license.LicenseKey; user.PremiumExpirationDate = license.Expires; } else { user.MaxStorageGb = (short)(1 + additionalStorageGb); user.LicenseKey = CoreHelpers.SecureRandomString(20); } try { await SaveUserAsync(user); } catch when(!_globalSettings.SelfHosted) { await paymentService.CancelAndRecoverChargesAsync(user); throw; } }