示例#1
0
        public async Task <SubscriptionItem> UpdateSubscriptionItemMetaData(string subscriptionItemId, Dictionary <string, string> metadata)
        {
            var service  = new SubscriptionItemService();
            var response = await service.UpdateAsync(subscriptionItemId, new SubscriptionItemUpdateOptions
            {
                Metadata = metadata
            });

            return(response);
        }
示例#2
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);
        }