001    /**
002     * Copyright (c) 2000-2013 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.ShippingCityException;
041    import com.liferay.portlet.shopping.ShippingCountryException;
042    import com.liferay.portlet.shopping.ShippingEmailAddressException;
043    import com.liferay.portlet.shopping.ShippingFirstNameException;
044    import com.liferay.portlet.shopping.ShippingLastNameException;
045    import com.liferay.portlet.shopping.ShippingPhoneException;
046    import com.liferay.portlet.shopping.ShippingStateException;
047    import com.liferay.portlet.shopping.ShippingStreetException;
048    import com.liferay.portlet.shopping.ShippingZipException;
049    import com.liferay.portlet.shopping.model.ShoppingCart;
050    import com.liferay.portlet.shopping.model.ShoppingOrder;
051    import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
052    import com.liferay.portlet.shopping.util.ShoppingPreferences;
053    import com.liferay.portlet.shopping.util.ShoppingUtil;
054    
055    import javax.portlet.ActionRequest;
056    import javax.portlet.ActionResponse;
057    import javax.portlet.PortletConfig;
058    
059    import org.apache.struts.action.ActionForm;
060    import org.apache.struts.action.ActionMapping;
061    
062    /**
063     * @author Brian Wing Shun Chan
064     */
065    public class CheckoutAction extends CartAction {
066    
067            @Override
068            public void processAction(
069                            ActionMapping actionMapping, ActionForm actionForm,
070                            PortletConfig portletConfig, ActionRequest actionRequest,
071                            ActionResponse actionResponse)
072                    throws Exception {
073    
074                    if (redirectToLogin(actionRequest, actionResponse)) {
075                            return;
076                    }
077    
078                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
079    
080                    getLatestOrder(actionRequest);
081    
082                    if (cmd.equals(Constants.SAVE)) {
083                            updateCart(actionRequest, actionResponse);
084                            updateLatestOrder(actionRequest);
085                            saveLatestOrder(actionRequest);
086                            forwardCheckout(actionRequest, actionResponse);
087                    }
088                    else if (cmd.equals(Constants.UPDATE)) {
089                            try {
090                                    updateLatestOrder(actionRequest);
091    
092                                    setForward(actionRequest, "portlet.shopping.checkout_second");
093                            }
094                            catch (Exception e) {
095                                    if (e instanceof BillingCityException ||
096                                            e instanceof BillingCountryException ||
097                                            e instanceof BillingEmailAddressException ||
098                                            e instanceof BillingFirstNameException ||
099                                            e instanceof BillingLastNameException ||
100                                            e instanceof BillingPhoneException ||
101                                            e instanceof BillingStateException ||
102                                            e instanceof BillingStreetException ||
103                                            e instanceof BillingZipException ||
104                                            e instanceof CCExpirationException ||
105                                            e instanceof CCNameException ||
106                                            e instanceof CCNumberException ||
107                                            e instanceof CCTypeException ||
108                                            e instanceof ShippingCityException ||
109                                            e instanceof ShippingCountryException ||
110                                            e instanceof ShippingEmailAddressException ||
111                                            e instanceof ShippingFirstNameException ||
112                                            e instanceof ShippingLastNameException ||
113                                            e instanceof ShippingPhoneException ||
114                                            e instanceof ShippingStateException ||
115                                            e instanceof ShippingStreetException ||
116                                            e instanceof ShippingZipException) {
117    
118                                            SessionErrors.add(actionRequest, e.getClass());
119    
120                                            setForward(
121                                                    actionRequest, "portlet.shopping.checkout_first");
122                                    }
123                                    else if (e instanceof PrincipalException) {
124                                            setForward(actionRequest, "portlet.shopping.error");
125                                    }
126                                    else {
127                                            throw e;
128                                    }
129                            }
130                    }
131                    else if (cmd.equals(Constants.VIEW)) {
132                            setForward(actionRequest, "portlet.shopping.checkout_third");
133                    }
134                    else {
135                            setForward(actionRequest, "portlet.shopping.checkout_first");
136                    }
137            }
138    
139            protected void forwardCheckout(
140                            ActionRequest actionRequest, ActionResponse actionResponse)
141                    throws Exception {
142    
143                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
144                            WebKeys.THEME_DISPLAY);
145    
146                    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
147    
148                    ShoppingOrder order = (ShoppingOrder)actionRequest.getAttribute(
149                            WebKeys.SHOPPING_ORDER);
150    
151                    ShoppingPreferences preferences = ShoppingPreferences.getInstance(
152                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
153    
154                    String returnURL = ShoppingUtil.getPayPalReturnURL(
155                            ((ActionResponseImpl)actionResponse).createActionURL(), order);
156                    String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
157    
158                    if (preferences.usePayPal()) {
159                            double total = ShoppingUtil.calculateTotal(
160                                    cart.getItems(), order.getBillingState(), cart.getCoupon(),
161                                    cart.getAltShipping(), cart.isInsure());
162    
163                            String redirectURL = ShoppingUtil.getPayPalRedirectURL(
164                                    preferences, order, total, returnURL, notifyURL);
165    
166                            actionResponse.sendRedirect(redirectURL);
167                    }
168                    else {
169                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
170                                    actionRequest);
171    
172                            ShoppingOrderLocalServiceUtil.sendEmail(
173                                    order, "confirmation", serviceContext);
174    
175                            actionResponse.sendRedirect(returnURL);
176                    }
177            }
178    
179            protected void getLatestOrder(ActionRequest actionRequest)
180                    throws Exception {
181    
182                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
183                            WebKeys.THEME_DISPLAY);
184    
185                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
186                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
187    
188                    actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
189            }
190    
191            @Override
192            protected boolean isCheckMethodOnProcessAction() {
193                    return _CHECK_METHOD_ON_PROCESS_ACTION;
194            }
195    
196            protected void saveLatestOrder(ActionRequest actionRequest)
197                    throws Exception {
198    
199                    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
200    
201                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.saveLatestOrder(
202                            cart);
203    
204                    actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
205            }
206    
207            protected void updateLatestOrder(ActionRequest actionRequest)
208                    throws Exception {
209    
210                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
211                            WebKeys.THEME_DISPLAY);
212    
213                    String billingFirstName = ParamUtil.getString(
214                            actionRequest, "billingFirstName");
215                    String billingLastName = ParamUtil.getString(
216                            actionRequest, "billingLastName");
217                    String billingEmailAddress = ParamUtil.getString(
218                            actionRequest, "billingEmailAddress");
219                    String billingCompany = ParamUtil.getString(
220                            actionRequest, "billingCompany");
221                    String billingStreet = ParamUtil.getString(
222                            actionRequest, "billingStreet");
223                    String billingCity = ParamUtil.getString(actionRequest, "billingCity");
224    
225                    String billingStateSel = ParamUtil.getString(
226                            actionRequest, "billingStateSel");
227                    String billingState = billingStateSel;
228    
229                    if (Validator.isNull(billingStateSel)) {
230                            billingState = ParamUtil.getString(actionRequest, "billingState");
231                    }
232    
233                    String billingZip = ParamUtil.getString(actionRequest, "billingZip");
234                    String billingCountry = ParamUtil.getString(
235                            actionRequest, "billingCountry");
236                    String billingPhone = ParamUtil.getString(
237                            actionRequest, "billingPhone");
238    
239                    boolean shipToBilling = ParamUtil.getBoolean(
240                            actionRequest, "shipToBilling");
241                    String shippingFirstName = ParamUtil.getString(
242                            actionRequest, "shippingFirstName");
243                    String shippingLastName = ParamUtil.getString(
244                            actionRequest, "shippingLastName");
245                    String shippingEmailAddress = ParamUtil.getString(
246                            actionRequest, "shippingEmailAddress");
247                    String shippingCompany = ParamUtil.getString(
248                            actionRequest, "shippingCompany");
249                    String shippingStreet = ParamUtil.getString(
250                            actionRequest, "shippingStreet");
251                    String shippingCity = ParamUtil.getString(
252                            actionRequest, "shippingCity");
253    
254                    String shippingStateSel = ParamUtil.getString(
255                            actionRequest, "shippingStateSel");
256                    String shippingState = shippingStateSel;
257    
258                    if (Validator.isNull(shippingStateSel)) {
259                            shippingState = ParamUtil.getString(actionRequest, "shippingState");
260                    }
261    
262                    String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
263                    String shippingCountry = ParamUtil.getString(
264                            actionRequest, "shippingCountry");
265                    String shippingPhone = ParamUtil.getString(
266                            actionRequest, "shippingPhone");
267    
268                    String ccName = ParamUtil.getString(actionRequest, "ccName");
269                    String ccType = ParamUtil.getString(actionRequest, "ccType");
270                    String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
271                    int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
272                    int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
273                    String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
274    
275                    String comments = ParamUtil.getString(actionRequest, "comments");
276    
277                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
278                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
279                            billingFirstName, billingLastName, billingEmailAddress,
280                            billingCompany, billingStreet, billingCity, billingState,
281                            billingZip, billingCountry, billingPhone, shipToBilling,
282                            shippingFirstName, shippingLastName, shippingEmailAddress,
283                            shippingCompany, shippingStreet, shippingCity, shippingState,
284                            shippingZip, shippingCountry, shippingPhone, ccName, ccType,
285                            ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
286    
287                    actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
288            }
289    
290            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
291    
292    }