public async Task <Subscription> ActivateSubscriptionAsync(Guid subscriptionId, string activatedBy = "system")
        {
            Subscription subscription = await _context.Subscriptions.FindAsync(subscriptionId);

            ValidateSubscriptionAndInputState(subscription);

            try
            {
                Offer offer = await FindOfferById(subscription.OfferId);

                if (subscription.ProvisioningStatus.Equals(ProvisioningState.NotificationPending.ToString(), StringComparison.InvariantCultureIgnoreCase) &&
                    offer.ManualActivation)
                {
                    _logger.LogInformation($"ManualActivation of offer {offer.OfferName} is set to true. Will not activate the subscription.");

                    return(await TransitToNextState(subscription, ProvisioningState.ManualActivationPending));
                }

                Plan plan = await FindPlanById(subscription.PlanId);

                ActivatedSubscriptionResult activatedResult = new ActivatedSubscriptionResult
                {
                    PlanId   = plan.PlanName,
                    Quantity = subscription.Quantity.ToString()
                };

                _logger.LogInformation(
                    LoggingUtils.ComposeHttpClientLogMessage(
                        _fulfillmentClient.GetType().Name,
                        nameof(_fulfillmentClient.ActivateSubscriptionAsync),
                        subscriptionId));

                var result = await _fulfillmentClient.ActivateSubscriptionAsync(
                    subscriptionId,
                    activatedResult,
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    default);

                _logger.LogInformation(
                    LoggingUtils.ComposeSubscriptionActionMessage(
                        "Activated",
                        subscriptionId,
                        activatedResult.PlanId,
                        activatedResult.Quantity,
                        activatedBy));

                await _subscriptionCustomMeterUsageService.EnableSubscriptionCustomMeterUsageBySubscriptionId(subscriptionId);

                subscription.Status        = nameof(FulfillmentState.Subscribed);
                subscription.ActivatedTime = DateTime.UtcNow;
                subscription.ActivatedBy   = activatedBy;
                return(await TransitToNextState(subscription, ProvisioningState.Succeeded));
            }
            catch (Exception e)
            {
                return(await HandleExceptions(subscription, e));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Activate a subscription
        /// </summary>
        /// <param name="subscriptionId">The subscription id</param>
        /// <param name="planId">The plan id</param>
        /// <param name="quantity">The quantity</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <MarketplaceSubscription> ActivateSubscriptionAsync(
            Guid subscriptionId,
            string planId,
            int?quantity,
            CancellationToken cancellationToken = default)
        {
            Guid requestId                 = Guid.NewGuid();
            Guid correlationId             = Guid.NewGuid();
            var  subscriptionToBeActivated = new ActivatedSubscriptionResult {
                PlanId = planId
            };

            if (quantity.HasValue)
            {
                subscriptionToBeActivated.Quantity = quantity.Value.ToString();
            }

            try
            {
                _logger.LogInformation(
                    LoggingUtils.ComposeHttpClientLogMessage(
                        _fulfillmentClient.GetType().Name,
                        nameof(_fulfillmentClient.ActivateSubscriptionAsync),
                        subscriptionId));

                var result = await _fulfillmentClient.ActivateSubscriptionAsync(
                    subscriptionId,
                    subscriptionToBeActivated,
                    requestId,
                    correlationId,
                    cancellationToken);

                _logger.LogInformation(
                    LoggingUtils.ComposeSubscriptionActionMessage(
                        "Activated",
                        subscriptionId,
                        planId,
                        quantity.ToString()));

                var returnValue = new MarketplaceSubscription
                {
                    PlanId         = planId,
                    State          = StatusEnum.Subscribed,
                    SubscriptionId = subscriptionId
                };

                if (quantity.HasValue)
                {
                    returnValue.Quantity = quantity.Value;
                }

                return(returnValue);
            }
            catch (Exception e)
            {
                var errorMessage = LoggingUtils.ComposeSubscriptionActionMessage(
                    "Failed to activate",
                    subscriptionId,
                    planId,
                    quantity.ToString());
                throw new LunaFulfillmentException(errorMessage, e);
            }
        }