⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkoutevents.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * $Id: CheckOutEvents.java,v 1.28 2004/03/08 19:57:56 ajzeneski Exp $
 *
 *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.ofbiz.order.shoppingcart;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.GeneralRuntimeException;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.content.stats.VisitHandler;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.marketing.tracking.TrackingCodeEvents;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.product.store.ProductStoreWorker;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceUtil;

/**
 * Events used for processing checkout and orders.
 *
 * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
 * @author     <a href="mailto:cnelson@einnovation.com">Chris Nelson</a>
 * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
 * @author     <a href="mailto:tristana@twibble.org">Tristan Austin</a>
 * @version    $Revision: 1.28 $
 * @since      2.0
 */
public class CheckOutEvents {

    public static final String module = CheckOutEvents.class.getName();
    public static final String resource = "OrderUiLabels";

    public static String cartNotEmpty(HttpServletRequest request, HttpServletResponse response) {
        ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
        Locale locale = UtilHttp.getLocale(request);
        String errMsg = null;

        if (cart != null && cart.size() > 0) {
            return "success";
        } else {
            errMsg = UtilProperties.getMessage(resource,"checkevents.cart_empty", (cart != null ? cart.getLocale() : Locale.getDefault()));
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
    }

    public static String cancelOrderItem(HttpServletRequest request, HttpServletResponse response) {
        ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
        GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
        String orderId = request.getParameter("order_id");
        String itemSeqId = request.getParameter("item_seq");
        Locale locale = UtilHttp.getLocale(request);

        Map fields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", itemSeqId, "userLogin", userLogin);
        Map result = null;
        String errMsg = null;

        try {
                result = dispatcher.runSync("cancelOrderItem", fields);
        } catch (GenericServiceException e) {
                Debug.logError(e, module);
                errMsg = UtilProperties.getMessage(resource,"checkevents.cannot_cancel_item", (cart != null ? cart.getLocale() : Locale.getDefault()));
                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg );
                return "error";
        }

        if (result.containsKey(ModelService.ERROR_MESSAGE)) {
                request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE));
                return "error";
        }

        return "success";
    }

    public static String setCheckOutPages(HttpServletRequest request, HttpServletResponse response) {
      if ("error".equals(CheckOutEvents.cartNotEmpty(request, response)) == true) {
        return "error";
      }

      Locale locale = UtilHttp.getLocale(request);
      String curPage = request.getParameter("checkoutpage");
      Debug.logInfo("CheckoutPage: " + curPage, module);

      Map callResult = null;
      String errMsg = null;

      ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
      LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
      GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
      CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart);

      if ("shippingaddress".equals(curPage) == true) {
        // Set the shipping address options
        String shippingContactMechId = request.getParameter("shipping_contact_mech_id");
        callResult = checkOutHelper.setCheckOutShippingAddress( shippingContactMechId );

        ServiceUtil.getMessages(request, callResult, null, "<li>", "</li>", "<ul>", "</ul>", null, null);

        if ( !( callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) ) {
          // No errors so push the user onto the next page
          curPage = "shippingoptions";
        }
      } else if ("shippingoptions".equals(curPage) == true) {
        // Set the general shipping options
        String shippingMethod = request.getParameter("shipping_method");
        String correspondingPoId = request.getParameter("corresponding_po_id");
        String shippingInstructions = request.getParameter("shipping_instructions");
        String orderAdditionalEmails = request.getParameter("order_additional_emails");
        String maySplit = request.getParameter("may_split");
        String giftMessage = request.getParameter("gift_message");
        String isGift = request.getParameter("is_gift");
        callResult = checkOutHelper.setCheckOutShippingOptions(shippingMethod, correspondingPoId,
            shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift);

        ServiceUtil.getMessages(request, callResult, null, "<li>", "</li>", "<ul>", "</ul>", null, null);

        if (!(callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) {
          // No errors so push the user onto the next page
          curPage = "payment";
        }
      } else if ("payment".equals(curPage) == true) {
        // get the currency format
        String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
        DecimalFormat formatter = new DecimalFormat(currencyFormat);

        // Set the payment options
        Map selectedPaymentMethods = getSelectedPaymentMethods(request);
        if (selectedPaymentMethods == null) {
            return "error";
        }

        String billingAccountId = request.getParameter("billingAccountId");
        String billingAcctAmtStr = request.getParameter("amount_" + billingAccountId);
        Double billingAccountAmt = null;
        // parse the amount to a decimal
        if (billingAcctAmtStr != null) {
            try {
                billingAccountAmt = new Double(formatter.parse(billingAcctAmtStr).doubleValue());
            } catch (ParseException e) {
                Debug.logError(e, module);
                Map messageMap = UtilMisc.toMap("billingAccountId", billingAccountId );
                errMsg = UtilProperties.getMessage(resource,"checkevents.invalid_amount_set_for_billing_account", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault()));
                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg );
                return "error";
            }
        }

        List singleUsePayments = new ArrayList();

        // check for gift card not on file
        Map params = UtilHttp.getParameterMap(request);
        Map gcResult = checkOutHelper.checkGiftCard(params, selectedPaymentMethods);
        ServiceUtil.getMessages(request, gcResult, null, "<li>", "</li>", "<ul>", "</ul>", null, null);
        if (gcResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
            return "error";
        } else {
            String gcPaymentMethodId = (String) gcResult.get("paymentMethodId");
            Double gcAmount = (Double) gcResult.get("amount");
            if (gcPaymentMethodId != null) {
                selectedPaymentMethods.put(gcPaymentMethodId, gcAmount);
                if ("Y".equalsIgnoreCase(request.getParameter("singleUseGiftCard"))) {
                    singleUsePayments.add(gcPaymentMethodId);
                }
            }
        }

        callResult = checkOutHelper.setCheckOutPayment(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt);

        ServiceUtil.getMessages(request, callResult, null, "<li>", "</li>", "<ul>", "</ul>", null, null);

        if (!(callResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))) {
          // No errors so push the user onto the next page
          curPage = "confirm";
        }
      } else {
        curPage = "shippingaddress";
      }

      return curPage;
    }

    public static String setCheckOutError(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("checkoutpage");
        if (currentPage == null || currentPage.length() == 0) {
            return "error";
        } else {
            return currentPage;
        }
    }

    public static String setPartialCheckOutOptions(HttpServletRequest request, HttpServletResponse response) {
        String resp = setCheckOutOptions(request, response);
        request.setAttribute("_ERROR_MESSAGE_", null);
        return "success";
    }

    public static String checkPaymentMethods(HttpServletRequest request, HttpServletResponse response) {
        ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
        GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
        Locale locale = UtilHttp.getLocale(request);

        String errMsg = null;

        CheckOutHelper checkOutHelper = new CheckOutHelper(dispatcher, delegator, cart);
        String billingAccountId = cart.getBillingAccountId();
        double billingAccountAmt = cart.getBillingAccountAmount();
        double availableAmount = checkOutHelper.availableAccountBalance(billingAccountId);
        if (billingAccountAmt > availableAmount) {
            Map messageMap = UtilMisc.toMap("billingAccountId", billingAccountId );
            errMsg = UtilProperties.getMessage(resource,"checkevents.not_enough_available_on_account", messageMap, (cart != null ? cart.getLocale() : Locale.getDefault()));
            request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);
            return "error";
        }

        // payment by billing account only requires more checking
        List paymentMethods = cart.getPaymentMethodIds();
        List paymentTypes = cart.getPaymentMethodTypeIds();
        if (paymentTypes.contains("EXT_BILLACT") && paymentTypes.size() == 1 && paymentMethods.size() == 0) {
            if (cart.getGrandTotal() > availableAmount) {
                errMsg = UtilProperties.getMessage(resource,"checkevents.insufficient_credit_available_on_account", (cart != null ? cart.getLocale() : Locale.getDefault()));
                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg );
                return "error";
            }
        }

        // validate any gift card balances
        CheckOutEvents.validateGiftCardAmounts(request);

        // update the selected payment methods amount with valid numbers
        if (paymentMethods != null) {
            List nullPaymentIds = new ArrayList();
            Iterator i = paymentMethods.iterator();
            while (i.hasNext()) {
                String paymentMethodId = (String) i.next();
                Double paymentAmount = cart.getPaymentMethodAmount(paymentMethodId);
                if (paymentAmount == null || paymentAmount.doubleValue() == 0) {
                    nullPaymentIds.add(paymentMethodId);
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -