public async Task <FulfillmentRequestResult> ActivateSubscriptionAsync(Guid subscriptionId,
                                                                               ActivatedSubscription subscriptionDetails, Guid requestId, Guid correlationId,
                                                                               CancellationToken cancellationToken)
        {
            var requestUrl = FluentUriBuilder
                             .Start(this.baseUri)
                             .AddPath("subscriptions")
                             .AddPath(subscriptionId.ToString())
                             .AddPath("activate")
                             .AddQuery(DefaultApiVersionParameterName, this.apiVersion)
                             .Uri;

            requestId     = requestId == default ? Guid.NewGuid() : requestId;
            correlationId = correlationId == default ? Guid.NewGuid() : correlationId;

            var response = await this.SendRequestAndReturnResult(
                HttpMethod.Post,
                requestUrl,
                requestId,
                correlationId,
                null,
                JsonConvert.SerializeObject(subscriptionDetails),
                cancellationToken);

            return(await FulfillmentRequestResult.ParseAsync <FulfillmentRequestResult>(response));
        }
Esempio n. 2
0
        public async Task CanUpdateSubscription()
        {
            var correlationId = Guid.NewGuid();
            var requestId     = Guid.NewGuid();

            var subscriptions = await this.client.GetSubscriptionsAsync(
                requestId,
                correlationId,
                string.Empty,
                new CancellationTokenSource().Token);

            Assert.Equal(requestId, subscriptions.First().RequestId);

            requestId = Guid.NewGuid();

            var update = new ActivatedSubscription {
                PlanId = "Gold", Quantity = ""
            };

            var result = await this.client.UpdateSubscriptionAsync(
                subscriptions.First().SubscriptionId,
                update,
                requestId,
                correlationId,
                string.Empty,
                new CancellationTokenSource().Token);

            Assert.NotNull(result.Operation);
            Assert.NotNull(result);
            Assert.NotEqual(0, result.RetryAfter);
        }
        public async Task <MarketplaceSubscription> ActivateSubscriptionAsync(
            Guid subscriptionId,
            string planId,
            int?quantity,
            CancellationToken cancellationToken = default)
        {
            var requestId                 = Guid.NewGuid();
            var correlationId             = Guid.NewGuid();
            var subscriptionToBeActivated = new ActivatedSubscription {
                PlanId = planId
            };

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

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

            if (result.Success)
            {
                this.logger.LogInformation(
                    $"Activated subscription {subscriptionId} for plan {planId} with quantitiy {quantity}");
                var returnValue = new MarketplaceSubscription
                {
                    PlanId         = planId,
                    State          = StatusEnum.Subscribed,
                    SubscriptionId = subscriptionId
                };

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

                return(returnValue);
            }

            this.logger.LogError(
                $"Cannot activate subscription {subscriptionId} for plan {planId} with quantitiy {quantity}");

            return(default);
        public async Task <UpdateOrDeleteSubscriptionRequestResult> UpdateSubscriptionAsync(Guid subscriptionId,
                                                                                            ActivatedSubscription update, Guid requestId, Guid correlationId, CancellationToken cancellationToken)
        {
            var requestUrl = FluentUriBuilder
                             .Start(this.baseUri)
                             .AddPath("subscriptions")
                             .AddPath(subscriptionId.ToString())
                             .AddQuery(DefaultApiVersionParameterName, this.apiVersion)
                             .Uri;

            requestId     = requestId == default ? Guid.NewGuid() : requestId;
            correlationId = correlationId == default ? Guid.NewGuid() : correlationId;
            var updateContent = JsonConvert.SerializeObject(update);

            if (!string.IsNullOrEmpty(update.PlanId) && !string.IsNullOrEmpty(update.Quantity))
            {
                throw new ApplicationException("Plan Id and quantity cannot be patched at the same time.");
            }

            var response = await this.SendRequestAndReturnResult(
                new HttpMethod("PATCH"),
                requestUrl,
                requestId,
                correlationId,
                null,
                updateContent,
                cancellationToken);

            return(await FulfillmentRequestResult.ParseAsync <UpdateOrDeleteSubscriptionRequestResult>(response));
        }