public async Task <SubscriptionsSummary> ProcessOrderForUnAuthenticatedCustomer(string customerId, string paymentId, string payerId)
        {
            var startTime = DateTime.Now;

            customerId.AssertNotEmpty(nameof(customerId));

            paymentId.AssertNotEmpty(nameof(paymentId));
            payerId.AssertNotEmpty(nameof(payerId));
            PayPalGateway paymentGateway = new PayPalGateway(ApplicationDomain.Instance, "ProcessingOrder");

            // use payment gateway to extract order information.
            OrderViewModel orderToProcess = await paymentGateway.GetOrderDetailsFromPaymentAsync(payerId, paymentId, string.Empty, string.Empty);

            CommerceOperations commerceOperation = new CommerceOperations(ApplicationDomain.Instance, customerId, paymentGateway);
            await commerceOperation.PurchaseAsync(orderToProcess);

            SubscriptionsSummary summaryResult = await this.GetSubscriptionSummaryAsync(customerId);

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

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

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

            return(summaryResult);
        }
Exemplo n.º 2
0
        public void SubscribeTest_Case_Ok()
        {
            var user = new User
            {
                Email               = "*****@*****.**",
                Role                = Role.User,
                FamilyName          = "",
                GivenName           = "",
                Picture             = "",
                Id                  = Guid.NewGuid(),
                TvShowSubscriptions = new List <TvShowSubscription>()
            };

            userRepository.Setup(c => c.FindUser(It.IsAny <Guid>())).Returns(user);
            userRepository.Setup(c => c.IsTvShowSubscribedByOtherUsers(It.IsAny <int>(), It.IsAny <Guid>())).Returns(false);

            var emptySummary = new SubscriptionsSummary {
                SubscribedShows = new List <TvShowRss>()
            };

            showRssService.Setup(c => c.Authenticate(out emptySummary)).Returns(new ShowRssAuthenticationContext());
            showRssService
            .Setup(c => c.SubscribeToShow(It.IsAny <ShowRssAuthenticationContext>(), It.IsAny <int>()))
            .Returns(new ShowRssGlobalSubscriptionService.SubscriptionResult {
                Succeeded = true, Summary = new SubscriptionsSummary {
                    SubscribedShows = new List <TvShowRss> {
                        new TvShowRss {
                            ShowRssId = 123
                        }
                    }
                }
            });

            var controller = new TvShowController(logger.Object, showRssService.Object, userRepository.Object, memoryCache.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext {
                        User = user.MapToClaimPrincipal()
                    }
                }
            };

            var res = controller.Subscribe(123);

            Assert.AreEqual(200, res.StatusCode);
            Assert.AreEqual("Subscribed", res.Value);
            Assert.AreEqual(1, user.TvShowSubscriptions.Count);
            Assert.AreEqual(123, user.TvShowSubscriptions.First().ShowRssId);
        }
Exemplo n.º 3
0
        public void UnsubscribeTest_Case_ShowRssUnsubscribeFailed()
        {
            var user = new User
            {
                Email               = "*****@*****.**",
                Role                = Role.User,
                FamilyName          = "",
                GivenName           = "",
                Picture             = "",
                Id                  = Guid.NewGuid(),
                TvShowSubscriptions =
                    new List <TvShowSubscription>
                {
                    new TvShowSubscription
                    {
                        Id        = new Guid(),
                        ShowRssId = 123,
                        ShowTitle = "ABC"
                    }
                }
            };

            userRepository.Setup(c => c.FindUser(It.IsAny <Guid>())).Returns(user);
            userRepository.Setup(c => c.IsTvShowSubscribedByOtherUsers(It.IsAny <int>(), It.IsAny <Guid>())).Returns(false);

            var emptySummary = new SubscriptionsSummary();

            showRssService.Setup(c => c.Authenticate(out emptySummary)).Returns(new ShowRssAuthenticationContext());
            showRssService
            .Setup(c => c.UnsubscribeToShow(It.IsAny <ShowRssAuthenticationContext>(), It.IsAny <int>()))
            .Returns(new ShowRssGlobalSubscriptionService.UnsubscriptionResult {
                Succeeded = false
            });

            var controller = new TvShowController(logger.Object, showRssService.Object, userRepository.Object, memoryCache.Object)
            {
                ControllerContext = new ControllerContext {
                    HttpContext = new DefaultHttpContext {
                        User = user.MapToClaimPrincipal()
                    }
                }
            };

            var res = controller.Unsubscribe(123);

            Assert.AreEqual(400, res.StatusCode);
            Assert.AreEqual("Internal error, failed to unsubscribe to show", res.Value);
        }
