Exemple #1
0
        public async Task AdjustStorageAsync(IStorableSubscriber storableSubscriber, int additionalStorage, string storagePlanId)
        {
            var sub = await _gateway.Subscription.FindAsync(storableSubscriber.GatewaySubscriptionId);

            if (sub == null)
            {
                throw new GatewayException("Subscription was not found.");
            }

            var req = new SubscriptionRequest
            {
                AddOns  = new AddOnsRequest(),
                Options = new SubscriptionOptionsRequest
                {
                    ProrateCharges = true,
                    RevertSubscriptionOnProrationFailure = true
                }
            };

            var storageItem = sub.AddOns?.FirstOrDefault(a => a.Id == storagePlanId);

            if (additionalStorage > 0 && storageItem == null)
            {
                req.AddOns.Add = new AddAddOnRequest[]
                {
                    new AddAddOnRequest
                    {
                        InheritedFromId = storagePlanId,
                        Quantity        = additionalStorage,
                        NeverExpires    = true
                    }
                };
            }
            else if (additionalStorage > 0 && storageItem != null)
            {
                req.AddOns.Update = new UpdateAddOnRequest[]
                {
                    new UpdateAddOnRequest
                    {
                        ExistingId   = storageItem.Id,
                        Quantity     = additionalStorage,
                        NeverExpires = true
                    }
                };
            }
            else if (additionalStorage == 0 && storageItem != null)
            {
                req.AddOns.Remove = new string[] { storageItem.Id };
            }

            var result = await _gateway.Subscription.UpdateAsync(sub.Id, req);

            if (!result.IsSuccess())
            {
                throw new GatewayException("Failed to adjust storage.");
            }
        }
Exemple #2
0
        public async Task AdjustStorageAsync(IStorableSubscriber storableSubscriber, int additionalStorage,
                                             string storagePlanId)
        {
            var subscriptionItemService = new StripeSubscriptionItemService();
            var subscriptionService     = new StripeSubscriptionService();
            var sub = await subscriptionService.GetAsync(storableSubscriber.GatewaySubscriptionId);

            if (sub == null)
            {
                throw new GatewayException("Subscription not found.");
            }

            var storageItem = sub.Items?.Data?.FirstOrDefault(i => i.Plan.Id == storagePlanId);

            if (additionalStorage > 0 && storageItem == null)
            {
                await subscriptionItemService.CreateAsync(new StripeSubscriptionItemCreateOptions
                {
                    PlanId         = storagePlanId,
                    Quantity       = additionalStorage,
                    Prorate        = true,
                    SubscriptionId = sub.Id
                });
            }
            else if (additionalStorage > 0 && storageItem != null)
            {
                await subscriptionItemService.UpdateAsync(storageItem.Id, new StripeSubscriptionItemUpdateOptions
                {
                    PlanId   = storagePlanId,
                    Quantity = additionalStorage,
                    Prorate  = true
                });
            }
            else if (additionalStorage == 0 && storageItem != null)
            {
                await subscriptionItemService.DeleteAsync(storageItem.Id);
            }

            if (additionalStorage > 0)
            {
                await PreviewUpcomingInvoiceAndPayAsync(storableSubscriber, storagePlanId, 400);
            }
        }
Exemple #3
0
        internal static async Task <string> AdjustStorageAsync(IPaymentService paymentService, IStorableSubscriber storableSubscriber,
                                                               short storageAdjustmentGb, string storagePlanId)
        {
            if (storableSubscriber == null)
            {
                throw new ArgumentNullException(nameof(storableSubscriber));
            }

            if (string.IsNullOrWhiteSpace(storableSubscriber.GatewayCustomerId))
            {
                throw new BadRequestException("No payment method found.");
            }

            if (string.IsNullOrWhiteSpace(storableSubscriber.GatewaySubscriptionId))
            {
                throw new BadRequestException("No subscription found.");
            }

            if (!storableSubscriber.MaxStorageGb.HasValue)
            {
                throw new BadRequestException("No access to storage.");
            }

            var newStorageGb = (short)(storableSubscriber.MaxStorageGb.Value + storageAdjustmentGb);

            if (newStorageGb < 1)
            {
                newStorageGb = 1;
            }

            if (newStorageGb > 100)
            {
                throw new BadRequestException("Maximum storage is 100 GB.");
            }

            var remainingStorage = storableSubscriber.StorageBytesRemaining(newStorageGb);

            if (remainingStorage < 0)
            {
                throw new BadRequestException("You are currently using " +
                                              $"{CoreHelpers.ReadableBytesSize(storableSubscriber.Storage.GetValueOrDefault(0))} of storage. " +
                                              "Delete some stored data first.");
            }

            var additionalStorage         = newStorageGb - 1;
            var paymentIntentClientSecret = await paymentService.AdjustStorageAsync(storableSubscriber,
                                                                                    additionalStorage, storagePlanId);

            storableSubscriber.MaxStorageGb = newStorageGb;
            return(paymentIntentClientSecret);
        }
Exemple #4
0
        public async Task AdjustStorageAsync(IStorableSubscriber storableSubscriber, int additionalStorage,
                                             string storagePlanId)
        {
            var subscriptionItemService = new SubscriptionItemService();
            var subscriptionService     = new SubscriptionService();
            var sub = await subscriptionService.GetAsync(storableSubscriber.GatewaySubscriptionId);

            if (sub == null)
            {
                throw new GatewayException("Subscription not found.");
            }

            Func <bool, Task <SubscriptionItem> > subUpdateAction = null;
            var storageItem    = sub.Items?.FirstOrDefault(i => i.Plan.Id == storagePlanId);
            var subItemOptions = sub.Items.Where(i => i.Plan.Id != storagePlanId)
                                 .Select(i => new InvoiceSubscriptionItemOptions
            {
                Id       = i.Id,
                PlanId   = i.Plan.Id,
                Quantity = i.Quantity,
            }).ToList();

            if (additionalStorage > 0 && storageItem == null)
            {
                subItemOptions.Add(new InvoiceSubscriptionItemOptions
                {
                    PlanId   = storagePlanId,
                    Quantity = additionalStorage,
                });
                subUpdateAction = (prorate) => subscriptionItemService.CreateAsync(
                    new SubscriptionItemCreateOptions
                {
                    PlanId         = storagePlanId,
                    Quantity       = additionalStorage,
                    SubscriptionId = sub.Id,
                    Prorate        = prorate
                });
            }
            else if (additionalStorage > 0 && storageItem != null)
            {
                subItemOptions.Add(new InvoiceSubscriptionItemOptions
                {
                    Id       = storageItem.Id,
                    PlanId   = storagePlanId,
                    Quantity = additionalStorage,
                });
                subUpdateAction = (prorate) => subscriptionItemService.UpdateAsync(storageItem.Id,
                                                                                   new SubscriptionItemUpdateOptions
                {
                    PlanId   = storagePlanId,
                    Quantity = additionalStorage,
                    Prorate  = prorate
                });
            }
            else if (additionalStorage == 0 && storageItem != null)
            {
                subItemOptions.Add(new InvoiceSubscriptionItemOptions
                {
                    Id      = storageItem.Id,
                    Deleted = true
                });
                subUpdateAction = (prorate) => subscriptionItemService.DeleteAsync(storageItem.Id);
            }

            var invoicedNow = false;

            if (additionalStorage > 0)
            {
                invoicedNow = await PreviewUpcomingInvoiceAndPayAsync(
                    storableSubscriber, storagePlanId, subItemOptions, 400);
            }

            await subUpdateAction(!invoicedNow);
        }