KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MPayment


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19 import java.io.Serializable JavaDoc;
20
21 import org.compiere.util.*;
22 import org.compiere.process.*;
23
24 /**
25  * Payment Model.
26  * - retrieve and create payments for invoice
27  * <pre>
28  * Event chain
29  * - Payment inserted
30  * C_Payment_Trg fires
31  * update DocumentNo with payment summary
32  * - Payment posted (C_Payment_Post)
33  * create allocation line
34  * C_Allocation_Trg fires
35  * Update C_BPartner Open Item Amount
36  * update invoice (IsPaid)
37  * link invoice-payment if batch
38  *
39  * Lifeline:
40  * - Created by VPayment or directly
41  * - When changed in VPayment
42  * - old payment is reversed
43  * - new payment created
44  *
45  * When Payment is posed, the Allocation is made
46  * </pre>
47  * @author Jorg Janke
48  * @version $Id: MPayment.java,v 1.30 2003/10/04 03:51:50 jjanke Exp $
49  */

50 public final class MPayment extends X_C_Payment
51     implements ProcessCall
52 {
53     /**
54      * Default Constructor
55      * @param ctx context
56      * @param C_Payment_ID payment to load, (0 create new payment)
57      */

58     public MPayment (Properties ctx, int C_Payment_ID)
59     {
60         super (ctx, C_Payment_ID);
61         // New
62
if (C_Payment_ID == 0)
63         {
64             setDocAction(DOCACTION_Complete);
65             setDocStatus(DOCSTATUS_Drafted);
66             setTrxType(TRXTYPE_Sales);
67             //
68
setR_AvsAddr (R_AVSZIP_Unavailable);
69             setR_AvsZip (R_AVSZIP_Unavailable);
70             //
71
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             //
81
setPayAmt(Env.ZERO);
82             setDiscountAmt(Env.ZERO);
83             setTaxAmt(Env.ZERO);
84             setWriteOffAmt(Env.ZERO);
85             setIsOverUnderPayment (false);
86             setOverUnderAmt(Env.ZERO);
87             //
88
setDateTrx (new Timestamp(System.currentTimeMillis()));
89         }
90     } // MPayment
91

92     /**
93      * Load Constructor
94      * @param ctx context
95      * @param rs result set record
96      */

97     public MPayment (Properties ctx, ResultSet rs)
98     {
99         super (ctx, rs);
100     } // MPayment
101

102     // Temporary
103
private MPaymentProcessor[] m_mPaymentProcessors = null;
104     private MPaymentProcessor m_mPaymentProcessor = null;
105     private static Logger s_log = Logger.getCLogger (MPayment.class);
106     private String JavaDoc m_errorMessage = null;
107
108
109     /*************************************************************************/
110
111     /**
112      * Set Credit Card.
113      * Need to set PatmentProcessor after Amount/Currency Set
114      *
115      * @param TrxType Transaction Type see TRX_
116      * @param creditCardType CC type
117      * @param creditCardNumber CC number
118      * @param creditCardVV CC verification
119      * @param creditCardExpMM CC Exp MM
120      * @param creditCardExpYY CC Exp YY
121      * @return true if valid
122      */

123     public boolean setCreditCard (String JavaDoc TrxType, String JavaDoc creditCardType, String JavaDoc creditCardNumber,
124         String JavaDoc creditCardVV, int creditCardExpMM, int creditCardExpYY)
125     {
126         setTenderType(TENDERTYPE_CreditCard);
127         setTrxType(TrxType);
128         //
129
setCreditCardType (creditCardType);
130         setCreditCardNumber (creditCardNumber);
131         setCreditCardVV (creditCardVV);
132         setCreditCardExpMM (creditCardExpMM);
133         setCreditCardExpYY (creditCardExpYY);
134         //
135
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     } // setCreditCard
141

142     /**
143      * Set Credit Card - Exp.
144      * Need to set PatmentProcessor after Amount/Currency Set
145      *
146      * @param TrxType Transaction Type see TRX_
147      * @param creditCardType CC type
148      * @param creditCardNumber CC number
149      * @param creditCardVV CC verification
150      * @param creditCardExp CC Exp
151      * @return true if valid
152      */

153     public boolean setCreditCard (String JavaDoc TrxType, String JavaDoc creditCardType, String JavaDoc creditCardNumber,
154         String JavaDoc creditCardVV, String JavaDoc creditCardExp)
155     {
156         return setCreditCard(TrxType, creditCardType, creditCardNumber,
157             creditCardVV, getCreditCardExpMM(creditCardExp), getCreditCardExpYY(creditCardExp));
158     } // setCreditCard
159

160     /**
161      * Set ACH BankAccount Info
162      *
163      * @param C_BankAccount_ID bank account
164      * @param isReceipt true if receipt
165      * @return true if valid
166      */

167     public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt)
168     {
169         return setBankACH(C_BankAccount_ID, isReceipt);
170     } // setBankACH
171

172     /**
173      * Set ACH BankAccount Info
174      *
175      * @param C_BankAccount_ID bank account
176      * @param isReceipt true if receipt
177      * @param routingNo routing
178      * @param accountNo account
179      * @return true if valid
180      */

181     public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt, String JavaDoc routingNo, String JavaDoc accountNo)
182     {
183         setTenderType (TENDERTYPE_ACH);
184         setIsReceipt (isReceipt);
185         //
186
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         //
197
int check = validateRoutingNo(routingNo).length()
198             + validateAccountNo(accountNo).length();
199         return check == 0;
200     } // setBankACH
201

202     /**
203      * Set Check BankAccount Info
204      *
205      * @param C_BankAccount_ID bank account
206      * @param isReceipt true if receipt
207      * @param checkNo chack no
208      * @return true if valid
209      */

210     public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String JavaDoc checkNo)
211     {
212         return setBankCheck (C_BankAccount_ID, isReceipt, null, null, checkNo);
213     } // setBankCheck
214

215     /**
216      * Set Check BankAccount Info
217      *
218      * @param C_BankAccount_ID bank account
219      * @param isReceipt true if receipt
220      * @param routingNo routing no
221      * @param accountNo account no
222      * @param checkNo chack no
223      * @return true if valid
224      */

225     public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String JavaDoc routingNo, String JavaDoc accountNo, String JavaDoc checkNo)
226     {
227         setTenderType (TENDERTYPE_Check);
228         setIsReceipt (isReceipt);
229         //
230
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         //
241
int check = validateRoutingNo(routingNo).length()
242             + validateAccountNo(accountNo).length()
243             + validateCheckNo(checkNo).length();
244         return check == 0; // no error message
245
} // setBankCheck
246

247     /**
248      * Set Bank Account Details.
249      * Look up Routing No & Bank Acct No
250      * @param C_BankAccount_ID bank account
251      */

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         //
258
String JavaDoc 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     } // setBankAccountDetails
280

281     /**
282      * Set Account Address
283      *
284      * @param name name
285      * @param street street
286      * @param city city
287      * @param state state
288      * @param zip zip
289      * @param country country
290      */

291     public void setAccountAddress (String JavaDoc name, String JavaDoc street,
292         String JavaDoc city, String JavaDoc state, String JavaDoc zip, String JavaDoc 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     } // setAccountAddress
301

302     /*************************************************************************/
303
304     /**
305      * Process Payment
306      * @return true if approved
307      */

308     public boolean processOnline()
309     {
310         log.info ("processOnline - " + getPayAmt());
311         //
312
setIsOnline(true);
313         setErrorMessage(null);
314         // prevent charging twice
315
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 JavaDoc e)
347         {
348             log.error("processOnline", e);
349             setErrorMessage("Payment Processor Error");
350         }
351         setIsApproved(approved);
352         return approved;
353     } // processOnline
354

355     /**
356      * Process Online Payment.
357      * implements ProcessCall after standard constructor
358      * Called when pressing the Process_Online button in C_Payment
359      *
360      * @param ctx Context
361      * @param pi Process Info
362      * @return true if the next process should be performed
363      */

364     public boolean startProcess (Properties ctx, ProcessInfo pi)
365     {
366         log.info("startProcess - " + pi.getRecord_ID());
367         boolean retValue = false;
368         //
369
if (pi.getRecord_ID() != getID())
370         {
371             log.error("startProcess - Not same Payment - " + pi.getRecord_ID());
372             return false;
373         }
374         // Process it
375
retValue = processOnline();
376         save();
377         return retValue; // Payment processed
378
} // startProcess
379

380     /**
381      * Save
382      * @return true if success
383      */

384     public boolean save ()
385     {
386         log.info("save");
387         //
388
if (getC_DocType_ID() == 0)
389             setC_DocType_ID();
390         if (getDocumentNo() == null)
391             setDocumentNo();
392         return super.save();
393     } // save
394

395     /**
396      * Post Payment
397      * @return true if success
398      */

399     public boolean post()
400     {
401         if (!save()) // save first
402
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     } // post
420

421     /**
422      * Cancel Payment
423      * @return true if cancelled
424      */

425     public boolean cancel()
426     {
427         log.info("cancel");
428         // Check Status
429
if (!"CO".equals(getDocStatus()))
430             return false;
431         // Paid
432
if (isApproved())
433             log.warn("cancel - Payment was approved");
434         // Set Action
435
setDocAction (DOCACTION_ReverseMinusCorrection);
436         save();
437         return post();
438     } // Cancel
439

440     /**
441      * Reset Payment to new status
442      */

443     public void resetNew()
444     {
445         setC_Payment_ID(0); // forces new Record
446
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     } // resetNew
456

457     /*************************************************************************/
458
459     /**
460      * Set Error Message
461      * @param errorMessage error message
462      */

463     public void setErrorMessage(String JavaDoc errorMessage)
464     {
465         m_errorMessage = errorMessage;
466     } // setErrorMessage
467

468     /**
469      * Get Error Message
470      * @return error message
471      */

472     public String JavaDoc getErrorMessage()
473     {
474         return m_errorMessage;
475     } // getErrorMessage
476

477
478     /**
479      * Set Bank Account for Payment.
480      * @param C_BankAccount_ID C_BankAccount_ID
481      */

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 JavaDoc("MPayment.setC_BankAccount_ID - can't find Bank Account");
489         }
490         else
491             super.setC_BankAccount_ID(C_BankAccount_ID);
492     } // setC_BankAccount_ID
493

494     /**
495      * Set BankAccount and PaymentProcessor
496      * @return true if found
497      */

498     public boolean setPaymentProcessor ()
499     {
500         return setPaymentProcessor (getTenderType(), getCreditCardType());
501     } // setPaymentProcessor
502

503     /**
504      * Set BankAccount and PaymentProcessor
505      * @param tender TenderType see TENDER_
506      * @param CCType CC Type see CC_
507      * @return true if found
508      */

509     public boolean setPaymentProcessor (String JavaDoc tender, String JavaDoc CCType)
510     {
511         m_mPaymentProcessor = null;
512         // Get Processor List
513
if (m_mPaymentProcessors == null || m_mPaymentProcessors.length == 0)
514             m_mPaymentProcessors = findPaymentProcessors (getCtx(), tender, CCType, getAD_Client_ID(),
515                 getC_Currency_ID(), getPayAmt());
516         // Relax Amount
517
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         // Find the first right one
524
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         //
534
return m_mPaymentProcessor != null;
535     } // setPaymentProcessor
536

537     /**
538      * Get BankAccount & PaymentProcessor
539      * @param ctx context
540      * @param tender optional Tender see TENDER_
541      * @param CCType optional CC Type see CC_
542      * @param AD_Client_ID Client
543      * @param C_Currency_ID Currency (ignored)
544      * @param Amt Amount (ignored)
545      * @return Array of BankAccount[0] & PaymentProcessor[1] or null
546      */

547     protected static MPaymentProcessor[] findPaymentProcessors (Properties ctx,
548         String JavaDoc tender, String JavaDoc CCType,
549         int AD_Client_ID, int C_Currency_ID, BigDecimal Amt)
550     {
551         ArrayList list = new ArrayList();
552         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT * "
553             + "FROM C_PaymentProcessor "
554             + "WHERE AD_Client_ID=? AND IsActive='Y'" // #1
555
+ " AND (C_Currency_ID IS NULL OR C_Currency_ID=?)" // #2
556
+ " AND (MinimumAmt IS NULL OR MinimumAmt = 0 OR MinimumAmt <= ?)"); // #3
557
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         // CreditCards
562
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         //
575
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         //
593
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     } // findPaymentProcessor
603

604     /**
605      * Get Accepted Credit Cards for PayAmt (default 0)
606      * @return credit cards
607      */

608     public ValueNamePair[] getCreditCards ()
609     {
610         return getCreditCards(getPayAmt());
611     } // getCreditCards
612

613
614     /**
615      * Get Accepted Credit Cards for amount
616      * @param amt trx amount
617      * @return credit cards
618      */

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                 //
627
HashMap map = new HashMap (); // to eliminate duplicates
628
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             } // for all payment processors
643
//
644
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 JavaDoc ex)
650         {
651             ex.printStackTrace();
652             return null;
653         }
654     } // getCreditCards
655

656     /**
657      * Get Type and name pair
658      * @param CreditCardType credit card Type
659      * @return pair
660      */

661     private ValueNamePair getCreditCardPair (String JavaDoc CreditCardType)
662     {
663         return new ValueNamePair (CreditCardType, getCreditCardName(CreditCardType));
664     } // getCreditCardPair
665

666     /*************************************************************************/
667
668     /**
669      * Credit Card Number
670      * @param CreditCardNumber CreditCard Number
671      */

672     public void setCreditCardNumber (String JavaDoc CreditCardNumber)
673     {
674         super.setCreditCardNumber (checkNumeric(CreditCardNumber));
675     } // setCreditCardNumber
676

677     /**
678      * Validate Credit Card Number.
679      * - Based on LUHN formula
680      * @param creditCardNumber credit card number
681      * @return "" or Error AD_Message
682      */

683     public static String JavaDoc validateCreditCardNumber (String JavaDoc creditCardNumber)
684     {
685         if (creditCardNumber == null || creditCardNumber.length() == 0)
686             return "CreditCardNumberError";
687
688         /**
689          * 1: Double the value of alternate digits beginning with
690          * the first right-hand digit (low order).
691          * 2: Add the individual digits comprising the products
692          * obtained in step 1 to each of the unaffected digits
693          * in the original number.
694          * 3: Subtract the total obtained in step 2 from the next higher
695          * number ending in 0 [this in the equivalent of calculating
696          * the "tens complement" of the low order digit (unit digit)
697          * of the total].
698          * If the total obtained in step 2 is a number ending in zero
699          * (30, 40 etc.), the check digit is 0.
700          * Example:
701          * Account number: 4992 73 9871 6
702          *
703          * 4 9 9 2 7 3 9 8 7 1 6
704          * x2 x2 x2 x2 x2
705          * -------------------------------
706          * 4 18 9 4 7 6 9 16 7 2 6
707          *
708          * 4 + 1 + 8 + 9 + 4 + 7 + 6 + 9 + 1 + 6 + 7 + 2 + 6 = 70
709          * 70 % 10 = 0
710          */

711
712         // Clean up number
713
String JavaDoc ccNumber1 = checkNumeric(creditCardNumber);
714         int ccLength = ccNumber1.length();
715         // Reverse string
716
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
717         for (int i = ccLength; i != 0; i--)
718             buf.append(ccNumber1.charAt(i-1));
719         String JavaDoc 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     } // validateCreditCardNumber
740

741     /**
742      * Validate Credit Card Number.
743      * - Check Card Type and Length
744      * @param creditCardNumber CC Number
745      * @param creditCardType CC Type
746      * @return "" or Error AD_Message
747      */

