Example usage for org.apache.commons.lang StringUtils substring

List of usage examples for org.apache.commons.lang StringUtils substring

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils substring.

Prototype

public static String substring(String str, int start) 

Source Link

Document

Gets a substring from the specified String avoiding exceptions.

Usage

From source file:webservice.restful.creditcard.CreditCardAuthorizationService.java

@POST
@Produces(MediaType.APPLICATION_JSON)/*from  w w  w.j a  v  a  2  s .c o  m*/
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response authorizeCC(@FormParam("ccNumber") String ccNumber, @FormParam("ccAmount") String ccAmount,
        @FormParam("referenceNum") String referenceNum, @FormParam("fromBankCode") String fromBankCode,
        @FormParam("ccTcode") String ccTcode, @FormParam("ccDescription") String ccDescription) {

    // get value from form
    System.out.println(". ");
    System.out.println("[MBS]");
    System.out.println("Received transaction request from VISA:");

    System.out.println(".        Reference Num: " + referenceNum);
    System.out.println(".        Credit Card number: " + ccNumber);
    System.out.println(".        Transaction Amount: " + ccAmount);
    System.out.println(".        Fund to Bank Code: " + fromBankCode);
    System.out.println("Authorizing with ccTcode: " + ccTcode);
    System.out.println("Authorizing with ccDescription: " + ccDescription);
    // return value
    Boolean authorized = true;
    // validate
    // if fail, code = -1;
    String code = "-1";
    // after it is done
    CreditCardDTO c = new CreditCardDTO();
    c.setAmount(ccAmount);
    c.setDescription(ccDescription);
    c.setCreditCardNumber(ccNumber);
    c.setTransactionCode(ccTcode);
    boolean validDailyStatus = false;
    boolean validMonthlyStatus = false;
    boolean validCreditLimit = false;

    if (!checkAbnormalAction(ccAmount)) {
        CreditCardAccount thisAccount = null;
        try {
            thisAccount = ccBean.getCreditCardAccountByCardNumber(ccNumber);
        } catch (Exception e) {
            System.out.println("No account retrieved");
        }

        if (thisAccount != null) {
            System.out.println("Validating daily transaction limit...");

            try {
                validDailyStatus = cardTransactionSessionBean
                        .validateCreditCardDailyTransactionLimit(thisAccount, Double.parseDouble(ccAmount));
            } catch (Exception e) {
                System.out.println("Exceed daily transaction limit!");
            }
            if (validDailyStatus == false) {
                System.out.println("Exceed daily transaction limit!");
                authorized = false;
                c.setMessage("Exceed daily transaction limit!");
            }

            try {
                validMonthlyStatus = cardTransactionSessionBean
                        .validateCreditCardMonthlyTransactionLimit(thisAccount, Double.parseDouble(ccAmount));
            } catch (Exception e) {
                System.out.println("Exceed monthly transaction limit!");
            }
            if (validMonthlyStatus == false) {
                System.out.println("Exceed monthly transaction limit!");
                authorized = false;
                c.setMessage("Exceed monthly transaction limit!");
            }

            try {
                validCreditLimit = cardTransactionSessionBean.validateCreditLimit(thisAccount,
                        Double.parseDouble(ccAmount));
            } catch (Exception e) {
                System.out.println("Exceed credit limit!");
            }
            if (validCreditLimit == false) {
                System.out.println("Exceed credit limit!");
                authorized = false;
                c.setMessage("Exceed credit limit!");
            }
        } else {
            authorized = false;
            System.out.println("No account retrieved");
            c.setMessage("No account retrieved!");
        }

        if (authorized) {
            code = PincodeGenerationUtils.generateRandom(true, 8);
            c.setMessage("Authorized");

            BillFundTransferRecord bft = new BillFundTransferRecord();
            bft.setReferenceNumber(referenceNum);
            bft.setBillReferenceNumber(ccNumber);
            bft.setFromBankCode("001");
            bft.setToBankCode(fromBankCode);
            bft.setAmount(new BigDecimal(ccAmount));
            bft.setSettled(Boolean.FALSE);
            bft.setCreationDate(new Date());

            billSessionBean.createBillFundTransferRecord(bft);

        }
        c.setAuthorizationCode(code);
    } else {
        c.setAuthorizationCode("-2");
        c.setMessage("Not Authorized!");
        // TODO: Send Notification message

        CreditCardAccount cca = ccBean.getCreditCardAccountByCardNumber(ccNumber);
        String phoneNumber = cca.getMainAccount().getCustomer().getPhone();
        System.out.println("######## " + phoneNumber + " #######");
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat dateOnly = new SimpleDateFormat("dd/MM/yyyy");

        String lastFourDigit = StringUtils.substring(ccNumber, ccNumber.length() - 4);
        System.out.println("print last 4 digit: " + lastFourDigit);
        String msg = "Card Transaction of SGD " + ccAmount + " was performed on your MBS account ending with "
                + lastFourDigit + " on " + dateOnly.format(currentDate.getTime())
                + ". If unauthorised, pls call " + "1800 222 2313.";
        emailServiceSessionBean.sendEmailUnauthorised(cca.getMainAccount().getCustomer().getEmail(), msg);
        //            SendNEXONMessage.sendText(phoneNumber, msg);
        //            SendTextMessage.sendText(phoneNumber, msg);

    }

    System.out.println("Sending back result with single code: " + c.getAuthorizationCode());

    String jsonString = new JSONObject(c).toString();
    return Response.ok(jsonString, MediaType.APPLICATION_JSON).build();
}