1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.ActionResponseImpl;
32  import com.liferay.portlet.shopping.BillingCityException;
33  import com.liferay.portlet.shopping.BillingCountryException;
34  import com.liferay.portlet.shopping.BillingEmailAddressException;
35  import com.liferay.portlet.shopping.BillingFirstNameException;
36  import com.liferay.portlet.shopping.BillingLastNameException;
37  import com.liferay.portlet.shopping.BillingPhoneException;
38  import com.liferay.portlet.shopping.BillingStateException;
39  import com.liferay.portlet.shopping.BillingStreetException;
40  import com.liferay.portlet.shopping.BillingZipException;
41  import com.liferay.portlet.shopping.CCExpirationException;
42  import com.liferay.portlet.shopping.CCNameException;
43  import com.liferay.portlet.shopping.CCNumberException;
44  import com.liferay.portlet.shopping.CCTypeException;
45  import com.liferay.portlet.shopping.ShippingCityException;
46  import com.liferay.portlet.shopping.ShippingCountryException;
47  import com.liferay.portlet.shopping.ShippingEmailAddressException;
48  import com.liferay.portlet.shopping.ShippingFirstNameException;
49  import com.liferay.portlet.shopping.ShippingLastNameException;
50  import com.liferay.portlet.shopping.ShippingPhoneException;
51  import com.liferay.portlet.shopping.ShippingStateException;
52  import com.liferay.portlet.shopping.ShippingStreetException;
53  import com.liferay.portlet.shopping.ShippingZipException;
54  import com.liferay.portlet.shopping.model.ShoppingCart;
55  import com.liferay.portlet.shopping.model.ShoppingOrder;
56  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
57  import com.liferay.portlet.shopping.util.ShoppingPreferences;
58  import com.liferay.portlet.shopping.util.ShoppingUtil;
59  import com.liferay.util.servlet.SessionErrors;
60  
61  import javax.portlet.ActionRequest;
62  import javax.portlet.ActionResponse;
63  import javax.portlet.PortletConfig;
64  
65  import org.apache.struts.action.ActionForm;
66  import org.apache.struts.action.ActionMapping;
67  
68  /**
69   * <a href="CheckoutAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   *
73   */
74  public class CheckoutAction extends CartAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig config,
78              ActionRequest req, ActionResponse res)
79          throws Exception {
80  
81          if (redirectToLogin(req, res)) {
82              return;
83          }
84  
85          String cmd = ParamUtil.getString(req, Constants.CMD);
86  
87          getLatestOrder(req);
88  
89          if (cmd.equals(Constants.SAVE)) {
90              updateCart(req);
91              updateLatestOrder(req);
92              saveLatestOrder(req);
93              forwardCheckout(req, res);
94          }
95          else if (cmd.equals(Constants.UPDATE)) {
96              try {
97                  updateLatestOrder(req);
98  
99                  setForward(req, "portlet.shopping.checkout_second");
100             }
101             catch (Exception e) {
102                 if (e instanceof BillingCityException ||
103                     e instanceof BillingCountryException ||
104                     e instanceof BillingEmailAddressException ||
105                     e instanceof BillingFirstNameException ||
106                     e instanceof BillingLastNameException ||
107                     e instanceof BillingPhoneException ||
108                     e instanceof BillingStateException ||
109                     e instanceof BillingStreetException ||
110                     e instanceof BillingZipException ||
111                     e instanceof CCExpirationException ||
112                     e instanceof CCNameException ||
113                     e instanceof CCNumberException ||
114                     e instanceof CCTypeException ||
115                     e instanceof ShippingCityException ||
116                     e instanceof ShippingCountryException ||
117                     e instanceof ShippingEmailAddressException ||
118                     e instanceof ShippingFirstNameException ||
119                     e instanceof ShippingLastNameException ||
120                     e instanceof ShippingPhoneException ||
121                     e instanceof ShippingStateException ||
122                     e instanceof ShippingStreetException ||
123                     e instanceof ShippingZipException) {
124 
125                     SessionErrors.add(req, e.getClass().getName());
126 
127                     setForward(req, "portlet.shopping.checkout_first");
128                 }
129                 else if (e instanceof PrincipalException) {
130                     setForward(req, "portlet.shopping.error");
131                 }
132                 else {
133                     throw e;
134                 }
135             }
136         }
137         else if (cmd.equals(Constants.VIEW)) {
138             setForward(req, "portlet.shopping.checkout_third");
139         }
140         else {
141             setForward(req, "portlet.shopping.checkout_first");
142         }
143     }
144 
145     protected void forwardCheckout(ActionRequest req, ActionResponse res)
146         throws Exception {
147 
148         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
149             WebKeys.THEME_DISPLAY);
150 
151         ShoppingCart cart = ShoppingUtil.getCart(req);
152 
153         ShoppingOrder order = (ShoppingOrder)req.getAttribute(
154             WebKeys.SHOPPING_ORDER);
155 
156         ShoppingPreferences prefs = ShoppingPreferences.getInstance(
157             themeDisplay.getCompanyId(), themeDisplay.getPortletGroupId());
158 
159         String returnURL = ShoppingUtil.getPayPalReturnURL(
160             ((ActionResponseImpl)res).createActionURL(), order);
161         String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
162 
163         if (prefs.usePayPal()) {
164             double total = ShoppingUtil.calculateTotal(
165                 cart.getItems(), order.getBillingState(), cart.getCoupon(),
166                 cart.getAltShipping(), cart.isInsure());
167 
168             String redirectURL = ShoppingUtil.getPayPalRedirectURL(
169                 prefs, order, total, returnURL, notifyURL);
170 
171             res.sendRedirect(redirectURL);
172         }
173         else {
174             ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
175 
176             res.sendRedirect(returnURL);
177         }
178     }
179 
180     protected void getLatestOrder(ActionRequest req) throws Exception {
181         ThemeDisplay themeDisplay =
182             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
183 
184         ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
185             themeDisplay.getUserId(), themeDisplay.getPortletGroupId());
186 
187         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
188     }
189 
190     protected boolean isCheckMethodOnProcessAction() {
191         return _CHECK_METHOD_ON_PROCESS_ACTION;
192     }
193 
194     protected void saveLatestOrder(ActionRequest req) throws Exception {
195         ShoppingCart cart = ShoppingUtil.getCart(req);
196 
197         ShoppingOrder order =
198             ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
199 
200         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
201     }
202 
203     protected void updateLatestOrder(ActionRequest req) throws Exception {
204         ThemeDisplay themeDisplay =
205             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
206 
207         String billingFirstName = ParamUtil.getString(req, "billingFirstName");
208         String billingLastName = ParamUtil.getString(req, "billingLastName");
209         String billingEmailAddress = ParamUtil.getString(
210             req, "billingEmailAddress");
211         String billingCompany = ParamUtil.getString(req, "billingCompany");
212         String billingStreet = ParamUtil.getString(req, "billingStreet");
213         String billingCity = ParamUtil.getString(req, "billingCity");
214 
215         String billingStateSel = ParamUtil.getString(req, "billingStateSel");
216         String billingState = billingStateSel;
217         if (Validator.isNull(billingStateSel)) {
218             billingState = ParamUtil.getString(req, "billingState");
219         }
220 
221         String billingZip = ParamUtil.getString(req, "billingZip");
222         String billingCountry = ParamUtil.getString(req, "billingCountry");
223         String billingPhone = ParamUtil.getString(req, "billingPhone");
224 
225         boolean shipToBilling = ParamUtil.getBoolean(req, "shipToBilling");
226         String shippingFirstName = ParamUtil.getString(
227             req, "shippingFirstName");
228         String shippingLastName = ParamUtil.getString(req, "shippingLastName");
229         String shippingEmailAddress = ParamUtil.getString(
230             req, "shippingEmailAddress");
231         String shippingCompany = ParamUtil.getString(req, "shippingCompany");
232         String shippingStreet = ParamUtil.getString(req, "shippingStreet");
233         String shippingCity = ParamUtil.getString(req, "shippingCity");
234 
235         String shippingStateSel = ParamUtil.getString(req, "shippingStateSel");
236         String shippingState = shippingStateSel;
237         if (Validator.isNull(shippingStateSel)) {
238             shippingState = ParamUtil.getString(req, "shippingState");
239         }
240 
241         String shippingZip = ParamUtil.getString(req, "shippingZip");
242         String shippingCountry = ParamUtil.getString(req, "shippingCountry");
243         String shippingPhone = ParamUtil.getString(req, "shippingPhone");
244 
245         String ccName = ParamUtil.getString(req, "ccName");
246         String ccType = ParamUtil.getString(req, "ccType");
247         String ccNumber = ParamUtil.getString(req, "ccNumber");
248         int ccExpMonth = ParamUtil.getInteger(req, "ccExpMonth");
249         int ccExpYear = ParamUtil.getInteger(req, "ccExpYear");
250         String ccVerNumber = ParamUtil.getString(req, "ccVerNumber");
251 
252         String comments = ParamUtil.getString(req, "comments");
253 
254         ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
255             themeDisplay.getUserId(), themeDisplay.getPortletGroupId(),
256             billingFirstName, billingLastName, billingEmailAddress,
257             billingCompany, billingStreet, billingCity, billingState,
258             billingZip, billingCountry, billingPhone, shipToBilling,
259             shippingFirstName, shippingLastName, shippingEmailAddress,
260             shippingCompany, shippingStreet, shippingCity, shippingState,
261             shippingZip, shippingCountry, shippingPhone, ccName, ccType,
262             ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
263 
264         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
265     }
266 
267     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
268 
269 }