Esempio n. 1
0
        protected virtual async Task <Payment> PreparePaymentSwitch(UpdatePaymentMethodParam param, Payment activePayment)
        {
            //TODO: Remove for now, but we should void (Bug with Moneris when payment is PendingVerification).
            await PaymentRepository.RemovePaymentAsync(new VoidOrRemovePaymentParam
            {
                CartName    = param.CartName,
                CultureInfo = param.CultureInfo,
                CustomerId  = param.CustomerId,
                PaymentId   = activePayment.Id,
                Scope       = param.Scope
            }).ConfigureAwait(false);

            var cart = await CartRepository.AddPaymentAsync(new AddPaymentParam
            {
                BillingAddress = activePayment.BillingAddress.Clone(),
                CartName       = param.CartName,
                CultureInfo    = param.CultureInfo,
                CustomerId     = param.CustomerId,
                Scope          = param.Scope
            });

            var newPayment = GetActivePayment(cart);

            return(newPayment);
        }
        public async Task <IHttpActionResult> UpdateCheckoutPayment(UpdatePaymentMethodViewModel request)
        {
            if (request == null)
            {
                return(BadRequest("Request cannot be null."));
            }

            var param = new UpdatePaymentMethodParam
            {
                CartName            = CartConfiguration.ShoppingCartName,
                CultureInfo         = ComposerContext.CultureInfo,
                CustomerId          = ComposerContext.CustomerId,
                PaymentId           = request.PaymentId.GetValueOrDefault(),
                Scope               = ComposerContext.Scope,
                PaymentMethodId     = request.PaymentMethodId.GetValueOrDefault(),
                PaymentProviderName = request.PaymentProviderName,
                PaymentType         = request.PaymentType,
                ProviderNames       = request.Providers.ToList(),
                IsAuthenticated     = ComposerContext.IsAuthenticated
            };

            var vm = await PaymentViewService.UpdateActivePaymentMethodAsync(param);

            return(Ok(vm));
        }
Esempio n. 3
0
 protected virtual Task <List <Payment> > GetCartPaymentsAsync(UpdatePaymentMethodParam param)
 {
     return(PaymentRepository.GetCartPaymentsAsync(new GetCartPaymentsParam
     {
         CartName = param.CartName,
         CultureInfo = param.CultureInfo,
         CustomerId = param.CustomerId,
         Scope = param.Scope
     }));
 }
        public virtual Task <ProcessedCart> UpdatePaymentMethodAsync(UpdatePaymentMethodParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.CartName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.CartName)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.PaymentProviderName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.PaymentProviderName)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.CustomerId)), nameof(param));
            }
            if (param.PaymentId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.PaymentId)), nameof(param));
            }
            if (param.PaymentMethodId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.PaymentMethodId)), nameof(param));
            }

            var cacheKey = BuildCartCacheKey(param.Scope, param.CartName, param.CustomerId);

            var request = new UpdatePaymentMethodRequest
            {
                CartName            = param.CartName,
                CultureName         = param.CultureInfo.Name,
                CustomerId          = param.CustomerId,
                PaymentId           = param.PaymentId,
                PaymentMethodId     = param.PaymentMethodId,
                PaymentProviderName = param.PaymentProviderName,
                ScopeId             = param.Scope
            };

            return(CacheProvider.ExecuteAndSetAsync(cacheKey, () => OvertureClient.SendAsync(request)));
        }
