public AddPaymentMethodResponse AddPaymentMethod( AddPaymentMethodRequest request, int accountId) { //this class allows a user to create and edit their payment details var expiry = new DateTime(request.ExpiryYear, request.ExpiryMonth, DateTime.DaysInMonth(request.ExpiryYear, request.ExpiryMonth)); //find the user and validate if they exist var user = UserRepository.Find(accountId); if (user == null) { return new AddPaymentMethodResponse { Message = $"Account {accountId} does not exist", Success = false } } ; //validate that the user must be activated in the system first if (user.Status != Constants.UserActiveStatus) { return new AddPaymentMethodResponse { Message = "Only activated users can add payment methods", Success = false } } ; //validate that the card number entered and cvv is not empty if (string.IsNullOrEmpty(request.CardNumber) || string.IsNullOrEmpty(request.CardVerificationValue)) { return new AddPaymentMethodResponse { Message = "A credit card is required", Success = false } } ; request.CardNumber = request.CardNumber.Replace(" ", ""); //luhn check to validate that the entered card number is correct var sumOfDigits = request.CardNumber.Where( e => e >= '0' && e <= '9') .Reverse() .Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2)) .Sum(e => e / 10 + e % 10); //if luhn check fails if (sumOfDigits % 10 != 0) { return new AddPaymentMethodResponse { Message = "The entered card number is invalid.", Success = false } } ; //if the card expiry exceeds the historic date if (DateTime.Now > expiry) { return new AddPaymentMethodResponse { Message = "The entered credit card has expired.", Success = false } } ; //calculate the card type string cardType; switch (request.CardNumber.Substring(0, 1)) { case "3": cardType = "AMEX"; break; case "4": cardType = "Visa"; break; case "5": cardType = "Mastercard"; break; default: cardType = "Mastercard"; break; } try { //if an existing payment method exists update the old one var existingPaymentMethod = PaymentMethodRepository.Find(accountId); if (existingPaymentMethod != null) { existingPaymentMethod.CardName = request.CardName; existingPaymentMethod.CardNumber = request.CardNumber; existingPaymentMethod.CardType = cardType; existingPaymentMethod.ExpiryMonth = request.ExpiryMonth; existingPaymentMethod.ExpiryYear = request.ExpiryYear; existingPaymentMethod.CardVerificationValue = request.CardVerificationValue; PaymentMethodRepository.Update(existingPaymentMethod); } else { //otherwise create a new payment method var payment = new PaymentMethod { AccountID = accountId, CardNumber = request.CardNumber, CardName = request.CardName, CardType = cardType, ExpiryMonth = request.ExpiryMonth, ExpiryYear = request.ExpiryYear, CardVerificationValue = request.CardVerificationValue }; PaymentMethodRepository.Add(payment); }; } catch (Exception e) { return(new AddPaymentMethodResponse { Message = $"Error in updating payment method. Error: {e}", Success = false }); } return(new AddPaymentMethodResponse { Success = true, Message = "Payment method has been successfull added!" }); }