private async Task <IHttpActionResult> OwnerProductSuspend(Guid accountId, Guid accountProductId, bool suspend)
        {
            return(await CurrentAccountExecuteAsync(async delegate(Account account)
            {
                var product = await _authProduct.OwnerProductGetAsync(new AccountProductPair(accountId, accountProductId));

                if (object.Equals(product, null) || product.IsDisabled)
                {
                    return ProductNotFound();
                }

                if (!product.IsRenewal || IsLocalSubscription(product.SpId))
                {
                    return ErrorContent(string.Empty, "This product can't be suspend or resumed.");
                }

                DateTime?nextRebillDate = null;
                var subscriptionDetails = await UpclickClient.SubscriptionDetails(product.SpId);

                if (object.Equals(subscriptionDetails, null))
                {
                    return ErrorContent(string.Empty, "Product is not exists in Customer info(Upclick).");
                }

                var subscription = suspend ?
                                   await UpclickClient.SubscriptionSuspend(product.SpId) :
                                   await UpclickClient.SubscriptionResume(product.SpId);

                if (suspend && string.Equals(subscription.Status, "Active"))
                {
                    return ErrorContent(string.Empty, "Product can't be suspend. Product is still active in Customer info(Upclick).");
                }

                if (!suspend && !string.Equals(subscription.Status, "Active"))
                {
                    return ErrorContent(string.Empty, "Product can't be resumed. Product is not active in Customer info(Upclick).");
                }

                if (string.Equals(subscription.Status, "Active"))
                {
                    nextRebillDate = subscription.NextRebillDate;
                }

                var pair = new AccountProductPair(accountId, product.AccountProductId);
                await _authProduct.ProductNextRebillDateSetAsync(pair, nextRebillDate);

                var log = string.Format("Product({0}, {1}) has been {2} successfully.",
                                        product.ProductName, product.AccountProductId, nextRebillDate.HasValue ? "resumed" : "suspended");

                await LogInsertAsync(log, LogActionTypeEnum.AccountProductDeactivate, accountId);

                product = await _authProduct.OwnerProductDetailsGetAsync(pair);

                return Ok(ProductConvertor.OwnerAccountProductConvertor(product));
            }, accountId));
        }
        public async Task <IHttpActionResult> OwnerProductDeactivate(Guid accountId, Guid accountProductId)
        {
            try
            {
                var product = await _authProduct.OwnerProductGetAsync(new AccountProductPair(accountId, accountProductId));

                if (object.Equals(product, null))
                {
                    return(ProductNotFound());
                }

                var ownerAccount = await _auth.AccountGetAsync(accountId);

                if (!object.Equals(ownerAccount, null) && !product.IsFree && !product.IsTrial && !product.IsDisabled)
                {
                    if (!IsLocalSubscription(product.SpId) && !string.IsNullOrEmpty(product.SpId))
                    {
                        var subscription = await UpclickClient.SubscriptionDetails(product.SpId);

                        if (object.Equals(subscription, null) || object.Equals(subscription.Status, null))
                        {
                            return(ErrorContent("Unknown subscription", "Upclick error. Unknown subscription."));
                        }

                        if (string.Equals(subscription.Status.Name, "Active"))
                        {
                            subscription = await UpclickClient.SubscriptionCancel(product.SpId);

                            //cuz Upclick has bug in API
                            Thread.Sleep(TimeSpan.FromSeconds(3));

                            subscription = await UpclickClient.SubscriptionDetails(product.SpId);

                            if (string.Equals(subscription.Status.Name, "Active"))
                            {
                                return(ErrorContent("Subscription is active", "Upclick error. Subscription is active."));
                            }
                        }
                    }

                    await _authProduct.ProductDeactivateAsync(new AccountProductPair(accountId, product.AccountProductId));

                    var log = string.Format("Product({0}, {1}) has been deactivated successfully. Customer's email is {2}.",
                                            product.ProductName, product.AccountProductId, ownerAccount.Email);

                    await LogInsertAsync(log, LogActionTypeEnum.AccountProductDeactivate, ownerAccount.Id);
                }

                return(Ok());
            }
            catch (Exception exc) { return(ErrorContent(exc)); }
        }
        public async Task <IHttpActionResult> OwnerProductNextRebillDate(Guid accountId, Guid accountProductId, [FromUri] DateTime nextRebillDate)
        {
            return(await CurrentAccountExecuteAsync(async delegate(Account account)
            {
                var product = await _authProduct.OwnerProductGetAsync(new AccountProductPair(accountId, accountProductId));

                if (object.Equals(product, null) || product.IsDisabled || !product.IsRenewal)
                {
                    return ProductNotFound();
                }

                if (!product.IsRenewal || IsLocalSubscription(product.SpId))
                {
                    return ErrorContent(string.Empty, "You can't change next rebill date for this product.");
                }

                var subscription = await UpclickClient.SubscriptionDetails(product.SpId);
                if (object.Equals(subscription, null))
                {
                    return ErrorContent(string.Empty, "Product is not exists in Customer info(Upclick).");
                }

                subscription = await UpclickClient.SubscriptionUpdate(product.SpId, nextRebillDate);

                DateTime?newNextRebillDate = null;
                if (string.Equals(subscription.Status.Name, "Active") && !object.Equals(subscription.NextCycleBill, null))
                {
                    newNextRebillDate = subscription.NextCycleBill.Date;
                }

                var pair = new AccountProductPair(accountId, product.AccountProductId);
                if (newNextRebillDate.HasValue)
                {
                    await _authProduct.ProductNextRebillDateSetAsync(pair, newNextRebillDate);

                    var log = string.Format("Next rebill date has been changed successfully. Product({0}, {1}).",
                                            product.ProductName, product.AccountProductId);

                    await LogInsertAsync(log, LogActionTypeEnum.AccountProductNextRebillDateEdit, accountId);
                }

                product = await _authProduct.OwnerProductDetailsGetAsync(pair);

                return Ok(ProductConvertor.OwnerAccountProductConvertor(product));
            }, accountId));
        }