1 24 package org.ofbiz.order.shoppingcart; 25 26 import java.text.DecimalFormat ; 27 import java.text.ParseException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.http.HttpSession ; 37 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.GeneralException; 40 import org.ofbiz.base.util.GeneralRuntimeException; 41 import org.ofbiz.base.util.UtilHttp; 42 import org.ofbiz.base.util.UtilMisc; 43 import org.ofbiz.base.util.UtilProperties; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.webapp.stats.VisitHandler; 46 import org.ofbiz.entity.GenericDelegator; 47 import org.ofbiz.entity.GenericEntityException; 48 import org.ofbiz.entity.GenericValue; 49 import org.ofbiz.marketing.tracking.TrackingCodeEvents; 50 import org.ofbiz.product.catalog.CatalogWorker; 51 import org.ofbiz.product.store.ProductStoreWorker; 52 import org.ofbiz.service.GenericServiceException; 53 import org.ofbiz.service.LocalDispatcher; 54 import org.ofbiz.service.ModelService; 55 import org.ofbiz.service.ServiceUtil; 56 57 67 public class CheckOutEvents { 68 69 public static final String module = CheckOutEvents.class.getName(); 70 public static final String resource = "OrderUiLabels"; 71 public static final String resource_error = "OrderErrorUiLabels"; 72 73 public static String cartNotEmpty(HttpServletRequest request, HttpServletResponse response) { 74 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 75 String errMsg = null; 77 78 if (cart != null && cart.size() > 0) { 79 return "success"; 80 } else { 81 errMsg = UtilProperties.getMessage(resource, "checkevents.cart_empty", (cart != null ? cart.getLocale() : Locale.getDefault())); 82 request.setAttribute("_ERROR_MESSAGE_", errMsg); 83 return "error"; 84 } 85 } 86 87 public static String cancelOrderItem(HttpServletRequest request, HttpServletResponse response) { 88 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 89 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 90 String orderId = request.getParameter("orderId"); 91 String itemSeqId = request.getParameter("item_seq"); 92 String groupSeqId = request.getParameter("group_seq"); 93 Locale locale = UtilHttp.getLocale(request); 94 95 Map fields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", itemSeqId, "shipGroupSeqId", groupSeqId, "userLogin", userLogin); 96 Map result = null; 97 String errMsg = null; 98 99 try { 100 result = dispatcher.runSync("cancelOrderItem", fields); 101 } catch (GenericServiceException e) { 102 Debug.logError(e, module); 103 errMsg = UtilProperties.getMessage(resource, "checkevents.cannot_cancel_item", locale); 104 request.setAttribute("_ERROR_MESSAGE_", errMsg); 105 return "error"; 106 } 107 if (result.containsKey(ModelService.ERROR_MESSAGE)) { 108 request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE)); 109 return "error"; 110 } 111 112 try { 113 result = dispatcher.runSync("recreateOrderAdjustments", UtilMisc.toMap("userLogin", userLogin, "orderId", orderId)); 114 } catch (GenericServiceException e) { 115 Debug.logError(e, module); 116 errMsg = UtilProperties.getMessage(resource, "checkevents.cannot_recalc_adjustments", locale); 117 request.setAttribute("_ERROR_MESSAGE_", errMsg); 118 return "error"; 119 } 120 if (result.containsKey(ModelService.ERROR_MESSAGE)) { 121 request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE)); 122 return "error"; 123 } 124 125 return "success"; 126 } 127 128 public static String setCheckOutPages(HttpServletRequest request, HttpServletResponse response) { 129 if ("error".equals(CheckOutEvents.cartNotEmpty(request, response)) == true) { 130 return "error"; 131 } 132 133 HttpSession session = request.getSession(); 134 135 String curPage = request.getParameter("checkoutpage"); 137 Debug.logInfo("CheckoutPage: " + curPage, module); 138 139 ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); 140 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 141 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 142 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 143 GenericValue userLogin = cart.getUserLogin(); 144 if (userLogin == null) userLogin = (GenericValue) session.getAttribute("userLogin"); 145 146 if ("shippingaddress".equals(curPage) == true) { 147 String shippingContactMechId = request.getParameter("shipping_contact_mech_id"); 149 150 String taxAuthPartyGeoIds = request.getParameter("taxAuthPartyGeoIds"); 151 String partyTaxId = request.getParameter("partyTaxId"); 152 String isExempt = request.getParameter("isExempt"); 153 154 if (UtilValidate.isNotEmpty(taxAuthPartyGeoIds)) { 156 try { 157 Map createCustomerTaxAuthInfoResult = dispatcher.runSync("createCustomerTaxAuthInfo", 158 UtilMisc.toMap("partyId", cart.getPartyId(), "taxAuthPartyGeoIds", taxAuthPartyGeoIds, "partyTaxId", partyTaxId, "isExempt", isExempt, "userLogin", userLogin)); 159 ServiceUtil.getMessages(request, createCustomerTaxAuthInfoResult, null); 160 if (ServiceUtil.isError(createCustomerTaxAuthInfoResult)) { 161 return "error"; 162 } 163 } catch (GenericServiceException e) { 164 String errMsg = "Error setting customer tax info: " + e.toString(); 165 request.setAttribute("_ERROR_MESSAGE_", errMsg); 166 return "error"; 167 } 168 } 169 170 Map callResult = checkOutHelper.setCheckOutShippingAddress(shippingContactMechId); 171 ServiceUtil.getMessages(request, callResult, null); 172 173 if (!(ServiceUtil.isError(callResult))) { 174 curPage = "shippingoptions"; 176 } 177 } else if ("shippingoptions".equals(curPage) == true) { 178 String shippingMethod = request.getParameter("shipping_method"); 180 String correspondingPoId = request.getParameter("corresponding_po_id"); 181 String shippingInstructions = request.getParameter("shipping_instructions"); 182 String orderAdditionalEmails = request.getParameter("order_additional_emails"); 183 String maySplit = request.getParameter("may_split"); 184 String giftMessage = request.getParameter("gift_message"); 185 String isGift = request.getParameter("is_gift"); 186 String internalCode = request.getParameter("internalCode"); 187 String shipBeforeDate = request.getParameter("shipBeforeDate"); 188 String shipAfterDate = request.getParameter("shipAfterDate"); 189 Map callResult = checkOutHelper.setCheckOutShippingOptions(shippingMethod, correspondingPoId, 190 shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift, internalCode, shipBeforeDate, shipAfterDate); 191 192 ServiceUtil.getMessages(request, callResult, null); 193 194 if (!(callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) { 195 curPage = "payment"; 197 } 198 } else if ("payment".equals(curPage) == true) { 199 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 201 DecimalFormat formatter = new DecimalFormat (currencyFormat); 202 203 Map selectedPaymentMethods = getSelectedPaymentMethods(request); 205 if (selectedPaymentMethods == null) { 206 return "error"; 207 } 208 209 String billingAccountId = request.getParameter("billingAccountId"); 210 String billingAcctAmtStr = request.getParameter("amount_" + billingAccountId); 211 Double billingAccountAmt = null; 212 if (billingAcctAmtStr != null) { 214 try { 215 billingAccountAmt = new Double (formatter.parse(billingAcctAmtStr).doubleValue()); 216 } catch (ParseException e) { 217 Debug.logError(e, module); 218 Map messageMap = UtilMisc.toMap("billingAccountId", billingAccountId); 219 String errMsg = UtilProperties.getMessage(resource, "checkevents.invalid_amount_set_for_billing_account", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 220 request.setAttribute("_ERROR_MESSAGE_", errMsg); 221 return "error"; 222 } 223 } 224 225 List singleUsePayments = new ArrayList (); 226 227 Map params = UtilHttp.getParameterMap(request); 229 Map gcResult = checkOutHelper.checkGiftCard(params, selectedPaymentMethods); 230 ServiceUtil.getMessages(request, gcResult, null); 231 if (gcResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { 232 return "error"; 233 } else { 234 String gcPaymentMethodId = (String ) gcResult.get("paymentMethodId"); 235 Double gcAmount = (Double ) gcResult.get("amount"); 236 if (gcPaymentMethodId != null) { 237 selectedPaymentMethods.put(gcPaymentMethodId, gcAmount); 238 if ("Y".equalsIgnoreCase(request.getParameter("singleUseGiftCard"))) { 239 singleUsePayments.add(gcPaymentMethodId); 240 } 241 } 242 } 243 244 Map callResult = checkOutHelper.setCheckOutPayment(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt); 245 ServiceUtil.getMessages(request, callResult, null); 246 247 if (!(callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) { 248 curPage = "confirm"; 250 } 251 } else { 252 curPage = determineInitialCheckOutPage(cart); 253 } 254 255 return curPage; 256 } 257 258 private static final String DEFAULT_INIT_CHECKOUT_PAGE = "shippingaddress"; 259 260 265 public static String determineInitialCheckOutPage(ShoppingCart cart) { 266 String page = DEFAULT_INIT_CHECKOUT_PAGE; 267 if (cart == null) return page; 268 269 if (!cart.shippingApplies()) { 271 cart.setShipmentMethodTypeId("NO_SHIPPING"); 272 cart.setCarrierPartyId("_NA_"); 273 page = "payment"; 274 } 275 276 return page; 277 } 278 279 public static String setCheckOutError(HttpServletRequest request, HttpServletResponse response) { 280 String currentPage = request.getParameter("checkoutpage"); 281 if (currentPage == null || currentPage.length() == 0) { 282 return "error"; 283 } else { 284 return currentPage; 285 } 286 } 287 288 public static String setPartialCheckOutOptions(HttpServletRequest request, HttpServletResponse response) { 289 String resp = setCheckOutOptions(request, response); 290 request.setAttribute("_ERROR_MESSAGE_", null); 291 return "success"; 292 } 293 294 public static String checkPaymentMethods(HttpServletRequest request, HttpServletResponse response) { 295 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 296 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 297 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 298 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 299 Map resp = checkOutHelper.validatePaymentMethods(); 300 if (ServiceUtil.isError(resp)) { 301 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(resp)); 302 return "error"; 303 } 304 return "success"; 305 } 306 307 public static Map getSelectedPaymentMethods(HttpServletRequest request) { 308 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 309 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 311 DecimalFormat formatter = new DecimalFormat (currencyFormat); 312 Map selectedPaymentMethods = new HashMap (); 313 String [] paymentMethods = request.getParameterValues("checkOutPaymentId"); 314 String errMsg = null; 315 316 if (paymentMethods != null) { 317 for (int i = 0; i < paymentMethods.length; i++) { 318 String amountStr = request.getParameter("amount_" + paymentMethods[i]); 319 Double amount = null; 320 if (amountStr != null && amountStr.length() > 0 && !"REMAINING".equals(amountStr)) { 321 try { 322 amount = new Double (formatter.parse(amountStr).doubleValue()); 323 } catch (ParseException e) { 324 Debug.logError(e, module); 325 errMsg = UtilProperties.getMessage(resource, "checkevents.invalid_amount_set_for_payment_method", (cart != null ? cart.getLocale() : Locale.getDefault())); 326 request.setAttribute("_ERROR_MESSAGE_", errMsg); 327 return null; 328 } 329 } 330 selectedPaymentMethods.put(paymentMethods[i], amount); 331 } 332 } 333 Debug.logInfo("Selected Payment Methods : " + selectedPaymentMethods, module); 334 return selectedPaymentMethods; 335 } 336 337 public static String setCheckOutOptions(HttpServletRequest request, HttpServletResponse response) { 338 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 339 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 340 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 341 342 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 344 DecimalFormat formatter = new DecimalFormat (currencyFormat); 345 346 Map selectedPaymentMethods = getSelectedPaymentMethods(request); 348 if (selectedPaymentMethods == null) { 349 return "error"; 350 } 351 352 String shippingMethod = request.getParameter("shipping_method"); 353 String shippingContactMechId = request.getParameter("shipping_contact_mech_id"); 354 355 String taxAuthPartyGeoIds = request.getParameter("taxAuthPartyGeoIds"); 356 String partyTaxId = request.getParameter("partyTaxId"); 357 String isExempt = request.getParameter("isExempt"); 358 359 String correspondingPoId = request.getParameter("corresponding_po_id"); 360 String shippingInstructions = request.getParameter("shipping_instructions"); 361 String orderAdditionalEmails = request.getParameter("order_additional_emails"); 362 String maySplit = request.getParameter("may_split"); 363 String giftMessage = request.getParameter("gift_message"); 364 String isGift = request.getParameter("is_gift"); 365 String internalCode = request.getParameter("internalCode"); 366 String shipBeforeDate = request.getParameter("shipBeforeDate"); 367 String shipAfterDate = request.getParameter("shipAfterDate"); 368 List singleUsePayments = new ArrayList (); 369 370 if (UtilValidate.isNotEmpty(taxAuthPartyGeoIds)) { 372 try { 373 Map createCustomerTaxAuthInfoResult = dispatcher.runSync("createCustomerTaxAuthInfo", 374 UtilMisc.toMap("partyId", cart.getPartyId(), "taxAuthPartyGeoIds", taxAuthPartyGeoIds, "partyTaxId", partyTaxId, "isExempt", isExempt)); 375 ServiceUtil.getMessages(request, createCustomerTaxAuthInfoResult, null); 376 if (ServiceUtil.isError(createCustomerTaxAuthInfoResult)) { 377 return "error"; 378 } 379 } catch (GenericServiceException e) { 380 String errMsg = "Error setting customer tax info: " + e.toString(); 381 request.setAttribute("_ERROR_MESSAGE_", errMsg); 382 return "error"; 383 } 384 } 385 386 String billingAccountId = request.getParameter("billingAccountId"); 388 String billingAcctAmtStr = request.getParameter("amount_" + billingAccountId); 389 Double billingAccountAmt = null; 390 if (billingAcctAmtStr != null) { 392 try { 393 billingAccountAmt = new Double (formatter.parse(billingAcctAmtStr).doubleValue()); 394 } catch (ParseException e) { 395 Debug.logError(e, module); 396 Map messageMap = UtilMisc.toMap("billingAccountId", billingAccountId); 397 String errMsg = UtilProperties.getMessage(resource, "checkevents.invalid_amount_set_for_billing_account", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 398 request.setAttribute("_ERROR_MESSAGE_", errMsg); 399 return "error"; 400 } 401 } 402 403 Map params = UtilHttp.getParameterMap(request); 405 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 406 407 Map gcResult = checkOutHelper.checkGiftCard(params, selectedPaymentMethods); 409 ServiceUtil.getMessages(request, gcResult, null); 410 if (ServiceUtil.isError(gcResult)) { 411 return "error"; 412 } 413 414 String gcPaymentMethodId = (String ) gcResult.get("paymentMethodId"); 415 Double gcAmount = (Double ) gcResult.get("amount"); 416 if (gcPaymentMethodId != null) { 417 selectedPaymentMethods.put(gcPaymentMethodId, gcAmount); 418 if ("Y".equalsIgnoreCase(request.getParameter("singleUseGiftCard"))) { 419 singleUsePayments.add(gcPaymentMethodId); 420 } 421 } 422 423 Map optResult = checkOutHelper.setCheckOutOptions(shippingMethod, shippingContactMechId, selectedPaymentMethods, 424 singleUsePayments, billingAccountId, billingAccountAmt, correspondingPoId, shippingInstructions, 425 orderAdditionalEmails, maySplit, giftMessage, isGift, internalCode, shipBeforeDate, shipAfterDate); 426 427 ServiceUtil.getMessages(request, optResult, null); 428 if (ServiceUtil.isError(optResult)) { 429 return "error"; 430 } 431 432 return "success"; 433 } 434 435 public static String createOrder(HttpServletRequest request, HttpServletResponse response) { 437 HttpSession session = request.getSession(); 438 ShoppingCart cart = ShoppingCartEvents.getCartObject(request); 439 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 440 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 441 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 442 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 443 Map callResult; 444 445 session.removeAttribute("_QUICK_REORDER_PRODUCTS_"); 447 448 boolean areOrderItemsExploded = explodeOrderItems(delegator, cart); 449 450 List trackingCodeOrders = TrackingCodeEvents.makeTrackingCodeOrders(request); 452 String distributorId = (String ) session.getAttribute("_DISTRIBUTOR_ID_"); 453 String affiliateId = (String ) session.getAttribute("_AFFILIATE_ID_"); 454 String visitId = VisitHandler.getVisitId(session); 455 String webSiteId = CatalogWorker.getWebSiteId(request); 456 457 callResult = checkOutHelper.createOrder(userLogin, distributorId, affiliateId, trackingCodeOrders, areOrderItemsExploded, visitId, webSiteId); 458 if (callResult != null) { 459 ServiceUtil.getMessages(request, callResult, null); 460 if (ServiceUtil.isError(callResult)) { 461 return "error"; 463 } 464 if (callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) { 465 String orderId = cart.getOrderId(); 467 request.setAttribute("orderId", orderId); 468 request.setAttribute("orderAdditionalEmails", cart.getOrderAdditionalEmails()); 469 } 470 } 471 472 return cart.getOrderType().toLowerCase(); 473 } 474 475 public static String calcTax(HttpServletRequest request, HttpServletResponse response) { 477 try { 478 calcTax(request); 479 } catch (GeneralException e) { 480 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 481 return "error"; 482 } 483 return "success"; 484 } 485 486 private static void calcTax(HttpServletRequest request) throws GeneralException { 488 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 489 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 490 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 491 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 492 493 checkOutHelper.calcAndAddTax(); 495 } 496 497 public static boolean explodeOrderItems(GenericDelegator delegator, ShoppingCart cart) { 498 if (cart == null) return false; 499 GenericValue productStore = ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator); 500 if (productStore == null || productStore.get("explodeOrderItems") == null) { 501 return false; 502 } 503 return productStore.getBoolean("explodeOrderItems").booleanValue(); 504 } 505 506 public static String processPayment(HttpServletRequest request, HttpServletResponse response) { 508 int failureCode = 0; 510 try { 511 if (!processPayment(request)) { 512 failureCode = 1; 513 } 514 } catch (GeneralException e) { 515 Debug.logError(e, module); 516 ServiceUtil.setMessages(request, e.getMessage(), null, null); 517 failureCode = 2; 518 } catch (GeneralRuntimeException e) { 519 Debug.logError(e, module); 520 ServiceUtil.setMessages(request, e.getMessage(), null, null); 521 } 522 523 switch (failureCode) { 525 case 0: 526 return "success"; 527 case 1: 528 return "fail"; 529 default: 530 return "error"; 531 } 532 } 533 534 private static boolean processPayment(HttpServletRequest request) throws GeneralException { 535 HttpSession session = request.getSession(); 536 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 537 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 538 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 539 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 540 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 541 542 boolean holdOrder = cart.getHoldOrder(); 544 545 GenericValue productStore = ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator); 547 Map callResult = checkOutHelper.processPayment(productStore, userLogin, false, holdOrder); 548 549 ServiceUtil.getMessages(request, callResult, null); 551 552 List messages = (List ) callResult.get("authResultMsgs"); 554 if (messages != null && messages.size() > 0) { 555 request.setAttribute("_EVENT_MESSAGE_LIST_", messages); 556 } 557 558 return (callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)); 560 } 561 562 public static String checkOrderBlacklist(HttpServletRequest request, HttpServletResponse response) { 563 HttpSession session = request.getSession(); 564 ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); 565 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 566 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 567 CheckOutHelper checkOutHelper = new CheckOutHelper(null, delegator, cart); 568 String result; 569 570 Map callResult = checkOutHelper.checkOrderBlacklist(userLogin); 571 if (callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { 572 result = (String ) callResult.get(ModelService.ERROR_MESSAGE); 573 } else { 574 result = (String ) callResult.get(ModelService.SUCCESS_MESSAGE); 575 } 576 577 return result; 578 } 579 580 public static String failedBlacklistCheck(HttpServletRequest request, HttpServletResponse response) { 581 HttpSession session = request.getSession(); 582 ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart"); 583 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 584 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 585 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 586 String result; 587 588 GenericValue productStore = ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator); 590 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 591 Map callResult = checkOutHelper.failedBlacklistCheck(userLogin, productStore); 592 593 ServiceUtil.getMessages(request, callResult, null); 595 596 session.invalidate(); 598 599 if (callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { 601 result = (String ) callResult.get(ModelService.ERROR_MESSAGE); 602 request.setAttribute("_ERROR_MESSAGE_", result); 603 result = "error"; 604 } else { 605 result = (String ) callResult.get(ModelService.ERROR_MESSAGE); 606 request.setAttribute("_ERROR_MESSAGE_", result); 607 result = "success"; 608 } 609 return result; 610 } 611 612 public static String checkExternalPayment(HttpServletRequest request, HttpServletResponse response) { 613 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 616 String result; 617 618 String orderId = (String ) request.getAttribute("orderId"); 619 CheckOutHelper checkOutHelper = new CheckOutHelper(null, delegator, null); 620 Map callResult = checkOutHelper.checkExternalPayment(orderId); 621 622 ServiceUtil.getMessages(request, callResult, null); 624 625 result = (String ) callResult.get("type"); 627 return result; 628 } 629 630 public static String finalizeOrderEntry(HttpServletRequest request, HttpServletResponse response) { 631 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 632 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 633 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 634 635 Map paramMap = UtilHttp.getParameterMap(request); 636 Boolean offlinePayments; 637 String shippingContactMechId = null; 638 String shippingMethod = null; 639 String shippingInstructions = null; 640 String maySplit = null; 641 String giftMessage = null; 642 String isGift = null; 643 String internalCode = null; 644 String methodType = null; 645 String checkOutPaymentId = null; 646 String singleUsePayment = null; 647 String appendPayment = null; 648 String shipBeforeDate = null; 649 String shipAfterDate = null; 650 651 String mode = request.getParameter("finalizeMode"); 652 Debug.logInfo("FinalizeMode: " + mode, module); 653 if (mode == null) { 655 return "customer"; 656 } 657 658 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 660 661 if (userLogin == null) { 663 request.getSession().removeAttribute("autoUserLogin"); 665 request.getSession().removeAttribute("autoName"); 666 try { 668 cart.setAutoUserLogin(null, dispatcher); 669 } catch (CartItemModifyException e) { 670 Debug.logError(e, module); 671 } 672 } 673 674 if (mode != null && mode.equals("default")) { 676 cart.setDefaultCheckoutOptions(dispatcher); 677 } 678 679 if (mode != null && mode.equals("cust")) { 681 String partyId = (String ) request.getAttribute("partyId"); 682 if (partyId != null) { 683 cart.setOrderPartyId(partyId); 684 if (userLogin == null) { 686 try { 687 userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "anonymous")); 688 } catch (GenericEntityException e) { 689 Debug.logError(e, module); 690 } 691 if (userLogin != null) { 692 userLogin.set("partyId", partyId); 693 } 694 request.getSession().setAttribute("userLogin", userLogin); 695 try { 696 cart.setUserLogin(userLogin, dispatcher); 697 } catch (CartItemModifyException e) { 698 Debug.logError(e, module); 699 } 700 Debug.logInfo("Anonymous user-login has been activated", module); 701 } 702 } 703 } 704 705 if (mode != null && mode.equals("addpty")) { 706 cart.setAttribute("addpty", "Y"); 707 } 708 709 if (mode != null && mode.equals("term")) { 710 cart.setOrderTermSet(true); 711 } 712 713 boolean isAnonymousCheckout = false; 715 if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) { 716 isAnonymousCheckout = true; 717 } 718 719 shippingContactMechId = request.getParameter("shipping_contact_mech_id"); 721 if (shippingContactMechId == null) { 722 shippingContactMechId = (String ) request.getAttribute("contactMechId"); 723 } 724 725 shippingMethod = request.getParameter("shipping_method"); 727 shippingInstructions = request.getParameter("shipping_instructions"); 728 maySplit = request.getParameter("may_split"); 729 giftMessage = request.getParameter("gift_message"); 730 isGift = request.getParameter("is_gift"); 731 internalCode = request.getParameter("internalCode"); 732 shipBeforeDate = request.getParameter("shipBeforeDate"); 733 shipAfterDate = request.getParameter("shipAfterDate"); 734 Locale locale = UtilHttp.getLocale(request); 735 736 methodType = request.getParameter("paymentMethodType"); 738 739 checkOutPaymentId = request.getParameter("checkOutPaymentId"); 741 if (checkOutPaymentId == null) { 742 checkOutPaymentId = (String ) request.getAttribute("paymentMethodId"); 743 } 744 745 if ("offline".equals(methodType)) { 747 Debug.log("Changing mode from->to: " + mode + "->payment", module); 748 checkOutPaymentId = "EXT_OFFLINE"; 749 mode = "payment"; 750 } 751 singleUsePayment = request.getParameter("singleUsePayment"); 752 appendPayment = request.getParameter("appendPayment"); 753 boolean isSingleUsePayment = singleUsePayment != null && "Y".equalsIgnoreCase(singleUsePayment) ? true : false; 754 boolean doAppendPayment = appendPayment != null && "Y".equalsIgnoreCase(appendPayment) ? true : false; 755 756 CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart); 757 758 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 760 DecimalFormat formatter = new DecimalFormat (currencyFormat); 761 762 Map selectedPaymentMethods = getSelectedPaymentMethods(request); 764 if (selectedPaymentMethods == null) { 765 return "error"; 766 } 767 768 String billingAccountId = request.getParameter("billingAccountId"); 769 String billingAcctAmtStr = request.getParameter("amount_" + billingAccountId); 770 Double billingAccountAmt = null; 771 772 if (billingAcctAmtStr != null) { 774 try { 775 billingAccountAmt = new Double (formatter.parse(billingAcctAmtStr).doubleValue()); 776 } catch (ParseException e) { 777 Debug.logError(e, module); 778 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderInvalidAmountSetForBillingAccount", UtilMisc.toMap("billingAccountId",billingAccountId), locale)); 779 return "error"; 780 } 781 } 782 783 if (mode != null && mode.equals("payment")) { 784 checkOutHelper.setCheckOutPayment(selectedPaymentMethods, null, billingAccountId, billingAccountAmt); 785 } 786 787 Map callResult = checkOutHelper.finalizeOrderEntry(mode, shippingContactMechId, shippingMethod, shippingInstructions, 788 maySplit, giftMessage, isGift, methodType, checkOutPaymentId, isSingleUsePayment, doAppendPayment, paramMap, 789 internalCode, shipBeforeDate, shipAfterDate); 790 791 ServiceUtil.getMessages(request, callResult, null); 793 794 if (callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { 796 return "error"; 797 } 798 799 boolean requireCustomer = true; 802 boolean requireShipping = true; 803 boolean requireOptions = true; 804 boolean requirePayment = !cart.getOrderType().equals("PURCHASE_ORDER"); 805 boolean requireTerm = cart.getOrderType().equals("PURCHASE_ORDER"); 806 boolean requireAdditionalParty = isAnonymousCheckout; 807 808 if (userLogin != null && !"anonymous".equals(userLogin.getString("userLoginId"))) { 810 String requireCustomerStr = request.getParameter("finalizeReqCustInfo"); 811 String requireShippingStr = request.getParameter("finalizeReqShipInfo"); 812 String requireOptionsStr = request.getParameter("finalizeReqOptions"); 813 String requirePaymentStr = request.getParameter("finalizeReqPayInfo"); 814 String requireTermStr = request.getParameter("finalizeReqTermInfo"); 815 String requireAdditionalPartyStr = request.getParameter("finalizeReqAdditionalParty"); 816 requireCustomer = requireCustomerStr == null || requireCustomerStr.equalsIgnoreCase("true"); 817 requireShipping = requireShippingStr == null || requireShippingStr.equalsIgnoreCase("true"); 818 requireOptions = requireOptionsStr == null || requireOptionsStr.equalsIgnoreCase("true"); 819 if (requirePayment) { 820 requirePayment = requirePaymentStr == null || requirePaymentStr.equalsIgnoreCase("true"); 821 } 822 if (requireTerm) { 823 requireTerm = requireTermStr == null || requireTermStr.equalsIgnoreCase("true"); 824 } 825 requireAdditionalParty = requireAdditionalPartyStr == null || requireAdditionalPartyStr.equalsIgnoreCase("true"); 826 } 827 828 String shipContactMechId = cart.getShippingContactMechId(); 829 String customerPartyId = cart.getPartyId(); 830 String shipmentMethodTypeId = cart.getShipmentMethodTypeId(); 831 List paymentMethodIds = cart.getPaymentMethodIds(); 832 List paymentMethodTypeIds = cart.getPaymentMethodTypeIds(); 833 834 if (requireCustomer && (customerPartyId == null || customerPartyId.equals("_NA_"))) { 835 return "customer"; 836 } 837 838 if (requireShipping && shipContactMechId == null) { 839 return "shipping"; 840 } 841 842 if (requireOptions && shipmentMethodTypeId == null) { 843 return "options"; 844 } 845 846 if (requireTerm && !cart.isOrderTermSet()) { 847 return "term"; 848 } 849 if (requirePayment && (paymentMethodIds == null || paymentMethodIds.size() == 0) && (paymentMethodTypeIds == null || paymentMethodTypeIds.size() == 0)) { 850 return "payment"; 851 } 852 853 if (requireAdditionalParty && cart.getAttribute("addpty") == null) { 854 return "addparty"; 855 } 856 857 if (isSingleUsePayment) { 858 return "paysplit"; 859 } 860 861 String checkoutGoTo = request.getParameter("checkoutGoTo"); 863 if (UtilValidate.isNotEmpty(checkoutGoTo)) { 864 return checkoutGoTo; 865 } 866 867 if ("SALES_ORDER".equals(cart.getOrderType())) { 868 return "sales"; 869 } else { 870 return "po"; 871 } 872 } 873 874 public static String finalizeOrderEntryError(HttpServletRequest request, HttpServletResponse response) { 875 String finalizePage = request.getParameter("finalizeMode"); 876 if (finalizePage == null || finalizePage.length() == 0) { 877 return "error"; 878 } else { 879 return finalizePage; 880 } 881 } 882 } 883 | Popular Tags |