📄 mpayment.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.io.Serializable;
import org.apache.log4j.Logger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.process.*;
/**
* Payment Model.
* - retrieve and create payments for invoice
* <pre>
* Event chain
* - Payment inserted
* C_Payment_Trg fires
* update DocumentNo with payment summary
* - Payment posted (C_Payment_Post)
* create allocation line
* C_Allocation_Trg fires
* Update C_BPartner Open Item Amount
* update invoice (IsPaid)
* link invoice-payment if batch
*
* Lifeline:
* - Created by VPayment or directly
* - When changed in VPayment
* - old payment is reversed
* - new payment created
*
* When Payment is posed, the Allocation is made
* </pre>
* @author Jorg Janke
* @version $Id: MPayment.java,v 1.19 2003/04/28 04:18:58 jjanke Exp $
*/
public final class MPayment extends PO
implements ProcessCall, Serializable
{
/**
* Default Constructor
* @param ctx context
* @param C_Payment_ID payment to load, (0 create new payment)
*/
public MPayment (Properties ctx, int C_Payment_ID)
{
super (ctx, C_Payment_ID);
// New
if (C_Payment_ID == 0)
{
setDocAction("CO");
setDocStatus("DR");
//
setR_AvsAddr (AVS_UNAVAILABLE);
setR_AvsZip (AVS_UNAVAILABLE);
//
setReceipt (true);
setProcessed(false);
setApproved (false);
setPosted (false);
setReconciled (false);
setAllocated(false);
setOnline (false);
//
setPayAmt(Env.ZERO);
setDiscountAmt(Env.ZERO);
setTaxAmt(Env.ZERO);
setWriteOffAmt(Env.ZERO);
setOverUnderPayment (false);
setOverUnderAmt(Env.ZERO);
//
setDateTrx (new Timestamp(System.currentTimeMillis()));
}
} // MPayment
/**
* Load Constructor
* @param ctx context
* @param rs result set record
*/
public MPayment (Properties ctx, ResultSet rs)
{
super (ctx, rs);
} // MPayment
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
protected POInfo initPO (Properties ctx)
{
int AD_Table_ID = 335;
POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
return poi;
} // initPO
// Tender Types
public final static String TENDER_ACH = "A";
public final static String TENDER_CREDITCARD = "C";
public final static String TENDER_CHECK = "K";
// Trx Type
public final static String TRX_AUTHORIZATION = "A";
public final static String TRX_CREDIT = "C";
public final static String TRX_DELAYED = "D";
public final static String TRX_VOICE = "F";
public final static String TRX_SALES = "S"; // default
public final static String TRX_VOID = "V";
// AVS
public final static String AVS_NOMATCH = "N";
public final static String AVS_MATCH = "Y";
public final static String AVS_UNAVAILABLE = "X";
// Credit Card Type
public final static String CC_AMEX = "A";
public final static String CC_MASTERCARD = "M";
public final static String CC_VISA = "V";
public final static String CC_DISCOVER = "N";
public final static String CC_DINERS = "D";
public final static String CC_PURCHASE = "P";
// Temporary
private MPaymentProcessor m_mpp = null;
private static Logger s_log = Logger.getLogger (MPayment.class);
private String m_errorMessage = null;
/*************************************************************************/
/**
* Set Credit Card.
* Need to set PatmentProcessor after Amount/Currency Set
*
* @param TrxType Transaction Type see TRX_
* @param creditCardType CC type
* @param creditCardNumber CC number
* @param creditCardVV CC verification
* @param creditCardExpMM CC Exp MM
* @param creditCardExpYY CC Exp YY
* @return true if valid
*/
public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber,
String creditCardVV, int creditCardExpMM, int creditCardExpYY)
{
setTenderType(TENDER_CREDITCARD);
setTrxType(TrxType);
//
setCreditCardType (creditCardType);
setCreditCardNumber (creditCardNumber);
setCreditCardVV (creditCardVV);
setCreditCardExpMM (creditCardExpMM);
setCreditCardExpYY (creditCardExpYY);
//
int check = validateCreditCardNumber(creditCardNumber, creditCardType).length()
+ validateCreditCardExp(creditCardExpMM, creditCardExpYY).length();
if (creditCardVV.length() > 0)
check += validateCreditCardVV(creditCardVV, creditCardType).length();
return check == 0;
} // setCreditCard
/**
* Set Credit Card - Exp.
* Need to set PatmentProcessor after Amount/Currency Set
*
* @param TrxType Transaction Type see TRX_
* @param creditCardType CC type
* @param creditCardNumber CC number
* @param creditCardVV CC verification
* @param creditCardExp CC Exp
* @return true if valid
*/
public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber,
String creditCardVV, String creditCardExp)
{
return setCreditCard(TrxType, creditCardType, creditCardNumber,
creditCardVV, getCreditCardExpMM(creditCardExp), getCreditCardExpYY(creditCardExp));
} // setCreditCard
/**
* Set ACH BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @return true if valid
*/
public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt)
{
return setBankACH(C_BankAccount_ID, isReceipt);
} // setBankACH
/**
* Set ACH BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param routingNo routing
* @param accountNo account
* @return true if valid
*/
public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt, String routingNo, String accountNo)
{
setTenderType (TENDER_ACH);
setReceipt (isReceipt);
//
if (C_BankAccount_ID > 0
&& (routingNo == null || routingNo.length() == 0 || accountNo == null || accountNo.length() == 0))
setBankAccountDetails(C_BankAccount_ID);
else
{
setC_BankAccount_ID(C_BankAccount_ID);
setBankRoutingNo (routingNo);
setBankAccountNo (accountNo);
}
setBankCheckNo ("");
//
int check = validateBankRoutingNo(routingNo).length()
+ validateBankAccountNo(accountNo).length();
return check == 0;
} // setBankACH
/**
* Set Check BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param checkNo chack no
* @return true if valid
*/
public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String checkNo)
{
return setBankCheck (C_BankAccount_ID, isReceipt, null, null, checkNo);
} // setBankCheck
/**
* Set Check BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param routingNo routing no
* @param accountNo account no
* @param checkNo chack no
* @return true if valid
*/
public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String routingNo, String accountNo, String checkNo)
{
setTenderType (TENDER_CHECK);
setReceipt (isReceipt);
//
if (C_BankAccount_ID > 0
&& (routingNo == null || routingNo.length() == 0 || accountNo == null || accountNo.length() == 0))
setBankAccountDetails(C_BankAccount_ID);
else
{
setC_BankAccount_ID(C_BankAccount_ID);
setBankRoutingNo (routingNo);
setBankAccountNo (accountNo);
}
setBankCheckNo (checkNo);
//
int check = validateBankRoutingNo(routingNo).length()
+ validateBankAccountNo(accountNo).length()
+ validateBankCheckNo(checkNo).length();
return check == 0; // no error message
} // setBankCheck
/**
* Set Bank Account Details.
* Look up Routing No & Bank Acct No
* @param C_BankAccount_ID bank account
*/
public void setBankAccountDetails (int C_BankAccount_ID)
{
if (C_BankAccount_ID == 0)
return;
setC_BankAccount_ID(C_BankAccount_ID);
//
String sql = "SELECT b.RoutingNo, ba.AccountNo "
+ "FROM C_BankAccount ba"
+ " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID) "
+ "WHERE C_BankAccount_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, C_BankAccount_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
setBankRoutingNo (rs.getString(1));
setBankAccountNo (rs.getString(2));
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("setsetBankAccountDetails", e);
}
} // setBankAccountDetails
/**
* Set Account Address
*
* @param name name
* @param street street
* @param city city
* @param state state
* @param zip zip
* @param country country
*/
public void setAccountAddress (String name, String street,
String city, String state, String zip, String country)
{
setA_Name (name);
setA_Street (street);
setA_City (city);
setA_State (state);
setA_Zip (zip);
setA_Country(country);
} // setAccountAddress
/*************************************************************************/
/**
* Process Payment
* @return true if approved
*/
public boolean processOnline()
{
log.info ("processOnline - " + getPayAmt());
setOnline(true);
setErrorMessage(null);
// prevent charging twice
if (isApproved())
{
log.info("processOnline - already processed - " + getR_Result() + " - " + getR_RespMsg());
setErrorMessage("Payment already Processed");
return true;
}
if (m_mpp == null)
setPaymentProcessor();
if (m_mpp == null)
{
log.error("processOnline - No Payment Processor Model");
setErrorMessage("No Payment Processor Model");
return false;
}
boolean approved = false;
try
{
PaymentProcessor pp = PaymentProcessor.create(m_mpp, this);
if (pp == null)
setErrorMessage("No Payment Processor");
else
{
approved = pp.processCC ();
if (approved)
setErrorMessage(null);
else
setErrorMessage(getR_RespMsg());
}
}
catch (Exception e)
{
log.error("processOnline", e);
setErrorMessage("Payment Processor Error");
}
setApproved(approved);
return approved;
} // processOnline
/**
* Process Online Payment.
* implements ProcessCall after standard constructor
* Called when pressing the Process_Online button in C_Payment
*
* @param ctx Context
* @param pi Process Info
* @return true if the next process should be performed
*/
public boolean startProcess (Properties ctx, ProcessInfo pi)
{
log.info("startProcess - " + pi.Record_ID);
boolean retValue = false;
//
if (pi.Record_ID != getID())
{
log.error("startProcess - Not same Payment - " + pi.Record_ID);
return false;
}
// Process it
retValue = processOnline();
save();
return retValue; // Payment processed
} // startProcess
/**
* Save
* @return true if success
*/
public boolean save ()
{
log.info("save");
//
if (getC_DocType_ID() == 0)
setC_DocType_ID();
if (getDocumentNo() == null)
setDocumentNo();
return super.save();
} // save
/**
* Post Payment
* @return true if success
*/
public boolean post()
{
if (!save()) // save first
return false;
log.info("post");
boolean retValue = false;
try
{
CallableStatement cstmt = DB.prepareCall("{CALL C_Payment_Post(NULL,?)}");
cstmt.setInt(1, getID());
retValue = cstmt.execute();
cstmt.close();
}
catch (SQLException e)
{
log.error("post", e);
}
load();
return retValue;
} // post
/**
* Cancel Payment
* @return true if cancelled
*/
public boolean cancel()
{
log.info("cancel");
// Check Status
if (!"CO".equals(getDocStatus()))
return false;
// Set Action
setDocAction ("RC");
save();
return post();
} // Cancel
/*************************************************************************/
/**
* Set Error Message
* @param errorMessage error message
*/
public void setErrorMessage(String errorMessage)
{
m_errorMessage = errorMessage;
} // setErrorMessage
/**
* Get Error Message
* @return error message
*/
public String getErrorMessage()
{
return m_errorMessage;
} // getErrorMessage
/**
* Get Payment
* @return C_Payment_ID
*/
public int getC_Payment_ID()
{
return getID();
} // getC_Payment_ID
/**
* Set Transaction Type if valid
* @param TrxType trx type
*/
public void setTrxType (String TrxType)
{
if (TrxType == null)
return;
if (TrxType.equals(TRX_AUTHORIZATION)
|| TrxType.equals(TRX_CREDIT)
|| TrxType.equals(TRX_DELAYED)
|| TrxType.equals(TRX_SALES)
|| TrxType.equals(TRX_VOICE)
|| TrxType.equals(TRX_VOID))
{
setValue("TrxType", TrxType);
if (TrxType.equals(TRX_SALES))
setReceipt(true);
}
} // setTrxType
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -