示例#1
0
        public async Task <Tuple <bool, string> > SignUpPremiumAsync(User user, string paymentToken,
                                                                     PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license,
                                                                     TaxInfo taxInfo)
        {
            if (user.Premium)
            {
                throw new BadRequestException("Already a premium user.");
            }

            if (additionalStorageGb < 0)
            {
                throw new BadRequestException("You can't subtract storage!");
            }

            if ((paymentMethodType == PaymentMethodType.GoogleInApp ||
                 paymentMethodType == PaymentMethodType.AppleInApp) && additionalStorageGb > 0)
            {
                throw new BadRequestException("You cannot add storage with this payment method.");
            }

            string          paymentIntentClientSecret = null;
            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
            {
                paymentIntentClientSecret = await _paymentService.PurchasePremiumAsync(user, paymentMethodType,
                                                                                       paymentToken, additionalStorageGb, taxInfo);
            }

            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);

                await _pushService.PushSyncVaultAsync(user.Id);

                await _referenceEventService.RaiseEventAsync(
                    new ReferenceEvent(ReferenceEventType.UpgradePlan, user)
                {
                    Storage  = user.MaxStorageGb,
                    PlanName = PremiumPlanId,
                });
            }
            catch when(!_globalSettings.SelfHosted)
            {
                await paymentService.CancelAndRecoverChargesAsync(user);

                throw;
            }
            return(new Tuple <bool, string>(string.IsNullOrWhiteSpace(paymentIntentClientSecret),
                                            paymentIntentClientSecret));
        }
示例#2
0
        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;
            }
        }
示例#3
0
        public async Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType paymentMethodType,
                                             short additionalStorageGb, UserLicense license)
        {
            if (user.Premium)
            {
                throw new BadRequestException("Already a premium user.");
            }

            if (additionalStorageGb < 0)
            {
                throw new BadRequestException("You can't subtract storage!");
            }

            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
            {
                await _paymentService.PurchasePremiumAsync(user, paymentMethodType, paymentToken, additionalStorageGb);
            }

            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);

                await _pushService.PushSyncVaultAsync(user.Id);
            }
            catch when(!_globalSettings.SelfHosted)
            {
                await paymentService.CancelAndRecoverChargesAsync(user);

                throw;
            }
        }