1 24 package org.ofbiz.order.shoppingcart; 25 26 import java.math.BigDecimal ; 27 import java.sql.Timestamp ; 28 import java.text.DecimalFormat ; 29 import java.text.ParseException ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import org.ofbiz.base.util.*; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 import org.ofbiz.entity.condition.EntityExpr; 44 import org.ofbiz.entity.condition.EntityFieldValue; 45 import org.ofbiz.entity.condition.EntityFunction; 46 import org.ofbiz.entity.condition.EntityOperator; 47 import org.ofbiz.entity.util.EntityUtil; 48 import org.ofbiz.order.finaccount.FinAccountHelper; 49 import org.ofbiz.order.order.OrderChangeHelper; 50 import org.ofbiz.party.contact.ContactHelper; 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 68 public class CheckOutHelper { 69 70 public static final String module = CheckOutHelper.class.getName(); 71 public static final String resource = "OrderUiLabels"; 72 public static final String resource_error = "OrderErrorUiLabels"; 73 74 protected LocalDispatcher dispatcher = null; 75 protected GenericDelegator delegator = null; 76 protected ShoppingCart cart = null; 77 78 public CheckOutHelper(LocalDispatcher dispatcher, GenericDelegator delegator, ShoppingCart cart) { 79 this.delegator = delegator; 80 this.dispatcher = dispatcher; 81 this.cart = cart; 82 } 83 84 public Map setCheckOutShippingAddress(String shippingContactMechId) { 85 List errorMessages = new ArrayList (); 86 Map result; 87 String errMsg = null; 88 89 if (this.cart != null && this.cart.size() > 0) { 90 errorMessages.addAll(setCheckOutShippingAddressInternal(shippingContactMechId)); 91 } else { 92 errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault())); 93 errorMessages.add(errMsg); 94 } 95 if (errorMessages.size() == 1) { 96 result = ServiceUtil.returnError(errorMessages.get(0).toString()); 97 } else if (errorMessages.size() > 0) { 98 result = ServiceUtil.returnError(errorMessages); 99 } else { 100 result = ServiceUtil.returnSuccess(); 101 } 102 103 return result; 104 } 105 106 private List setCheckOutShippingAddressInternal(String shippingContactMechId) { 107 List errorMessages = new ArrayList (); 108 String errMsg = null; 109 110 if (UtilValidate.isNotEmpty(shippingContactMechId)) { 112 this.cart.setShippingContactMechId(shippingContactMechId); 113 } else if (cart.shippingApplies()) { 114 errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_destination", (cart != null ? cart.getLocale() : Locale.getDefault())); 116 errorMessages.add(errMsg); 117 } 118 119 return errorMessages; 120 } 121 122 public Map setCheckOutShippingOptions(String shippingMethod, String correspondingPoId, String shippingInstructions, 123 String orderAdditionalEmails, String maySplit, String giftMessage, String isGift, String internalCode, String shipBeforeDate, String shipAfterDate ) { 124 List errorMessages = new ArrayList (); 125 Map result; 126 String errMsg = null; 127 128 if (this.cart != null && this.cart.size() > 0) { 129 errorMessages.addAll(setCheckOutShippingOptionsInternal(shippingMethod, correspondingPoId, 130 shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift, internalCode, shipBeforeDate, shipAfterDate)); 131 } else { 132 errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault())); 133 errorMessages.add(errMsg); 134 } 135 136 if (errorMessages.size() == 1) { 137 result = ServiceUtil.returnError(errorMessages.get(0).toString()); 138 } else if (errorMessages.size() > 0) { 139 result = ServiceUtil.returnError(errorMessages); 140 } else { 141 result = ServiceUtil.returnSuccess(); 142 } 143 144 return result; 145 } 146 147 private List setCheckOutShippingOptionsInternal(String shippingMethod, String correspondingPoId, String shippingInstructions, 148 String orderAdditionalEmails, String maySplit, String giftMessage, String isGift, String internalCode, String shipBeforeDate, String shipAfterDate ) { 149 List errorMessages = new ArrayList (); 150 String errMsg = null; 151 152 if (UtilValidate.isNotEmpty(shippingMethod)) { 154 int delimiterPos = shippingMethod.indexOf('@'); 155 String shipmentMethodTypeId = null; 156 String carrierPartyId = null; 157 158 if (delimiterPos > 0) { 159 shipmentMethodTypeId = shippingMethod.substring(0, delimiterPos); 160 carrierPartyId = shippingMethod.substring(delimiterPos + 1); 161 } 162 163 this.cart.setShipmentMethodTypeId(shipmentMethodTypeId); 164 this.cart.setCarrierPartyId(carrierPartyId); 165 } else if (cart.shippingApplies()) { 166 errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_method", (cart != null ? cart.getLocale() : Locale.getDefault())); 168 errorMessages.add(errMsg); 169 } 170 171 this.cart.setShippingInstructions(shippingInstructions); 173 174 if (UtilValidate.isNotEmpty(maySplit)) { 175 cart.setMaySplit(Boolean.valueOf(maySplit)); 176 } else { 177 errMsg = UtilProperties.getMessage(resource,"checkhelper.select_splitting_preference", (cart != null ? cart.getLocale() : Locale.getDefault())); 178 errorMessages.add(errMsg); 179 } 180 181 this.cart.setGiftMessage(giftMessage); 183 184 if (UtilValidate.isNotEmpty(isGift)) { 185 cart.setIsGift(Boolean.valueOf(isGift)); 186 } else { 187 errMsg = UtilProperties.getMessage(resource, "checkhelper.specify_if_order_is_gift", (cart != null ? cart.getLocale() : Locale.getDefault())); 188 errorMessages.add(errMsg); 189 } 190 191 this.cart.setInternalCode(internalCode); 193 194 if (shipBeforeDate != null && shipBeforeDate.length() > 0) { 195 if (UtilValidate.isDate(shipBeforeDate)) { 196 cart.setShipBeforeDate(UtilDateTime.toTimestamp(shipBeforeDate)); 197 } else { 198 errMsg = UtilProperties.getMessage(resource, "checkhelper.specify_if_shipBeforeDate_is_date", (cart != null ? cart.getLocale() : Locale.getDefault())); 199 errorMessages.add(errMsg); 200 } 201 } 202 203 if (shipAfterDate != null && shipAfterDate.length() > 0) { 204 if (UtilValidate.isDate(shipAfterDate)) { 205 cart.setShipAfterDate(UtilDateTime.toTimestamp(shipAfterDate)); 206 } else { 207 errMsg = UtilProperties.getMessage(resource, "checkhelper.specify_if_shipAfterDate_is_date", (cart != null ? cart.getLocale() : Locale.getDefault())); 208 errorMessages.add(errMsg); 209 } 210 } 211 212 this.cart.setOrderAdditionalEmails(orderAdditionalEmails); 214 215 if (UtilValidate.isNotEmpty(correspondingPoId)) { 217 this.cart.setPoNumber(correspondingPoId); 218 } else { 219 this.cart.setPoNumber(null); 220 } 221 222 return errorMessages; 223 } 224 225 public Map setCheckOutPayment(Map selectedPaymentMethods, List singleUsePayments, String billingAccountId, Double billingAccountAmt) { 226 List errorMessages = new ArrayList (); 227 Map result; 228 String errMsg = null; 229 230 if (this.cart != null && this.cart.size() > 0) { 231 errorMessages.addAll(setCheckOutPaymentInternal(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt)); 232 } else { 233 errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault())); 234 errorMessages.add(errMsg); 235 } 236 237 if (errorMessages.size() == 1) { 238 result = ServiceUtil.returnError(errorMessages.get(0).toString()); 239 } else if (errorMessages.size() > 0) { 240 result = ServiceUtil.returnError(errorMessages); 241 } else { 242 result = ServiceUtil.returnSuccess(); 243 } 244 245 return result; 246 } 247 248 private List setCheckOutPaymentInternal(Map selectedPaymentMethods, List singleUsePayments, String billingAccountId, Double billingAccountAmt) { 249 List errorMessages = new ArrayList (); 250 String errMsg = null; 251 252 if (singleUsePayments == null) { 253 singleUsePayments = new ArrayList (); 254 } 255 256 if (billingAccountId != null && billingAccountAmt != null && !billingAccountId.equals("_NA_")) { 258 cart.setBillingAccount(billingAccountId, billingAccountAmt.doubleValue()); 259 } else { 260 cart.setBillingAccount(null, 0.00); 261 } 262 263 if (selectedPaymentMethods != null && selectedPaymentMethods.size() > 0) { 265 cart.clearPayments(); 267 268 if (selectedPaymentMethods.containsKey("EXT_BILLACT")) { 270 double accountCredit = this.availableAccountBalance(cart.getBillingAccountId()); 271 if (cart.getGrandTotal() > accountCredit) { 273 errMsg = UtilProperties.getMessage(resource,"checkhelper.insufficient_credit_available_on_account", 274 (cart != null ? cart.getLocale() : Locale.getDefault())); 275 errorMessages.add(errMsg); 276 } 277 } 278 279 Set paymentMethods = selectedPaymentMethods.keySet(); 280 Iterator i = paymentMethods.iterator(); 281 while (i.hasNext()) { 282 String checkOutPaymentId = (String ) i.next(); 283 284 Double paymentAmount = null; 286 if (selectedPaymentMethods.get(checkOutPaymentId) != null) { 287 paymentAmount = (Double ) selectedPaymentMethods.get(checkOutPaymentId); 288 } 289 290 boolean singleUse = singleUsePayments.contains(checkOutPaymentId); 291 cart.addPaymentAmount(checkOutPaymentId, paymentAmount, singleUse); 292 } 293 } else if (cart.getGrandTotal() != 0.00) { 294 errMsg = UtilProperties.getMessage(resource,"checkhelper.select_method_of_payment", 296 (cart != null ? cart.getLocale() : Locale.getDefault())); 297 errorMessages.add(errMsg); 298 } 299 300 return errorMessages; 301 } 302 303 public Map setCheckOutDates(Timestamp shipBefore, Timestamp shipAfter) { 304 List errorMessages = new ArrayList (); 305 Map result = null; 306 String errMsg = null; 307 308 if (this.cart != null && this.cart.size() > 0) { 309 this.cart.setShipBeforeDate(shipBefore); 310 this.cart.setShipAfterDate(shipAfter); 311 } else { 312 errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", 313 (cart != null ? cart.getLocale() : Locale.getDefault())); 314 errorMessages.add(errMsg); 315 } 316 317 if (errorMessages.size() == 1) { 318 result = ServiceUtil.returnError(errorMessages.get(0).toString()); 319 } else if (errorMessages.size() > 0) { 320 result = ServiceUtil.returnError(errorMessages); 321 } else { 322 result = ServiceUtil.returnSuccess(); 323 } 324 return result; 325 } 326 327 328 public Map setCheckOutOptions(String shippingMethod, String shippingContactMechId, Map selectedPaymentMethods, 329 List singleUsePayments, String billingAccountId, Double billingAccountAmt, String correspondingPoId, 330 String shippingInstructions, String orderAdditionalEmails, String maySplit, String giftMessage, String isGift, String internalCode, String shipBeforeDate, String shipAfterDate) { 331 List errorMessages = new ArrayList (); 332 Map result = null; 333 String errMsg = null; 334 335 336 if (this.cart != null && this.cart.size() > 0) { 337 errorMessages.addAll(setCheckOutShippingOptionsInternal(shippingMethod, correspondingPoId, 339 shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift, internalCode, shipBeforeDate, shipAfterDate)); 340 341 errorMessages.addAll(setCheckOutShippingAddressInternal(shippingContactMechId)); 343 344 errorMessages.addAll(setCheckOutPaymentInternal(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt)); 346 347 } else { 348 errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault())); 349 errorMessages.add(errMsg); 350 } 351 352 if (errorMessages.size() == 1) { 353 result = ServiceUtil.returnError(errorMessages.get(0).toString()); 354 } else if (errorMessages.size() > 0) { 355 result = ServiceUtil.returnError(errorMessages); 356 } else { 357 result = ServiceUtil.returnSuccess(); 358 } 359 360 return result; 361 } 362 363 public Map checkGiftCard(Map params, Map selectedPaymentMethods) { 364 List errorMessages = new ArrayList (); 365 Map errorMaps = new HashMap (); 366 Map result = new HashMap (); 367 String errMsg = null; 368 if (params.get("addGiftCard") != null) { 370 String gcNum = (String ) params.get("giftCardNumber"); 371 String gcPin = (String ) params.get("giftCardPin"); 372 String gcAmt = (String ) params.get("giftCardAmount"); 373 double gcAmount = -1; 374 375 boolean gcFieldsOkay = true; 376 if (gcNum == null || gcNum.length() == 0) { 377 errMsg = UtilProperties.getMessage(resource,"checkhelper.enter_gift_card_number", (cart != null ? cart.getLocale() : Locale.getDefault())); 378 errorMessages.add(errMsg); 379 gcFieldsOkay = false; 380 } 381 if (cart.isPinRequiredForGC(delegator)) { 382 if ((gcPin == null) || (gcPin.length() == 0)) { 384 errMsg = UtilProperties.getMessage(resource,"checkhelper.enter_gift_card_pin_number", (cart != null ? cart.getLocale() : Locale.getDefault())); 385 errorMessages.add(errMsg); 386 gcFieldsOkay = false; 387 } 388 } 389 if (cart.isValidateGCFinAccount(delegator)) { 391 try { 392 if (!cart.isPinRequiredForGC(delegator)) { 394 GenericValue finAccount = FinAccountHelper.getFinAccountFromCode(gcNum, delegator); 395 if (finAccount == null) { 396 errMsg = UtilProperties.getMessage(resource,"checkhelper.gift_card_does_not_exist", (cart != null ? cart.getLocale() : Locale.getDefault())); 397 errorMessages.add(errMsg); 398 gcFieldsOkay = false; 399 } else if ((FinAccountHelper.getAvailableBalance(finAccount.getString("finAccountId"), cart.getCurrency(), delegator) == null) || 400 !(FinAccountHelper.getAvailableBalance(finAccount.getString("finAccountId"), cart.getCurrency(), delegator).compareTo(FinAccountHelper.ZERO) == 1)) { 401 errMsg = UtilProperties.getMessage(resource,"checkhelper.gift_card_has_no_value", (cart != null ? cart.getLocale() : Locale.getDefault())); 403 errorMessages.add(errMsg); 404 gcFieldsOkay = false; 405 } 406 } 407 } catch (GenericEntityException ex) { 409 errorMessages.add(ex.getMessage()); 410 gcFieldsOkay = false; 411 } 412 } 413 414 if (selectedPaymentMethods != null && selectedPaymentMethods.size() > 0) { 415 if (gcAmt == null || gcAmt.length() == 0) { 416 errMsg = UtilProperties.getMessage(resource,"checkhelper.enter_amount_to_place_on_gift_card", (cart != null ? cart.getLocale() : Locale.getDefault())); 417 errorMessages.add(errMsg); 418 gcFieldsOkay = false; 419 } 420 } 421 if (gcAmt != null && gcAmt.length() > 0) { 422 try { 423 gcAmount = Double.parseDouble(gcAmt); 424 } catch (NumberFormatException e) { 425 Debug.logError(e, module); 426 errMsg = UtilProperties.getMessage(resource,"checkhelper.invalid_amount_for_gift_card", (cart != null ? cart.getLocale() : Locale.getDefault())); 427 errorMessages.add(errMsg); 428 gcFieldsOkay = false; 429 } 430 } 431 432 if (gcFieldsOkay) { 433 Map gcCtx = new HashMap (); 435 gcCtx.put("partyId", params.get("partyId")); 436 gcCtx.put("cardNumber", gcNum); 437 if (cart.isPinRequiredForGC(delegator)) { 438 gcCtx.put("pinNumber", gcPin); 439 } 440 gcCtx.put("userLogin", cart.getUserLogin()); 441 Map gcResult = null; 442 try { 443 gcResult = dispatcher.runSync("createGiftCard", gcCtx); 444 } catch (GenericServiceException e) { 445 Debug.logError(e, module); 446 errorMessages.add(e.getMessage()); 447 } 448 if (gcResult != null) { 449 this.addErrors(errorMessages, errorMaps, gcResult); 450 451 if (errorMessages.size() == 0 && errorMaps.size() == 0) { 452 Double giftCardAmount = null; 454 if (gcAmount > 0) { 455 giftCardAmount = new Double (gcAmount); 456 } 457 String gcPaymentMethodId = (String ) gcResult.get("paymentMethodId"); 458 result = ServiceUtil.returnSuccess(); 459 result.put("paymentMethodId", gcPaymentMethodId); 460 result.put("amount", giftCardAmount); 461 } 462 } else { 463 errMsg = UtilProperties.getMessage(resource,"checkhelper.problem_with_gift_card_information", (cart != null ? cart.getLocale() : Locale.getDefault())); 464 errorMessages.add(errMsg); 465 } 466 } 467 } else { 468 result = ServiceUtil.returnSuccess(); 469 } 470 471 if (errorMessages.size() > 0) { 473 result.put(ModelService.ERROR_MESSAGE_LIST, errorMessages); 474 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 475 } 476 if (errorMaps.size() > 0) { 477 result.put(ModelService.ERROR_MESSAGE_MAP, errorMaps); 478 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 479 } 480 481 return result; 482 } 483 484 public Map createOrder(GenericValue userLogin) { 485 return createOrder(userLogin, null, null, null, false, null, null); 486 } 487 488 public Map createOrder(GenericValue userLogin, String distributorId, String affiliateId, 490 List trackingCodeOrders, boolean areOrderItemsExploded, String visitId, String webSiteId) { 491 if (this.cart == null) { 492 return null; 493 } 494 String orderId = this.cart.getOrderId(); 495 this.cart.clearAllItemStatus(); 496 497 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 499 DecimalFormat formatter = new DecimalFormat (currencyFormat); 500 double cartTotal = this.cart.getGrandTotal(); 501 String grandTotalString = formatter.format(cartTotal); 502 Double grandTotal = null; 503 try { 504 grandTotal = new Double (formatter.parse(grandTotalString).doubleValue()); 505 } catch (ParseException e) { 506 Debug.logError(e, "Problem getting parsed currency amount from DecimalFormat", module); 507 String errMsg = UtilProperties.getMessage(resource,"checkhelper.could_not_create_order_parsing_totals", (cart != null ? cart.getLocale() : Locale.getDefault())); 508 return ServiceUtil.returnError(errMsg); 509 } 510 511 Map context = this.cart.makeCartMap(this.dispatcher, areOrderItemsExploded); 513 514 context.put("trackingCodeOrders", trackingCodeOrders); 516 517 if (distributorId != null) context.put("distributorId", distributorId); 518 if (affiliateId != null) context.put("affiliateId", affiliateId); 519 520 String partyId = this.cart.getPartyId(); 522 String facilityId = this.cart.getFacilityId(); 523 String terminalId = this.cart.getTerminalId(); 524 String transactionId = cart.getTransactionId(); 525 String productStoreId = cart.getProductStoreId(); 526 527 context.put("grandTotal", grandTotal); 528 context.put("userLogin", userLogin); 529 context.put("partyId", partyId); 530 context.put("productStoreId", productStoreId); 531 context.put("transactionId", transactionId); 532 context.put("originFacilityId", facilityId); 533 context.put("visitId", visitId); 534 context.put("terminalId", terminalId); 535 context.put("webSiteId", webSiteId); 536 537 Map storeResult = null; 539 540 try { 541 storeResult = dispatcher.runSync("storeOrder", context); 542 orderId = (String ) storeResult.get("orderId"); 543 if (orderId != null && orderId.length() > 0) { 544 this.cart.setOrderId(orderId); 545 if (this.cart.getFirstAttemptOrderId() == null) { 546 this.cart.setFirstAttemptOrderId(orderId); 547 } 548 } 549 } catch (GenericServiceException e) { 550 String service = e.getMessage(); 551 Map messageMap = UtilMisc.toMap("service", service); 552 String errMsg = UtilProperties.getMessage(resource, "checkhelper.could_not_create_order_invoking_service", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 553 Debug.logError(e, errMsg, module); 554 return ServiceUtil.returnError(errMsg); 555 } 556 557 if (ServiceUtil.isError(storeResult)) { 559 String errMsg = UtilProperties.getMessage(resource, "checkhelper.did_not_complete_order_following_occurred", (cart != null ? cart.getLocale() : Locale.getDefault())); 560 List resErrorMessages = new LinkedList (); 561 resErrorMessages.add(errMsg); 562 resErrorMessages.add(ServiceUtil.getErrorMessage(storeResult)); 563 return ServiceUtil.returnError(resErrorMessages); 564 } 565 566 Iterator orderItems = ((List )context.get("orderItems")).iterator(); 570 int counter = 0; 571 while (orderItems.hasNext()) { 572 GenericValue orderItem = (GenericValue)orderItems.next(); 573 String productId = orderItem.getString("productId"); 574 if (productId != null) { 575 try { 576 GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); 577 GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 578 if ("AGGREGATED".equals(product.getString("productTypeId"))) { 579 org.ofbiz.product.config.ProductConfigWrapper config = this.cart.findCartItem(counter).getConfigWrapper(); 580 Map inputMap = new HashMap (); 581 inputMap.put("config", config); 582 inputMap.put("facilityId", productStore.getString("inventoryFacilityId")); 583 inputMap.put("orderId", orderId); 584 inputMap.put("orderItemSeqId", orderItem.getString("orderItemSeqId")); 585 inputMap.put("quantity", orderItem.getDouble("quantity")); 586 inputMap.put("userLogin", userLogin); 587 588 Map prunResult = dispatcher.runSync("createProductionRunFromConfiguration", inputMap); 589 if (ServiceUtil.isError(prunResult)) { 590 Debug.logError(ServiceUtil.getErrorMessage(prunResult) + " for input:" + inputMap, module); 591 } 592 } 593 } catch (Exception e) { 594 String service = e.getMessage(); 595 Map messageMap = UtilMisc.toMap("service", service); 596 String errMsg = UtilProperties.getMessage(resource, "checkhelper.could_not_create_order_invoking_service", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 597 Debug.logError(e, errMsg, module); 598 return ServiceUtil.returnError(errMsg); 599 } 600 } 601 counter++; 602 } 603 605 Iterator shoppingCartItems = this.cart.items().iterator(); 609 while (shoppingCartItems.hasNext()) { 610 ShoppingCartItem shoppingCartItem = (ShoppingCartItem)shoppingCartItems.next(); 611 String requirementId = shoppingCartItem.getRequirementId(); 612 if (requirementId != null) { 613 try { 614 Map inputMap = UtilMisc.toMap("requirementId", requirementId, "statusId", "REQ_ORDERED"); 615 inputMap.put("userLogin", userLogin); 616 Map outMap = dispatcher.runSync("updateRequirement", inputMap); 618 } catch (Exception e) { 619 String service = e.getMessage(); 620 Map messageMap = UtilMisc.toMap("service", service); 621 String errMsg = UtilProperties.getMessage(resource, "checkhelper.could_not_create_order_invoking_service", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 622 Debug.logError(e, errMsg, module); 623 return ServiceUtil.returnError(errMsg); 624 } 625 } 626 } 627 629 Map result = ServiceUtil.returnSuccess(); 631 result.put("orderId", orderId); 632 result.put("orderAdditionalEmails", this.cart.getOrderAdditionalEmails()); 633 634 List toBeStored = new LinkedList (); 636 637 GenericValue party = null; 638 try { 639 party = this.delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 640 } catch (GenericEntityException e) { 641 Debug.logWarning(e, UtilProperties.getMessage(resource_error,"OrderProblemsGettingPartyRecord", cart.getLocale()), module); 642 party = null; 643 } 644 645 if (party != null) { 647 Iterator emailIter = UtilMisc.toIterator(ContactHelper.getContactMechByType(party, "EMAIL_ADDRESS", false)); 648 while (emailIter != null && emailIter.hasNext()) { 649 GenericValue email = (GenericValue) emailIter.next(); 650 GenericValue orderContactMech = this.delegator.makeValue("OrderContactMech", 651 UtilMisc.toMap("orderId", orderId, "contactMechId", email.getString("contactMechId"), "contactMechPurposeTypeId", "ORDER_EMAIL")); 652 toBeStored.add(orderContactMech); 653 } 654 } 655 656 String additionalEmails = this.cart.getOrderAdditionalEmails(); 658 List emailList = StringUtil.split(additionalEmails, ","); 659 if (emailList == null) emailList = new ArrayList (); 660 Iterator eli = emailList.iterator(); 661 while (eli.hasNext()) { 662 String email = (String ) eli.next(); 663 String contactMechId = this.delegator.getNextSeqId("ContactMech").toString(); 664 GenericValue contactMech = this.delegator.makeValue("ContactMech", 665 UtilMisc.toMap("contactMechId", contactMechId, "contactMechTypeId", "EMAIL_ADDRESS", "infoString", email)); 666 667 GenericValue orderContactMech = this.delegator.makeValue("OrderContactMech", 668 UtilMisc.toMap("orderId", orderId, "contactMechId", contactMechId, "contactMechPurposeTypeId", "ORDER_EMAIL")); 669 toBeStored.add(contactMech); 670 toBeStored.add(orderContactMech); 671 } 672 673 if (toBeStored.size() > 0) { 674 try { 675 if (Debug.verboseOn()) Debug.logVerbose("To Be Stored: " + toBeStored, module); 676 this.delegator.storeAll(toBeStored); 677 } catch (GenericEntityException e) { 678 Debug.logWarning(e, UtilProperties.getMessage(resource_error,"OrderProblemsStoringOrderEmailContactInformation", cart.getLocale()), module); 680 } 681 } 682 683 return result; 684 } 685 686 public void calcAndAddTax() throws GeneralException { 687 calcAndAddTax(null); 688 } 689 690 public void calcAndAddTax(GenericValue shipAddress) throws GeneralException { 691 if (!"SALES_ORDER".equals(cart.getOrderType())) { 692 return; 693 } 694 695 int shipGroups = this.cart.getShipGroupSize(); 696 for (int i = 0; i < shipGroups; i++) { 697 Map serviceContext = this.makeTaxContext(i, shipAddress); 698 List taxReturn = this.getTaxAdjustments(dispatcher, "calcTax", serviceContext); 700 701 if (Debug.verboseOn()) Debug.logVerbose("ReturnList: " + taxReturn, module); 702 ShoppingCart.CartShipInfo csi = cart.getShipInfo(i); 703 List orderAdj = (List ) taxReturn.get(0); 704 List itemAdj = (List ) taxReturn.get(1); 705 706 if (itemAdj != null) { 708 for (int x = 0; x < itemAdj.size(); x++) { 709 List adjs = (List ) itemAdj.get(x); 710 ShoppingCartItem item = (ShoppingCartItem) csi.shipItemInfo.get(x); 711 csi.setItemInfo(item, adjs); 712 Debug.log("Added item adjustments to ship group [" + i + " / " + x + "] - " + adjs, module); 713 } 714 } 715 716 csi.shipTaxAdj.clear(); 718 csi.shipTaxAdj.addAll(orderAdj); 719 } 720 } 721 722 private Map makeTaxContext(int shipGroup, GenericValue shipAddress) throws GeneralException { 723 String productStoreId = cart.getProductStoreId(); 724 String billToPartyId = cart.getBillToCustomerPartyId(); 725 ShoppingCart.CartShipInfo csi = cart.getShipInfo(shipGroup); 726 int totalItems = csi.shipItemInfo.size(); 727 728 List product = new ArrayList (totalItems); 729 List amount = new ArrayList (totalItems); 730 List price = new ArrayList (totalItems); 731 List shipAmt = new ArrayList (totalItems); 732 733 for (int i = 0; i < totalItems; i++) { 734 ShoppingCartItem cartItem = (ShoppingCartItem) csi.shipItemInfo.get(i); 735 ShoppingCart.CartShipInfo.CartShipItemInfo itemInfo = csi.getShipItemInfo(cartItem); 736 737 740 product.add(i, cartItem.getProduct()); 741 amount.add(i, new BigDecimal (cartItem.getItemSubTotal(itemInfo.quantity))); 742 price.add(i, new BigDecimal (cartItem.getBasePrice())); 743 shipAmt.add(i, new BigDecimal ("0.00")); } 745 746 BigDecimal shipAmount = new BigDecimal (csi.shipEstimate); 747 if (shipAddress == null) { 748 shipAddress = cart.getShippingAddress(shipGroup); 749 } 750 751 if (shipAddress == null) { 753 for (int i = 0; i < cart.selectedPayments(); i++) { 754 ShoppingCart.CartPaymentInfo cpi = cart.getPaymentInfo(i); 755 GenericValue billAddr = cpi.getBillingAddress(delegator); 756 if (billAddr != null) { 757 shipAddress = billAddr; 758 Debug.logInfo("Found address from payment method.", module); 759 } 760 break; 761 } 762 } 763 764 Map serviceContext = UtilMisc.toMap("productStoreId", productStoreId); 765 serviceContext.put("billToPartyId", billToPartyId); 766 serviceContext.put("itemProductList", product); 767 serviceContext.put("itemAmountList", amount); 768 serviceContext.put("itemPriceList", price); 769 serviceContext.put("itemShippingList", shipAmt); 770 serviceContext.put("orderShippingAmount", shipAmount); 771 serviceContext.put("shippingAddress", shipAddress); 772 773 return serviceContext; 774 } 775 776 private List getTaxAdjustments(LocalDispatcher dispatcher, String taxService, Map serviceContext) throws GeneralException { 778 Map serviceResult = null; 779 780 try { 781 serviceResult = dispatcher.runSync(taxService, serviceContext); 782 } catch (GenericServiceException e) { 783 Debug.logError(e, module); 784 throw new GeneralException("Problem occurred in tax service (" + e.getMessage() + ")", e); 785 } 786 787 if (ServiceUtil.isError(serviceResult)) { 788 throw new GeneralException(ServiceUtil.getErrorMessage(serviceResult)); 789 } 790 791 List orderAdj = (List ) serviceResult.get("orderAdjustments"); 793 List itemAdj = (List ) serviceResult.get("itemAdjustments"); 794 795 return UtilMisc.toList(orderAdj, itemAdj); 796 } 797 798 public Map processPayment(GenericValue productStore, GenericValue userLogin) throws GeneralException { 799 return processPayment(productStore, userLogin, false, false); 800 } 801 802 public Map processPayment(GenericValue productStore, GenericValue userLogin, boolean faceToFace) throws GeneralException { 803 return processPayment(productStore, userLogin, faceToFace, false); 804 } 805 806 public Map processPayment(GenericValue productStore, GenericValue userLogin, boolean faceToFace, boolean manualHold) throws GeneralException { 807 String DECLINE_MESSAGE = productStore.getString("authDeclinedMessage"); 809 String ERROR_MESSAGE = productStore.getString("authErrorMessage"); 810 String RETRY_ON_ERROR = productStore.getString("retryFailedAuths"); 811 if (RETRY_ON_ERROR == null) { 812 RETRY_ON_ERROR = "Y"; 813 } 814 815 double orderTotal = this.cart.getGrandTotal(); 817 String orderId = this.cart.getOrderId(); 818 819 List paymentMethodTypeIds = this.cart.getPaymentMethodTypeIds(); 821 822 boolean requireAuth = false; 824 List allPaymentPreferences = null; 825 try { 826 allPaymentPreferences = this.delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId)); 827 } catch (GenericEntityException e) { 828 throw new GeneralException("Problems getting payment preferences", e); 829 } 830 831 List canExpr = UtilMisc.toList(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED")); 833 allPaymentPreferences = EntityUtil.filterByAnd(allPaymentPreferences, canExpr); 834 835 List exprs = UtilMisc.toList(new EntityExpr("manualRefNum", EntityOperator.NOT_EQUAL, null)); 837 List manualRefPaymentPrefs = EntityUtil.filterByAnd(allPaymentPreferences, exprs); 838 if (manualRefPaymentPrefs != null && manualRefPaymentPrefs.size() > 0) { 839 Iterator i = manualRefPaymentPrefs.iterator(); 840 while (i.hasNext()) { 841 GenericValue opp = (GenericValue) i.next(); 842 Map authCtx = new HashMap (); 843 authCtx.put("orderPaymentPreference", opp); 844 if (opp.get("paymentMethodId") == null) { 845 authCtx.put("serviceTypeEnum", "PRDS_PAY_EXTERNAL"); 846 } 847 authCtx.put("processAmount", opp.getDouble("maxAmount")); 848 authCtx.put("authRefNum", opp.getString("manualRefNum")); 849 authCtx.put("authResult", Boolean.TRUE); 850 authCtx.put("userLogin", userLogin); 851 authCtx.put("currencyUomId", cart.getCurrency()); 852 853 Map authResp = dispatcher.runSync("processAuthResult", authCtx); 854 if (authResp != null && ServiceUtil.isError(authResp)) { 855 throw new GeneralException(ServiceUtil.getErrorMessage(authResp)); 856 } 857 858 OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); 860 861 if ("Y".equalsIgnoreCase(productStore.getString("manualAuthIsCapture"))) { 862 Map captCtx = new HashMap (); 863 captCtx.put("orderPaymentPreference", opp); 864 if (opp.get("paymentMethodId") == null) { 865 captCtx.put("serviceTypeEnum", "PRDS_PAY_EXTERNAL"); 866 } 867 captCtx.put("payToPartyId", productStore.get("payToPartyId")); 868 captCtx.put("captureResult", Boolean.TRUE); 869 captCtx.put("captureAmount", opp.getDouble("maxAmount")); 870 captCtx.put("captureRefNum", opp.getString("manualRefNum")); 871 captCtx.put("userLogin", userLogin); 872 captCtx.put("currencyUomId", cart.getCurrency()); 873 874 Map capResp = dispatcher.runSync("processCaptureResult", captCtx); 875 if (capResp != null && ServiceUtil.isError(capResp)) { 876 throw new GeneralException(ServiceUtil.getErrorMessage(capResp)); 877 } 878 } 879 } 880 } 881 882 Map paymentFields = UtilMisc.toMap("statusId", "PAYMENT_NOT_AUTH"); 884 List onlinePaymentPrefs = EntityUtil.filterByAnd(allPaymentPreferences, paymentFields); 885 886 if (onlinePaymentPrefs != null && onlinePaymentPrefs.size() > 0) { 887 requireAuth = true; 888 } 889 890 if (requireAuth) { 892 if (orderTotal == 0) { 893 boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); 895 if (!ok) { 896 throw new GeneralException("Problem with order change; see above error"); 897 } 898 } 899 900 Map paymentResult = null; 902 try { 903 paymentResult = dispatcher.runSync("authOrderPayments", 905 UtilMisc.toMap("orderId", orderId, "userLogin", userLogin), 180, true); 906 } catch (GenericServiceException e) { 907 Debug.logWarning(e, module); 908 throw new GeneralException("Error in authOrderPayments service: " + e.toString(), e.getNested()); 909 } 910 if (Debug.verboseOn()) Debug.logVerbose("Finsished w/ Payment Service", module); 911 912 if (paymentResult != null && ServiceUtil.isError(paymentResult)) { 913 throw new GeneralException(ServiceUtil.getErrorMessage(paymentResult)); 914 } 915 916 List messages = (List ) paymentResult.get("authResultMsgs"); 918 919 if (paymentResult != null && paymentResult.containsKey("processResult")) { 920 String authResp = (String ) paymentResult.get("processResult"); 921 922 if (authResp.equals("FAILED")) { 923 if (Debug.verboseOn()) Debug.logVerbose("Payment auth was NOT a success!", module); 925 926 boolean ok = OrderChangeHelper.rejectOrder(dispatcher, userLogin, orderId); 927 if (!ok) { 928 throw new GeneralException("Problem with order change; see above error"); 929 } 930 931 cart.setOrderId(null); 933 if (messages == null || messages.size() == 0) { 934 return ServiceUtil.returnError(DECLINE_MESSAGE); 935 } else { 936 return ServiceUtil.returnError(messages); 937 } 938 } else if (authResp.equals("APPROVED")) { 939 if (Debug.verboseOn()) Debug.logVerbose("Payment auth was a success!", module); 941 942 boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); 944 if (!ok) { 945 throw new GeneralException("Problem with order change; see above error"); 946 } 947 } else if (authResp.equals("ERROR")) { 948 if (Debug.verboseOn()) Debug.logVerbose("Payment auth failed due to processor trouble.", module); 950 if (!faceToFace && "Y".equalsIgnoreCase(RETRY_ON_ERROR)) { 951 return ServiceUtil.returnSuccess(ERROR_MESSAGE); 953 } else { 954 boolean ok = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId); 955 if (!ok) { 956 throw new GeneralException("Problem with order change; see above error"); 957 } 958 this.cart.setOrderId(null); 960 if (messages == null || messages.size() == 0) { 961 return ServiceUtil.returnError(ERROR_MESSAGE); 962 } else { 963 return ServiceUtil.returnError(messages); 964 } 965 } 966 } else { 967 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderPleaseContactCustomerService;PaymentReturnCodeUnknown.", (cart != null ? cart.getLocale() : Locale.getDefault()))); 969 } 970 } else { 971 if (Debug.verboseOn()) Debug.logVerbose("Payment auth failed due to processor trouble.", module); 973 if (!faceToFace && "Y".equalsIgnoreCase(RETRY_ON_ERROR)) { 974 return ServiceUtil.returnSuccess(ERROR_MESSAGE); 976 } else { 977 boolean ok = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId); 978 if (!ok) { 979 throw new GeneralException("Problem with order change; see above error"); 980 } 981 this.cart.setOrderId(null); 983 return ServiceUtil.returnError(ERROR_MESSAGE); 984 } 985 } 986 } else if (paymentMethodTypeIds.contains("CASH") || paymentMethodTypeIds.contains("EXT_COD") || paymentMethodTypeIds.contains("EXT_BILLACT")) { 987 boolean hasOther = false; 988 boolean validAmount = false; 990 991 Iterator pmti = paymentMethodTypeIds.iterator(); 992 while (pmti.hasNext()) { 993 String type = (String ) pmti.next(); 994 if (!"CASH".equals(type) && !"EXT_COD".equals(type) && !"EXT_BILLACT".equals(type)) { 995 hasOther = true; 996 break; 997 } 998 } 999 1000 if (!hasOther) { 1001 if (!paymentMethodTypeIds.contains("CASH") && !paymentMethodTypeIds.contains("EXT_COD")) { 1002 String billingAccountId = cart.getBillingAccountId(); 1004 double billAcctCredit = this.availableAccountBalance(billingAccountId); 1005 double billingAcctAmt = cart.getBillingAccountAmount(); 1006 if (billAcctCredit >= billingAcctAmt) { 1007 if (cart.getGrandTotal() > billAcctCredit) { 1008 validAmount = false; 1009 } else { 1010 validAmount = true; 1011 } 1012 } 1013 } 1014 1015 boolean ok = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId, manualHold); 1017 if (!ok) { 1018 throw new GeneralException("Problem with order change; see above error"); 1019 } 1020 } 1021 } else { 1022 } 1024 1025 if (faceToFace) { 1027 Debug.log("Face-To-Face Sale - " + orderId, module); 1028 this.adjustFaceToFacePayment(allPaymentPreferences, userLogin); 1029 boolean ok = OrderChangeHelper.completeOrder(dispatcher, userLogin, orderId); 1030 Debug.log("Complete Order Result - " + ok, module); 1031 if (!ok) { 1032 throw new GeneralException("Problem with order change; see error logs"); 1033 } 1034 } 1035 return ServiceUtil.returnSuccess(); 1036 } 1037 1038 public void adjustFaceToFacePayment(List allPaymentPrefs, GenericValue userLogin) throws GeneralException { 1039 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 1040 DecimalFormat formatter = new DecimalFormat (currencyFormat); 1041 1042 double cartTotal = this.cart.getGrandTotal(); 1043 String grandTotalString = formatter.format(cartTotal); 1044 Double grandTotal = null; 1045 try { 1046 grandTotal = new Double (formatter.parse(grandTotalString).doubleValue()); 1047 } catch (ParseException e) { 1048 throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e); 1049 } 1050 1051 double prefTotal = 0.00; 1052 if (allPaymentPrefs != null) { 1053 Iterator i = allPaymentPrefs.iterator(); 1054 while (i.hasNext()) { 1055 GenericValue pref = (GenericValue) i.next(); 1056 Double maxAmount = pref.getDouble("maxAmount"); 1057 if (maxAmount == null) maxAmount = new Double (0.00); 1058 prefTotal += maxAmount.doubleValue(); 1059 } 1060 } 1061 1062 String payTotalString = formatter.format(prefTotal); 1063 Double payTotal = null; 1064 try { 1065 payTotal = new Double (formatter.parse(payTotalString).doubleValue()); 1066 } catch (ParseException e) { 1067 throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e); 1068 } 1069 1070 if (grandTotal == null) grandTotal = new Double (0.00); 1071 if (payTotal == null) payTotal = new Double (0.00); 1072 1073 if (payTotal.doubleValue() > grandTotal.doubleValue()) { 1074 double diff = (payTotal.doubleValue() - grandTotal.doubleValue()) * -1; 1075 String diffString = formatter.format(diff); 1076 Double change = null; 1077 try { 1078 change = new Double (formatter.parse(diffString).doubleValue()); 1079 } catch (ParseException e) { 1080 throw new GeneralException("Problem getting parsed currency amount from DecimalFormat", e); 1081 } 1082 GenericValue newPref = delegator.makeValue("OrderPaymentPreference", null); 1083 newPref.set("orderPaymentPreferenceId", delegator.getNextSeqId("OrderPaymentPreference")); 1084 newPref.set("orderId", cart.getOrderId()); 1085 newPref.set("paymentMethodTypeId", "CASH"); 1086 newPref.set("statusId", "PAYMENT_RECEIVED"); 1087 newPref.set("maxAmount", change); 1088 newPref.set("createdDate", UtilDateTime.nowTimestamp()); 1089 if (userLogin != null) { 1090 newPref.set("createdByUserLogin", userLogin.getString("userLoginId")); 1091 } 1092 delegator.create(newPref); 1093 } 1094 } 1095 1096 public Map checkOrderBlacklist(GenericValue userLogin) { 1097 if (cart == null) { 1098 return ServiceUtil.returnSuccess("success"); 1099 } 1100 GenericValue shippingAddressObj = this.cart.getShippingAddress(); 1101 if (shippingAddressObj == null) { 1102 return ServiceUtil.returnSuccess("success"); 1103 } 1104 String shippingAddress = UtilFormatOut.checkNull(shippingAddressObj.getString("address1")).toUpperCase(); 1105 List exprs = UtilMisc.toList(new EntityExpr( 1106 new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("blacklistString")), EntityOperator.EQUALS, new EntityFunction.UPPER(shippingAddress)), EntityOperator.AND, 1107 new EntityExpr("orderBlacklistTypeId", EntityOperator.EQUALS, "BLACKLIST_ADDRESS"))); 1108 String errMsg=null; 1109 1110 List paymentMethods = this.cart.getPaymentMethods(); 1111 Iterator i = paymentMethods.iterator(); 1112 while (i.hasNext()) { 1113 GenericValue paymentMethod = (GenericValue) i.next(); 1114 if ((paymentMethod != null) && ("CREDIT_CARD".equals(paymentMethod.getString("paymentMethodTypeId")))) { 1115 GenericValue creditCard = null; 1116 GenericValue billingAddress = null; 1117 try { 1118 creditCard = paymentMethod.getRelatedOne("CreditCard"); 1119 if (creditCard != null) 1120 billingAddress = creditCard.getRelatedOne("PostalAddress"); 1121 } catch (GenericEntityException e) { 1122 Debug.logError(e, "Problems getting credit card from payment method", module); 1123 errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_reading_database", (cart != null ? cart.getLocale() : Locale.getDefault())); 1124 return ServiceUtil.returnError(errMsg); 1125 } 1126 if (creditCard != null) { 1127 String creditCardNumber = UtilFormatOut.checkNull(creditCard.getString("cardNumber")); 1128 exprs.add(new EntityExpr( 1129 new EntityExpr("blacklistString", EntityOperator.EQUALS, creditCardNumber), EntityOperator.AND, 1130 new EntityExpr("orderBlacklistTypeId", EntityOperator.EQUALS, "BLACKLIST_CREDITCARD"))); 1131 } 1132 if (billingAddress != null) { 1133 String address = UtilFormatOut.checkNull(billingAddress.getString("address1").toUpperCase()); 1134 exprs.add(new EntityExpr( 1135 new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("blacklistString")), EntityOperator.EQUALS, new EntityFunction.UPPER(address)), EntityOperator.AND, 1136 new EntityExpr("orderBlacklistTypeId", EntityOperator.EQUALS, "BLACKLIST_ADDRESS"))); 1137 } 1138 } 1139 } 1140 1141 List blacklistFound = null; 1142 if (exprs.size() > 0) { 1143 try { 1144 blacklistFound = this.delegator.findByOr("OrderBlacklist", exprs); 1145 } catch (GenericEntityException e) { 1146 Debug.logError(e, "Problems with OrderBlacklist lookup.", module); 1147 errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_reading_database", (cart != null ? cart.getLocale() : Locale.getDefault())); 1148 return ServiceUtil.returnError(errMsg); 1149 } 1150 } 1151 1152 if (blacklistFound != null && blacklistFound.size() > 0) { 1153 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderFailed", (cart != null ? cart.getLocale() : Locale.getDefault()))); 1154 } else { 1155 return ServiceUtil.returnSuccess("success"); 1156 } 1157 } 1158 1159 public Map failedBlacklistCheck(GenericValue userLogin, GenericValue productStore) { 1160 Map result; 1161 String errMsg=null; 1162 1163 String REJECT_MESSAGE = productStore.getString("authFraudMessage"); 1164 1165 String orderId = this.cart.getOrderId(); 1167 1168 OrderChangeHelper.rejectOrder(dispatcher, userLogin, orderId); 1170 1171 userLogin.set("enabled", "N"); 1173 try { 1174 userLogin.store(); 1175 } catch (GenericEntityException e) { 1176 Debug.logError(e, "Problems de-activating userLogin.", module); 1177 errMsg = UtilProperties.getMessage(resource,"checkhelper.database_error", (cart != null ? cart.getLocale() : Locale.getDefault())); 1178 result = ServiceUtil.returnError(errMsg); 1179 return result; 1180 } 1181 result = ServiceUtil.returnSuccess(); 1182 result.put(ModelService.ERROR_MESSAGE, REJECT_MESSAGE); 1183 1184 this.cart.clear(); 1186 return result; 1187 } 1188 1189 public Map checkExternalPayment(String orderId) { 1190 Map result; 1191 String errMsg=null; 1192 GenericValue orderHeader = null; 1195 try { 1196 orderHeader = this.delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); 1197 } catch (GenericEntityException e) { 1198 Debug.logError(e, "Problems getting order header", module); 1199 errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_getting_order_header", (cart != null ? cart.getLocale() : Locale.getDefault())); 1200 result = ServiceUtil.returnError(errMsg); 1201 return result; 1202 } 1203 if (orderHeader != null) { 1204 List paymentPrefs = null; 1205 try { 1206 paymentPrefs = orderHeader.getRelated("OrderPaymentPreference"); 1207 } catch (GenericEntityException e) { 1208 Debug.logError(e, "Problems getting order payments", module); 1209 errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_getting_payment_preference", (cart != null ? cart.getLocale() : Locale.getDefault())); 1210 result = ServiceUtil.returnError(errMsg); 1211 return result; 1212 } 1213 if (paymentPrefs != null && paymentPrefs.size() > 0) { 1214 if (paymentPrefs.size() > 1) { 1215 Debug.logError("Too many payment preferences, you cannot have more then one when using external gateways", module); 1216 } 1217 GenericValue paymentPreference = EntityUtil.getFirst(paymentPrefs); 1218 String paymentMethodTypeId = paymentPreference.getString("paymentMethodTypeId"); 1219 if (paymentMethodTypeId.startsWith("EXT_")) { 1220 String type = paymentMethodTypeId.substring(4); 1221 result = ServiceUtil.returnSuccess(); 1222 result.put("type", type.toLowerCase()); 1223 return result; 1224 } 1225 } 1226 result = ServiceUtil.returnSuccess(); 1227 result.put("type", "none"); 1228 return result; 1229 } else { 1230 errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_getting_order_header", (cart != null ? cart.getLocale() : Locale.getDefault())); 1231 result = ServiceUtil.returnError(errMsg); 1232 result.put("type", "error"); 1233 return result; 1234 } 1235 } 1236 1237 1244 public Map finalizeOrderEntryShip(String shippingContactMechId) { 1245 Map result; 1246 String errMsg=null; 1247 if (UtilValidate.isNotEmpty(shippingContactMechId)) { 1249 this.cart.setShippingContactMechId(shippingContactMechId); 1250 result = ServiceUtil.returnSuccess(); 1251 } else { 1252 errMsg = UtilProperties.getMessage(resource,"checkhelper.enter_shipping_address", (cart != null ? cart.getLocale() : Locale.getDefault())); 1253 result = ServiceUtil.returnError(errMsg); 1254 } 1255 1256 return result; 1257 } 1258 1259 1272 public Map finalizeOrderEntryOptions(String shippingMethod, String shippingInstructions, String maySplit, 1273 String giftMessage, String isGift, String internalCode, String shipBeforeDate, String shipAfterDate) { 1274 1275 Map result; 1276 String errMsg=null; 1277 if (UtilValidate.isNotEmpty(shippingMethod)) { 1279 int delimiterPos = shippingMethod.indexOf('@'); 1280 String shipmentMethodTypeId = null; 1281 String carrierPartyId = null; 1282 1283 if (delimiterPos > 0) { 1284 shipmentMethodTypeId = shippingMethod.substring(0, delimiterPos); 1285 carrierPartyId = shippingMethod.substring(delimiterPos + 1); 1286 } 1287 1288 this.cart.setShipmentMethodTypeId(shipmentMethodTypeId); 1289 this.cart.setCarrierPartyId(carrierPartyId); 1290 } else { 1291 errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_method", (cart != null ? cart.getLocale() : Locale.getDefault())); 1292 result = ServiceUtil.returnError(errMsg); 1293 } 1294 1295 this.cart.setShippingInstructions(shippingInstructions); 1297 this.cart.setGiftMessage(giftMessage); 1298 this.cart.setMaySplit(Boolean.valueOf(maySplit)); 1299 this.cart.setIsGift(Boolean.valueOf(isGift)); 1300 this.cart.setInternalCode(internalCode); 1301 1302 if ((shipBeforeDate != null) && (shipBeforeDate.length() > 8)) { 1304 shipBeforeDate = shipBeforeDate.trim(); 1305 if (shipBeforeDate.length() < 14) { 1306 shipBeforeDate = shipBeforeDate + " " + "00:00:00.000"; 1307 } 1308 1309 try { 1310 this.cart.setShipBeforeDate((Timestamp ) ObjectType.simpleTypeConvert(shipBeforeDate, "Timestamp", null, null)); 1311 } catch (Exception e) { 1312 errMsg = "Ship Before Date must be a valid date formed "; 1313 result = ServiceUtil.returnError(errMsg); 1314 } 1315 } 1316 1317 if ((shipAfterDate != null) && (shipAfterDate.length() > 8)) { 1319 shipAfterDate = shipAfterDate.trim(); 1320 if (shipAfterDate.length() < 14) { 1321 shipAfterDate = shipAfterDate + " " + "00:00:00.000"; 1322 } 1323 1324 try { 1325 this.cart.setShipAfterDate((Timestamp ) ObjectType.simpleTypeConvert(shipAfterDate,"Timestamp", null, null)); 1326 } catch (Exception e) { 1327 errMsg = "Ship After Date must be a valid date formed "; 1328 result = ServiceUtil.returnError(errMsg); 1329 } 1330 } 1331 1332 result = ServiceUtil.returnSuccess(); 1333 return result; 1334 } 1335 1336 1343 public Map finalizeOrderEntryPayment(String checkOutPaymentId, Double amount, boolean singleUse, boolean append) { 1344 Map result = ServiceUtil.returnSuccess(); 1345 1346 if (UtilValidate.isNotEmpty(checkOutPaymentId)) { 1347 if (!append) { 1348 cart.clearPayments(); 1349 } 1350 cart.addPaymentAmount(checkOutPaymentId, amount, singleUse); 1351 } 1352 1353 return result; 1354 } 1355 1356 1369 public Map finalizeOrderEntry(String finalizeMode, String shippingContactMechId, String shippingMethod, 1370 String shippingInstructions, String maySplit, String giftMessage, String isGift, String methodType, 1371 String checkOutPaymentId, boolean isSingleUsePayment, boolean appendPayment, Map params, 1372 String internalCode, String shipBeforeDate, String shipAfterDate) { 1373 1374 Map result = ServiceUtil.returnSuccess(); 1375 Map errorMaps = new HashMap (); 1376 Map callResult; 1377 List errorMessages = new ArrayList (); 1378 1379 if (finalizeMode != null && finalizeMode.equals("ship")) { 1381 callResult = this.finalizeOrderEntryShip(shippingContactMechId); 1382 this.addErrors(errorMessages, errorMaps, callResult); 1383 } 1384 1385 if (finalizeMode != null && finalizeMode.equals("options")) { 1387 callResult = this.finalizeOrderEntryOptions(shippingMethod, shippingInstructions, maySplit, giftMessage, isGift, internalCode, shipBeforeDate, shipAfterDate); 1388 this.addErrors(errorMessages, errorMaps, callResult); 1389 } 1390 1391 if (finalizeMode != null && finalizeMode.equals("payment")) { 1393 Map selectedPaymentMethods = null; 1394 if (checkOutPaymentId != null) { 1395 callResult = this.finalizeOrderEntryPayment(checkOutPaymentId, null, isSingleUsePayment, appendPayment); 1396 this.addErrors(errorMessages, errorMaps, callResult); 1397 selectedPaymentMethods = UtilMisc.toMap(checkOutPaymentId, null); 1398 } 1399 callResult = checkGiftCard(params, selectedPaymentMethods); 1400 this.addErrors(errorMessages, errorMaps, callResult); 1401 if (errorMessages.size() == 0 && errorMaps.size() == 0) { 1402 String gcPaymentMethodId = (String ) callResult.get("paymentMethodId"); 1403 Double giftCardAmount = (Double ) callResult.get("amount"); 1404 Map gcCallRes = this.finalizeOrderEntryPayment(gcPaymentMethodId, giftCardAmount, true, true); 1405 this.addErrors(errorMessages, errorMaps, gcCallRes); 1406 } 1407 } 1408 1409 if (errorMessages.size() > 0) { 1411 result.put(ModelService.ERROR_MESSAGE_LIST, errorMessages); 1412 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 1413 } 1414 if (errorMaps.size() > 0) { 1415 result.put(ModelService.ERROR_MESSAGE_MAP, errorMaps); 1416 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 1417 } 1418 1419 return result; 1420 } 1421 1422 1431 private void addErrors(List targetList, Map targetMap, Map callResult) { 1432 List newList; 1433 Map errorMsgMap; 1436 1438 if (callResult.containsKey(ModelService.ERROR_MESSAGE)) { 1440 targetList.add(callResult.get(ModelService.ERROR_MESSAGE)); 1441 } 1442 1443 if (callResult.containsKey(ModelService.ERROR_MESSAGE_LIST)) { 1445 newList = (List ) callResult.get(ModelService.ERROR_MESSAGE_LIST); 1446 targetList.addAll(newList); 1447 } 1448 1449 if (callResult.containsKey(ModelService.ERROR_MESSAGE_MAP)) { 1451 errorMsgMap = (Map ) callResult.get(ModelService.ERROR_MESSAGE_MAP); 1452 targetMap.putAll(errorMsgMap); 1453 } 1454 } 1455 1456 public double availableAccountBalance(String billingAccountId) { 1457 GenericValue billingAccount = null; 1458 Double availableBalance = new Double (0.00); 1459 1460 if (billingAccountId != null) { 1461 try { 1462 Map res = dispatcher.runSync("calcBillingAccountBalance", UtilMisc.toMap("billingAccountId", billingAccountId)); 1463 availableBalance = (Double ) res.get("availableBalance"); 1464 } catch (GenericServiceException e) { 1465 Debug.logError(e, module); 1466 } 1467 } 1468 1469 return availableBalance.doubleValue(); 1470 } 1471 1472 public Map makeBillingAccountMap(List paymentPrefs) { 1473 Map accountMap = new HashMap (); 1474 if (paymentPrefs != null) { 1475 Iterator i = accountMap.keySet().iterator(); 1476 while (i.hasNext()) { 1477 GenericValue pp = (GenericValue) i.next(); 1478 if (pp.get("billingAccountId") != null) { 1479 accountMap.put(pp.getString("billingAccountId"), pp.getDouble("maxAmount")); 1480 } 1481 } 1482 } 1483 return accountMap; 1484 } 1485 1486 public Map validatePaymentMethods() { 1487 String errMsg = null; 1488 String billingAccountId = cart.getBillingAccountId(); 1489 double billingAccountAmt = cart.getBillingAccountAmount(); 1490 double availableAmount = this.availableAccountBalance(billingAccountId); 1491 if (billingAccountAmt > availableAmount) { 1492 Map messageMap = UtilMisc.toMap("billingAccountId", billingAccountId); 1493 errMsg = UtilProperties.getMessage(resource, "checkevents.not_enough_available_on_account", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault())); 1494 return ServiceUtil.returnError(errMsg); 1495 } 1496 1497 List paymentMethods = cart.getPaymentMethodIds(); 1499 List paymentTypes = cart.getPaymentMethodTypeIds(); 1500 if (paymentTypes.contains("EXT_BILLACT") && paymentTypes.size() == 1 && paymentMethods.size() == 0) { 1501 if (cart.getGrandTotal() > availableAmount) { 1502 errMsg = UtilProperties.getMessage(resource, "checkevents.insufficient_credit_available_on_account", (cart != null ? cart.getLocale() : Locale.getDefault())); 1503 return ServiceUtil.returnError(errMsg); 1504 } 1505 } 1506 1507 this.validateGiftCardAmounts(); 1509 1510 String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); 1511 DecimalFormat formatter = new DecimalFormat (currencyFormat); 1512 1513 if (paymentMethods != null) { 1515 List nullPaymentIds = new ArrayList (); 1516 Iterator i = paymentMethods.iterator(); 1517 while (i.hasNext()) { 1518 String paymentMethodId = (String ) i.next(); 1519 Double paymentAmount = cart.getPaymentAmount(paymentMethodId); 1520 if (paymentAmount == null || paymentAmount.doubleValue() == 0) { 1521 Debug.log("Found null paymentMethodId - " + paymentMethodId, module); 1522 nullPaymentIds.add(paymentMethodId); 1523 } 1524 } 1525 Iterator npi = nullPaymentIds.iterator(); 1526 while (npi.hasNext()) { 1527 String paymentMethodId = (String ) npi.next(); 1528 double selectedPaymentTotal = cart.getPaymentTotal(); 1529 double requiredAmount = cart.getGrandTotal() - cart.getBillingAccountAmount(); 1530 double nullAmount = requiredAmount - selectedPaymentTotal; 1531 boolean setOverflow = false; 1532 1533 ShoppingCart.CartPaymentInfo info = cart.getPaymentInfo(paymentMethodId); 1534 String amountString = formatter.format(nullAmount); 1535 double newAmount = 0; 1536 try { 1537 newAmount = formatter.parse(amountString).doubleValue(); 1538 } catch (ParseException e) { 1539 Debug.logError(e, "Problem getting parsed new amount; unable to update payment info!", module); 1540 } 1541 1542 Debug.log("Remaining total is - " + newAmount, module); 1543 if (newAmount > 0) { 1544 info.amount = new Double (newAmount); 1545 Debug.log("Set null paymentMethodId - " + info.paymentMethodId + " / " + info.amount, module); 1546 } else { 1547 info.amount = new Double (0); 1548 Debug.log("Set null paymentMethodId - " + info.paymentMethodId + " / " + info.amount, module); 1549 } 1550 if (!setOverflow) { 1551 info.overflow = setOverflow = true; 1552 Debug.log("Set overflow flag on payment - " + info.paymentMethodId, module); 1553 } 1554 } 1555 } 1556 1557 double reqAmtPreParse = cart.getGrandTotal() - cart.getBillingAccountAmount(); 1559 double selectedPaymentTotal = cart.getPaymentTotal(); 1560 1561 String preParseString = formatter.format(reqAmtPreParse); 1562 double requiredAmount = 0; 1563 try { 1564 requiredAmount = formatter.parse(preParseString).doubleValue(); 1565 } catch (ParseException e) { 1566 requiredAmount = reqAmtPreParse; 1567 Debug.logError(e, "Problem getting parsed required amount; unable to update payment info!", module); 1568 } 1569 if (paymentMethods != null && paymentMethods.size() > 0 && requiredAmount > selectedPaymentTotal) { 1570 Debug.logError("Required Amount : " + requiredAmount + " / Selected Amount : " + selectedPaymentTotal, module); 1571 errMsg = UtilProperties.getMessage(resource, "checkevents.payment_not_cover_this_order", (cart != null ? cart.getLocale() : Locale.getDefault())); 1572 return ServiceUtil.returnError(errMsg); 1573 } 1574 1575 return ServiceUtil.returnSuccess(); 1576 } 1577 1578 public void validateGiftCardAmounts() { 1579 GenericValue productStore = ProductStoreWorker.getProductStore(cart.getProductStoreId(), delegator); 1581 if (productStore != null && !"Y".equalsIgnoreCase(productStore.getString("checkGcBalance"))) { 1582 return; 1583 } 1584 1585 String paymentConfig = ProductStoreWorker.getProductStorePaymentProperties(delegator, cart.getProductStoreId(), "GIFT_CARD", null, true); 1587 String giftCardType = UtilProperties.getPropertyValue(paymentConfig, "", "ofbiz"); 1588 String balanceField = null; 1589 1590 Iterator i = cart.getGiftCards().iterator(); 1592 while (i.hasNext()) { 1593 GenericValue gc = (GenericValue) i.next(); 1594 Map gcBalanceMap = null; 1595 double gcBalance = 0.00; 1596 try { 1597 Map ctx = UtilMisc.toMap("userLogin", cart.getUserLogin()); 1598 ctx.put("currency", cart.getCurrency()); 1599 if ("ofbiz".equalsIgnoreCase(giftCardType)) { 1600 balanceField = "balance"; 1601 ctx.put("cardNumber", gc.getString("cardNumber")); 1602 ctx.put("pinNumber", gc.getString("pinNumber")); 1603 gcBalanceMap = dispatcher.runSync("checkGiftCertificateBalance", ctx); 1604 } 1605 if ("valuelink".equalsIgnoreCase(giftCardType)) { 1606 balanceField = "balance"; 1607 ctx.put("paymentConfig", paymentConfig); 1608 ctx.put("cardNumber", gc.getString("cardNumber")); 1609 ctx.put("pin", gc.getString("pinNumber")); 1610 gcBalanceMap = dispatcher.runSync("balanceInquireGiftCard", ctx); 1611 } 1612 } catch (GenericServiceException e) { 1613 Debug.logError(e, module); 1614 } 1615 if (gcBalanceMap != null) { 1616 Double bal = (Double ) gcBalanceMap.get(balanceField); 1617 if (bal != null) { 1618 gcBalance = bal.doubleValue(); 1619 } 1620 } 1621 1622 Double billUpTo = cart.getPaymentAmount(gc.getString("paymentMethodId")); 1624 1625 if (billUpTo == null || billUpTo.doubleValue() == 0 || gcBalance < billUpTo.doubleValue()) { 1627 cart.addPaymentAmount(gc.getString("paymentMethodId"), new Double (gcBalance)); 1628 } 1629 } 1630 } 1631} 1632 | Popular Tags |