📄 paymentservlet.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-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wstore;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.math.*;
import org.apache.log4j.Logger;
import org.compiere.www.*;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Web Store Payment Entry & Confirmation
*
* @author Jorg Janke
* @version $Id: PaymentServlet.java,v 1.5 2003/05/04 06:47:27 jjanke Exp $
*/
public class PaymentServlet extends HttpServlet
{
/** Logging */
private Logger log = Logger.getLogger(getClass());
public static final String ATTR_PAYMENT = "payment";
/**
* Initialize global variables
* @param config servlet configuration
* @throws ServletException
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
if (!WEnv.initWeb(config))
throw new ServletException("PaymentServlet.init");
} // init
/**
* Get Servlet information
* @return Info
*/
public String getServletInfo()
{
return "Compiere Payment Servlet";
} // getServletInfo
/**
* Clean up resources
*/
public void destroy()
{
log.info("destroy");
} // destroy
/*************************************************************************/
/**
* Process the initial HTTP Get request.
* Reads the Parameter Amt and optional C_Invoice_ID
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
Properties ctx = JSPEnv.getCtx(request);
HttpSession session = request.getSession(true);
// WEnv.dump(session);
WEnv.dump(request);
// Non existing user or Existing Web Payment
WebUser wu = (WebUser)session.getAttribute(WebUser.NAME);
MPayment p = (MPayment)session.getAttribute (ATTR_PAYMENT);
if (wu == null || (p != null && !p.isProcessed()))
{
log.info ("doGet - User=" + wu + " - or existing Payment=" + p);
doPost (request, response);
return;
}
// Payment Amount
String amtParam = request.getParameter("Amt");
if (amtParam == null || amtParam.length() == 0)
{
log.info ("doGet - No Payment Amount (" + amtParam + ")");
doPost (request, response);
return;
}
char[] chars = amtParam.toCharArray();
StringBuffer sb = new StringBuffer();
boolean decimal = false;
for (int i = chars.length-1; i >=0; i--)
{
char c = chars[i];
if (c == ',' || c == '.')
{
if (!decimal)
{
sb.insert (0, '.');
decimal = true;
}
}
else if (Character.isDigit(c))
sb.insert(0,c);
}
BigDecimal amt = null;
try
{
amt = new BigDecimal (sb.toString());
}
catch (Exception ex)
{
log.warn("doGet - Parsing Amount=" + amtParam + " (" + sb + ") - " + ex.toString());
}
if (amt == null || amt.compareTo(Env.ZERO) == 0)
{
log.info("doGet - No Payment Amount (" + amtParam + ") - " + amt);
doPost (request, response);
return;
}
String invoiceParam = request.getParameter("C_Invoice_ID");
int C_Invoice_ID = 0;
try
{
C_Invoice_ID = Integer.parseInt (invoiceParam);
}
catch (NumberFormatException ex)
{
log.warn("doGet - Parsing C_Invoice_ID=" + invoiceParam + " - " + ex.toString());
}
log.info("doGet - Amt=" + amt + ", C_Invoice_ID=" + C_Invoice_ID);
// Create New Payment for Amt & optional Invoice
// see OrderServlet.createPayment
p = new MPayment(ctx, 0);
// Bank Account
MBankAccount ba = OrderServlet.getBankAccount(session, ctx);
ba.setPayAmt(amt); // for CC selection
p.setC_BankAccount_ID(ba.getC_BankAccount_ID());
p.setC_Currency_ID(ba.getC_Currency_ID());
p.setOnline(true);
// Sales CC Trx
p.setC_DocType_ID(true);
p.setTrxType(MPayment.TRX_SALES);
p.setTenderType(MPayment.TENDER_CREDITCARD);
// Payment Info
p.setPayAmt(amt);
p.setC_Invoice_ID(C_Invoice_ID);
// BP Info
p.setBP_BankAccount(wu.getBankAccount());
//
// p.save();
session.setAttribute (ATTR_PAYMENT, p);
String url = "paymentInfo.jsp";
log.info ("doGet - Forward to " + url);
RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url);
dispatcher.forward (request, response);
} // doGet
/**
* Process the HTTP Post request.
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
Properties ctx = JSPEnv.getCtx(request);
HttpSession session = request.getSession(true);
WEnv.dump(session);
WEnv.dump(request);
// Web User/Payment
WebUser wu = (WebUser)session.getAttribute(WebUser.NAME);
MPayment p = (MPayment)session.getAttribute (ATTR_PAYMENT);
String url = null;
if (wu == null || p == null)
url = "paymentInfo.jsp";
else if (processPayment(request, ctx, p, wu))
url = "confirm.jsp";
else
url = "paymentInfo.jsp";
log.info ("doPost - Forward to " + url);
RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url);
dispatcher.forward (request, response);
} // doPost
/*************************************************************************/
/**
* Process Payment
* @param request request
* @param ctx context
* @param p payment
* @param wu web user
* @return true if processed
*/
private boolean processPayment(HttpServletRequest request, Properties ctx, MPayment p, WebUser wu)
{
boolean ok = processParameter(request, ctx, p, wu);
if (ok)
{
ok = p.processOnline();
if (ok)
{
p.post ();
sendEMail (request, ctx, p, wu);
}
else
{
log.debug("processPayment - " + p.getErrorMessage());
String errMsg = p.getErrorMessage();
p.save ();
p.setErrorMessage(errMsg);
}
}
return ok;
} // processPayment
/**
* Process Parameter and check them
* @param request request
* @param ctx context
* @param p payment
* @param wu web user
* @return true if processed
*/
private boolean processParameter (HttpServletRequest request, Properties ctx, MPayment p, WebUser wu)
{
StringBuffer sb = new StringBuffer();
p.setTenderType(MPayment.TENDER_CREDITCARD);
p.setTrxType(MPayment.TRX_SALES);
p.setA_EMail(wu.getEmail());
// CC & Number
String ccType = request.getParameter("CreditCard");
p.setCreditCardType(ccType);
String ccNumber = request.getParameter("CreditCardNumber");
p.setCreditCardNumber (ccNumber);
String AD_Message = MPayment.validateCreditCardNumber(ccNumber, ccType);
if (AD_Message.length() > 0)
sb.append(Msg.getMsg(ctx, AD_Message)).append(" - ");
// Optional Verification Code
String ccVV = request.getParameter("CreditCardVV");
p.setCreditCardVV(ccVV);
if (ccVV != null && ccVV.length() > 0)
{
AD_Message = MPayment.validateCreditCardVV (ccVV, ccType);
if (AD_Message.length () > 0)
sb.append (Msg.getMsg (ctx, AD_Message)).append (" - ");
}
// Exp
int mm = WUtil.getParameterAsInt(request, "CreditCardExpMM");
p.setCreditCardExpMM (mm);
int yy = WUtil.getParameterAsInt(request, "CreditCardExpYY");
p.setCreditCardExpYY (yy);
AD_Message = MPayment.validateCreditCardExp(mm, yy);
if (AD_Message.length() > 0)
sb.append(Msg.getMsg(ctx, AD_Message)).append(" - ");
// Account Info
String aName = request.getParameter("A_Name");
p.setA_Name(aName);
if (aName == null || aName.length() == 0)
sb.append("Name - ");
String aStreet = request.getParameter("A_Street");
p.setA_Street(aStreet);
String aCity = request.getParameter("A_City");
p.setA_City(aCity);
if (aCity == null || aCity.length() == 0)
sb.append("City - ");
String aState = request.getParameter("A_State");
p.setA_State(aState);
String aZip = request.getParameter("A_Zip");
p.setA_Zip(aZip);
if (aZip == null || aZip.length() == 0)
sb.append("Zip - ");
String aCountry = request.getParameter("A_Country");
p.setA_Country(aCountry);
// Error Message
boolean ok = sb.length() == 0;
p.setErrorMessage(sb.toString()); // always set
// Save BP Bank Account
if (ok)
{
String SP = "SavePayment";
String SavePayment = request.getParameter (SP);
if (SP.equals(SavePayment))
p.saveToBP_BankAccount(wu.getBankAccount());
}
//
return sb.length() == 0;
} // processParameter
/**
* Send Payment EMail.
* @param request request
* @param ctx context
* @param p payment
* @param wu web user
*/
private void sendEMail (HttpServletRequest request, Properties ctx, MPayment p, WebUser wu)
{
String subject = "Compiere Web - Payment " + p.getDocumentNo();
String message = "Thank you for your payment of " + p.getPayAmt() + " (Reference=" + p.getR_PnRef() + ")"
+ "\nYou can view your orders, payments and assets at http://"
+ request.getServerName()
+ request.getContextPath() + "/";
String SMTPHost = ctx.getProperty("SMTPHost", "localhost");
String RequestEMail = ctx.getProperty("RequestEMail");
String RequestUser = ctx.getProperty("RequestUser");
String RequestUserPw = ctx.getProperty("RequestUserPw");
//
EMail em = new EMail(SMTPHost, RequestEMail, wu.getEmail(), subject, message);
em.setEMailUser(RequestUser, RequestUserPw);
//
String webOrderEMail = ctx.getProperty("webOrderEMail");
em.addBcc(webOrderEMail);
//
em.send();
/**
Name=GardenWorld
webDir=compiere,
Description=GardenWorld
**/
} // sendEMail
} // PaymentServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -