Exemple #1
0
  private static GenericValue getPaymentMethodGatewayPayPal(
      DispatchContext dctx,
      Map<String, ? extends Object> context,
      String paymentServiceTypeEnumId) {
    Delegator delegator = dctx.getDelegator();
    String paymentGatewayConfigId = (String) context.get("paymentGatewayConfigId");
    GenericValue payPalGatewayConfig = null;

    if (paymentGatewayConfigId == null) {
      String productStoreId = null;
      GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
      if (orderPaymentPreference != null) {
        OrderReadHelper orh =
            new OrderReadHelper(delegator, orderPaymentPreference.getString("orderId"));
        productStoreId = orh.getProductStoreId();
      } else {
        ShoppingCart cart = (ShoppingCart) context.get("cart");
        if (cart != null) {
          productStoreId = cart.getProductStoreId();
        }
      }
      if (productStoreId != null) {
        GenericValue payPalPaymentSetting =
            ProductStoreWorker.getProductStorePaymentSetting(
                delegator, productStoreId, "EXT_PAYPAL", paymentServiceTypeEnumId, true);
        if (payPalPaymentSetting != null) {
          paymentGatewayConfigId = payPalPaymentSetting.getString("paymentGatewayConfigId");
        }
      }
    }
    if (paymentGatewayConfigId != null) {
      try {
        payPalGatewayConfig =
            EntityQuery.use(delegator)
                .from("PaymentGatewayPayPal")
                .where("paymentGatewayConfigId", paymentGatewayConfigId)
                .cache()
                .queryOne();
      } catch (GenericEntityException e) {
        Debug.logError(e, module);
      }
    }
    return payPalGatewayConfig;
  }
  /** Create the guest cookies for a shopping list */
  public static String createGuestShoppingListCookies(
      HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    HttpSession session = request.getSession(true);
    ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    Properties systemProps = System.getProperties();
    String guestShoppingUserName =
        "******" + systemProps.getProperty("user.name").replace(" ", "_");
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    int cookieAge = (60 * 60 * 24 * 30);
    String autoSaveListId = null;
    Cookie[] cookies = request.getCookies();

    // check userLogin
    if (UtilValidate.isNotEmpty(userLogin)) {
      String partyId = userLogin.getString("partyId");
      if (UtilValidate.isEmpty(partyId)) {
        return "success";
      }
    }

    // find shopping list ID
    if (cookies != null) {
      for (Cookie cookie : cookies) {
        if (cookie.getName().equals(guestShoppingUserName)) {
          autoSaveListId = cookie.getValue();
          break;
        }
      }
    }

    // clear the auto-save info
    if (ProductStoreWorker.autoSaveCart(delegator, productStoreId)) {
      if (UtilValidate.isEmpty(autoSaveListId)) {
        try {
          Map<String, Object> listFields =
              UtilMisc.<String, Object>toMap(
                  "userLogin",
                  userLogin,
                  "productStoreId",
                  productStoreId,
                  "shoppingListTypeId",
                  "SLT_SPEC_PURP",
                  "listName",
                  PERSISTANT_LIST_NAME);
          Map<String, Object> newListResult = dispatcher.runSync("createShoppingList", listFields);
          if (newListResult != null) {
            autoSaveListId = (String) newListResult.get("shoppingListId");
          }
        } catch (GeneralException e) {
          Debug.logError(e, module);
        }
        Cookie guestShoppingListCookie = new Cookie(guestShoppingUserName, autoSaveListId);
        guestShoppingListCookie.setMaxAge(cookieAge);
        guestShoppingListCookie.setPath("/");
        response.addCookie(guestShoppingListCookie);
      }
    }
    if (UtilValidate.isNotEmpty(autoSaveListId)) {
      if (UtilValidate.isNotEmpty(cart)) {
        cart.setAutoSaveListId(autoSaveListId);
      } else {
        cart = ShoppingCartEvents.getCartObject(request);
        cart.setAutoSaveListId(autoSaveListId);
      }
    }
    return "success";
  }
  /** Restores the specialized (auto-save) shopping list back into the shopping cart */
  public static String restoreAutoSaveList(
      HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    GenericValue productStore = ProductStoreWorker.getProductStore(request);

    if (!ProductStoreWorker.autoSaveCart(productStore)) {
      // if auto-save is disabled just return here
      return "success";
    }

    HttpSession session = request.getSession();
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);

    // safety check for missing required parameter.
    if (cart.getWebSiteId() == null) {
      cart.setWebSiteId(WebSiteWorker.getWebSiteId(request));
    }

    // locate the user's identity
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    if (userLogin == null) {
      userLogin = (GenericValue) session.getAttribute("autoUserLogin");
    }

    // find the list ID
    String autoSaveListId = cart.getAutoSaveListId();
    if (autoSaveListId == null) {
      try {
        autoSaveListId =
            getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
      } catch (GeneralException e) {
        Debug.logError(e, module);
      }
      cart.setAutoSaveListId(autoSaveListId);
    } else if (userLogin != null) {
      String existingAutoSaveListId = null;
      try {
        existingAutoSaveListId =
            getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
      } catch (GeneralException e) {
        Debug.logError(e, module);
      }
      if (existingAutoSaveListId != null) {
        if (!existingAutoSaveListId.equals(autoSaveListId)) {
          // Replace with existing shopping list
          cart.setAutoSaveListId(existingAutoSaveListId);
          autoSaveListId = existingAutoSaveListId;
          cart.setLastListRestore(null);
        } else {
          // CASE: User first login and logout and then re-login again. This condition does not
          // require a restore at all
          // because at this point items in the cart and the items in the shopping list are same so
          // just return.
          return "success";
        }
      }
    }

    // check to see if we are okay to load this list
    java.sql.Timestamp lastLoad = cart.getLastListRestore();
    boolean okayToLoad = autoSaveListId == null ? false : (lastLoad == null ? true : false);
    if (!okayToLoad && lastLoad != null) {
      GenericValue shoppingList = null;
      try {
        shoppingList =
            EntityQuery.use(delegator)
                .from("ShoppingList")
                .where("shoppingListId", autoSaveListId)
                .queryOne();
      } catch (GenericEntityException e) {
        Debug.logError(e, module);
      }
      if (shoppingList != null) {
        java.sql.Timestamp lastModified = shoppingList.getTimestamp("lastAdminModified");
        if (lastModified != null) {
          if (lastModified.after(lastLoad)) {
            okayToLoad = true;
          }
          if (cart.size() == 0 && lastModified.after(cart.getCartCreatedTime())) {
            okayToLoad = true;
          }
        }
      }
    }

    // load (restore) the list of we have determined it is okay to load
    if (okayToLoad) {
      String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
      try {
        addListToCart(
            delegator,
            dispatcher,
            cart,
            prodCatalogId,
            autoSaveListId,
            false,
            false,
            userLogin != null ? true : false);
        cart.setLastListRestore(UtilDateTime.nowTimestamp());
      } catch (IllegalArgumentException e) {
        Debug.logError(e, module);
      }
    }

    return "success";
  }
  private static String createFinAcctPaymentTransaction(
      Delegator delegator,
      LocalDispatcher dispatcher,
      GenericValue userLogin,
      BigDecimal amount,
      String productStoreId,
      String partyId,
      String orderId,
      String orderItemSeqId,
      String currencyUom,
      String txType,
      String finAccountId,
      String reasonEnumId)
      throws GeneralException {

    final String coParty =
        ProductStoreWorker.getProductStorePayToPartyId(productStoreId, delegator);
    final String paymentMethodType = "FIN_ACCOUNT";

    if (UtilValidate.isEmpty(partyId)) {
      partyId = "_NA_";
    }

    String paymentType;
    String partyIdFrom;
    String partyIdTo;
    BigDecimal paymentAmount;

    // determine the payment type and which direction the parties should go
    if ("DEPOSIT".equals(txType)) {
      paymentType = "RECEIPT";
      partyIdFrom = partyId;
      partyIdTo = coParty;
      paymentAmount = amount;
    } else if ("WITHDRAWAL".equals(txType)) {
      paymentType = "DISBURSEMENT";
      partyIdFrom = coParty;
      partyIdTo = partyId;
      paymentAmount = amount;
    } else if ("ADJUSTMENT".equals(txType)) {
      if (amount.compareTo(BigDecimal.ZERO) < 0) {
        paymentType = "DISBURSEMENT";
        partyIdFrom = coParty;
        partyIdTo = partyId;
        paymentAmount = amount.negate(); // must be positive
      } else {
        paymentType = "RECEIPT";
        partyIdFrom = partyId;
        partyIdTo = coParty;
        paymentAmount = amount;
      }
    } else {
      throw new GeneralException("Unable to create financial account transaction!");
    }

    // payment amount should always be positive; adjustments may
    // create the payment for the transaction
    Map<String, Object> paymentCtx = UtilMisc.<String, Object>toMap("paymentTypeId", paymentType);
    paymentCtx.put("paymentMethodTypeId", paymentMethodType);
    paymentCtx.put("partyIdTo", partyIdTo);
    paymentCtx.put("partyIdFrom", partyIdFrom);
    paymentCtx.put("statusId", "PMNT_RECEIVED");
    paymentCtx.put("currencyUomId", currencyUom);
    paymentCtx.put("amount", paymentAmount);
    paymentCtx.put("userLogin", userLogin);
    paymentCtx.put("paymentRefNum", Long.toString(UtilDateTime.nowTimestamp().getTime()));

    String paymentId;
    Map<String, Object> payResult;
    try {
      payResult = dispatcher.runSync("createPayment", paymentCtx);
    } catch (GenericServiceException e) {
      throw new GeneralException(e);
    }
    if (payResult == null) {
      throw new GeneralException("Unknow error in creating financial account transaction!");
    }
    if (ServiceUtil.isError(payResult)) {
      throw new GeneralException(ServiceUtil.getErrorMessage(payResult));
    }
    paymentId = (String) payResult.get("paymentId");

    // create the initial transaction
    Map<String, Object> transCtx = UtilMisc.<String, Object>toMap("finAccountTransTypeId", txType);
    transCtx.put("finAccountId", finAccountId);
    transCtx.put("partyId", partyId);
    transCtx.put("orderId", orderId);
    transCtx.put("orderItemSeqId", orderItemSeqId);
    transCtx.put("reasonEnumId", reasonEnumId);
    transCtx.put("amount", amount);
    transCtx.put("userLogin", userLogin);
    transCtx.put("paymentId", paymentId);

    Map<String, Object> transResult;
    try {
      transResult = dispatcher.runSync("createFinAccountTrans", transCtx);
    } catch (GenericServiceException e) {
      throw new GeneralException(e);
    }
    if (transResult == null) {
      throw new GeneralException("Unknown error in creating financial account transaction!");
    }
    if (ServiceUtil.isError(transResult)) {
      throw new GeneralException(ServiceUtil.getErrorMessage(transResult));
    }

    return (String) transResult.get("finAccountTransId");
  }