1 25 package org.ofbiz.accounting.thirdparty.worldpay; 26 27 import java.io.IOException ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.ServletRequest ; 38 import javax.servlet.http.HttpSession ; 39 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.GeneralException; 42 import org.ofbiz.base.util.StringUtil; 43 import org.ofbiz.base.util.UtilMisc; 44 import org.ofbiz.webapp.view.JPublishWrapper; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericEntityException; 47 import org.ofbiz.entity.GenericValue; 48 import org.ofbiz.entity.transaction.GenericTransactionException; 49 import org.ofbiz.entity.transaction.TransactionUtil; 50 import org.ofbiz.order.order.OrderChangeHelper; 51 import org.ofbiz.service.ModelService; 52 import org.ofbiz.service.DispatchContext; 53 import org.ofbiz.service.GenericServiceException; 54 import org.ofbiz.service.LocalDispatcher; 55 import org.ofbiz.service.ServiceDispatcher; 56 57 import com.worldpay.select.SelectDefs; 58 import com.worldpay.select.merchant.SelectServlet; 59 import com.worldpay.select.merchant.SelectServletRequest; 60 import com.worldpay.select.merchant.SelectServletResponse; 61 62 69 public class SelectRespServlet extends SelectServlet implements SelectDefs { 70 71 public static final String module = SelectRespServlet.class.getName(); 72 protected JPublishWrapper jp = null; 73 74 protected void doRequest(SelectServletRequest request, SelectServletResponse response) throws ServletException , IOException { 75 Debug.logInfo("Response received from worldpay..", module); 76 77 String localLocaleStr = request.getParameter("M_localLocale"); 78 String webSiteId = request.getParameter("M_webSiteId"); 79 String delegatorName = request.getParameter("M_delegatorName"); 80 String dispatchName = request.getParameter("M_dispatchName"); 81 String userLoginId = request.getParameter("M_userLoginId"); 82 String confirmTemplate = request.getParameter("M_confirmTemplate"); 83 84 ServletContext context = (ServletContext ) request.getAttribute("servletContext"); 86 if (this.jp == null) { 87 this.jp = (JPublishWrapper) context.getAttribute("jpublishWrapper"); 88 if (this.jp == null) { 89 this.jp = new JPublishWrapper(context); 90 } 91 } 92 93 GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorName); 95 96 ServiceDispatcher serviceDisp = ServiceDispatcher.getInstance(dispatchName, delegator); 98 DispatchContext dctx = serviceDisp.getLocalContext(dispatchName); 99 LocalDispatcher dispatcher = dctx.getDispatcher(); 100 101 GenericValue userLogin = null; 103 try { 104 userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); 105 } catch (GenericEntityException e) { 106 Debug.logError(e, "Cannot get admin UserLogin entity", module); 107 callError(request); 108 } 109 110 List localeSplit = StringUtil.split(localLocaleStr, "_"); 112 Locale localLocale = new Locale ((String ) localeSplit.get(0), (String ) localeSplit.get(1)); 113 114 String configString = null; 116 try { 117 GenericValue webSitePayment = delegator.findByPrimaryKey("WebSitePaymentSetting", UtilMisc.toMap("webSiteId", webSiteId, "paymentMethodTypeId", "EXT_WORLDPAY")); 118 if (webSitePayment != null) 119 configString = webSitePayment.getString("paymentConfiguration"); 120 } catch (GenericEntityException e) { 121 Debug.logWarning(e, "Cannot find webSitePayment Settings", module); 122 } 123 if (configString == null) 124 configString = "payment.properties"; 125 Debug.logInfo("Got the payment configuration", module); 126 127 String orderId = request.getParameter(SelectDefs.SEL_cartId); 128 String authAmount = request.getParameter(SelectDefs.SEL_authAmount); 129 String transStatus = request.getParameter(SelectDefs.SEL_transStatus); 130 131 GenericValue orderHeader = null; 133 try { 134 orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); 135 } catch (GenericEntityException e) { 136 Debug.logError(e, "Cannot get the order header for the returned orderId", module); 137 callError(request); 138 } 139 140 Double wpTotal = new Double (authAmount); 142 Double orderTotal = orderHeader != null ? orderHeader.getDouble("grandTotal") : null; 143 if (orderTotal != null && wpTotal != null) { 144 if (orderTotal.doubleValue() != wpTotal.doubleValue()) { 145 Debug.logError("AuthAmount (" + wpTotal + ") does not match OrderTotal (" + orderTotal + ")", module); 146 callError(request); 147 } 148 } 149 150 HttpSession session = request.getSession(true); 152 session.setAttribute("userLogin", userLogin); 153 154 request.setAttribute("delegator", delegator); 155 request.setAttribute("dispatcher", dispatcher); 156 request.setAttribute("orderId", orderId); 157 request.setAttribute("notifyEmail", request.getParameter("M_notifyEmail")); 158 request.setAttribute("confirmEmail", request.getParameter("M_confirmEmail")); 159 request.setAttribute("_CONTROL_PATH_", request.getParameter("M_controlPath")); 160 161 boolean beganTransaction = false; 163 try { 164 beganTransaction = TransactionUtil.begin(); 165 } catch (GenericTransactionException gte) { 166 Debug.logError(gte, "Unable to begin transaction", module); 167 } 168 169 boolean okay = false; 170 if (transStatus.equalsIgnoreCase("Y")) { 171 Debug.logInfo("Order #" + orderId + " approved", module); 173 okay = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId); 174 } else { 175 Debug.logInfo("Order #" + orderId + " cancelled", module); 177 okay = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId); 178 } 179 180 if (okay) { 181 okay = setPaymentPreferences(delegator, userLogin, orderId, request); 183 } 184 185 if (okay) { 186 try { 187 TransactionUtil.commit(beganTransaction); 188 } catch (GenericTransactionException gte) { 189 Debug.logError(gte, "Unable to commit transaction", module); 190 } 191 } else { 192 try { 193 TransactionUtil.rollback(beganTransaction, "Failure in Worldpay callback/response processing.", null); 194 } catch (GenericTransactionException gte) { 195 Debug.logError(gte, "Unable to rollback transaction", module); 196 } 197 } 198 199 OrderChangeHelper.releaseInitialOrderHold(dispatcher, orderId); 201 202 Map emailContext = UtilMisc.toMap("orderId", orderId); 204 try { 205 Map emailResult = dispatcher.runSync("sendOrderConfirmation", emailContext); 206 } catch (GenericServiceException e) { 207 Debug.logError(e, "Problems sending email confirmation", module); 208 } 209 210 response.setContentType("text/html"); 212 ServletOutputStream out = response.getOutputStream(); 213 String content = "Error getting confirm content"; 214 if (confirmTemplate != null) { 215 try { 217 content = jp.render(confirmTemplate, request, response); 218 } catch (GeneralException e) { 219 Debug.logError(e, "Trouble rendering confirm page", module); 220 } 221 } 222 out.println(content); 223 out.flush(); 224 } 225 226 private boolean setPaymentPreferences(GenericDelegator delegator, GenericValue userLogin, String orderId, ServletRequest request) { 227 List paymentPrefs = null; 228 boolean okay = true; 229 try { 230 Map paymentFields = UtilMisc.toMap("orderId", orderId, "statusId", "PAYMENT_NOT_RECEIVED"); 231 paymentPrefs = delegator.findByAnd("OrderPaymentPreference", paymentFields); 232 } catch (GenericEntityException e) { 233 Debug.logError(e, "Cannot get payment preferences for order #" + orderId, module); 234 } 235 if (paymentPrefs != null && paymentPrefs.size() > 0) { 236 Iterator i = paymentPrefs.iterator(); 237 while (okay && i.hasNext()) { 238 GenericValue pref = (GenericValue) i.next(); 239 okay = setPaymentPreference(pref, userLogin, request); 240 } 241 } 242 return okay; 243 } 244 245 private boolean setPaymentPreference(GenericValue paymentPreference, GenericValue userLogin, ServletRequest request) { 246 String transId = request.getParameter(SelectDefs.SEL_transId); 247 String transTime = request.getParameter(SelectDefs.SEL_transTime); 248 String transStatus = request.getParameter(SelectDefs.SEL_transStatus); 249 String avsCode = request.getParameter("AVS"); String authCode = request.getParameter(SelectDefs.SEL_authCode); 251 String authAmount = request.getParameter(SelectDefs.SEL_authAmount); 252 String rawAuthMessage = request.getParameter(SelectDefs.SEL_rawAuthMessage); 253 254 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 256 257 if (transStatus.equalsIgnoreCase("Y")) { 258 paymentPreference.set("authCode", authCode); 259 paymentPreference.set("statusId", "PAYMENT_RECEIVED"); 260 } else { 261 paymentPreference.set("statusId", "PAYMENT_CANCELLED"); 262 } 263 Long transTimeLong = new Long (transTime); 264 java.sql.Timestamp authDate = new java.sql.Timestamp (transTimeLong.longValue()); 265 266 paymentPreference.set("avsCode", avsCode); 267 paymentPreference.set("authRefNum", transId); 268 paymentPreference.set("authDate", authDate); 269 paymentPreference.set("authFlag", transStatus); 270 paymentPreference.set("authMessage", rawAuthMessage); 271 paymentPreference.set("maxAmount", new Double (authAmount)); 272 273 Map results = null; 275 try { 276 results = dispatcher.runSync("createPaymentFromPreference", UtilMisc.toMap("userLogin", userLogin, 277 "orderPaymentPreferenceId", paymentPreference.get("orderPaymentPreferenceId"), "comments", "Payment received via WorldPay")); 278 } catch (GenericServiceException e) { 279 Debug.logError(e, "Failed to execute service createPaymentFromPreference", module); 280 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 281 return false; 282 } 283 284 if ((results == null) || (results.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) { 285 Debug.logError((String ) results.get(ModelService.ERROR_MESSAGE), module); 286 request.setAttribute("_ERROR_MESSAGE_", (String ) results.get(ModelService.ERROR_MESSAGE)); 287 return false; 288 } 289 290 try { 291 paymentPreference.store(); 292 paymentPreference.getDelegator().create(paymentPreference); 293 } catch (GenericEntityException e) { 294 Debug.logError(e, "Cannot set payment preference/payment info", module); 295 return false; 296 } 297 return true; 298 } 299 300 private void callError(ServletRequest request) throws ServletException { 301 Enumeration e = request.getParameterNames(); 302 Debug.logError("###### SelectRespServlet Error:", module); 303 while (e.hasMoreElements()) { 304 String name = (String ) e.nextElement(); 305 String value = request.getParameter(name); 306 Debug.logError("### Parameter: " + name + " => " + value, module); 307 } 308 Debug.logError("###### The order was not processed!", module); 309 throw new ServletException ("Order Error"); 310 } 311 } 312 | Popular Tags |