1 14 package org.compiere.wstore; 15 16 import javax.servlet.*; 17 import javax.servlet.http.*; 18 import java.io.*; 19 import java.util.*; 20 import java.math.*; 21 22 import org.apache.log4j.Logger; 23 24 import org.compiere.www.*; 25 import org.compiere.model.*; 26 import org.compiere.util.*; 27 28 34 public class PaymentServlet extends HttpServlet 35 { 36 37 private Logger log = Logger.getLogger(getClass()); 38 39 public static final String ATTR_PAYMENT = "payment"; 40 41 46 public void init(ServletConfig config) throws ServletException 47 { 48 super.init(config); 49 if (!WEnv.initWeb(config)) 50 throw new ServletException("PaymentServlet.init"); 51 } 53 57 public String getServletInfo() 58 { 59 return "Compiere Payment Servlet"; 60 } 62 65 public void destroy() 66 { 67 log.info("destroy"); 68 } 70 71 72 81 public void doGet(HttpServletRequest request, HttpServletResponse response) 82 throws ServletException, IOException 83 { 84 log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr()); 85 Properties ctx = JSPEnv.getCtx(request); 86 HttpSession session = request.getSession(true); 87 session.removeAttribute(JSPEnv.HDR_MESSAGE); 88 91 WebUser wu = (WebUser)session.getAttribute(WebUser.NAME); 93 MPayment p = (MPayment)session.getAttribute (ATTR_PAYMENT); 94 if (wu == null) 95 { 96 log.info ("doGet - No User"); 97 String url = "index.jsp"; 98 log.info ("doPost - Forward to " + url); 99 RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url); 100 dispatcher.forward (request, response); 101 } 102 103 session.removeAttribute(WebOrder.NAME); 105 String amtParam = request.getParameter("Amt"); 107 if (amtParam == null || amtParam.length() == 0) 108 { 109 log.info ("doGet - No Payment Amount (" + amtParam + ")"); 110 doPost (request, response); 111 return; 112 } 113 char[] chars = amtParam.toCharArray(); 114 StringBuffer sb = new StringBuffer (); 115 boolean decimal = false; 116 for (int i = chars.length-1; i >=0; i--) 117 { 118 char c = chars[i]; 119 if (c == ',' || c == '.') 120 { 121 if (!decimal) 122 { 123 sb.insert (0, '.'); 124 decimal = true; 125 } 126 } 127 else if (Character.isDigit(c)) 128 sb.insert(0,c); 129 } 130 BigDecimal amt = null; 131 try 132 { 133 if (sb.length() > 0) 134 { 135 amt = new BigDecimal (sb.toString ()); 136 amt = amt.abs (); } 138 } 139 catch (Exception ex) 140 { 141 log.warn("doGet - Parsing Amount=" + amtParam + " (" + sb + ") - " + ex.toString()); 142 } 143 if (amt == null || amt.compareTo(Env.ZERO) < 0) 145 { 146 log.info("doGet - No valid Payment Amount (" + amtParam + ") - " + amt); 147 doPost (request, response); 148 return; 149 } 150 151 String invoiceParam = request.getParameter("C_Invoice_ID"); 152 int C_Invoice_ID = 0; 153 try 154 { 155 if (invoiceParam != null) 156 C_Invoice_ID = Integer.parseInt (invoiceParam); 157 } 158 catch (NumberFormatException ex) 159 { 160 log.warn("doGet - Parsing C_Invoice_ID=" + invoiceParam + " - " + ex.toString()); 161 } 162 log.info("doGet - Amt=" + amt + ", C_Invoice_ID=" + C_Invoice_ID); 163 164 p = new MPayment(ctx, 0); 167 p.setIsSelfService(true); 168 p.setAmount(0, amt); p.setIsOnline(true); 170 171 p.setC_DocType_ID(true); 173 p.setTrxType(MPayment.TRXTYPE_Sales); 174 p.setTenderType(MPayment.TENDERTYPE_CreditCard); 175 p.setC_Invoice_ID(C_Invoice_ID); 177 p.setBP_BankAccount(wu.getBankAccount()); 179 session.setAttribute (ATTR_PAYMENT, p); 182 183 String url = "paymentInfo.jsp"; 184 log.info ("doGet - Forward to " + url); 185 RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url); 186 dispatcher.forward (request, response); 187 } 189 198 public void doPost(HttpServletRequest request, HttpServletResponse response) 199 throws ServletException, IOException 200 { 201 log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr()); 202 Properties ctx = JSPEnv.getCtx(request); 203 HttpSession session = request.getSession(true); 204 WEnv.dump(session); 205 WEnv.dump(request); 206 207 WebUser wu = (WebUser)session.getAttribute(WebUser.NAME); 209 MPayment p = (MPayment)session.getAttribute (ATTR_PAYMENT); 210 WebOrder wo = (WebOrder)session.getAttribute (WebOrder.NAME); 211 212 String url = null; 213 if (wu == null || p == null) 214 url = "index.jsp"; 215 else if (processPayment(request, ctx, p, wu, wo)) 216 url = "confirm.jsp"; 217 else 218 url = "paymentInfo.jsp"; 219 220 log.info ("doPost - Forward to " + url); 221 RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url); 222 dispatcher.forward (request, response); 223 } 225 226 227 228 237 private boolean processPayment(HttpServletRequest request, Properties ctx, MPayment p, WebUser wu, WebOrder wo) 238 { 239 boolean ok = processParameter(request, ctx, p, wu); 240 if (ok) 241 { 242 if (p.getPayAmt().compareTo(Env.ZERO) < 0) 244 p.setPayAmt(p.getPayAmt().abs()); 245 ok = p.processOnline(); 246 if (ok) 247 { 248 if (wo != null) 250 { 251 if (!wo.isProcessed()) 252 wo.process(); 253 if (wo.isProcessed()) 254 p.setC_Invoice_ID(wo.getInvoice_ID()); 255 else 256 log.warn("processPayment - Order not processed " + wo); 257 } 258 else 259 log.warn("processPayment - no Order"); 260 p.post (); 261 sendEMail (request, ctx, p, wu); 262 } 263 else 264 { 265 log.debug("processPayment - " + p.getErrorMessage()); 266 String errMsg = p.getErrorMessage(); 267 p.save (); 268 p.setErrorMessage(errMsg); 269 request.getSession().setAttribute(JSPEnv.HDR_MESSAGE, errMsg); 270 sendDeclineEMail(request, ctx, p, wu); 272 } 273 } 274 return ok; 275 } 277 285 private boolean processParameter (HttpServletRequest request, Properties ctx, MPayment p, WebUser wu) 286 { 287 StringBuffer sb = new StringBuffer (); 288 p.setTenderType(MPayment.TENDERTYPE_CreditCard); 289 p.setTrxType(MPayment.TRXTYPE_Sales); 290 p.setA_EMail(wu.getEmail()); 291 String ccType = request.getParameter("CreditCard"); 293 p.setCreditCardType(ccType); 294 String ccNumber = request.getParameter("CreditCardNumber"); 295 p.setCreditCardNumber (ccNumber); 296 String AD_Message = MPayment.validateCreditCardNumber(ccNumber, ccType); 297 if (AD_Message.length() > 0) 298 sb.append(Msg.getMsg(ctx, AD_Message)).append(" - "); 299 300 String ccVV = request.getParameter("CreditCardVV"); 302 p.setCreditCardVV(ccVV); 303 if (ccVV != null && ccVV.length() > 0) 304 { 305 AD_Message = MPayment.validateCreditCardVV (ccVV, ccType); 306 if (AD_Message.length () > 0) 307 sb.append (Msg.getMsg (ctx, AD_Message)).append (" - "); 308 } 309 int mm = WUtil.getParameterAsInt(request, "CreditCardExpMM"); 311 p.setCreditCardExpMM (mm); 312 int yy = WUtil.getParameterAsInt(request, "CreditCardExpYY"); 313 p.setCreditCardExpYY (yy); 314 AD_Message = MPayment.validateCreditCardExp(mm, yy); 315 if (AD_Message.length() > 0) 316 sb.append(Msg.getMsg(ctx, AD_Message)).append(" - "); 317 318 String aName = request.getParameter("A_Name"); 320 if (aName == null || aName.length() == 0) 321 sb.append("Name - "); 322 else 323 p.setA_Name(aName); 324 String aStreet = request.getParameter("A_Street"); 325 p.setA_Street(aStreet); 326 String aCity = request.getParameter("A_City"); 327 if (aCity == null || aCity.length() == 0) 328 sb.append("City - "); 329 else 330 p.setA_City(aCity); 331 String aState = request.getParameter("A_State"); 332 p.setA_State(aState); 333 String aZip = request.getParameter("A_Zip"); 334 if (aZip == null || aZip.length() == 0) 335 sb.append("Zip - "); 336 else 337 p.setA_Zip(aZip); 338 String aCountry = request.getParameter("A_Country"); 339 p.setA_Country(aCountry); 340 341 boolean ok = sb.length() == 0; 343 p.setErrorMessage(sb.toString()); 345 if (ok) 347 { 348 String SP = "SavePayment"; 349 String SavePayment = request.getParameter (SP); 350 if (SP.equals(SavePayment)) 351 p.saveToBP_BankAccount(wu.getBankAccount()); 352 } 353 return sb.length() == 0; 355 } 357 358 365 private void sendEMail (HttpServletRequest request, Properties ctx, MPayment p, WebUser wu) 366 { 367 String subject = "Compiere Web - " + p.getPayAmt() + " Payment - " + p.getDocumentNo(); 368 String message = "Dear " + wu.getName() 369 + "\nThank you for your payment of " + p.getPayAmt() + " (Reference=" + p.getR_PnRef() + ")" 370 + "\nYou can view your orders, payments and assets at http://" 371 + request.getServerName() + request.getContextPath() + "/" 372 + "\nSincerely," 373 + "\n The " + ctx.getProperty("description") + " Team"; 374 375 String SMTPHost = ctx.getProperty("SMTPHost", "localhost"); 376 String RequestEMail = ctx.getProperty("RequestEMail"); 377 String RequestUser = ctx.getProperty("RequestUser"); 378 String RequestUserPw = ctx.getProperty("RequestUserPw"); 379 EMail em = new EMail(SMTPHost, RequestEMail, wu.getEmail(), subject, message); 381 em.setEMailUser(RequestUser, RequestUserPw); 382 String webOrderEMail = ctx.getProperty("webOrderEMail"); 384 em.addBcc(webOrderEMail); 385 em.send(); 387 } 389 396 private void sendDeclineEMail (HttpServletRequest request, Properties ctx, MPayment p, WebUser wu) 397 { 398 String subject = "Compiere Web - Declined Payment " + p.getDocumentNo(); 399 String message = "Payment of " + p.getPayAmt() + " (Reference=" + p.getR_PnRef() + ")" 400 + "\nwas declined " + p.getErrorMessage() 401 + "\nUser=" + wu.getName() + " - " + wu.getEmail(); 402 403 String SMTPHost = ctx.getProperty("SMTPHost", "localhost"); 404 String RequestEMail = ctx.getProperty("RequestEMail"); 405 String RequestUser = ctx.getProperty("RequestUser"); 406 String RequestUserPw = ctx.getProperty("RequestUserPw"); 407 EMail em = new EMail(SMTPHost, RequestEMail, RequestEMail, subject, message); 409 em.setEMailUser(RequestUser, RequestUserPw); 410 String webOrderEMail = ctx.getProperty("webOrderEMail"); 412 em.addBcc(webOrderEMail); 413 em.send(); 415 } 417 } | Popular Tags |