748     public static String JavaDoc validateCreditCardNumber (String JavaDoc creditCardNumber, String JavaDoc creditCardType)
749     {
750         if (creditCardNumber == null || creditCardType == null)
751             return "CreditCardNumberError";
752
753         // http://www.beachnet.com/~hstiles/cardtype.html
754
// http://staff.semel.fi/~kribe/document/luhn.htm
755

756         String JavaDoc ccStartList = ""; // comma separated list of starting numbers
757
String JavaDoc ccLengthList = ""; // comma separated list of lengths
758
//
759
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             // enRouteCard
787
ccStartList = "2014,2149";
788             ccLengthList = "15";
789             // JCBCard
790
ccStartList += ",3088,3096,3112,3158,3337,3528";
791             ccLengthList += ",16";
792             // JCBCard
793
ccStartList += ",2131,1800";
794             ccLengthList += ",15";
795         }
796
797         // Clean up number
798
String JavaDoc ccNumber = checkNumeric(creditCardNumber);
799
800         /**
801          * Check Length
802          */

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         /**
820          * Check Start Digits
821          */

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         //
834
String JavaDoc check = validateCreditCardNumber(ccNumber);
835         if (check.length() != 0)
836             return check;
837         if (!ccIdentified)
838             return "CreditCardNumberProblem?";
839         return "";
840     } // validateCreditCardNumber
841

842     /*************************************************************************/
843
844     /**
845      * Verification Code
846      * @param newCreditCardVV CC verification
847      */

848     public void setCreditCardVV(String JavaDoc newCreditCardVV)
849     {
850         super.setCreditCardVV (checkNumeric(newCreditCardVV));
851     } // setCreditCardVV
852

853     /**
854      * Validate Validation Code
855      * @param creditCardVV CC Verification Code
856      * @return "" or Error AD_Message
857      */

858     public static String JavaDoc validateCreditCardVV (String JavaDoc 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 JavaDoc ex)
871         {
872             s_log.debug("validateCreditCardVV - " + ex);
873         }
874         s_log.debug("validateCreditCardVV - length=" + length);
875         return "CreditCardVVError";
876     } // validateCreditCardVV
877

878     /**
879      * Validate Validation Code
880      * @param creditCardVV CC Verification Code
881      * @param creditCardType CC Type see CC_
882      * @return "" or Error AD_Message
883      */

