public async Task ProcessFormAsync_SavesPaymentMethodIfNoMatchingFound()
        {
            var savePaymentMethodCalled = false;
            var newPaymentMethod = new PaymentMethod
            {
                CardNumber = "1234",
                CardholderName = "testcardholdername"
            };

            var existingPaymentMethods = new List<PaymentMethod>();
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllPaymentMethodsAsyncDelegate =
                () => Task.FromResult<ICollection<PaymentMethod>>(new Collection<PaymentMethod>(existingPaymentMethods));

            checkoutDataRepository.SavePaymentMethodAsyncDelegate = paymentMethod =>
            {
                savePaymentMethodCalled = true;
                Assert.AreEqual("testcardholdername",
                                paymentMethod.CardholderName);
                return Task.Delay(0);
            };
            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);
            target.PaymentMethod = newPaymentMethod;

            await target.ProcessFormAsync();

            Assert.IsTrue(savePaymentMethodCalled);
        }
        public async Task ProcessFormAsync_UsesExistingIfMatchingFound()
        {
            var newPaymentMethod = new PaymentMethod
            {
                CardNumber = "1234",
                CardholderName = "testcardholdername"
            };

            var existingPaymentMethods = new List<PaymentMethod>
                                        {
                                            new PaymentMethod
                                                {
                                                    Id = "testId",
                                                    CardNumber = "1234",
                                                    CardholderName = "testcardholdername"
                                                }
                                        };

            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllPaymentMethodsAsyncDelegate =
                () => Task.FromResult<ICollection<PaymentMethod>>(new ReadOnlyCollection<PaymentMethod>(existingPaymentMethods));

            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);
            target.PaymentMethod = newPaymentMethod;

            await target.ProcessFormAsync();

            Assert.AreEqual("testId", target.PaymentMethod.Id);
        }
 public async Task SavePaymentMethodAsync(PaymentMethod paymentMethod)
 {
     using (var client = new HttpClient())
     {
         var response = await client.PostAsync(new Uri(_clientBaseUrl), new HttpStringContent(JsonConvert.SerializeObject(paymentMethod), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"));
         await response.EnsureSuccessWithValidationSupportAsync();
     }
 }
 public Task SavePaymentMethodAsync(PaymentMethod paymentMethod)
 {
     var matchingPaymentMethod = PaymentMethods.FirstOrDefault(p => p.Id == paymentMethod.Id);
     if (matchingPaymentMethod != null)
     {
         PaymentMethods.Remove(matchingPaymentMethod);
     }
     PaymentMethods.Add(paymentMethod);
     return Task.Delay(0);
 }
        public async Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
        {
            Order order = new Order
                {
                    UserId = userId,
                    ShoppingCart = shoppingCart,
                    ShippingAddress = shippingAddress,
                    BillingAddress = billingAddress,
                    PaymentMethod = paymentMethod,
                    ShippingMethod = shippingMethod
                };

            order.Id = await _orderService.CreateOrderAsync(order);

            return order;
        }
        public void OnNavigateTo_LoadsDefault_IfTryLoadDefaultTrue()
        {
            var defaultPaymentMethod = new PaymentMethod
            {
                CardholderName = "CardHolderName",
                CardNumber = "32323232",
                CardVerificationCode = "222",
                Phone = "22224232",
                ExpirationMonth = "12",
                ExpirationYear = "2016"
            };
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetDefaultPaymentMethodAsyncDelegate = () => Task.FromResult(defaultPaymentMethod);
            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);

            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.IsNull(target.PaymentMethod.CardholderName);

            target.SetLoadDefault(true);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.AreEqual("CardHolderName", target.PaymentMethod.CardholderName);
        }
 public Task SavePaymentMethodAsync(PaymentMethod paymentMethod)
 {
     return SavePaymentMethodAsyncDelegate(paymentMethod);
 }
 public async Task CreateBasicOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod)
 {
     var basicShippingMethod = await _shippingMethodService.GetBasicShippingMethodAsync();
     _currentOrder = await CreateOrderAsync(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, basicShippingMethod);
     _sessionStateService.SessionState[OrderKey] = _currentOrder;
 }
 public void SaveEntity(PaymentMethod paymentMethod)
 {
     PaymentMethods.Add(paymentMethod);
 }
 public Task<Order> CreateOrderAsync(string userId, ShoppingCart shoppingCart, Address shippingAddress, Address billingAddress, PaymentMethod paymentMethod, ShippingMethod shippingMethod)
 {
     return CreateOrderAsyncDelegate(userId, shoppingCart, shippingAddress, billingAddress, paymentMethod, shippingMethod);
 }
 private CheckoutDataViewModel CreateCheckoutData(PaymentMethod paymentMethod)
 {
     return new CheckoutDataViewModel()
     {
         EntityId = paymentMethod.Id,
         DataType = Constants.PaymentMethod,
         Title = _resourceLoader.GetString("PaymentMethod"),
         FirstLine = string.Format(CultureInfo.CurrentCulture, _resourceLoader.GetString("CardEndingIn"), paymentMethod.CardNumber.Substring(paymentMethod.CardNumber.Length - 4)),
         SecondLine = string.Format(CultureInfo.CurrentCulture, _resourceLoader.GetString("CardExpiringOn"), string.Format(CultureInfo.CurrentCulture, "{0}/{1}", paymentMethod.ExpirationMonth, paymentMethod.ExpirationYear)),
         BottomLine = paymentMethod.CardholderName,
         LogoUri = new Uri(Constants.PaymentMethodLogo, UriKind.Absolute)
     };
 }
        public async Task SavePaymentMethodAsync(PaymentMethod paymentMethod)
        {
            if (paymentMethod == null)
            {
                throw new ArgumentNullException("paymentMethod");
            }

            paymentMethod.Id = paymentMethod.Id ?? Guid.NewGuid().ToString();

            // Sensitive data replaced with asterisks. Configure secure transport layer (SSL)
            // so that you can securely send sensitive data such as credit card data.
            var paymentMethodToSave = new PaymentMethod()
            {
                Id = paymentMethod.Id,
                CardNumber = "********",
                CardVerificationCode = "****",
                CardholderName = paymentMethod.CardholderName,
                ExpirationMonth = paymentMethod.ExpirationMonth,
                ExpirationYear = paymentMethod.ExpirationYear,
                Phone = paymentMethod.Phone
            };

            // If there's no default value stored, use this one
            if (await GetDefaultPaymentMethodAsync() == null)
            {
                paymentMethodToSave.IsDefault = true;
            }

            // Save the payment method to the service
            await _paymentMethodService.SavePaymentMethodAsync(paymentMethodToSave);

            ExpireCachedPaymentMethods();
        }
 public async Task ProcessFormAsync()
 {
     var existingPaymentMethods = await _checkoutDataRepository.GetAllPaymentMethodsAsync();
     var matchingExistingPaymentMethod = FindMatchingPaymentMethod(PaymentMethod, existingPaymentMethods);
     if (matchingExistingPaymentMethod != null)
     {
         PaymentMethod = matchingExistingPaymentMethod;
     }
     else
     {
         await _checkoutDataRepository.SavePaymentMethodAsync(PaymentMethod);
     }
 }
 public PaymentMethodUserControlViewModel(ICheckoutDataRepository checkoutDataRepository)
 {
     _paymentMethod = new PaymentMethod();
     _checkoutDataRepository = checkoutDataRepository;
 }
 private static PaymentMethod FindMatchingPaymentMethod(PaymentMethod searchPaymentMethod, IEnumerable<PaymentMethod> paymentMethods)
 {
     // This method is not comparing the Card Number since the Card Number value is being replaced with asterisks
     // when persisted to the service. In a real production app using SSL, you would send/receive the actual card number
     // securely.
     return paymentMethods.FirstOrDefault(paymentMethod =>
         searchPaymentMethod.CardVerificationCode == paymentMethod.CardVerificationCode &&
         searchPaymentMethod.CardholderName == paymentMethod.CardholderName &&
         searchPaymentMethod.ExpirationMonth == paymentMethod.ExpirationMonth &&
         searchPaymentMethod.ExpirationYear == paymentMethod.ExpirationYear &&
         searchPaymentMethod.Phone == paymentMethod.Phone);
 }