Esempio n. 5
0
        /// <summary>
        /// Builds an instance of <see cref="InitializePaymentParam"/> that will be used to make the request to the repository.
        /// </summary>
        /// <param name="cart">Cart previously retrieved from the UpdatePaymentMethodAsync call.</param>
        /// <param name="updatePaymentMethodParam">Parameters passed to the UpdatePaymentMethodAsync method.</param>
        /// <returns>Instance of the <see cref="InitializePaymentParam"/> that will be used to make the call.</returns>
        /// <remarks>It may be useful to override this method to augment the request with AdditionalData or Options for the request.</remarks>
        protected virtual InitializePaymentParam BuildInitializePaymentParam(Overture.ServiceModel.Orders.Cart cart,
                                                                             UpdatePaymentMethodParam updatePaymentMethodParam)
        {
            var param = new InitializePaymentParam
            {
                CartName    = updatePaymentMethodParam.CartName,
                CultureInfo = updatePaymentMethodParam.CultureInfo,
                CustomerId  = updatePaymentMethodParam.CustomerId,
                PaymentId   = updatePaymentMethodParam.PaymentId,
                Scope       = updatePaymentMethodParam.Scope,
                PaymentType = updatePaymentMethodParam.PaymentType
            };

            return(param);
        }
        public async Task WHEN_param_ok_SHOULD_call_Overture_UpdatePaymentMethodRequest()
        {
            //Arrange
            var param = new UpdatePaymentMethodParam
            {
                CartName            = GetRandom.String(10),
                CultureInfo         = CultureInfo.InvariantCulture,
                CustomerId          = GetRandom.Guid(),
                PaymentId           = GetRandom.Guid(),
                PaymentMethodId     = GetRandom.Guid(),
                PaymentProviderName = GetRandom.String(15),
                Scope = GetRandom.String(10)
            };

            var sut = _container.CreateInstance <PaymentRepository>();

            //Act
            var cart = await sut.UpdatePaymentMethodAsync(param);

            //Assert
            cart.Should().NotBeNull();
            _container.Verify <IOvertureClient>(ov => ov.SendAsync(It.IsNotNull <UpdatePaymentMethodRequest>()));
        }
        public void WHEN_param_PaymentMethodId_is_invalid_SHOULD_throw_ArgumentException()
        {
            //Arrange
            var param = new UpdatePaymentMethodParam
            {
                CartName            = GetRandom.String(10),
                CultureInfo         = CultureInfo.InvariantCulture,
                CustomerId          = GetRandom.Guid(),
                PaymentId           = GetRandom.Guid(),
                PaymentMethodId     = Guid.Empty,
                PaymentProviderName = GetRandom.String(15),
                Scope = GetRandom.String(10)
            };

            var sut = _container.CreateInstance <PaymentRepository>();

            //Act
            var exception = Assert.ThrowsAsync <ArgumentException>(() => sut.UpdatePaymentMethodAsync(param));

            //Assert
            exception.ParamName.Should().BeEquivalentTo("param");
            exception.Message.Should().ContainEquivalentOf("paymentMethodId");
        }
        public void WHEN_param_CartName_is_invalid_SHOULD_throw_ArgumentException(string cartName)
        {
            //Arrange
            var param = new UpdatePaymentMethodParam
            {
                CartName            = cartName,
                CultureInfo         = CultureInfo.InvariantCulture,
                CustomerId          = GetRandom.Guid(),
                PaymentId           = GetRandom.Guid(),
                PaymentMethodId     = GetRandom.Guid(),
                PaymentProviderName = GetRandom.String(15),
                Scope = GetRandom.String(10)
            };

            var sut = _container.CreateInstance <PaymentRepository>();

            //Act
            Expression <Func <Task <ProcessedCart> > > expression = () => sut.UpdatePaymentMethodAsync(param);
            var exception = Assert.ThrowsAsync <ArgumentException>(() => expression.Compile().Invoke());

            //Assert
            exception.ParamName.Should().BeEquivalentTo(GetParamsInfo(expression)[0].Name);
            exception.Message.Should().StartWith(GetMessageOfNullWhiteSpace(nameof(param.CartName)));
        }
Esempio n. 9
0
        public virtual async Task <CartViewModel> UpdateRecurringOrderCartPaymentMethodAsync(UpdatePaymentMethodParam param, string baseUrl)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.CartName))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("CartName"), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.PaymentProviderName))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("PaymentProviderName"), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("Scope"), nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("CultureInfo"), nameof(param));
            }
            if (param.CustomerId == default(Guid))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("CustomerId"), nameof(param));
            }
            if (param.PaymentId == default(Guid))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("PaymentId"), nameof(param));
            }
            if (param.PaymentMethodId == default(Guid))
            {
                throw new ArgumentException(ArgumentNullMessageFormatter.FormatErrorMessage("PaymentMethodId"), nameof(param));
            }

            var activePayment = await PaymentRepository.GetPaymentAsync(new GetPaymentParam {
                CartName    = param.CartName,
                CultureInfo = param.CultureInfo,
                CustomerId  = param.CustomerId,
                Scope       = param.Scope,
                PaymentId   = param.PaymentId
            }).ConfigureAwait(false);

            if (activePayment == null)
            {
                return(null);
            }

            if (activePayment.ShouldInvokePrePaymentSwitch())
            {
                activePayment = await PreparePaymentSwitch(param, activePayment);

                param.PaymentId = activePayment.Id;
            }

            var validatePaymentMethodParam = new ValidatePaymentMethodParam()
            {
                CartName        = param.CartName,
                CultureInfo     = param.CultureInfo,
                CustomerId      = param.CustomerId,
                IsAuthenticated = param.IsAuthenticated,
                ProviderName    = param.PaymentProviderName,
                Scope           = param.Scope,
                PaymentMethodId = param.PaymentMethodId
            };

            var isPaymentMethodValid = await ValidatePaymentMethod(validatePaymentMethodParam);

            if (!isPaymentMethodValid)
            {
                throw new Exception($"Payment method for provider name /'{param.PaymentProviderName}/' not valid. Credit card has probably expired.");
            }

            Overture.ServiceModel.Orders.Cart cart = await PaymentRepository.UpdatePaymentMethodAsync(param);

            var initParam       = BuildInitializePaymentParam(cart, param);
            var paymentProvider = ObtainPaymentProvider(param.PaymentProviderName);

            cart = await paymentProvider.InitializePaymentAsync(cart, initParam);

            var vm = await RecurringOrderCartsViewService.CreateCartViewModelAsync(new CreateRecurringOrderCartViewModelParam
            {
                Cart        = cart,
                CultureInfo = param.CultureInfo,
                IncludeInvalidCouponsMessages = false,
                BaseUrl = baseUrl
            });

            return(vm);
        }