884     public static String JavaDoc validateCreditCardVV (String JavaDoc creditCardVV, String JavaDoc creditCardType)
885     {
886         // no data
887
if (creditCardVV == null || creditCardVV.length() == 0
888             || creditCardType == null || creditCardType.length() == 0)
889             return "";
890
891         int length = checkNumeric(creditCardVV).length();
892
893         // Amex = 4 digits
894
if (creditCardType.equals(CREDITCARDTYPE_Amex))
895         {
896             if (length == 4)
897             {
898                 try
899                 {
900                     Integer.parseInt (creditCardVV);
901                     return "";
902                 }
903                 catch (NumberFormatException JavaDoc 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         // Visa & MasterCard - 3 digits
912
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 JavaDoc 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         // Other
931
return "";
932     } // validateCreditCardVV
933

934     /*************************************************************************/
935
936     /**
937      * Two Digit CreditCard MM
938      * @param CreditCardExpMM Exp month
939      */

940     public void setCreditCardExpMM (int CreditCardExpMM)
941     {
942         if (CreditCardExpMM < 1 || CreditCardExpMM > 12)
943             ;
944         else
945             super.setCreditCardExpMM (CreditCardExpMM);
946     } // setCreditCardExpMM
947

948     /**
949      * Two digit CreditCard YY (til 2020)
950      * @param newCreditCardExpYY 2 or 4 digit year
951      */

952     public void setCreditCardExpYY (int newCreditCardExpYY)
953     {
954         int CreditCardExpYY = newCreditCardExpYY;
955         if (newCreditCardExpYY > 1999)
956             CreditCardExpYY = newCreditCardExpYY-2000;
957         super.setCreditCardExpYY(CreditCardExpYY);
958     } // setCreditCardExpYY
959

960     /**
961      * CreditCard Exp MMYY
962      * @param mmyy Exp in form of mmyy
963      * @return true if valid
964      */

965     public boolean setCreditCardExp (String JavaDoc mmyy)
966     {
967         if (validateCreditCardExp(mmyy).length() != 0)
968             return false;
969         //
970
String JavaDoc exp = checkNumeric(mmyy);
971         String JavaDoc mmStr = exp.substring(0,2);
972         String JavaDoc yyStr = exp.substring(2,4);
973         setCreditCardExpMM (Integer.parseInt(mmStr));
974         setCreditCardExpYY (Integer.parseInt(yyStr));
975         return true;
976     } // setCreditCardExp
977

978     /**
979      * Is this a valid Credit Card Exp Date?
980      * @param mmyy Exp in form of mmyy
981      * @return "" or Error AD_Message
982      */

983     public static String JavaDoc validateCreditCardExp (String JavaDoc mmyy)
984     {
985         String JavaDoc exp = checkNumeric(mmyy);
986         if (exp.length() != 4)
987             return "CreditCardExpFormat";
988         //
989
String JavaDoc mmStr = exp.substring(0,2);
990         String JavaDoc yyStr = exp.substring(2,4);
991         //
992
int mm = 0;
993         int yy = 0;
994         try
995         {
996             mm = Integer.parseInt(mmStr);
997             yy = Integer.parseInt(yyStr);
998         }
999         catch (Exception JavaDoc e)
1000        {
1001            return "CreditCardExpFormat";
1002        }
1003        return validateCreditCardExp(mm,yy);
1004    } // validateCreditCardExp
1005

1006    /**
1007     * Return Month of Exp
1008     * @param mmyy Exp in form of mmyy
1009     * @return month
1010     */

1011    public static int getCreditCardExpMM (String JavaDoc mmyy)
1012    {
1013        String JavaDoc mmStr = mmyy.substring(0,2);
1014        int mm = 0;
1015        try
1016        {
1017            mm = Integer.parseInt(mmStr);
1018        }
1019        catch (Exception JavaDoc e)
1020        {
1021        }
1022        return mm;
1023    } // getCreditCardExpMM
1024

1025    /**
1026     * Return Year of Exp
1027     * @param mmyy Exp in form of mmyy
1028     * @return year
1029     */

1030    public static int getCreditCardExpYY (String JavaDoc mmyy)
1031    {
1032        String JavaDoc yyStr = mmyy.substring(2);
1033        int yy = 0;
1034        try
1035        {
1036            yy = Integer.parseInt(yyStr);
1037        }
1038        catch (Exception JavaDoc e)
1039        {
1040        }
1041        return yy;
1042    } // getCreditCardExpYY
1043

1044    /**
1045     * Is this a valid Credit Card Exp Date?
1046     * @param mm month
1047     * @param yy year
1048     * @return "" or Error AD_Message
1049     */

1050    public static String JavaDoc validateCreditCardExp (int mm, int yy)
1051    {
1052        if (mm < 1 || mm > 12)
1053            return "CreditCardExpMonth";
1054    // if (yy < 0 || yy > EXP_YEAR)
1055
// return "CreditCardExpYear";
1056

1057        // Today's date
1058
Calendar cal = Calendar.getInstance();
1059        int year = cal.get(Calendar.YEAR) - 2000; // two digits
1060
int month = cal.get(Calendar.MONTH) + 1; // zero based
1061
//
1062
if (yy < year)
1063            return "CreditCardExpired";
1064        else if (yy == year && mm < month)
1065            return "CreditCardExpired";
1066        return "";
1067    } // validateCreditCardExp
1068

1069    /**
1070     * CreditCard Exp MMYY
1071     * @return Exp
1072     */

1073    public String JavaDoc getCreditCardExp()
1074    {
1075        String JavaDoc mm = String.valueOf(getCreditCardExpMM());
1076        String JavaDoc yy = String.valueOf(getCreditCardExpYY());
1077
1078        StringBuffer JavaDoc retValue = new StringBuffer JavaDoc();
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        //
1086
return (retValue.toString());
1087    } // getCreditCardExp
1088

1089    /**
1090     * MICR
1091     * @param MICR MICR
1092     */

1093    public void setMicr (String JavaDoc MICR)
1094    {
1095        super.setMicr (checkNumeric(MICR));
1096    } // setBankMICR
1097

1098    /**
1099     * Routing No
1100     * @param RoutingNo Routing No
1101     */

1102    public void setRoutingNo(String JavaDoc RoutingNo)
1103    {
1104        super.setRoutingNo (checkNumeric(RoutingNo));
1105    } // setBankRoutingNo
1106

1107    /**
1108     * Validate Routing Number
1109     * @param routingNo Routing No
1110     * @return "" or Error AD_Message
1111     */

1112    public static String JavaDoc validateRoutingNo (String JavaDoc routingNo)
1113    {
1114        int length = checkNumeric(routingNo).length();
1115        // US - length 9
1116
// Germany - length 8
1117
if (length == 8 || length == 9)
1118            return "";
1119        return "PaymentBankRoutingNotValid";
1120    } // validateBankRoutingNo
1121

1122    /**
1123     * Bank Account No
1124     * @param AccountNo AccountNo
1125     */

1126    public void setAccountNo (String JavaDoc AccountNo)
1127    {
1128        super.setAccountNo (checkNumeric(AccountNo));
1129    } // setBankAccountNo
1130

1131    /**
1132     * Validate Account No
1133     * @param AccountNo AccountNo
1134     * @return "" or Error AD_Message
1135     */

1136    public static String JavaDoc validateAccountNo (String JavaDoc AccountNo)
1137    {
1138        int length = checkNumeric(AccountNo).length();
1139        if (length > 0)
1140            return "";
1141        return "PaymentBankAccountNotValid";
1142    } // validateBankAccountNo
1143

1144    /**
1145     * Check No
1146     * @param CheckNo Check No
1147     */

1148    public void setCheckNo(String JavaDoc CheckNo)
1149    {
1150        super.setCheckNo(checkNumeric(CheckNo));
1151    } // setBankCheckNo
1152

1153    /**
1154     * Validate Check No
1155     * @param CheckNo CheckNo
1156     * @return "" or Error AD_Message
1157     */

1158    public static String JavaDoc validateCheckNo (String JavaDoc CheckNo)
1159    {
1160        int length = checkNumeric(CheckNo).length();
1161        if (length > 0)
1162            return "";
1163        return "PaymentBankCheckNotValid";
1164    } // validateBankCheckNo
1165

1166    /**
1167     * Set DocumentNo to Payment info
1168     */

1169    protected void setDocumentNo()
1170    {
1171        String JavaDoc 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        //
1185
super.setDocumentNo(DocumentNo);
1186    } // setDocumentNo
1187

1188    // ---------------
1189

1190    /**
1191     * Set Response Info
1192     * @param R_Info info
1193     */

1194    void setR_Info (String JavaDoc 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    } // setR_Info
1201

1202    /**
1203     * Set Response Msg
1204     * @param R_RespMsg msg
1205     */

1206    void setR_RespMsg (String JavaDoc 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    } // setR_RespMsg
1213

1214    /**
1215     * Set Payment Amount
1216     * @param PayAmt Pay Amt
1217     */

1218    public void setPayAmt (BigDecimal PayAmt)
1219    {
1220        super.setPayAmt(PayAmt == null ? Env.ZERO : PayAmt);
1221    } // setPayAmt
1222

1223    /**
1224     * Set Payment Amount
1225     *
1226     * @param C_Currency_ID currency
1227     * @param payAmt amount
1228     */

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    } // setAmount
1236

1237    /**
1238     * Discount Amt
1239     * @param DiscountAmt Discount
1240     */

1241    public void setDiscountAmt (BigDecimal DiscountAmt)
1242    {
1243        super.setDiscountAmt (DiscountAmt == null ? Env.ZERO : DiscountAmt);
1244    } // setDiscountAmt
1245

1246    /**
1247     * WriteOff Amt
1248     * @param WriteOffAmt WriteOff
1249     */

1250    public void setWriteOffAmt (BigDecimal WriteOffAmt)
1251    {
1252        super.setWriteOffAmt (WriteOffAmt == null ? Env.ZERO : WriteOffAmt);
1253    } // setWriteOffAmt
1254

1255    /**
1256     * OverUnder Amt
1257     * @param OverUnderAmt OverUnder
1258     */

1259    public void setOverUnderAmt (BigDecimal OverUnderAmt)
1260    {
1261        super.setOverUnderAmt (OverUnderAmt == null ? Env.ZERO : OverUnderAmt);
1262    } // setOverUnderAmt
1263

1264    /**
1265     * Tax Amt
1266     * @param TaxAmt Tax
1267     */

1268    public void setTaxAmt (BigDecimal TaxAmt)
1269    {
1270        super.setTaxAmt (TaxAmt == null ? Env.ZERO : TaxAmt);
1271    } // setTaxAmt
1272

1273    /**
1274     * Set Info from BP Bank Account
1275     * @param ba BP bank account
1276     */

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        // CC
1289
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        // Bank
1300
if (ba.getAccountNo() != null)
1301            setAccountNo(ba.getAccountNo());
1302        if (ba.getRoutingNo() != null)
1303            setRoutingNo(ba.getRoutingNo());
1304    } // setBP_BankAccount
1305

1306    /**
1307     * Save Info from BP Bank Account
1308     * @param ba BP bank account
1309     * @return true if saved
1310     */

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        // CC
1325
ba.setCreditCardType(getCreditCardType());
1326        ba.setCreditCardNumber(getCreditCardNumber());
1327        ba.setCreditCardExpMM(getCreditCardExpMM());
1328        ba.setCreditCardExpYY(getCreditCardExpYY());
1329        ba.setCreditCardVV(getCreditCardVV());
1330        // Bank
1331
if (getAccountNo() != null)
1332            ba.setAccountNo(getAccountNo());
1333        if (getRoutingNo() != null)
1334            ba.setRoutingNo(getRoutingNo());
1335        // Trx
1336
ba.setR_AvsAddr(getR_AvsAddr());
1337        ba.setR_AvsZip(getR_AvsZip());
1338        //
1339
boolean ok = ba.save();
1340        log.debug("saveToBP_BankAccount - " + ba);
1341        return ok;
1342    } // setBP_BankAccount
1343

