001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.ActionResponseImpl;
027    import com.liferay.portlet.shopping.BillingCityException;
028    import com.liferay.portlet.shopping.BillingCountryException;
029    import com.liferay.portlet.shopping.BillingEmailAddressException;
030    import com.liferay.portlet.shopping.BillingFirstNameException;
031    import com.liferay.portlet.shopping.BillingLastNameException;
032    import com.liferay.portlet.shopping.BillingPhoneException;
033    import com.liferay.portlet.shopping.BillingStateException;
034    import com.liferay.portlet.shopping.BillingStreetException;
035    import com.liferay.portlet.shopping.BillingZipException;
036    import com.liferay.portlet.shopping.CCExpirationException;
037    import com.liferay.portlet.shopping.CCNameException;
038    import com.liferay.portlet.shopping.CCNumberException;
039    import com.liferay.portlet.shopping.CCTypeException;
040    import com.liferay.portlet.shopping.NoSuchOrderException;
041    import com.liferay.portlet.shopping.ShippingCityException;
042    import com.liferay.portlet.shopping.ShippingCountryException;
043    import com.liferay.portlet.shopping.ShippingEmailAddressException;
044    import com.liferay.portlet.shopping.ShippingFirstNameException;
045    import com.liferay.portlet.shopping.ShippingLastNameException;
046    import com.liferay.portlet.shopping.ShippingPhoneException;
047    import com.liferay.portlet.shopping.ShippingStateException;
048    import com.liferay.portlet.shopping.ShippingStreetException;
049    import com.liferay.portlet.shopping.ShippingZipException;
050    import com.liferay.portlet.shopping.ShoppingSettings;
051    import com.liferay.portlet.shopping.model.ShoppingCart;
052    import com.liferay.portlet.shopping.model.ShoppingOrder;
053    import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
054    import com.liferay.portlet.shopping.util.ShoppingUtil;
055    
056    import javax.portlet.ActionRequest;
057    import javax.portlet.ActionResponse;
058    import javax.portlet.PortletConfig;
059    
060    import org.apache.struts.action.ActionForm;
061    import org.apache.struts.action.ActionMapping;
062    
063    /**
064     * @author Brian Wing Shun Chan
065     */
066    public class CheckoutAction extends CartAction {
067    
068            @Override
069            public void processAction(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            PortletConfig portletConfig, ActionRequest actionRequest,
072                            ActionResponse actionResponse)
073                    throws Exception {
074    
075                    if (redirectToLogin(actionRequest, actionResponse)) {
076                            return;
077                    }
078    
079                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
080    
081                    if (cmd.equals(Constants.CHECKOUT)) {
082                            checkout(actionRequest);
083    
084                            setForward(actionRequest, "portlet.shopping.checkout_first");
085                    }
086                    else if (!hasLatestOrder(actionRequest)) {
087                            setForward(actionRequest, "portlet.shopping.checkout_third");
088                    }
089                    else if (cmd.equals(Constants.SAVE)) {
090                            saveLatestOrder(actionRequest, actionResponse);
091                    }
092                    else if (cmd.equals(Constants.UPDATE)) {
093                            try {
094                                    updateLatestOrder(actionRequest);
095    
096                                    setForward(actionRequest, "portlet.shopping.checkout_second");
097                            }
098                            catch (Exception e) {
099                                    if (e instanceof BillingCityException ||
100                                            e instanceof BillingCountryException ||
101                                            e instanceof BillingEmailAddressException ||
102                                            e instanceof BillingFirstNameException ||
103                                            e instanceof BillingLastNameException ||
104                                            e instanceof BillingPhoneException ||
105                                            e instanceof BillingStateException ||
106                                            e instanceof BillingStreetException ||
107                                            e instanceof BillingZipException ||
108                                            e instanceof CCExpirationException ||
109                                            e instanceof CCNameException ||
110                                            e instanceof CCNumberException ||
111                                            e instanceof CCTypeException ||
112                                            e instanceof ShippingCityException ||
113                                            e instanceof ShippingCountryException ||
114                                            e instanceof ShippingEmailAddressException ||
115                                            e instanceof ShippingFirstNameException ||
116                                            e instanceof ShippingLastNameException ||
117                                            e instanceof ShippingPhoneException ||
118                                            e instanceof ShippingStateException ||
119                                            e instanceof ShippingStreetException ||
120                                            e instanceof ShippingZipException) {
121    
122                                            SessionErrors.add(actionRequest, e.getClass());
123    
124                                            setForward(
125                                                    actionRequest, "portlet.shopping.checkout_first");
126                                    }
127                                    else if (e instanceof PrincipalException) {
128                                            setForward(actionRequest, "portlet.shopping.error");
129                                    }
130                                    else {
131                                            throw e;
132                                    }
133                            }
134                    }
135                    else if (cmd.equals(Constants.VIEW)) {
136                            setForward(actionRequest, "portlet.shopping.checkout_third");
137                    }
138                    else {
139                            setForward(actionRequest, "portlet.shopping.checkout_first");
140                    }
141            }
142    
143            protected void checkout(ActionRequest actionRequest) throws Exception {
144                    if (!hasLatestOrder(actionRequest)) {
145                            ThemeDisplay themeDisplay =
146                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
147    
148                            ShoppingOrderLocalServiceUtil.addLatestOrder(
149                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
150                    }
151            }
152    
153            protected void forwardCheckout(
154                            ActionRequest actionRequest, ActionResponse actionResponse,
155                            ShoppingOrder order)
156                    throws Exception {
157    
158                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
159                            WebKeys.THEME_DISPLAY);
160    
161                    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
162    
163                    ShoppingSettings shoppingSettings = ShoppingSettings.getInstance(
164                            themeDisplay.getScopeGroupId());
165    
166                    String returnURL = ShoppingUtil.getPayPalReturnURL(
167                            ((ActionResponseImpl)actionResponse).createActionURL(), order);
168                    String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
169    
170                    if (shoppingSettings.usePayPal()) {
171                            double total = ShoppingUtil.calculateTotal(
172                                    cart.getItems(), order.getBillingState(), cart.getCoupon(),
173                                    cart.getAltShipping(), cart.isInsure());
174    
175                            String redirectURL = ShoppingUtil.getPayPalRedirectURL(
176                                    shoppingSettings, order, total, returnURL, notifyURL);
177    
178                            actionResponse.sendRedirect(redirectURL);
179                    }
180                    else {
181                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
182                                    actionRequest);
183    
184                            ShoppingOrderLocalServiceUtil.sendEmail(
185                                    order, "confirmation", serviceContext);
186    
187                            actionResponse.sendRedirect(returnURL);
188                    }
189            }
190    
191            protected boolean hasLatestOrder(ActionRequest actionRequest)
192                    throws Exception {
193    
194                    try {
195                            ThemeDisplay themeDisplay =
196                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
197    
198                            ShoppingOrderLocalServiceUtil.getLatestOrder(
199                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
200    
201                            return true;
202                    }
203                    catch (NoSuchOrderException nsoe) {
204                            return false;
205                    }
206            }
207    
208            @Override
209            protected boolean isCheckMethodOnProcessAction() {
210                    return _CHECK_METHOD_ON_PROCESS_ACTION;
211            }
212    
213            protected void saveLatestOrder(
214                            ActionRequest actionRequest, ActionResponse actionResponse)
215                    throws Exception {
216    
217                    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
218    
219                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.saveLatestOrder(
220                            cart);
221    
222                    forwardCheckout(actionRequest, actionResponse, order);
223            }
224    
225            protected void updateLatestOrder(ActionRequest actionRequest)
226                    throws Exception {
227    
228                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
229                            WebKeys.THEME_DISPLAY);
230    
231                    String billingFirstName = ParamUtil.getString(
232                            actionRequest, "billingFirstName");
233                    String billingLastName = ParamUtil.getString(
234                            actionRequest, "billingLastName");
235                    String billingEmailAddress = ParamUtil.getString(
236                            actionRequest, "billingEmailAddress");
237                    String billingCompany = ParamUtil.getString(
238                            actionRequest, "billingCompany");
239                    String billingStreet = ParamUtil.getString(
240                            actionRequest, "billingStreet");
241                    String billingCity = ParamUtil.getString(actionRequest, "billingCity");
242    
243                    String billingStateSel = ParamUtil.getString(
244                            actionRequest, "billingStateSel");
245                    String billingState = billingStateSel;
246    
247                    if (Validator.isNull(billingStateSel)) {
248                            billingState = ParamUtil.getString(actionRequest, "billingState");
249                    }
250    
251                    String billingZip = ParamUtil.getString(actionRequest, "billingZip");
252                    String billingCountry = ParamUtil.getString(
253                            actionRequest, "billingCountry");
254                    String billingPhone = ParamUtil.getString(
255                            actionRequest, "billingPhone");
256    
257                    boolean shipToBilling = ParamUtil.getBoolean(
258                            actionRequest, "shipToBilling");
259                    String shippingFirstName = ParamUtil.getString(
260                            actionRequest, "shippingFirstName");
261                    String shippingLastName = ParamUtil.getString(
262                            actionRequest, "shippingLastName");
263                    String shippingEmailAddress = ParamUtil.getString(
264                            actionRequest, "shippingEmailAddress");
265                    String shippingCompany = ParamUtil.getString(
266                            actionRequest, "shippingCompany");
267                    String shippingStreet = ParamUtil.getString(
268                            actionRequest, "shippingStreet");
269                    String shippingCity = ParamUtil.getString(
270                            actionRequest, "shippingCity");
271    
272                    String shippingStateSel = ParamUtil.getString(
273                            actionRequest, "shippingStateSel");
274                    String shippingState = shippingStateSel;
275    
276                    if (Validator.isNull(shippingStateSel)) {
277                            shippingState = ParamUtil.getString(actionRequest, "shippingState");
278                    }
279    
280                    String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
281                    String shippingCountry = ParamUtil.getString(
282                            actionRequest, "shippingCountry");
283                    String shippingPhone = ParamUtil.getString(
284                            actionRequest, "shippingPhone");
285    
286                    String ccName = ParamUtil.getString(actionRequest, "ccName");
287                    String ccType = ParamUtil.getString(actionRequest, "ccType");
288                    String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
289                    int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
290                    int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
291                    String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
292    
293                    String comments = ParamUtil.getString(actionRequest, "comments");
294    
295                    ShoppingOrderLocalServiceUtil.updateLatestOrder(
296                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
297                            billingFirstName, billingLastName, billingEmailAddress,
298                            billingCompany, billingStreet, billingCity, billingState,
299                            billingZip, billingCountry, billingPhone, shipToBilling,
300                            shippingFirstName, shippingLastName, shippingEmailAddress,
301                            shippingCompany, shippingStreet, shippingCity, shippingState,
302                            shippingZip, shippingCountry, shippingPhone, ccName, ccType,
303                            ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
304            }
305    
306            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
307    
308    }