Monday, November 1, 2010

Credit Card Validation using Seeded API Call

Oracle has a seeded API which can be used to validate the Credit Card. Please find below the sample code to use.

DECLARE
-- each character specifies a possible filler characters in the credit
-- card number; i.e. a character that can safely be stripped away
p_fill_chars VARCHAR2(100) := '* -#';
p_cc_number VARCHAR2(200) := '111-111-11-11-1-1';
p_api_version NUMBER := 1.0;
p_init_msg_list VARCHAR2(2000) := ' ';
x_return_status VARCHAR2(2000);
x_msg_count NUMBER;
x_msg_data VARCHAR2(2000);
-- will hold the credit card number stripped of all characters except
-- digits; credit card numbers must be of this form for the GetCCType
-- and ValidateCC methods
v_clean_cc VARCHAR2(2000);
-- variable to be set by GetCCType method
v_cc_type IBY_CC_VALIDATE.CCType;
-- variable set by ValidateCC method; indicates if the credit card is
-- still usable
v_cc_valid BOOLEAN;
-- credit card expr date; rolled to the end of the month
-- by the ValidateCC method
v_expr_date DATE := SYSDATE();
BEGIN
-- the credit card number must first be stripped of all non-digits!!
DBMS_OUTPUT.PUT_LINE('Credit card Validation.');
  IBY_CC_VALIDATE.StripCC( p_api_version, p_init_msg_list, p_cc_number,
  p_fill_chars, x_return_status, x_msg_count, x_msg_data,
  v_clean_cc );
  IF x_return_status != FND_API.G_RET_STS_UNEXP_ERROR THEN
   IBY_CC_VALIDATE.GetCCType( p_api_version, p_init_msg_list, v_clean_cc,
   x_return_status, x_msg_count, x_msg_data, v_cc_type);
   IF x_return_status != FND_API.G_RET_STS_UNEXP_ERROR THEN
     IF v_cc_type=IBY_CC_VALIDATE.c_InvalidCC THEN
        DBMS_OUTPUT.PUT_LINE('Credit card number not a valid one.');
     ELSE
        DBMS_OUTPUT.PUT_LINE('Credit card number OK.');
        DBMS_OUTPUT.put_line('Credit Card Type is '||v_cc_type);
     END IF;
     IBY_CC_VALIDATE.ValidateCC( p_api_version, p_init_msg_list, v_clean_cc,
      v_expr_date, x_return_status, x_msg_count, x_msg_data, v_cc_valid);
      IF v_cc_valid THEN
         DBMS_OUTPUT.PUT_LINE('Credit card is valid.');
      ELSE
        DBMS_OUTPUT.PUT_LINE('Credit card number invalid or has expired.');
      END IF;
  END IF;
  END IF;
END;


The v_cc_type returned will be one of the following values.

c_InvalidCC CONSTANT CCType := -1; -- invalid; fits pattern but fails test
c_UnknownCC CONSTANT CCType := 0; -- fits no known pattern
c_McCC CONSTANT CCType := 1; -- Master Card
c_VisaCC CONSTANT CCType := 2; -- Visa
c_AmexCC CONSTANT CCType := 3; -- American Express
c_DClubCC CONSTANT CCType := 4; -- Diner's Club
c_DiscoverCC CONSTANT CCType := 5; -- The Discover Card
c_EnrouteCC CONSTANT CCType := 6; -- Enroute
c_JCBCC CONSTANT CCType := 7; -- JCB


Hope this should help to use within any custom applications that are built in any project.