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.ShoppingGroupServiceSettings;
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                    ShoppingGroupServiceSettings shoppingGroupServiceSettings =
164                            ShoppingGroupServiceSettings.getInstance(
165                                    themeDisplay.getScopeGroupId());
166    
167                    String returnURL = ShoppingUtil.getPayPalReturnURL(
168                            ((ActionResponseImpl)actionResponse).createActionURL(), order);
169                    String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
170    
171                    if (shoppingGroupServiceSettings.usePayPal()) {
172                            double total = ShoppingUtil.calculateTotal(
173                                    cart.getItems(), order.getBillingState(), cart.getCoupon(),
174                                    cart.getAltShipping(), cart.isInsure());
175    
176                            String redirectURL = ShoppingUtil.getPayPalRedirectURL(
177                                    shoppingGroupServiceSettings, order, total, returnURL,
178                                    notifyURL);
179    
180                            actionResponse.sendRedirect(redirectURL);
181                    }
182                    else {
183                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
184                                    actionRequest);
185    
186                            ShoppingOrderLocalServiceUtil.sendEmail(
187                                    order, "confirmation", serviceContext);
188    
189                            actionResponse.sendRedirect(returnURL);
190                    }
191            }
192    
193            protected boolean hasLatestOrder(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    try {
197                            ThemeDisplay themeDisplay =
198                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
199    
200                            ShoppingOrderLocalServiceUtil.getLatestOrder(
201                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
202    
203                            return true;
204                    }
205                    catch (NoSuchOrderException nsoe) {
206                            return false;
207                    }
208            }
209    
210            @Override
211            protected boolean isCheckMethodOnProcessAction() {
212                    return _CHECK_METHOD_ON_PROCESS_ACTION;
213            }
214    
215            protected void saveLatestOrder(
216                            ActionRequest actionRequest, ActionResponse actionResponse)
217                    throws Exception {
218    
219                    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
220    
221                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.saveLatestOrder(
222                            cart);
223    
224                    forwardCheckout(actionRequest, actionResponse, order);
225            }
226    
227            protected void updateLatestOrder(ActionRequest actionRequest)
228                    throws Exception {
229    
230                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
231                            WebKeys.THEME_DISPLAY);
232    
233                    String billingFirstName = ParamUtil.getString(
234                            actionRequest, "billingFirstName");
235                    String billingLastName = ParamUtil.getString(
236                            actionRequest, "billingLastName");
237                    String billingEmailAddress = ParamUtil.getString(
238                            actionRequest, "billingEmailAddress");
239                    String billingCompany = ParamUtil.getString(
240                            actionRequest, "billingCompany");
241                    String billingStreet = ParamUtil.getString(
242                            actionRequest, "billingStreet");
243                    String billingCity = ParamUtil.getString(actionRequest, "billingCity");
244    
245                    String billingStateSel = ParamUtil.getString(
246                            actionRequest, "billingStateSel");
247                    String billingState = billingStateSel;
248    
249                    if (Validator.isNull(billingStateSel)) {
250                            billingState = ParamUtil.getString(actionRequest, "billingState");
251                    }
252    
253                    String billingZip = ParamUtil.getString(actionRequest, "billingZip");
254                    String billingCountry = ParamUtil.getString(
255                            actionRequest, "billingCountry");
256                    String billingPhone = ParamUtil.getString(
257                            actionRequest, "billingPhone");
258    
259                    boolean shipToBilling = ParamUtil.getBoolean(
260                            actionRequest, "shipToBilling");
261                    String shippingFirstName = ParamUtil.getString(
262                            actionRequest, "shippingFirstName");
263                    String shippingLastName = ParamUtil.getString(
264                            actionRequest, "shippingLastName");
265                    String shippingEmailAddress = ParamUtil.getString(
266                            actionRequest, "shippingEmailAddress");
267                    String shippingCompany = ParamUtil.getString(
268                            actionRequest, "shippingCompany");
269                    String shippingStreet = ParamUtil.getString(
270                            actionRequest, "shippingStreet");
271                    String shippingCity = ParamUtil.getString(
272                            actionRequest, "shippingCity");
273    
274                    String shippingStateSel = ParamUtil.getString(
275                            actionRequest, "shippingStateSel");
276                    String shippingState = shippingStateSel;
277    
278                    if (Validator.isNull(shippingStateSel)) {
279                            shippingState = ParamUtil.getString(actionRequest, "shippingState");
280                    }
281    
282                    String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
283                    String shippingCountry = ParamUtil.getString(
284                            actionRequest, "shippingCountry");
285                    String shippingPhone = ParamUtil.getString(
286                            actionRequest, "shippingPhone");
287    
288                    String ccName = ParamUtil.getString(actionRequest, "ccName");
289                    String ccType = ParamUtil.getString(actionRequest, "ccType");
290                    String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
291                    int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
292                    int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
293                    String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
294    
295                    String comments = ParamUtil.getString(actionRequest, "comments");
296    
297                    ShoppingOrderLocalServiceUtil.updateLatestOrder(
298                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
299                            billingFirstName, billingLastName, billingEmailAddress,
300                            billingCompany, billingStreet, billingCity, billingState,
301                            billingZip, billingCountry, billingPhone, shipToBilling,
302                            shippingFirstName, shippingLastName, shippingEmailAddress,
303                            shippingCompany, shippingStreet, shippingCity, shippingState,
304                            shippingZip, shippingCountry, shippingPhone, ccName, ccType,
305                            ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
306            }
307    
308            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
309    
310    }