/// <summary>
        /// Manage Subscription Usage.
        /// </summary>
        /// <param name="subscriptionUsageRequest">The subscription usage request.</param>
        /// <returns>
        /// Subscription Usage.
        /// </returns>
        public async Task <MeteringUsageResult> EmitUsageEventAsync(MeteringUsageRequest subscriptionUsageRequest)
        {
            this.Logger?.Info($"Inside ManageSubscriptionUsageAsync() of FulfillmentApiClient, trying to Manage Subscription Usage :: {subscriptionUsageRequest.ResourceId}");

            var usage = new UsageEvent()
            {
                ResourceId         = subscriptionUsageRequest.ResourceId,
                PlanId             = subscriptionUsageRequest.PlanId,
                Dimension          = subscriptionUsageRequest.Dimension,
                Quantity           = subscriptionUsageRequest.Quantity,
                EffectiveStartTime = subscriptionUsageRequest.EffectiveStartTime,
            };

            try
            {
                var updateResult = (await this.meteringClient.Metering.PostUsageEventAsync(usage)).Value;
                return(new MeteringUsageResult()
                {
                    Dimension = updateResult.Dimension,
                    MessageTime = updateResult.MessageTime.Value.UtcDateTime,
                    PlanId = updateResult.PlanId,
                    Quantity = (long)updateResult.Quantity,
                    ResourceId = updateResult.ResourceId.Value,
                    Status = updateResult.Status.ToString(),
                    UsagePostedDate = updateResult.EffectiveStartTime.Value.UtcDateTime,
                    UsageEventId = updateResult.UsageEventId.Value
                });
            }
            catch (Exception ex)
            {
                this.ProcessErrorResponse(MarketplaceActionEnum.SUBSCRIPTION_USAGEEVENT, ex);
                return(null);
            }
        }
        public IActionResult ManageSubscriptionUsage(SubscriptionUsageViewModel subscriptionData)
        {
            this.logger.LogInformation("Home Controller / ManageSubscriptionUsage  subscriptionData: {0}", JsonConvert.SerializeObject(subscriptionData));
            try
            {
                if (Convert.ToBoolean(applicationConfigRepository.GetValuefromApplicationConfig(MainMenuStatusEnum.IsLicenseManagementEnabled.ToString())) == true)
                {
                    this.TempData["ShowLicensesMenu"] = true;
                }
                if (subscriptionData != null && subscriptionData.SubscriptionDetail != null)
                {
                    var currentUserDetail        = usersRepository.GetPartnerDetailFromEmail(this.CurrentUserEmailAddress);
                    var subscriptionUsageRequest = new MeteringUsageRequest()
                    {
                        Dimension          = subscriptionData.SelectedDimension,
                        EffectiveStartTime = DateTime.UtcNow,
                        PlanId             = subscriptionData.SubscriptionDetail.AmpplanId,
                        Quantity           = Convert.ToDouble(subscriptionData.Quantity ?? "0"),
                        ResourceId         = subscriptionData.SubscriptionDetail.AmpsubscriptionId
                    };
                    var meteringUsageResult = new MeteringUsageResult();
                    var requestJson         = JsonConvert.SerializeObject(subscriptionUsageRequest);
                    var responseJson        = string.Empty;
                    try
                    {
                        this.logger.LogInformation("EmitUsageEventAsync");
                        meteringUsageResult = apiClient.EmitUsageEventAsync(subscriptionUsageRequest).ConfigureAwait(false).GetAwaiter().GetResult();
                        responseJson        = JsonConvert.SerializeObject(meteringUsageResult);
                        this.logger.LogInformation(responseJson);
                    }
                    catch (MeteredBillingException mex)
                    {
                        responseJson = JsonConvert.SerializeObject(mex.MeteredBillingErrorDetail);
                        meteringUsageResult.Status = mex.ErrorCode;
                        this.logger.LogInformation(responseJson);
                    }

                    var newMeteredAuditLog = new MeteredAuditLogs()
                    {
                        RequestJson           = requestJson,
                        ResponseJson          = responseJson,
                        StatusCode            = meteringUsageResult.Status,
                        SubscriptionId        = subscriptionData.SubscriptionDetail.Id,
                        SubscriptionUsageDate = DateTime.UtcNow,
                        CreatedBy             = currentUserDetail == null ? 0 : currentUserDetail.UserId,
                        CreatedDate           = DateTime.Now
                    };
                    subscriptionUsageLogsRepository.Add(newMeteredAuditLog);
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, ex.Message);
            }
            return(RedirectToAction(nameof(RecordUsage), new { subscriptionId = subscriptionData.SubscriptionDetail.Id }));
        }
        public IActionResult ManageSubscriptionUsage(SubscriptionUsageViewModel subscriptionData)
        {
            this.logger.LogInformation("Home Controller / ManageSubscriptionUsage  subscriptionData: {0}", JsonSerializer.Serialize(subscriptionData));
            try
            {
                if (subscriptionData != null && subscriptionData.SubscriptionDetail != null)
                {
                    var currentUserDetail        = this.userRepository.GetPartnerDetailFromEmail(this.CurrentUserEmailAddress);
                    var subscriptionUsageRequest = new MeteringUsageRequest()
                    {
                        Dimension          = subscriptionData.SelectedDimension,
                        EffectiveStartTime = DateTime.UtcNow,
                        PlanId             = subscriptionData.SubscriptionDetail.AmpplanId,
                        Quantity           = Convert.ToDouble(subscriptionData.Quantity ?? "0"),
                        ResourceId         = subscriptionData.SubscriptionDetail.AmpsubscriptionId,
                    };
                    var meteringUsageResult = new MeteringUsageResult();
                    var requestJson         = JsonSerializer.Serialize(subscriptionUsageRequest);
                    var responseJson        = string.Empty;
                    try
                    {
                        this.logger.LogInformation("EmitUsageEventAsync");
                        meteringUsageResult = this.apiClient.EmitUsageEventAsync(subscriptionUsageRequest).ConfigureAwait(false).GetAwaiter().GetResult();
                        responseJson        = JsonSerializer.Serialize(meteringUsageResult);
                        this.logger.LogInformation(responseJson);
                    }
                    catch (MeteredBillingException mex)
                    {
                        responseJson = JsonSerializer.Serialize(mex.MeteredBillingErrorDetail);
                        meteringUsageResult.Status = mex.ErrorCode;
                        this.logger.LogInformation(responseJson);
                    }

                    var newMeteredAuditLog = new MeteredAuditLogs()
                    {
                        RequestJson           = requestJson,
                        ResponseJson          = responseJson,
                        StatusCode            = meteringUsageResult.Status,
                        SubscriptionId        = subscriptionData.SubscriptionDetail.Id,
                        SubscriptionUsageDate = DateTime.UtcNow,
                        CreatedBy             = currentUserDetail == null ? 0 : currentUserDetail.UserId,
                        CreatedDate           = DateTime.Now,
                    };
                    this.subscriptionUsageLogsRepository.Save(newMeteredAuditLog);
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, ex.Message);
            }

            return(this.RedirectToAction(nameof(this.RecordUsage), new { subscriptionId = subscriptionData.SubscriptionDetail.Id }));
        }
        public async Task TestSubscriptionUsage()
        {
            var allSubscriptions = await this.fulfillmentClient.GetAllSubscriptionAsync().ConfigureAwait(false);

            var defaultSubscription = allSubscriptions.FirstOrDefault();

            MeteringUsageRequest subscriptionUsageRequest = new MeteringUsageRequest()
            {
                Dimension          = "Test",
                EffectiveStartTime = DateTime.UtcNow,
                PlanId             = defaultSubscription?.PlanId,
                Quantity           = 5,
                ResourceId         = defaultSubscription.Id
            };
            var response = this.client.EmitUsageEventAsync(subscriptionUsageRequest).Result;

            Assert.AreEqual(response.Status, "Accepted");
            Assert.AreEqual(response.ResourceId, defaultSubscription?.Id);
            Assert.AreEqual(response.PlanId, defaultSubscription?.PlanId);
        }
