use of org.apache.commons.validator.CreditCardValidator in project BroadleafCommerce by BroadleafCommerce.
the class NullIntegrationGatewayTransactionServiceImpl method commonCreditCardProcessing.
/**
* Does minimal Credit Card Validation (luhn check and expiration date is after today).
* Mimics the Response of a real Payment Gateway.
*/
protected PaymentResponseDTO commonCreditCardProcessing(PaymentRequestDTO requestDTO, PaymentTransactionType paymentTransactionType) {
PaymentResponseDTO responseDTO = new PaymentResponseDTO(PaymentType.CREDIT_CARD, NullIntegrationGatewayType.NULL_INTEGRATION_GATEWAY);
responseDTO.valid(true).paymentTransactionType(paymentTransactionType);
CreditCardDTO creditCardDTO = requestDTO.getCreditCard();
String transactionAmount = requestDTO.getTransactionTotal();
CreditCardValidator visaValidator = new CreditCardValidator(CreditCardValidator.VISA);
CreditCardValidator amexValidator = new CreditCardValidator(CreditCardValidator.AMEX);
CreditCardValidator mcValidator = new CreditCardValidator(CreditCardValidator.MASTERCARD);
CreditCardValidator discoverValidator = new CreditCardValidator(CreditCardValidator.DISCOVER);
if (StringUtils.isNotBlank(transactionAmount) && StringUtils.isNotBlank(creditCardDTO.getCreditCardNum()) && (StringUtils.isNotBlank(creditCardDTO.getCreditCardExpDate()) || (StringUtils.isNotBlank(creditCardDTO.getCreditCardExpMonth()) && StringUtils.isNotBlank(creditCardDTO.getCreditCardExpYear())))) {
boolean validCard = false;
if (visaValidator.isValid(creditCardDTO.getCreditCardNum())) {
validCard = true;
} else if (amexValidator.isValid(creditCardDTO.getCreditCardNum())) {
validCard = true;
} else if (mcValidator.isValid(creditCardDTO.getCreditCardNum())) {
validCard = true;
} else if (discoverValidator.isValid(creditCardDTO.getCreditCardNum())) {
validCard = true;
}
boolean validDateFormat = false;
boolean validDate = false;
String[] parsedDate = null;
if (StringUtils.isNotBlank(creditCardDTO.getCreditCardExpDate())) {
parsedDate = creditCardDTO.getCreditCardExpDate().split("/");
} else {
parsedDate = new String[2];
parsedDate[0] = creditCardDTO.getCreditCardExpMonth();
parsedDate[1] = creditCardDTO.getCreditCardExpYear();
}
if (parsedDate.length == 2) {
String expMonth = parsedDate[0];
String expYear = parsedDate[1];
try {
DateTime expirationDate = new DateTime(Integer.parseInt("20" + expYear), Integer.parseInt(expMonth), 1, 0, 0);
expirationDate = expirationDate.dayOfMonth().withMaximumValue();
validDate = expirationDate.isAfterNow();
validDateFormat = true;
} catch (Exception e) {
// invalid date format
}
}
if (!validDate || !validDateFormat) {
responseDTO.amount(new Money(0)).rawResponse("cart.payment.expiration.invalid").successful(false);
} else if (!validCard) {
responseDTO.amount(new Money(0)).rawResponse("cart.payment.card.invalid").successful(false);
} else {
responseDTO.amount(new Money(requestDTO.getTransactionTotal())).rawResponse("Success!").successful(true);
}
} else {
responseDTO.amount(new Money(0)).rawResponse("cart.payment.invalid").successful(false);
}
return responseDTO;
}
Aggregations