Esempio n. 10
0
        /// <summary>
        /// Updates the payment method for a cart and initializes it.
        /// </summary>
        /// <returns>ViewModel representing the initialized Active Payment.</returns>
        public virtual async Task <CheckoutPaymentViewModel> UpdatePaymentMethodAsync(UpdatePaymentMethodParam param)
        {
            await UpdateActivePaymentMethodAsync(param).ConfigureAwait(false);

            var vm = await GetPaymentMethodsAsync(new GetPaymentMethodsParam
            {
                CartName        = param.CartName,
                CultureInfo     = param.CultureInfo,
                CustomerId      = param.CustomerId,
                IsAuthenticated = param.IsAuthenticated,
                ProviderNames   = param.ProviderNames,
                Scope           = param.Scope,
            });

            return(vm);
        }
        public async Task WHEN_param_ok_and_cart_SHOULD_return_valid_viewModel()
        {
            //Arrange
            _container.Use(CartViewModelFactoryMock.MockGetPaymentMethodViewModel());

            var param = new UpdatePaymentMethodParam
            {
                CartName            = GetRandom.String(12),
                CultureInfo         = CultureInfo.InvariantCulture,
                CustomerId          = GetRandom.Guid(),
                PaymentId           = GetRandom.Guid(),
                PaymentMethodId     = GetRandom.Guid(),
                PaymentProviderName = GetRandom.String(15),
                Scope = GetRandom.String(10)
            };

            var paymentProviderFactoryMock = _container.GetMock <IPaymentProviderFactory>();

            paymentProviderFactoryMock.Setup(factory => factory.ResolveProvider(It.IsNotNull <string>()))
            .Returns(new FakePaymentProvider());

            var paymentRepoMock = _container.GetMock <IPaymentRepository>();

            paymentRepoMock.Setup(pr => pr.GetPaymentMethodsAsync(It.IsNotNull <GetPaymentMethodsParam>())).ReturnsAsync(
                new List <PaymentMethod>
            {
                new PaymentMethod
                {
                    Id = param.PaymentMethodId,
                    PaymentProviderName = param.PaymentProviderName,
                    Enabled             = true,
                }
            }
                );
            paymentRepoMock.Setup(pr => pr.GetCartPaymentsAsync(It.IsNotNull <GetCartPaymentsParam>()))
            .ReturnsAsync(new List <Payment>()
            {
                new Payment
                {
                    Id             = param.PaymentId,
                    Amount         = 0.0m,
                    BillingAddress = new Address()
                    {
                        City                = GetRandom.String(18),
                        CountryCode         = GetRandom.String(2, true),
                        FirstName           = GetRandom.String(8),
                        LastName            = GetRandom.String(12),
                        IsPreferredBilling  = GetRandom.Boolean(),
                        IsPreferredShipping = GetRandom.Boolean(),
                        Email               = GetRandom.Email(),
                        Line1               = GetRandom.Phrase(20),
                        Line2               = string.Empty,
                        PhoneNumber         = GetRandom.Usa.PhoneNumber(),
                        PostalCode          = GetRandom.String(7, true),
                        RegionCode          = GetRandom.String(5, true)
                    },
                    PaymentStatus = PaymentStatus.New,
                    PaymentMethod = new PaymentMethod
                    {
                        Id      = param.PaymentMethodId,
                        Enabled = true
                    }
                }
            });

            var sut = _container.CreateInstance <PaymentViewService>();

            //Act
            var vm = await sut.UpdateActivePaymentMethodAsync(param);

            //Assert
            vm.Should().NotBeNull();
            vm.Id.Should().NotBeEmpty();
            vm.PaymentStatus.Should().NotBeNullOrWhiteSpace();
        }
Esempio n. 12
0
        public virtual async Task <ActivePaymentViewModel> UpdateActivePaymentMethodAsync(UpdatePaymentMethodParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.CartName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.CartName)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.PaymentProviderName))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.PaymentProviderName)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.CustomerId)), nameof(param));
            }
            if (param.PaymentId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.PaymentId)), nameof(param));
            }
            if (param.PaymentMethodId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.PaymentMethodId)), nameof(param));
            }

            var payments = await GetCartPaymentsAsync(param).ConfigureAwait(false);

            var activePayment = payments.GetPayment(param.PaymentId);

            if (activePayment.ShouldInvokePrePaymentSwitch())
            {
                activePayment = await PreparePaymentSwitch(param, activePayment);

                param.PaymentId = activePayment.Id;
            }

            var validatePaymentMethodParam = new ValidatePaymentMethodParam()
            {
                CartName        = param.CartName,
                CultureInfo     = param.CultureInfo,
                CustomerId      = param.CustomerId,
                IsAuthenticated = param.IsAuthenticated,
                ProviderName    = param.PaymentProviderName,
                Scope           = param.Scope,
                PaymentMethodId = param.PaymentMethodId
            };

            var isPaymentMethodValid = await ValidatePaymentMethod(validatePaymentMethodParam);

            if (!isPaymentMethodValid)
            {
                throw new Exception($"Payment method for provider name /'{param.PaymentProviderName}/' not valid. Credit card has probably expired.");
            }

            Overture.ServiceModel.Orders.Cart cart = await PaymentRepository.UpdatePaymentMethodAsync(param);

            var initParam       = BuildInitializePaymentParam(cart, param);
            var paymentProvider = ObtainPaymentProvider(param.PaymentProviderName);

            cart = await paymentProvider.InitializePaymentAsync(cart, initParam);

            var activePaymentVm = GetActivePaymentViewModel(new GetActivePaymentViewModelParam
            {
                Cart            = cart,
                CultureInfo     = param.CultureInfo,
                PaymentProvider = paymentProvider,
                IsAuthenticated = param.IsAuthenticated
            });

            return(activePaymentVm);
        }