Пример #5
0
        /// <summary>
        /// Manage Subscription Usage.
        /// </summary>
        /// <param name="subscriptionUsageRequest">The subscription usage request.</param>
        /// <returns>
        /// Subscription Usage.
        /// </returns>
        public async Task <MeteringUsageResult> EmitUsageEventAsync(MeteringUsageRequest subscriptionUsageRequest)
        {
            this.Logger?.Info($"Inside ManageSubscriptionUsageAsync() of FulfillmentApiClient, trying to Manage Subscription Usage :: {subscriptionUsageRequest.ResourceId}");

            var restClient = new MeteringApiRestClient <MeteringUsageResult>(this.ClientConfiguration, this.Logger);

            var url = UrlHelper.GetSaaSApiUrl(this.ClientConfiguration, subscriptionUsageRequest.ResourceId, SaaSResourceActionEnum.SUBSCRIPTION_USAGEEVENT);

            Dictionary <string, object> usageEventRequest = new Dictionary <string, object>();

            usageEventRequest.Add("resourceId", Convert.ToString(subscriptionUsageRequest.ResourceId));
            usageEventRequest.Add("quantity", subscriptionUsageRequest.Quantity);
            usageEventRequest.Add("dimension", subscriptionUsageRequest.Dimension);
            usageEventRequest.Add("effectiveStartTime", subscriptionUsageRequest.EffectiveStartTime);
            usageEventRequest.Add("planId", subscriptionUsageRequest.PlanId);

            var meteringUsageResult = await restClient.DoRequest(url, HttpMethods.POST, usageEventRequest).ConfigureAwait(false);

            return(meteringUsageResult);
        }