1344    /**
1345     * Set Doc Type bases on IsReceipt
1346     */

1347    private void setC_DocType_ID ()
1348    {
1349        setC_DocType_ID(isReceipt());
1350    } // setC_DocType_ID
1351

1352    /**
1353     * Set Doc Type
1354     * @param isReceipt is receipt
1355     */

1356    public void setC_DocType_ID (boolean isReceipt)
1357    {
1358        setIsReceipt(isReceipt);
1359        String JavaDoc 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    } // setC_DocType_ID
1381

1382    /**
1383     * Set Invoice - set null if 0
1384     * @param C_Invoice_ID invoice
1385     */

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    } // setC_Invoice_ID
1393

1394    /**
1395     * Get ISO Code of Currency
1396     * @return Currency ISO
1397     */

1398    public String JavaDoc getCurrencyISO()
1399    {
1400        return MCurrency.getISO_Code (getCtx(), getC_Currency_ID());
1401    } // getCurrencyISO
1402

1403    /**
1404     * Get Document Status
1405     * @return Document Status Clear Text
1406     */

1407    public String JavaDoc getDocStatusName()
1408    {
1409        return MRef_List.getListName(getCtx(), 131, getDocStatus());
1410    } // getDocStatusName
1411

1412    /**
1413     * Get Name of Credit Card
1414     * @return Name
1415     */

1416    public String JavaDoc getCreditCardName()
1417    {
1418        return getCreditCardName(getCreditCardType());
1419    } // getCreditCardName
1420

1421    /**
1422     * Get Name of Credit Card
1423     * @param CreditCardType credit card type
1424     * @return Name
1425     */

1426    public String JavaDoc getCreditCardName(String JavaDoc 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    } // getCreditCardName
1446

1447    /*************************************************************************/
1448
1449    /**
1450     * Check Numeric
1451     * @param data input
1452     * @return the digits of the data - ignore the rest
1453     */

1454    public static String JavaDoc checkNumeric (String JavaDoc data)
1455    {
1456        if (data == null || data.length() == 0)
1457            return "";
1458        // Remove all non Digits
1459
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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    } // checkNumeric
1467

1468} // MPayment
1469
Popular Tags