public async Task <TransactionResult> UpdateSubscription([FromBody] OrderViewModel orderUpdateInformation)
        {
            if (!ModelState.IsValid)
            {
                var errorList = (from item in ModelState.Values
                                 from error in item.Errors
                                 select error.ErrorMessage).ToList();
                string errorMessage = JsonConvert.SerializeObject(errorList);
                throw new PartnerDomainException(ErrorCode.InvalidInput).AddDetail("ErrorMessage", errorMessage);
            }

            string clientCustomerId = this.Principal.PartnerCenterCustomerId;

            orderUpdateInformation.Subscriptions.AssertNotNull(nameof(orderUpdateInformation.Subscriptions));
            List <OrderSubscriptionItemViewModel> orderSubscriptions = orderUpdateInformation.Subscriptions.ToList();

            if (!(orderSubscriptions.Count == 1))
            {
                throw new PartnerDomainException(ErrorCode.InvalidInput).AddDetail("ErrorMessage", Resources.MoreThanOneSubscriptionUpdateErrorMessage);
            }

            string subscriptionId  = orderSubscriptions.First().SubscriptionId;
            int    additionalSeats = orderSubscriptions.First().Quantity;

            subscriptionId.AssertNotEmpty(nameof(subscriptionId));      // required for commerce operation.
            additionalSeats.AssertPositive(nameof(additionalSeats));    // required & should be greater than 0.

            string             operationDescription = string.Format("Customer Id:[{0}] - Added to subscription:{1}.", clientCustomerId, subscriptionId);
            PayPalGateway      paymentGateway       = new PayPalGateway(ApplicationDomain.Instance, orderUpdateInformation.CreditCard, operationDescription);
            CommerceOperations commerceOperation    = new CommerceOperations(ApplicationDomain.Instance, clientCustomerId, paymentGateway);

            return(await commerceOperation.PurchaseAdditionalSeatsAsync(subscriptionId, additionalSeats));
        }
Ejemplo n.º 2
0
        public async Task <TransactionResult> ProcessOrderForAuthenticatedCustomer(string paymentId, string payerId, string orderId)
        {
            DateTime startTime = DateTime.Now;

            // extract order information and create payment payload.
            string clientCustomerId = Principal.PartnerCenterCustomerId;

            paymentId.AssertNotEmpty(nameof(paymentId));
            payerId.AssertNotEmpty(nameof(payerId));
            orderId.AssertNotEmpty(nameof(orderId));

            // Create the right payment gateway to use for customer oriented payment transactions.
            IPaymentGateway paymentGateway = await CreatePaymentGateway("ProcessingOrder", clientCustomerId).ConfigureAwait(false);

            // use payment gateway to extract order information.
            OrderViewModel orderToProcess = await paymentGateway.GetOrderDetailsFromPaymentAsync(payerId, paymentId, orderId, clientCustomerId).ConfigureAwait(false);

            orderToProcess.CustomerId = clientCustomerId;
            CommerceOperations commerceOperation = new CommerceOperations(ApplicationDomain.Instance, clientCustomerId, paymentGateway);

            TransactionResult transactionResult = null;

            switch (orderToProcess.OperationType)
            {
            case CommerceOperationType.Renewal:
                transactionResult = await commerceOperation.RenewSubscriptionAsync(orderToProcess).ConfigureAwait(false);

                break;

            case CommerceOperationType.AdditionalSeatsPurchase:
                transactionResult = await commerceOperation.PurchaseAdditionalSeatsAsync(orderToProcess).ConfigureAwait(false);

                break;

            case CommerceOperationType.NewPurchase:
                transactionResult = await commerceOperation.PurchaseAsync(orderToProcess).ConfigureAwait(false);

                break;
            }

            // Capture the request for the customer summary for analysis.
            Dictionary <string, string> eventProperties = new Dictionary <string, string> {
                { "CustomerId", orderToProcess.CustomerId }, { "OperationType", orderToProcess.OperationType.ToString() }, { "PayerId", payerId }, { "PaymentId", paymentId }
            };

            // Track the event measurements for analysis.
            Dictionary <string, double> eventMetrics = new Dictionary <string, double> {
                { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds }
            };

            ApplicationDomain.Instance.TelemetryService.Provider.TrackEvent("api/order/process", eventProperties, eventMetrics);

            return(transactionResult);
        }