Exemplo n.º 4
0
        public async Task <SubscriptionsSummary> ProcessOrderForUnAuthenticatedCustomer(string customerId, string paymentId, string payerId)
        {
            DateTime startTime = DateTime.Now;

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

            BrandingConfiguration branding = await ApplicationDomain.Instance.PortalBranding.RetrieveAsync().ConfigureAwait(false);

            CustomerViewModel customerRegistrationInfoPersisted = await ApplicationDomain.Instance.CustomerRegistrationRepository.RetrieveAsync(customerId).ConfigureAwait(false);

            Customer newCustomer = new Customer()
            {
                CompanyProfile = new CustomerCompanyProfile()
                {
                    Domain = customerRegistrationInfoPersisted.DomainName,
                },
                BillingProfile = new CustomerBillingProfile()
                {
                    Culture     = customerRegistrationInfoPersisted.BillingCulture,
                    Language    = customerRegistrationInfoPersisted.BillingLanguage,
                    Email       = customerRegistrationInfoPersisted.Email,
                    CompanyName = customerRegistrationInfoPersisted.CompanyName,

                    DefaultAddress = new Address()
                    {
                        FirstName    = customerRegistrationInfoPersisted.FirstName,
                        LastName     = customerRegistrationInfoPersisted.LastName,
                        AddressLine1 = customerRegistrationInfoPersisted.AddressLine1,
                        AddressLine2 = customerRegistrationInfoPersisted.AddressLine2,
                        City         = customerRegistrationInfoPersisted.City,
                        State        = customerRegistrationInfoPersisted.State,
                        Country      = customerRegistrationInfoPersisted.Country,
                        PostalCode   = customerRegistrationInfoPersisted.ZipCode,
                        PhoneNumber  = customerRegistrationInfoPersisted.Phone,
                    }
                }
            };

            // Register customer
            newCustomer = await ApplicationDomain.Instance.PartnerCenterClient.Customers.CreateAsync(newCustomer).ConfigureAwait(false);

            ResourceCollection <AgreementMetaData> agreements = await ApplicationDomain.Instance.PartnerCenterClient.AgreementDetails.GetAsync().ConfigureAwait(false);

            // Obtain reference to the Microsoft Cloud Agreement.
            AgreementMetaData microsoftCloudAgreement = agreements.Items.FirstOrDefault(agr => agr.AgreementType == AgreementType.MicrosoftCloudAgreement);

            // Attest that the customer has accepted the Microsoft Cloud Agreement (MCA).
            await ApplicationDomain.Instance.PartnerCenterClient.Customers[newCustomer.Id].Agreements.CreateAsync(
                new Agreement
            {
                DateAgreed     = DateTime.UtcNow,
                PrimaryContact = new PartnerCenter.Models.Agreements.Contact
                {
                    Email       = customerRegistrationInfoPersisted.Email,
                    FirstName   = customerRegistrationInfoPersisted.FirstName,
                    LastName    = customerRegistrationInfoPersisted.LastName,
                    PhoneNumber = customerRegistrationInfoPersisted.Phone
                },
                TemplateId = microsoftCloudAgreement.TemplateId,
                Type       = AgreementType.MicrosoftCloudAgreement,
                UserId     = branding.AgreementUserId
            });

            string newCustomerId = newCustomer.CompanyProfile.TenantId;

            CustomerViewModel customerViewModel = new CustomerViewModel()
            {
                AddressLine1     = newCustomer.BillingProfile.DefaultAddress.AddressLine1,
                AddressLine2     = newCustomer.BillingProfile.DefaultAddress.AddressLine2,
                City             = newCustomer.BillingProfile.DefaultAddress.City,
                State            = newCustomer.BillingProfile.DefaultAddress.State,
                ZipCode          = newCustomer.BillingProfile.DefaultAddress.PostalCode,
                Country          = newCustomer.BillingProfile.DefaultAddress.Country,
                Phone            = newCustomer.BillingProfile.DefaultAddress.PhoneNumber,
                Language         = newCustomer.BillingProfile.Language,
                FirstName        = newCustomer.BillingProfile.DefaultAddress.FirstName,
                LastName         = newCustomer.BillingProfile.DefaultAddress.LastName,
                Email            = newCustomer.BillingProfile.Email,
                CompanyName      = newCustomer.BillingProfile.CompanyName,
                MicrosoftId      = newCustomer.CompanyProfile.TenantId,
                UserName         = newCustomer.BillingProfile.Email,
                Password         = newCustomer.UserCredentials.Password,
                AdminUserAccount = newCustomer.UserCredentials.UserName + "@" + newCustomer.CompanyProfile.Domain
            };

            IPaymentGateway paymentGateway = PaymentGatewayConfig.GetPaymentGatewayInstance(ApplicationDomain.Instance, "ProcessingOrder");
            OrderViewModel  orderToProcess = await paymentGateway.GetOrderDetailsFromPaymentAsync(payerId, paymentId, string.Empty, string.Empty).ConfigureAwait(false);

            // Assign the actual customer Id
            orderToProcess.CustomerId = newCustomerId;

            CommerceOperations commerceOperation = new CommerceOperations(ApplicationDomain.Instance, newCustomerId, paymentGateway);
            await commerceOperation.PurchaseAsync(orderToProcess).ConfigureAwait(false);

            SubscriptionsSummary summaryResult = await GetSubscriptionSummaryAsync(newCustomerId).ConfigureAwait(false);

            // Remove the persisted customer registration info.
            await ApplicationDomain.Instance.CustomerRegistrationRepository.DeleteAsync(customerId).ConfigureAwait(false);

            // Capture the request for the customer summary for analysis.
            Dictionary <string, string> eventProperties = new Dictionary <string, string> {
                { "CustomerId", orderToProcess.CustomerId }
            };

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

            summaryResult.CustomerViewModel = customerViewModel;

            return(summaryResult);
        }
        public async Task <SubscriptionsSummary> ProcessOrderForUnAuthenticatedCustomer(string customerId, string paymentId, string payerId)
        {
            var startTime = DateTime.Now;

            customerId.AssertNotEmpty(nameof(customerId));

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

            // Retrieve customer registration details persisted
            CustomerRegistrationRepository customerRegistrationRepository = new CustomerRegistrationRepository(ApplicationDomain.Instance);

            CustomerViewModel customerRegistrationInfoPersisted = await ApplicationDomain.Instance.CustomerRegistrationRepository.RetrieveAsync(customerId);

            var newCustomer = new Customer()
            {
                CompanyProfile = new CustomerCompanyProfile()
                {
                    Domain = customerRegistrationInfoPersisted.DomainName,
                },
                BillingProfile = new CustomerBillingProfile()
                {
                    Culture     = customerRegistrationInfoPersisted.BillingCulture,
                    Language    = customerRegistrationInfoPersisted.BillingLanguage,
                    Email       = customerRegistrationInfoPersisted.Email,
                    CompanyName = customerRegistrationInfoPersisted.CompanyName,

                    DefaultAddress = new Address()
                    {
                        FirstName    = customerRegistrationInfoPersisted.FirstName,
                        LastName     = customerRegistrationInfoPersisted.LastName,
                        AddressLine1 = customerRegistrationInfoPersisted.AddressLine1,
                        AddressLine2 = customerRegistrationInfoPersisted.AddressLine2,
                        City         = customerRegistrationInfoPersisted.City,
                        State        = customerRegistrationInfoPersisted.State,
                        Country      = customerRegistrationInfoPersisted.Country,
                        PostalCode   = customerRegistrationInfoPersisted.ZipCode,
                        PhoneNumber  = customerRegistrationInfoPersisted.Phone,
                    }
                }
            };

            // Register customer
            newCustomer = await ApplicationDomain.Instance.PartnerCenterClient.Customers.CreateAsync(newCustomer);

            var newCustomerId = newCustomer.CompanyProfile.TenantId;

            CustomerViewModel customerViewModel = new CustomerViewModel()
            {
                AddressLine1     = newCustomer.BillingProfile.DefaultAddress.AddressLine1,
                AddressLine2     = newCustomer.BillingProfile.DefaultAddress.AddressLine2,
                City             = newCustomer.BillingProfile.DefaultAddress.City,
                State            = newCustomer.BillingProfile.DefaultAddress.State,
                ZipCode          = newCustomer.BillingProfile.DefaultAddress.PostalCode,
                Country          = newCustomer.BillingProfile.DefaultAddress.Country,
                Phone            = newCustomer.BillingProfile.DefaultAddress.PhoneNumber,
                Language         = newCustomer.BillingProfile.Language,
                FirstName        = newCustomer.BillingProfile.DefaultAddress.FirstName,
                LastName         = newCustomer.BillingProfile.DefaultAddress.LastName,
                Email            = newCustomer.BillingProfile.Email,
                CompanyName      = newCustomer.BillingProfile.CompanyName,
                MicrosoftId      = newCustomer.CompanyProfile.TenantId,
                UserName         = newCustomer.BillingProfile.Email,
                Password         = newCustomer.UserCredentials.Password,
                AdminUserAccount = newCustomer.UserCredentials.UserName + "@" + newCustomer.CompanyProfile.Domain
            };

            IPaymentGateway paymentGateway = PaymentGatewayConfig.GetPaymentGatewayInstance(ApplicationDomain.Instance, "ProcessingOrder");
            OrderViewModel  orderToProcess = await paymentGateway.GetOrderDetailsFromPaymentAsync(payerId, paymentId, string.Empty, string.Empty);

            // Assign the actual customer Id
            orderToProcess.CustomerId = newCustomerId;

            CommerceOperations commerceOperation = new CommerceOperations(ApplicationDomain.Instance, newCustomerId, paymentGateway);
            await commerceOperation.PurchaseAsync(orderToProcess);

            SubscriptionsSummary summaryResult = await this.GetSubscriptionSummaryAsync(newCustomerId);

            // Remove the persisted customer registration info.
            var deleteResult = ApplicationDomain.Instance.CustomerRegistrationRepository.DeleteAsync(customerId);

            // Capture the request for the customer summary for analysis.
            var eventProperties = new Dictionary <string, string> {
                { "CustomerId", orderToProcess.CustomerId }
            };

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

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

            summaryResult.CustomerViewModel = customerViewModel;

            return(summaryResult);
        }