Example usage for org.apache.commons.validator CreditCardValidator CreditCardValidator

List of usage examples for org.apache.commons.validator CreditCardValidator CreditCardValidator

Introduction

In this page you can find the example usage for org.apache.commons.validator CreditCardValidator CreditCardValidator.

Prototype

public CreditCardValidator(int options) 

Source Link

Document

Creates a new CreditCardValidator with the specified options.

Usage

From source file:com.att.aro.core.searching.impl.PatternSearchingHandler.java

private void initCreditCardValidator() {
    validator = new CreditCardValidator(CreditCardValidator.AMEX + CreditCardValidator.DISCOVER
            + CreditCardValidator.MASTERCARD + CreditCardValidator.VISA);
}

From source file:co.propack.sample.vendor.nullPaymentGateway.web.controller.NullPaymentGatewayProcessorController.java

@RequestMapping(value = "/null-checkout/process", method = RequestMethod.POST)
public @ResponseBody String processTransparentRedirectForm(HttpServletRequest request) {
    Map<String, String[]> paramMap = request.getParameterMap();

    String transactionAmount = "";
    String orderId = "";
    String billingFirstName = "";
    String billingLastName = "";
    String billingAddressLine1 = "";
    String billingAddressLine2 = "";
    String billingCity = "";
    String billingState = "";
    String billingZip = "";
    String billingCountry = "";
    String shippingFirstName = "";
    String shippingLastName = "";
    String shippingAddressLine1 = "";
    String shippingAddressLine2 = "";
    String shippingCity = "";
    String shippingState = "";
    String shippingZip = "";
    String shippingCountry = "";
    String creditCardName = "";
    String creditCardNumber = "";
    String creditCardExpDate = "";
    String creditCardCVV = "";
    String cardType = "UNKNOWN";

    String resultMessage = "";
    String resultSuccess = "";
    String gatewayTransactionId = UUID.randomUUID().toString();

    if (paramMap.get(NullPaymentGatewayConstants.TRANSACTION_AMT) != null
            && paramMap.get(NullPaymentGatewayConstants.TRANSACTION_AMT).length > 0) {
        transactionAmount = paramMap.get(NullPaymentGatewayConstants.TRANSACTION_AMT)[0];
    }/*from   www  . jav a 2  s.  c  o m*/

    if (paramMap.get(NullPaymentGatewayConstants.ORDER_ID) != null
            && paramMap.get(NullPaymentGatewayConstants.ORDER_ID).length > 0) {
        orderId = paramMap.get(NullPaymentGatewayConstants.ORDER_ID)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_FIRST_NAME) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_FIRST_NAME).length > 0) {
        billingFirstName = paramMap.get(NullPaymentGatewayConstants.BILLING_FIRST_NAME)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_LAST_NAME) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_LAST_NAME).length > 0) {
        billingLastName = paramMap.get(NullPaymentGatewayConstants.BILLING_LAST_NAME)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE1) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE1).length > 0) {
        billingAddressLine1 = paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE1)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE2) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE2).length > 0) {
        billingAddressLine2 = paramMap.get(NullPaymentGatewayConstants.BILLING_ADDRESS_LINE2)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_CITY) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_CITY).length > 0) {
        billingCity = paramMap.get(NullPaymentGatewayConstants.BILLING_CITY)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_STATE) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_STATE).length > 0) {
        billingState = paramMap.get(NullPaymentGatewayConstants.BILLING_STATE)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_ZIP) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_ZIP).length > 0) {
        billingZip = paramMap.get(NullPaymentGatewayConstants.BILLING_ZIP)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.BILLING_COUNTRY) != null
            && paramMap.get(NullPaymentGatewayConstants.BILLING_COUNTRY).length > 0) {
        billingCountry = paramMap.get(NullPaymentGatewayConstants.BILLING_COUNTRY)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_FIRST_NAME) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_FIRST_NAME).length > 0) {
        shippingFirstName = paramMap.get(NullPaymentGatewayConstants.SHIPPING_FIRST_NAME)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_LAST_NAME) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_LAST_NAME).length > 0) {
        shippingLastName = paramMap.get(NullPaymentGatewayConstants.SHIPPING_LAST_NAME)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE1) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE1).length > 0) {
        shippingAddressLine1 = paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE1)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE2) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE2).length > 0) {
        shippingAddressLine2 = paramMap.get(NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE2)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_CITY) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_CITY).length > 0) {
        shippingCity = paramMap.get(NullPaymentGatewayConstants.SHIPPING_CITY)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_STATE) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_STATE).length > 0) {
        shippingState = paramMap.get(NullPaymentGatewayConstants.SHIPPING_STATE)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_ZIP) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_ZIP).length > 0) {
        shippingZip = paramMap.get(NullPaymentGatewayConstants.SHIPPING_ZIP)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.SHIPPING_COUNTRY) != null
            && paramMap.get(NullPaymentGatewayConstants.SHIPPING_COUNTRY).length > 0) {
        shippingCountry = paramMap.get(NullPaymentGatewayConstants.SHIPPING_COUNTRY)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NAME) != null
            && paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NAME).length > 0) {
        creditCardName = paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NAME)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NUMBER) != null
            && paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NUMBER).length > 0) {
        creditCardNumber = paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_NUMBER)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_EXP_DATE) != null
            && paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_EXP_DATE).length > 0) {
        creditCardExpDate = paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_EXP_DATE)[0];
    }

    if (paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_CVV) != null
            && paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_CVV).length > 0) {
        creditCardCVV = paramMap.get(NullPaymentGatewayConstants.CREDIT_CARD_CVV)[0];
    }

    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(creditCardNumber)
            && StringUtils.isNotBlank(creditCardExpDate)) {

        boolean validCard = false;
        if (visaValidator.isValid(creditCardNumber)) {
            validCard = true;
            cardType = "VISA";
        } else if (amexValidator.isValid(creditCardNumber)) {
            validCard = true;
            cardType = "AMEX";
        } else if (mcValidator.isValid(creditCardNumber)) {
            validCard = true;
            cardType = "MASTERCARD";
        } else if (discoverValidator.isValid(creditCardNumber)) {
            validCard = true;
            cardType = "DISCOVER";
        }

        boolean validDateFormat = false;
        boolean validDate = false;
        String[] parsedDate = creditCardExpDate.split("/");
        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) {
            transactionAmount = "0";
            resultMessage = "cart.payment.expiration.invalid";
            resultSuccess = "false";
        } else if (!validCard) {
            transactionAmount = "0";
            resultMessage = "cart.payment.card.invalid";
            resultSuccess = "false";
        } else {
            resultMessage = "Success!";
            resultSuccess = "true";
        }

    } else {
        transactionAmount = "0";
        resultMessage = "cart.payment.invalid";
        resultSuccess = "false";
    }

    StringBuffer response = new StringBuffer();
    response.append("<!DOCTYPE HTML>");
    response.append("<!--[if lt IE 7]> <html class=\"no-js lt-ie9 lt-ie8 lt-ie7\" lang=\"en\"> <![endif]-->");
    response.append("<!--[if IE 7]> <html class=\"no-js lt-ie9 lt-ie8\" lang=\"en\"> <![endif]-->");
    response.append("<!--[if IE 8]> <html class=\"no-js lt-ie9\" lang=\"en\"> <![endif]-->");
    response.append("<!--[if gt IE 8]><!--> <html class=\"no-js\" lang=\"en\"> <!--<![endif]-->");
    response.append("<body>");
    response.append("<form action=\"" + paymentGatewayConfiguration.getTransparentRedirectReturnUrl()
            + "\" method=\"POST\" id=\"NullPaymentGatewayRedirectForm\" name=\"NullPaymentGatewayRedirectForm\">");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.TRANSACTION_AMT
            + "\" value=\"" + transactionAmount + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.ORDER_ID + "\" value=\""
            + orderId + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.GATEWAY_TRANSACTION_ID
            + "\" value=\"" + gatewayTransactionId + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.RESULT_MESSAGE
            + "\" value=\"" + resultMessage + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.RESULT_SUCCESS
            + "\" value=\"" + resultSuccess + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_FIRST_NAME
            + "\" value=\"" + billingFirstName + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_LAST_NAME
            + "\" value=\"" + billingLastName + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_ADDRESS_LINE1
            + "\" value=\"" + billingAddressLine1 + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_ADDRESS_LINE2
            + "\" value=\"" + billingAddressLine2 + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_CITY + "\" value=\""
            + billingCity + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_STATE + "\" value=\""
            + billingState + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_ZIP + "\" value=\""
            + billingZip + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.BILLING_COUNTRY
            + "\" value=\"" + billingCountry + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_FIRST_NAME
            + "\" value=\"" + shippingFirstName + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_LAST_NAME
            + "\" value=\"" + shippingLastName + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE1
            + "\" value=\"" + shippingAddressLine1 + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_ADDRESS_LINE2
            + "\" value=\"" + shippingAddressLine2 + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_CITY + "\" value=\""
            + shippingCity + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_STATE
            + "\" value=\"" + shippingState + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_ZIP + "\" value=\""
            + shippingZip + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.SHIPPING_COUNTRY
            + "\" value=\"" + shippingCountry + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.CREDIT_CARD_NAME
            + "\" value=\"" + creditCardName + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.CREDIT_CARD_LAST_FOUR
            + "\" value=\"" + StringUtils.right(creditCardNumber, 4) + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.CREDIT_CARD_TYPE
            + "\" value=\"" + cardType + "\"/>");
    response.append("<input type=\"hidden\" name=\"" + NullPaymentGatewayConstants.CREDIT_CARD_EXP_DATE
            + "\" value=\"" + creditCardExpDate + "\"/>");

    response.append("<input type=\"submit\" value=\"Please Click Here To Complete Checkout\"/>");
    response.append("</form>");
    response.append("<script type=\"text/javascript\">");
    response.append("document.getElementById('NullPaymentGatewayRedirectForm').submit();");
    response.append("</script>");
    response.append("</body>");
    response.append("</html>");

    return response.toString();
}

