1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.math.*; 19 import java.io.Serializable ; 20 21 import org.compiere.util.*; 22 import org.compiere.process.*; 23 24 50 public final class MPayment extends X_C_Payment 51 implements ProcessCall 52 { 53 58 public MPayment (Properties ctx, int C_Payment_ID) 59 { 60 super (ctx, C_Payment_ID); 61 if (C_Payment_ID == 0) 63 { 64 setDocAction(DOCACTION_Complete); 65 setDocStatus(DOCSTATUS_Drafted); 66 setTrxType(TRXTYPE_Sales); 67 setR_AvsAddr (R_AVSZIP_Unavailable); 69 setR_AvsZip (R_AVSZIP_Unavailable); 70 setIsReceipt (true); 72 setProcessed(false); 73 setIsApproved (false); 74 setPosted (false); 75 setIsReconciled (false); 76 setIsAllocated(false); 77 setIsOnline (false); 78 setIsSelfService(false); 79 setIsDelayedCapture (false); 80 setPayAmt(Env.ZERO); 82 setDiscountAmt(Env.ZERO); 83 setTaxAmt(Env.ZERO); 84 setWriteOffAmt(Env.ZERO); 85 setIsOverUnderPayment (false); 86 setOverUnderAmt(Env.ZERO); 87 setDateTrx (new Timestamp(System.currentTimeMillis())); 89 } 90 } 92 97 public MPayment (Properties ctx, ResultSet rs) 98 { 99 super (ctx, rs); 100 } 102 private MPaymentProcessor[] m_mPaymentProcessors = null; 104 private MPaymentProcessor m_mPaymentProcessor = null; 105 private static Logger s_log = Logger.getCLogger (MPayment.class); 106 private String m_errorMessage = null; 107 108 109 110 111 123 public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber, 124 String creditCardVV, int creditCardExpMM, int creditCardExpYY) 125 { 126 setTenderType(TENDERTYPE_CreditCard); 127 setTrxType(TrxType); 128 setCreditCardType (creditCardType); 130 setCreditCardNumber (creditCardNumber); 131 setCreditCardVV (creditCardVV); 132 setCreditCardExpMM (creditCardExpMM); 133 setCreditCardExpYY (creditCardExpYY); 134 int check = validateCreditCardNumber(creditCardNumber, creditCardType).length() 136 + validateCreditCardExp(creditCardExpMM, creditCardExpYY).length(); 137 if (creditCardVV.length() > 0) 138 check += validateCreditCardVV(creditCardVV, creditCardType).length(); 139 return check == 0; 140 } 142 153 public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber, 154 String creditCardVV, String creditCardExp) 155 { 156 return setCreditCard(TrxType, creditCardType, creditCardNumber, 157 creditCardVV, getCreditCardExpMM(creditCardExp), getCreditCardExpYY(creditCardExp)); 158 } 160 167 public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt) 168 { 169 return setBankACH(C_BankAccount_ID, isReceipt); 170 } 172 181 public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt, String routingNo, String accountNo) 182 { 183 setTenderType (TENDERTYPE_ACH); 184 setIsReceipt (isReceipt); 185 if (C_BankAccount_ID > 0 187 && (routingNo == null || routingNo.length() == 0 || accountNo == null || accountNo.length() == 0)) 188 setBankAccountDetails(C_BankAccount_ID); 189 else 190 { 191 setC_BankAccount_ID(C_BankAccount_ID); 192 setRoutingNo (routingNo); 193 setAccountNo (accountNo); 194 } 195 setCheckNo (""); 196 int check = validateRoutingNo(routingNo).length() 198 + validateAccountNo(accountNo).length(); 199 return check == 0; 200 } 202 210 public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String checkNo) 211 { 212 return setBankCheck (C_BankAccount_ID, isReceipt, null, null, checkNo); 213 } 215 225 public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String routingNo, String accountNo, String checkNo) 226 { 227 setTenderType (TENDERTYPE_Check); 228 setIsReceipt (isReceipt); 229 if (C_BankAccount_ID > 0 231 && (routingNo == null || routingNo.length() == 0 || accountNo == null || accountNo.length() == 0)) 232 setBankAccountDetails(C_BankAccount_ID); 233 else 234 { 235 setC_BankAccount_ID(C_BankAccount_ID); 236 setRoutingNo (routingNo); 237 setAccountNo (accountNo); 238 } 239 setCheckNo (checkNo); 240 int check = validateRoutingNo(routingNo).length() 242 + validateAccountNo(accountNo).length() 243 + validateCheckNo(checkNo).length(); 244 return check == 0; } 247 252 public void setBankAccountDetails (int C_BankAccount_ID) 253 { 254 if (C_BankAccount_ID == 0) 255 return; 256 setC_BankAccount_ID(C_BankAccount_ID); 257 String sql = "SELECT b.RoutingNo, ba.AccountNo " 259 + "FROM C_BankAccount ba" 260 + " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID) " 261 + "WHERE C_BankAccount_ID=?"; 262 try 263 { 264 PreparedStatement pstmt = DB.prepareStatement(sql); 265 pstmt.setInt(1, C_BankAccount_ID); 266 ResultSet rs = pstmt.executeQuery(); 267 if (rs.next()) 268 { 269 setRoutingNo (rs.getString(1)); 270 setAccountNo (rs.getString(2)); 271 } 272 rs.close(); 273 pstmt.close(); 274 } 275 catch (SQLException e) 276 { 277 log.error("setsetBankAccountDetails", e); 278 } 279 } 281 291 public void setAccountAddress (String name, String street, 292 String city, String state, String zip, String country) 293 { 294 setA_Name (name); 295 setA_Street (street); 296 setA_City (city); 297 setA_State (state); 298 setA_Zip (zip); 299 setA_Country(country); 300 } 302 303 304 308 public boolean processOnline() 309 { 310 log.info ("processOnline - " + getPayAmt()); 311 setIsOnline(true); 313 setErrorMessage(null); 314 if (isApproved()) 316 { 317 log.info("processOnline - already processed - " + getR_Result() + " - " + getR_RespMsg()); 318 setErrorMessage("Payment already Processed"); 319 return true; 320 } 321 322 if (m_mPaymentProcessor == null) 323 setPaymentProcessor(); 324 if (m_mPaymentProcessor == null) 325 { 326 log.error("processOnline - No Payment Processor Model"); 327 setErrorMessage("No Payment Processor Model"); 328 return false; 329 } 330 331 boolean approved = false; 332 try 333 { 334 PaymentProcessor pp = PaymentProcessor.create(m_mPaymentProcessor, this); 335 if (pp == null) 336 setErrorMessage("No Payment Processor"); 337 else 338 { 339 approved = pp.processCC (); 340 if (approved) 341 setErrorMessage(null); 342 else 343 setErrorMessage("From " + getCreditCardName() + ": " + getR_RespMsg()); 344 } 345 } 346 catch (Exception e) 347 { 348 log.error("processOnline", e); 349 setErrorMessage("Payment Processor Error"); 350 } 351 setIsApproved(approved); 352 return approved; 353 } 355 364 public boolean startProcess (Properties ctx, ProcessInfo pi) 365 { 366 log.info("startProcess - " + pi.getRecord_ID()); 367 boolean retValue = false; 368 if (pi.getRecord_ID() != getID()) 370 { 371 log.error("startProcess - Not same Payment - " + pi.getRecord_ID()); 372 return false; 373 } 374 retValue = processOnline(); 376 save(); 377 return retValue; } 380 384 public boolean save () 385 { 386 log.info("save"); 387 if (getC_DocType_ID() == 0) 389 setC_DocType_ID(); 390 if (getDocumentNo() == null) 391 setDocumentNo(); 392 return super.save(); 393 } 395 399 public boolean post() 400 { 401 if (!save()) return false; 403 log.info("post"); 404 boolean retValue = true; 405 try 406 { 407 CallableStatement cstmt = DB.prepareCall("{CALL C_Payment_Post(NULL,?)}"); 408 cstmt.setInt(1, getID()); 409 cstmt.execute(); 410 cstmt.close(); 411 } 412 catch (SQLException e) 413 { 414 log.error("post", e); 415 retValue = false; 416 } 417 load(); 418 return retValue; 419 } 421 425 public boolean cancel() 426 { 427 log.info("cancel"); 428 if (!"CO".equals(getDocStatus())) 430 return false; 431 if (isApproved()) 433 log.warn("cancel - Payment was approved"); 434 setDocAction (DOCACTION_ReverseMinusCorrection); 436 save(); 437 return post(); 438 } 440 443 public void resetNew() 444 { 445 setC_Payment_ID(0); setValueNoCheck ("DocumentNo", null); 447 setDocumentNo(); 448 setDocAction(DOCACTION_Process); 449 setDocStatus(DOCSTATUS_Drafted); 450 setProcessed(false); 451 setPosted (false); 452 setIsReconciled (false); 453 setIsAllocated(false); 454 setIsDelayedCapture (false); 455 } 457 458 459 463 public void setErrorMessage(String errorMessage) 464 { 465 m_errorMessage = errorMessage; 466 } 468 472 public String getErrorMessage() 473 { 474 return m_errorMessage; 475 } 477 478 482 public void setC_BankAccount_ID (int C_BankAccount_ID) 483 { 484 if (C_BankAccount_ID == 0) 485 { 486 setPaymentProcessor(); 487 if (getC_BankAccount_ID() == 0) 488 throw new IllegalArgumentException ("MPayment.setC_BankAccount_ID - can't find Bank Account"); 489 } 490 else 491 super.setC_BankAccount_ID(C_BankAccount_ID); 492 } 494 498 public boolean setPaymentProcessor () 499 { 500 return setPaymentProcessor (getTenderType(), getCreditCardType()); 501 } 503 509 public boolean setPaymentProcessor (String tender, String CCType) 510 { 511 m_mPaymentProcessor = null; 512 if (m_mPaymentProcessors == null || m_mPaymentProcessors.length == 0) 514 m_mPaymentProcessors = findPaymentProcessors (getCtx(), tender, CCType, getAD_Client_ID(), 515 getC_Currency_ID(), getPayAmt()); 516 if (m_mPaymentProcessors == null || m_mPaymentProcessors.length == 0) 518 m_mPaymentProcessors = findPaymentProcessors (getCtx(), tender, CCType, getAD_Client_ID(), 519 getC_Currency_ID(), Env.ZERO); 520 if (m_mPaymentProcessors == null || m_mPaymentProcessors.length == 0) 521 return false; 522 523 for (int i = 0; i < m_mPaymentProcessors.length; i++) 525 { 526 if (m_mPaymentProcessors[i].accepts (tender, CCType)) 527 { 528 m_mPaymentProcessor = m_mPaymentProcessors[i]; 529 } 530 } 531 if (m_mPaymentProcessor != null) 532 setC_BankAccount_ID (m_mPaymentProcessor.getC_BankAccount_ID()); 533 return m_mPaymentProcessor != null; 535 } 537 547 protected static MPaymentProcessor[] findPaymentProcessors (Properties ctx, 548 String tender, String CCType, 549 int AD_Client_ID, int C_Currency_ID, BigDecimal Amt) 550 { 551 ArrayList list = new ArrayList(); 552 StringBuffer sql = new StringBuffer ("SELECT * " 553 + "FROM C_PaymentProcessor " 554 + "WHERE AD_Client_ID=? AND IsActive='Y'" + " AND (C_Currency_ID IS NULL OR C_Currency_ID=?)" + " AND (MinimumAmt IS NULL OR MinimumAmt = 0 OR MinimumAmt <= ?)"); if (TENDERTYPE_ACH.equals(tender)) 558 sql.append(" AND AcceptACH='Y'"); 559 else if (TENDERTYPE_Check.equals(tender)) 560 sql.append(" AND AcceptCheck='Y'"); 561 else if (CREDITCARDTYPE_Amex.equals(CCType)) 563 sql.append(" AND AcceptAMEX='Y'"); 564 else if (CREDITCARDTYPE_Visa.equals(tender)) 565 sql.append(" AND AcceptVISA='Y'"); 566 else if (CREDITCARDTYPE_MasterCard.equals(tender)) 567 sql.append(" AND AcceptMC='Y'"); 568 else if (CREDITCARDTYPE_Diners.equals(tender)) 569 sql.append(" AND AcceptDiners='Y'"); 570 else if (CREDITCARDTYPE_Discover.equals(tender)) 571 sql.append(" AND AcceptDiscover='Y'"); 572 else if (CREDITCARDTYPE_PurchaseCard.equals(tender)) 573 sql.append(" AND AcceptCORPORATE='Y'"); 574 try 576 { 577 PreparedStatement pstmt = DB.prepareStatement(sql.toString()); 578 pstmt.setInt(1, AD_Client_ID); 579 pstmt.setInt(2, C_Currency_ID); 580 pstmt.setBigDecimal(3, Amt); 581 ResultSet rs = pstmt.executeQuery(); 582 while (rs.next()) 583 list.add(new MPaymentProcessor (ctx, rs)); 584 rs.close(); 585 pstmt.close(); 586 } 587 catch (SQLException e) 588 { 589 s_log.error("findPaymentProcessor - " + sql, e); 590 return null; 591 } 592 if (list.size() == 0) 594 s_log.warn("findPaymentProcessor - not found - AD_Client_ID=" + AD_Client_ID 595 + ", C_Currency_ID=" + C_Currency_ID + ", Amt=" + Amt); 596 else 597 s_log.debug("findPaymentProcessor - #" + list.size() + " - AD_Client_ID=" + AD_Client_ID 598 + ", C_Currency_ID=" + C_Currency_ID + ", Amt=" + Amt); 599 MPaymentProcessor[] retValue = new MPaymentProcessor[list.size()]; 600 list.toArray(retValue); 601 return retValue; 602 } 604 608 public ValueNamePair[] getCreditCards () 609 { 610 return getCreditCards(getPayAmt()); 611 } 613 614 619 public ValueNamePair[] getCreditCards (BigDecimal amt) 620 { 621 try 622 { 623 if (m_mPaymentProcessors == null || m_mPaymentProcessors.length == 0) 624 m_mPaymentProcessors = findPaymentProcessors (getCtx (), null, null, getAD_Client_ID (), 625 getC_Currency_ID (), amt); 626 HashMap map = new HashMap (); for (int i = 0; i < m_mPaymentProcessors.length; i++) 629 { 630 if (m_mPaymentProcessors[i].isAcceptAMEX ()) 631 map.put (MPayment.CREDITCARDTYPE_Amex, getCreditCardPair (MPayment.CREDITCARDTYPE_Amex)); 632 if (m_mPaymentProcessors[i].isAcceptDiners ()) 633 map.put (MPayment.CREDITCARDTYPE_Diners, getCreditCardPair (MPayment.CREDITCARDTYPE_Diners)); 634 if (m_mPaymentProcessors[i].isAcceptDiscover ()) 635 map.put (MPayment.CREDITCARDTYPE_Discover, getCreditCardPair (MPayment.CREDITCARDTYPE_Discover)); 636 if (m_mPaymentProcessors[i].isAcceptMC ()) 637 map.put (MPayment.CREDITCARDTYPE_MasterCard, getCreditCardPair (MPayment.CREDITCARDTYPE_MasterCard)); 638 if (m_mPaymentProcessors[i].isAcceptCorporate ()) 639 map.put (MPayment.CREDITCARDTYPE_PurchaseCard, getCreditCardPair (MPayment.CREDITCARDTYPE_PurchaseCard)); 640 if (m_mPaymentProcessors[i].isAcceptVisa ()) 641 map.put (MPayment.CREDITCARDTYPE_Visa, getCreditCardPair (MPayment.CREDITCARDTYPE_Visa)); 642 } ValueNamePair[] retValue = new ValueNamePair[map.size ()]; 645 map.values ().toArray (retValue); 646 log.debug ("getCreditCards - #" + retValue.length + " - Processors=" + m_mPaymentProcessors.length); 647 return retValue; 648 } 649 catch (Exception ex) 650 { 651 ex.printStackTrace(); 652 return null; 653 } 654 } 656 661 private ValueNamePair getCreditCardPair (String CreditCardType) 662 { 663 return new ValueNamePair (CreditCardType, getCreditCardName(CreditCardType)); 664 } 666 667 668 672 public void setCreditCardNumber (String CreditCardNumber) 673 { 674 super.setCreditCardNumber (checkNumeric(CreditCardNumber)); 675 } 677 683 public static String validateCreditCardNumber (String creditCardNumber) 684 { 685 if (creditCardNumber == null || creditCardNumber.length() == 0) 686 return "CreditCardNumberError"; 687 688 711 712 String ccNumber1 = checkNumeric(creditCardNumber); 714 int ccLength = ccNumber1.length(); 715 StringBuffer buf = new StringBuffer (); 717 for (int i = ccLength; i != 0; i--) 718 buf.append(ccNumber1.charAt(i-1)); 719 String ccNumber = buf.toString(); 720 721 int sum = 0; 722 for (int i = 0; i < ccLength; i++) 723 { 724 int digit = Character.getNumericValue(ccNumber.charAt(i)); 725 if (i % 2 == 1) 726 { 727 digit *= 2; 728 if (digit > 9) 729 digit -= 9; 730 } 731 sum += digit; 732 } 733 if (sum % 10 == 0) 734 return ""; 735 736 s_log.debug("validateCreditCardNumber - " + creditCardNumber + " -> " 737 + ccNumber + ", Luhn=" + sum); 738 return "CreditCardNumberError"; 739 } 741 748 public static String validateCreditCardNumber (String creditCardNumber, String creditCardType) 749 { 750 if (creditCardNumber == null || creditCardType == null) 751 return "CreditCardNumberError"; 752 753 756 String ccStartList = ""; String ccLengthList = ""; if (creditCardType.equals(CREDITCARDTYPE_MasterCard)) 760 { 761 ccStartList = "51,52,53,54,55"; 762 ccLengthList = "16"; 763 } 764 else if (creditCardType.equals(CREDITCARDTYPE_Visa)) 765 { 766 ccStartList = "4"; 767 ccLengthList = "13,16"; 768 } 769 else if (creditCardType.equals(CREDITCARDTYPE_Amex)) 770 { 771 ccStartList = "34,37"; 772 ccLengthList = "15"; 773 } 774 else if (creditCardType.equals(CREDITCARDTYPE_Discover)) 775 { 776 ccStartList = "6011"; 777 ccLengthList = "16"; 778 } 779 else if (creditCardType.equals(CREDITCARDTYPE_Diners)) 780 { 781 ccStartList = "300,301,302,303,304,305,36,38"; 782 ccLengthList = "14"; 783 } 784 else 785 { 786 ccStartList = "2014,2149"; 788 ccLengthList = "15"; 789 ccStartList += ",3088,3096,3112,3158,3337,3528"; 791 ccLengthList += ",16"; 792 ccStartList += ",2131,1800"; 794 ccLengthList += ",15"; 795 } 796 797 String ccNumber = checkNumeric(creditCardNumber); 799 800 803 int ccLength = ccNumber.length(); 804 boolean ccLengthOK = false; 805 StringTokenizer st = new StringTokenizer(ccLengthList, ",", false); 806 while (st.hasMoreTokens() && !ccLengthOK) 807 { 808 int l = Integer.parseInt(st.nextToken()); 809 if (ccLength == l) 810 ccLengthOK = true; 811 } 812 if (!ccLengthOK) 813 { 814 s_log.debug("validateCreditCardNumber Length=" 815 + ccLength + " <> " + ccLengthList); 816 return "CreditCardNumberError"; 817 } 818 819 822 boolean ccIdentified = false; 823 st = new StringTokenizer(ccStartList, ",", false); 824 while (st.hasMoreTokens() && !ccIdentified) 825 { 826 if (ccNumber.startsWith(st.nextToken())) 827 ccIdentified = true; 828 } 829 if (!ccIdentified) 830 s_log.debug("validateCreditCardNumber Type=" 831 + creditCardType + " <> " + ccStartList); 832 833 String check = validateCreditCardNumber(ccNumber); 835 if (check.length() != 0) 836 return check; 837 if (!ccIdentified) 838 return "CreditCardNumberProblem?"; 839 return ""; 840 } 842 843 844 848 public void setCreditCardVV(String newCreditCardVV) 849 { 850 super.setCreditCardVV (checkNumeric(newCreditCardVV)); 851 } 853 858 public static String validateCreditCardVV (String creditCardVV) 859 { 860 if (creditCardVV == null) 861 return ""; 862 int length = checkNumeric(creditCardVV).length(); 863 if (length == 3 || length == 4) 864 return ""; 865 try 866 { 867 Integer.parseInt (creditCardVV); 868 return ""; 869 } 870 catch (NumberFormatException ex) 871 { 872 s_log.debug("validateCreditCardVV - " + ex); 873 } 874 s_log.debug("validateCreditCardVV - length=" + length); 875 return "CreditCardVVError"; 876 } 878 884 public static String validateCreditCardVV (String creditCardVV, String creditCardType) 885 { 886 if (creditCardVV == null || creditCardVV.length() == 0 888 || creditCardType == null || creditCardType.length() == 0) 889 return ""; 890 891 int length = checkNumeric(creditCardVV).length(); 892 893 if (creditCardType.equals(CREDITCARDTYPE_Amex)) 895 { 896 if (length == 4) 897 { 898 try 899 { 900 Integer.parseInt (creditCardVV); 901 return ""; 902 } 903 catch (NumberFormatException ex) 904 { 905 s_log.debug("validateCreditCardVV - " + ex); 906 } 907 } 908 s_log.debug("validateCreditCardVV(4) CC=" + creditCardType + ", length=" + length); 909 return "CreditCardVVError"; 910 } 911 if (creditCardType.equals(CREDITCARDTYPE_Visa) || creditCardType.equals(CREDITCARDTYPE_MasterCard)) 913 { 914 if (length == 3) 915 { 916 try 917 { 918 Integer.parseInt (creditCardVV); 919 return ""; 920 } 921 catch (NumberFormatException ex) 922 { 923 s_log.debug("validateCreditCardVV - " + ex); 924 } 925 } 926 s_log.debug("validateCreditCardVV(3) CC=" + creditCardType + ", length=" + length); 927 return "CreditCardVVError"; 928 } 929 930 return ""; 932 } 934 935 936 940 public void setCreditCardExpMM (int CreditCardExpMM) 941 { 942 if (CreditCardExpMM < 1 || CreditCardExpMM > 12) 943 ; 944 else 945 super.setCreditCardExpMM (CreditCardExpMM); 946 } 948 952 public void setCreditCardExpYY (int newCreditCardExpYY) 953 { 954 int CreditCardExpYY = newCreditCardExpYY; 955 if (newCreditCardExpYY > 1999) 956 CreditCardExpYY = newCreditCardExpYY-2000; 957 super.setCreditCardExpYY(CreditCardExpYY); 958 } 960 965 public boolean setCreditCardExp (String mmyy) 966 { 967 if (validateCreditCardExp(mmyy).length() != 0) 968 return false; 969 String exp = checkNumeric(mmyy); 971 String mmStr = exp.substring(0,2); 972 String yyStr = exp.substring(2,4); 973 setCreditCardExpMM (Integer.parseInt(mmStr)); 974 setCreditCardExpYY (Integer.parseInt(yyStr)); 975 return true; 976 } 978 983 public static String validateCreditCardExp (String mmyy) 984 { 985 String exp = checkNumeric(mmyy); 986 if (exp.length() != 4) 987 return "CreditCardExpFormat"; 988 String mmStr = exp.substring(0,2); 990 String yyStr = exp.substring(2,4); 991 int mm = 0; 993 int yy = 0; 994 try 995 { 996 mm = Integer.parseInt(mmStr); 997 yy = Integer.parseInt(yyStr); 998 } 999 catch (Exception e) 1000 { 1001 return "CreditCardExpFormat"; 1002 } 1003 return validateCreditCardExp(mm,yy); 1004 } 1006 1011 public static int getCreditCardExpMM (String mmyy) 1012 { 1013 String mmStr = mmyy.substring(0,2); 1014 int mm = 0; 1015 try 1016 { 1017 mm = Integer.parseInt(mmStr); 1018 } 1019 catch (Exception e) 1020 { 1021 } 1022 return mm; 1023 } 1025 1030 public static int getCreditCardExpYY (String mmyy) 1031 { 1032 String yyStr = mmyy.substring(2); 1033 int yy = 0; 1034 try 1035 { 1036 yy = Integer.parseInt(yyStr); 1037 } 1038 catch (Exception e) 1039 { 1040 } 1041 return yy; 1042 } 1044 1050 public static String validateCreditCardExp (int mm, int yy) 1051 { 1052 if (mm < 1 || mm > 12) 1053 return "CreditCardExpMonth"; 1054 1057 Calendar cal = Calendar.getInstance(); 1059 int year = cal.get(Calendar.YEAR) - 2000; int month = cal.get(Calendar.MONTH) + 1; if (yy < year) 1063 return "CreditCardExpired"; 1064 else if (yy == year && mm < month) 1065 return "CreditCardExpired"; 1066 return ""; 1067 } 1069 1073 public String getCreditCardExp() 1074 { 1075 String mm = String.valueOf(getCreditCardExpMM()); 1076 String yy = String.valueOf(getCreditCardExpYY()); 1077 1078 StringBuffer retValue = new StringBuffer (); 1079 if (mm.length() == 1) 1080 retValue.append("0"); 1081 retValue.append(mm); 1082 if (yy.length() == 1) 1083 retValue.append("0"); 1084 retValue.append(yy); 1085 return (retValue.toString()); 1087 } 1089 1093 public void setMicr (String MICR) 1094 { 1095 super.setMicr (checkNumeric(MICR)); 1096 } 1098 1102 public void setRoutingNo(String RoutingNo) 1103 { 1104 super.setRoutingNo (checkNumeric(RoutingNo)); 1105 } 1107 1112 public static String validateRoutingNo (String routingNo) 1113 { 1114 int length = checkNumeric(routingNo).length(); 1115 if (length == 8 || length == 9) 1118 return ""; 1119 return "PaymentBankRoutingNotValid"; 1120 } 1122 1126 public void setAccountNo (String AccountNo) 1127 { 1128 super.setAccountNo (checkNumeric(AccountNo)); 1129 } 1131 1136 public static String validateAccountNo (String AccountNo) 1137 { 1138 int length = checkNumeric(AccountNo).length(); 1139 if (length > 0) 1140 return ""; 1141 return "PaymentBankAccountNotValid"; 1142 } 1144 1148 public void setCheckNo(String CheckNo) 1149 { 1150 super.setCheckNo(checkNumeric(CheckNo)); 1151 } 1153 1158 public static String validateCheckNo (String CheckNo) 1159 { 1160 int length = checkNumeric(CheckNo).length(); 1161 if (length > 0) 1162 return ""; 1163 return "PaymentBankCheckNotValid"; 1164 } 1166 1169 protected void setDocumentNo() 1170 { 1171 String DocumentNo = null; 1172 if (TENDERTYPE_CreditCard.equals(getTenderType())) 1173 DocumentNo = getCreditCardType() + " " + getCreditCardNumber() + " " + getCreditCardExpMM() + "/" + getCreditCardExpYY(); 1174 else 1175 { 1176 DocumentNo = getRoutingNo() + " " + getAccountNo(); 1177 if (TENDERTYPE_Check.equals(getTenderType())) 1178 DocumentNo += " " + getCheckNo(); 1179 } 1180 if (DocumentNo.length() == 0) 1181 DocumentNo = "."; 1182 if (DocumentNo.length() > 30) 1183 DocumentNo = DocumentNo.substring(0,29); 1184 super.setDocumentNo(DocumentNo); 1186 } 1188 1190 1194 void setR_Info (String R_Info) 1195 { 1196 if (R_Info != null && R_Info.length() > 2000) 1197 super.setR_Info (R_Info.substring(0,1999)); 1198 else 1199 super.setR_Info (R_Info); 1200 } 1202 1206 void setR_RespMsg (String R_RespMsg) 1207 { 1208 if (R_RespMsg != null && R_RespMsg.length() > 60) 1209 super.setR_RespMsg (R_RespMsg.substring(0,59)); 1210 else 1211 super.setR_RespMsg (R_RespMsg); 1212 } 1214 1218 public void setPayAmt (BigDecimal PayAmt) 1219 { 1220 super.setPayAmt(PayAmt == null ? Env.ZERO : PayAmt); 1221 } 1223 1229 public void setAmount (int C_Currency_ID, BigDecimal payAmt) 1230 { 1231 if (C_Currency_ID == 0) 1232 C_Currency_ID = MClient.get(getCtx()).getC_Currency_ID(); 1233 setC_Currency_ID(C_Currency_ID); 1234 setPayAmt(payAmt); 1235 } 1237 1241 public void setDiscountAmt (BigDecimal DiscountAmt) 1242 { 1243 super.setDiscountAmt (DiscountAmt == null ? Env.ZERO : DiscountAmt); 1244 } 1246 1250 public void setWriteOffAmt (BigDecimal WriteOffAmt) 1251 { 1252 super.setWriteOffAmt (WriteOffAmt == null ? Env.ZERO : WriteOffAmt); 1253 } 1255 1259 public void setOverUnderAmt (BigDecimal OverUnderAmt) 1260 { 1261 super.setOverUnderAmt (OverUnderAmt == null ? Env.ZERO : OverUnderAmt); 1262 } 1264 1268 public void setTaxAmt (BigDecimal TaxAmt) 1269 { 1270 super.setTaxAmt (TaxAmt == null ? Env.ZERO : TaxAmt); 1271 } 1273 1277 public void setBP_BankAccount (MBP_BankAccount ba) 1278 { 1279 log.debug("setBP_BankAccount - " + ba); 1280 if (ba == null) 1281 return; 1282 setC_BPartner_ID(ba.getC_BPartner_ID()); 1283 setAccountAddress(ba.getA_Name(), ba.getA_Street(), ba.getA_City(), 1284 ba.getA_State(), ba.getA_Zip(), ba.getA_Country()); 1285 setA_EMail(ba.getA_EMail()); 1286 setA_Ident_DL(ba.getA_Ident_DL()); 1287 setA_Ident_SSN(ba.getA_Ident_SSN()); 1288 if (ba.getCreditCardType() != null) 1290 setCreditCardType(ba.getCreditCardType()); 1291 if (ba.getCreditCardNumber() != null) 1292 setCreditCardNumber(ba.getCreditCardNumber()); 1293 if (ba.getCreditCardExpMM() != 0) 1294 setCreditCardExpMM(ba.getCreditCardExpMM()); 1295 if (ba.getCreditCardExpYY() != 0) 1296 setCreditCardExpYY(ba.getCreditCardExpYY()); 1297 if (ba.getCreditCardVV() != null) 1298 setCreditCardVV(ba.getCreditCardVV()); 1299 if (ba.getAccountNo() != null) 1301 setAccountNo(ba.getAccountNo()); 1302 if (ba.getRoutingNo() != null) 1303 setRoutingNo(ba.getRoutingNo()); 1304 } 1306 1311 public boolean saveToBP_BankAccount (MBP_BankAccount ba) 1312 { 1313 if (ba == null) 1314 return false; 1315 ba.setA_Name(getA_Name()); 1316 ba.setA_Street(getA_Street()); 1317 ba.setA_City(getA_City()); 1318 ba.setA_State(getA_State()); 1319 ba.setA_Zip(getA_Zip()); 1320 ba.setA_Country(getA_Country()); 1321 ba.setA_EMail(getA_EMail()); 1322 ba.setA_Ident_DL(getA_Ident_DL()); 1323 ba.setA_Ident_SSN(getA_Ident_SSN()); 1324 ba.setCreditCardType(getCreditCardType()); 1326 ba.setCreditCardNumber(getCreditCardNumber()); 1327 ba.setCreditCardExpMM(getCreditCardExpMM()); 1328 ba.setCreditCardExpYY(getCreditCardExpYY()); 1329 ba.setCreditCardVV(getCreditCardVV()); 1330 if (getAccountNo() != null) 1332 ba.setAccountNo(getAccountNo()); 1333 if (getRoutingNo() != null) 1334 ba.setRoutingNo(getRoutingNo()); 1335 ba.setR_AvsAddr(getR_AvsAddr()); 1337 ba.setR_AvsZip(getR_AvsZip()); 1338 boolean ok = ba.save(); 1340 log.debug("saveToBP_BankAccount - " + ba); 1341 return ok; 1342 } 1344 1347 private void setC_DocType_ID () 1348 { 1349 setC_DocType_ID(isReceipt()); 1350 } 1352 1356 public void setC_DocType_ID (boolean isReceipt) 1357 { 1358 setIsReceipt(isReceipt); 1359 String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType=?"; 1360 try 1361 { 1362 PreparedStatement pstmt = DB.prepareStatement(sql); 1363 pstmt.setInt(1, getAD_Client_ID()); 1364 if (isReceipt) 1365 pstmt.setString(2, "ARR"); 1366 else 1367 pstmt.setString(2, "APP"); 1368 ResultSet rs = pstmt.executeQuery(); 1369 if (rs.next()) 1370 setC_DocType_ID(rs.getInt(1)); 1371 else 1372 log.warn ("setDocType - NOT found - isReceipt=" + isReceipt); 1373 rs.close(); 1374 pstmt.close(); 1375 } 1376 catch (SQLException e) 1377 { 1378 log.error("setDocType", e); 1379 } 1380 } 1382 1386 public void setC_Invoice_ID (int C_Invoice_ID) 1387 { 1388 if (C_Invoice_ID == 0) 1389 setValue ("C_Invoice_ID", null); 1390 else 1391 super.setC_Invoice_ID(C_Invoice_ID); 1392 } 1394 1398 public String getCurrencyISO() 1399 { 1400 return MCurrency.getISO_Code (getCtx(), getC_Currency_ID()); 1401 } 1403 1407 public String getDocStatusName() 1408 { 1409 return MRef_List.getListName(getCtx(), 131, getDocStatus()); 1410 } 1412 1416 public String getCreditCardName() 1417 { 1418 return getCreditCardName(getCreditCardType()); 1419 } 1421 1426 public String getCreditCardName(String CreditCardType) 1427 { 1428 if (CreditCardType == null) 1429 return "--"; 1430 else if (CREDITCARDTYPE_MasterCard.equals(CreditCardType)) 1431 return "MasterCard"; 1432 else if (CREDITCARDTYPE_Visa.equals(CreditCardType)) 1433 return "Visa"; 1434 else if (CREDITCARDTYPE_Amex.equals(CreditCardType)) 1435 return "Amex"; 1436 else if (CREDITCARDTYPE_ATM.equals(CreditCardType)) 1437 return "ATM"; 1438 else if (CREDITCARDTYPE_Diners.equals(CreditCardType)) 1439 return "Diners"; 1440 else if (CREDITCARDTYPE_Discover.equals(CreditCardType)) 1441 return "Discover"; 1442 else if (CREDITCARDTYPE_PurchaseCard.equals(CreditCardType)) 1443 return "PurchaseCard"; 1444 return "?" + CreditCardType + "?"; 1445 } 1447 1448 1449 1454 public static String checkNumeric (String data) 1455 { 1456 if (data == null || data.length() == 0) 1457 return ""; 1458 StringBuffer sb = new StringBuffer (); 1460 for (int i = 0; i < data.length(); i++) 1461 { 1462 if (Character.isDigit(data.charAt(i))) 1463 sb.append(data.charAt(i)); 1464 } 1465 return sb.toString(); 1466 } 1468} | Popular Tags |