Exemplo n.º 1
0
        public async Task <string> PrepareOrderForUnAuthenticatedCustomer([FromBody] OrderViewModel orderDetails)
        {
            DateTime startTime = DateTime.Now;

            if (!ModelState.IsValid)
            {
                List <string> 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);
            }

            // Validate & Normalize the order information.
            OrderNormalizer orderNormalizer = new OrderNormalizer(ApplicationDomain.Instance, orderDetails);

            orderDetails = await orderNormalizer.NormalizePurchaseSubscriptionOrderAsync().ConfigureAwait(false);

            // prepare the redirect url so that client can redirect to PayPal.
            string redirectUrl = string.Format(CultureInfo.InvariantCulture, "{0}/#ProcessOrder?ret=true&customerId={1}", Request.RequestUri.GetLeftPart(UriPartial.Authority), orderDetails.CustomerId);

            // execute to paypal and get paypal action URI.
            IPaymentGateway paymentGateway = PaymentGatewayConfig.GetPaymentGatewayInstance(ApplicationDomain.Instance, Resources.NewPurchaseOperationCaption);
            string          generatedUri   = await paymentGateway.GeneratePaymentUriAsync(redirectUrl, orderDetails).ConfigureAwait(false);

            // 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/NewCustomerPrepareOrder", null, eventMetrics);

            return(generatedUri);
        }
Exemplo n.º 2
0
        public async Task <string> PrepareOrderForAuthenticatedCustomer([FromBody] OrderViewModel orderDetails)
        {
            DateTime startTime = DateTime.Now;

            if (!ModelState.IsValid)
            {
                List <string> 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);
            }

            orderDetails.CustomerId = Principal.PartnerCenterCustomerId;
            orderDetails.OrderId    = Guid.NewGuid().ToString();
            string operationDescription = string.Empty;

            // Validate & Normalize the order information.
            OrderNormalizer orderNormalizer = new OrderNormalizer(ApplicationDomain.Instance, orderDetails);

            switch (orderDetails.OperationType)
            {
            case CommerceOperationType.AdditionalSeatsPurchase:
                operationDescription = Resources.AddSeatsOperationCaption;
                orderDetails         = await orderNormalizer.NormalizePurchaseAdditionalSeatsOrderAsync().ConfigureAwait(false);

                break;

            case CommerceOperationType.NewPurchase:
                operationDescription = Resources.NewPurchaseOperationCaption;
                orderDetails         = await orderNormalizer.NormalizePurchaseSubscriptionOrderAsync().ConfigureAwait(false);

                break;

            case CommerceOperationType.Renewal:
                operationDescription = Resources.RenewOperationCaption;
                orderDetails         = await orderNormalizer.NormalizeRenewSubscriptionOrderAsync().ConfigureAwait(false);

                break;
            }

            // prepare the redirect url so that client can redirect to payment gateway.
            string redirectUrl = string.Format(CultureInfo.InvariantCulture, "{0}/#ProcessOrder?ret=true", Request.RequestUri.GetLeftPart(UriPartial.Authority));

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

            // execute and get payment gateway action URI.
            string generatedUri = await paymentGateway.GeneratePaymentUriAsync(redirectUrl, orderDetails).ConfigureAwait(false);

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

            // 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/prepare", eventProperties, eventMetrics);

            return(generatedUri);
        }