From source file:org.broadleafcommerce.core.payment.service.module.NullCreditCardPaymentModule.java

/**
 * The following method:// w  ww .  j  av a  2s  .co m
 * validates a credit card number against the Apache Commons CreditCardValidator.
 * validates the expiration date.
 * validates that the cvv != "000" in order to demonstrate a PaymentException
 *
 * This method will set the TransactionSuccess to false on the PaymentResponseItem
 * if any of the conditions above are invalid.
 *
 * This does NOT integrate with any Payment Gateway and should not be used in any production environment.
 * This class is for demonstration purposes only.
 *
 * @param paymentContext - the payment context injected from the workflow (see: blAuthorizeAndDebitWorkflow in bl-framework-applicationContext-workflow.xml)
 * @return PaymentResponseItem - the response item
 */
@Override
public PaymentResponseItem authorizeAndDebit(PaymentContext paymentContext) throws PaymentException {
    //Note that you cannot perform operations on paymentContext.getPaymentInfo() directly because that is a copy of the actual payment on the order.
    //In order to persist custom attributes to the credit card payment info on the order we must look it up first.
    PaymentInfo paymentInfo = null;
    for (PaymentInfo pi : paymentContext.getPaymentInfo().getOrder().getPaymentInfos()) {
        if (PaymentInfoType.CREDIT_CARD.equals(pi.getType())) {
            paymentInfo = pi;
        }
    }

    if (paymentInfo == null) {
        throw new PaymentException("PaymentInfo of type CREDIT_CARD must be on the order");
    }

    CreditCardPaymentInfo ccInfo = (CreditCardPaymentInfo) paymentContext.getReferencedPaymentInfo();
    String nameOnCard = ccInfo.getNameOnCard();
    String ccNumber = ccInfo.getPan().replaceAll("[\\s-]+", "");
    Integer expMonth = ccInfo.getExpirationMonth();
    Integer expYear = ccInfo.getExpirationYear();
    String cvv = ccInfo.getCvvCode();

    CreditCardValidator visaValidator = new CreditCardValidator(CreditCardValidator.VISA);
    CreditCardValidator amexValidator = new CreditCardValidator(CreditCardValidator.AMEX);
    CreditCardValidator mcValidator = new CreditCardValidator(CreditCardValidator.MASTERCARD);
    CreditCardValidator discoverValidator = new CreditCardValidator(CreditCardValidator.DISCOVER);

    boolean validCard = false;
    String cardType = "UNKNOWN";
    if (visaValidator.isValid(ccNumber)) {
        validCard = true;
        cardType = "VISA";
    } else if (amexValidator.isValid(ccNumber)) {
        validCard = true;
        cardType = "AMEX";
    } else if (mcValidator.isValid(ccNumber)) {
        validCard = true;
        cardType = "MASTERCARD";
    } else if (discoverValidator.isValid(ccNumber)) {
        validCard = true;
        cardType = "DISCOVER";
    }

    DateTime expirationDate = new DateTime(expYear, expMonth, 1, 0, 0);
    boolean validDate = expirationDate.isAfterNow();

    boolean validCVV = !cvv.equals("000");

    PaymentResponseItem responseItem = new PaymentResponseItemImpl();
    responseItem.setTransactionTimestamp(SystemTime.asDate());
    responseItem.setTransactionSuccess(validDate && validCard && validCVV);
    responseItem.setAmountPaid(paymentInfo.getAmount());
    responseItem.setCurrency(paymentInfo.getOrder().getCurrency());
    if (responseItem.getTransactionSuccess()) {
        Map<String, String> additionalFields = new HashMap<String, String>();
        additionalFields.put(PaymentInfoAdditionalFieldType.NAME_ON_CARD.getType(), nameOnCard);
        additionalFields.put(PaymentInfoAdditionalFieldType.CARD_TYPE.getType(), cardType);
        additionalFields.put(PaymentInfoAdditionalFieldType.EXP_MONTH.getType(), expMonth + "");
        additionalFields.put(PaymentInfoAdditionalFieldType.EXP_YEAR.getType(), expYear + "");
        additionalFields.put(PaymentInfoAdditionalFieldType.LAST_FOUR.getType(),
                StringUtils.right(ccNumber, 4));
        paymentInfo.setAdditionalFields(additionalFields);
    }

    return responseItem;
}

From source file:org.broadleafcommerce.core.payment.service.NullPaymentGatewayTransactionServiceImpl.java

/**
 * Does minimal Credit Card Validation (luhn check and expiration date is after today).
 * Mimics the Response of a real Payment Gateway.
 *
 * @param creditCardDTO//from ww w  .j  ava 2s  .c  o  m
 * @return
 */
protected PaymentResponseDTO commonCreditCardProcessing(PaymentRequestDTO requestDTO,
        PaymentTransactionType paymentTransactionType) {
    PaymentResponseDTO responseDTO = new PaymentResponseDTO(PaymentType.CREDIT_CARD,
            NullPaymentGatewayType.NULL_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;
}