public AddPaymentMethodResponse AddPaymentMethod( AddPaymentMethodRequest request) { Logger.Debug("Payment Method Request Received: {0}", JsonConvert.SerializeObject(request, Formatting.Indented)); var userPrincipal = new UserPrincipal(ClaimsPrincipal.Current); if (userPrincipal.Id.HasValue) { AddPaymentMethodResponse response; //use in built data annotations to ensure model has //binded correctly if (!ModelState.IsValid) { var errors = ModelState.Keys.SelectMany(key => ModelState[key].Errors.Select(x => x.ErrorMessage)); response = new AddPaymentMethodResponse { Success = false, Message = "Form has validation errors", Errors = errors.ToArray() }; } else { //send request to the user service and return the //response (success or fail) response = UserService.AddPaymentMethod( request, userPrincipal.Id.Value); } Logger.Debug("Sent Payment Method Response: {0}", JsonConvert.SerializeObject( response, Formatting.Indented)); return(response); } else { var response = new AddPaymentMethodResponse { Success = false, Message = "Invalid user ID", Errors = new[] { "No user is logged on" } }; Logger.Debug("The user ID session is invalid", JsonConvert.SerializeObject( response, Formatting.Indented)); return(response); } }
/// <summary> /// Successful response returned from a Create Payment Account request. /// https://apidocs.securenet.com/docs/vault.html?lang=csharp#createaccount /// </summary> public string SecureNet_Vault_Create_Payment_Account_Request_Returns_Successfully(string customerId) { // Arrange var request = new AddPaymentMethodRequest { CustomerId = customerId, Card = new Card { Number = "4444 3333 2222 1111", ExpirationDate = "04/2017", Address = new Address { Line1 = "123 Main St.", City = "Austin", State = "TX", Zip = "78759" }, FirstName = "Jack", LastName = "Test" }, Phone = "512-250-7865", Notes = "Create A Vault Account", AccountDuplicateCheckIndicator = 0, Primary = true, UserDefinedFields = new List <UserDefinedField> { new UserDefinedField { UdfName = "Udf1", UdfValue = "Udf1_Value" }, new UserDefinedField { UdfName = "Udf2", UdfValue = "Udf2_Value" }, new UserDefinedField { UdfName = "Udf3", UdfValue = "Udf3_Value" }, new UserDefinedField { UdfName = "Udf4", UdfValue = "Udf4_Value" }, new UserDefinedField { UdfName = "Udf5", UdfValue = "Udf5_Value" } }, DeveloperApplication = new DeveloperApplication { DeveloperId = 12345678, Version = "1.2" } }; var apiContext = new APIContext(); var controller = new CustomersController(); // Act var response = controller.ProcessRequest <AddPaymentMethodResponse>(apiContext, request); // Assert Assert.IsNotNull(response); Assert.IsTrue(response.Success); Assert.IsNotNull(response.VaultPaymentMethod); Assert.IsNotNull(response.VaultPaymentMethod.PaymentId); return(response.VaultPaymentMethod.PaymentId); }
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!" }); }