| 1 package com.dotmarketing.cms.product.action; 2 3 import java.util.Date ; 4 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 import javax.servlet.http.HttpSession ; 8 9 import org.apache.struts.Globals; 10 import org.apache.struts.action.ActionForm; 11 import org.apache.struts.action.ActionForward; 12 import org.apache.struts.action.ActionMapping; 13 import org.apache.struts.action.ActionMessage; 14 import org.apache.struts.action.ActionMessages; 15 import org.apache.struts.actions.DispatchAction; 16 17 import com.dotmarketing.beans.UserProxy; 18 import com.dotmarketing.cms.product.model.ShoppingCart; 19 import com.dotmarketing.db.DotHibernate; 20 import com.dotmarketing.factories.HostFactory; 21 import com.dotmarketing.factories.UserProxyFactory; 22 import com.dotmarketing.portlets.order_manager.factories.OrderFactory; 23 import com.dotmarketing.portlets.order_manager.model.Order; 24 import com.dotmarketing.portlets.order_manager.struts.OrderForm; 25 import com.dotmarketing.util.Config; 26 import com.dotmarketing.util.CreditCardProcessor; 27 import com.dotmarketing.util.CreditCardProcessorException; 28 import com.dotmarketing.util.CreditCardProcessorResponse; 29 import com.dotmarketing.util.Logger; 30 import com.dotmarketing.util.Mailer; 31 import com.dotmarketing.util.UtilMethods; 32 import com.dotmarketing.util.WebKeys; 33 import com.liferay.portal.model.User; 34 35 36 37 public class PurchaseAction extends DispatchAction 38 { 39 public ActionForward unspecified(ActionMapping mapping, ActionForm lf, HttpServletRequest request, HttpServletResponse response) throws Exception 40 { 41 HttpSession session = request.getSession(); 42 if(!isTokenValid(request)) 43 { 44 int i = 0; 45 for(i = 0;i < 120;i++) 46 { 47 if (session.getAttribute(WebKeys.SHOPPING_CART_ERRORS) != null) 48 { 49 generateToken(request); 50 saveToken(request); 51 saveErrors(request,(ActionMessages)session.getAttribute(WebKeys.SHOPPING_CART_ERRORS)); 52 ActionForward af = mapping.getInputForward(); 53 return af; 54 } 55 if (session.getAttribute(WebKeys.SHOPPING_CART_ORDER_FORM) != null) 56 { 57 ActionForward af = mapping.findForward("invoice"); 58 return af; 59 } 60 Thread.sleep(1000); 61 } 62 if (i == 120) 64 { 65 ActionMessages ae = new ActionMessages(); 66 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.timeout")); 67 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS, ae); 68 generateToken(request); 69 saveToken(request); 70 ActionForward af = mapping.getInputForward(); 71 return af; 72 } 73 } 74 resetToken(request); 75 DotHibernate.startTransaction(); 76 try 77 { 78 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS,null); 79 session.setAttribute(WebKeys.SHOPPING_CART_ORDER_FORM,null); 80 OrderForm orderForm = (OrderForm)lf; 81 ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute(WebKeys.SHOPPING_CART); 82 User user = (User) session.getAttribute(WebKeys.CMS_USER); 83 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 84 Date now = new Date (); 85 86 92 Order order = OrderFactory.getOrderById(String.valueOf(orderForm.getInode())); 93 order.setLastModDate(now); 94 order.setBillingAddress1(orderForm.getBillingAddress1()); 96 order.setBillingAddress2(orderForm.getBillingAddress2()); 97 order.setBillingCity(orderForm.getBillingCity()); 98 if (orderForm.getBillingState() != null){ 99 if (orderForm.getBillingState().equals("otherCountry")){ 100 order.setBillingState(orderForm.getBillingStateOtherCountryText()); 101 orderForm.setBillingState(orderForm.getBillingStateOtherCountryText()); 102 } 103 else 104 order.setBillingState(orderForm.getBillingState()); 105 } 106 order.setBillingZip(orderForm.getBillingZip()); 107 order.setBillingCountry(orderForm.getBillingCountry()); 108 order.setBillingPhone(orderForm.getBillingPhone()); 109 order.setBillingFax(orderForm.getBillingFax()); 110 order.setBillingContactName(orderForm.getBillingContactName()); 111 order.setBillingContactPhone(orderForm.getBillingContactPhone()); 112 order.setBillingContactEmail(orderForm.getBillingContactPhone()); 113 order.setShippingAddress1(orderForm.getShippingAddress1()); 115 order.setShippingAddress2(orderForm.getShippingAddress2()); 116 order.setShippingLabel(orderForm.getShippingLabel()); 117 order.setShippingCity(orderForm.getShippingCity()); 118 if (orderForm.getShippingState().equals("otherCountry")) 119 order.setShippingState(orderForm.getShippingStateOtherCountryText()); 120 else 121 order.setShippingState(orderForm.getShippingState()); 122 order.setShippingZip(orderForm.getShippingZip()); 123 order.setShippingCountry(orderForm.getShippingCountry()); 124 order.setShippingPhone(orderForm.getShippingPhone()); 125 order.setShippingFax(orderForm.getShippingFax()); 126 if (orderForm.getPaymentType().equals("cc")) 130 { 131 order.setNameOnCard(orderForm.getNameOnCard()); 132 String cardType = orderForm.getCardType(); 133 String [] creditCardTypes = Config.getStringArrayProperty("EREG_CREDIT_CARD_TYPES"); 134 if (cardType.equals("vs")) 135 { 136 cardType = creditCardTypes[0]; 137 } 138 else if (cardType.equals("mc")) 139 { 140 cardType = creditCardTypes[1]; 141 } 142 else if (cardType.equals("dc")) 143 { 144 cardType = creditCardTypes[2]; 145 } 146 else if (cardType.equals("ae")) 147 { 148 cardType = creditCardTypes[3]; 149 } 150 order.setCardType(cardType); 151 order.setCardNumber(UtilMethods.obfuscateCreditCard(orderForm.getCardNumber())); order.setCardExpMonth(orderForm.getCardExpMonth()); 153 order.setCardExpYear(orderForm.getCardExpYear()); 154 order.setCardVerificationValue(""); 156 order.setOrderTotalPaid(orderForm.getOrderTotal()); 157 order.setOrderTotalDue(0); 158 int paymentStatus = Config.getIntProperty("ECOM_PAY_PAID"); 159 order.setPaymentStatus(paymentStatus); 160 order.setPaymentType(Config.getStringProperty("ECOM_CREDIT_CARD")); 161 } 162 else if (orderForm.getPaymentType().equals("ch")) 163 { 164 order.setCheckNumber(orderForm.getCheckNumber()); 166 order.setCheckBankName(orderForm.getCheckBankName()); 167 168 order.setOrderTotalPaid(0); 169 order.setOrderTotalDue(orderForm.getOrderTotal()); 170 int paymentStatus = Config.getIntProperty("ECOM_PAY_WAITING"); 171 order.setPaymentStatus(paymentStatus); 172 order.setPaymentType(Config.getStringProperty("ECOM_CHECK")); 173 } 174 else if (orderForm.getPaymentType().equals("po")) 175 { 176 order.setPoNumber(orderForm.getPoNumber()); 178 179 order.setOrderTotalPaid(0); 180 order.setOrderTotalDue(orderForm.getOrderTotal()); 181 int paymentStatus = Config.getIntProperty("ECOM_PAY_INVOICED"); 182 order.setPaymentStatus(paymentStatus); 183 order.setPaymentType(Config.getStringProperty("ECOM_PURCHASE_ORDER")); 184 } 185 186 int orderStatus = Config.getIntProperty("ECOM_ORDER_PLACED"); 187 order.setOrderStatus(orderStatus); 188 order.setTaxExemptNumber(orderForm.getTaxExemptNumber()); 190 order.setOrderSubTotal(orderForm.getOrderSubTotal()); 192 order.setOrderDiscount(orderForm.getOrderDiscount()); 193 order.setOrderShipping(orderForm.getOrderShipping()); 194 order.setOrderShipType(orderForm.getOrderShipType()); 195 order.setOrderTax(orderForm.getOrderTax()); 196 order.setOrderTotal(orderForm.getOrderTotal()); 197 198 213 214 OrderFactory.saveOrder(order); 216 orderForm.setInode(order.getInode()); 217 218 219 236 237 if (orderForm.getPaymentType().equals("cc")) 238 { 239 CreditCardProcessorResponse ccResponse = CreditCardProcessor.processCreditCardOrderProducts(order.getInode(), 240 null, orderForm.getOrderTax()!=0?false:true, request.getRemoteUser(), orderForm.getOrderSubTotalDiscount(), 241 orderForm.getOrderTax()!=0?(Float )orderForm.getOrderTax():null, 242 orderForm.getOrderShipping(), orderForm.getOrderTotal(), orderForm.getCardNumber(), 243 orderForm.getCardExpMonth() + 1, orderForm.getCardExpYear(), 244 orderForm.getCardVerificationValue(), orderForm.getBillingContactName(), 245 null, orderForm.getBillingAddress1(), orderForm.getBillingAddress2(), 246 orderForm.getBillingCity(), orderForm.getBillingState(), 247 orderForm.getBillingZip(), Config.getStringProperty("US_COUNTRY_CODE"), 248 orderForm.getBillingContactPhone(), null, orderForm.getBillingContactEmail(), 249 Config.getStringProperty("WEB_EVENT_REGISTRATION_COMMENTS"), ""); 250 251 if (!ccResponse.orderApproved()) { 252 DotHibernate.rollbackTransaction(); 253 ActionMessages ae = new ActionMessages(); 254 ae.add(Globals.ERROR_KEY, new ActionMessage( 255 "error.cc_processing.card.denied", ccResponse.getError())); 256 saveErrors(request, ae); 257 generateToken(request); 258 saveToken(request); 259 ActionForward af = mapping.getInputForward(); 260 return af; 261 } 262 } 263 264 session.setAttribute(WebKeys.SHOPPING_CART_ORDER_FORM,lf); 265 DotHibernate.commitTransaction(); 266 session.removeAttribute("newOrderInode"); 267 try 269 { 270 String to = user.getEmailAddress(); 271 String from = user.getEmailAddress(); 272 String subject = "Thanks for ordering with Dotmarketing"; 273 String path = mapping.findForward("invoiceEmail").getPath(); 275 String URL = "http://" + HostFactory.getCurrentHost(request).getHostname() + path + "?"; 276 277 URL += "orderInode=" + order.getInode(); 278 StringBuffer writer = UtilMethods.getURL(URL); 279 String body = writer.toString().trim(); 280 sendEmail(to,from,subject,body); 281 } 282 catch(Exception ex) 283 { 284 Logger.debug(this,ex.toString()); 285 } 286 ActionForward af = mapping.findForward("invoice"); 287 return af; 288 } 289 catch (CreditCardProcessorException e) 290 { 291 Logger.warn(this,"Credit card processor exception placing a credit card order: "+ e.getMessage(), e); 293 DotHibernate.rollbackTransaction(); 294 ActionMessages ae = new ActionMessages(); 295 switch (e.getCode()) 296 { 297 case CreditCardProcessorException.DATA_MISSING: 298 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.invalid.card.data")); 299 break; 300 case CreditCardProcessorException.COMMUNICATION_ERROR: 301 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.communication.error")); 302 break; 303 default: 304 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.unknown")); 305 } 306 saveErrors(request, ae); 308 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS,ae); 309 generateToken(request); 311 saveToken(request); 312 ActionForward af = mapping.getInputForward(); 314 return af; 315 } 316 catch (Exception e) 317 { 318 Logger.error(this,"Unknown Error placing a credit card order: "+ e.getMessage(), e); 320 DotHibernate.rollbackTransaction(); 321 ActionMessages ae = new ActionMessages(); 322 ae.add(Globals.ERROR_KEY, new ActionMessage("error.cc_processing.unknown")); 323 saveErrors(request, ae); 325 session.setAttribute(WebKeys.SHOPPING_CART_ERRORS, ae); 326 generateToken(request); 328 saveToken(request); 329 ActionForward af = mapping.getInputForward(); 331 return af; 332 } 333 } 334 335 protected static boolean sendEmail(String to, String from, String subject, String body) 336 { 337 Mailer m = new Mailer(); 338 m.setToEmail(to); 339 m.setFromEmail(from); 340 m.setSubject(subject); 341 m.setHTMLBody(body); 342 return m.sendMessage(); 343 } 344 } 345 | Popular Tags |