1   /**
2    * Copyright (c) 2000-2009 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.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionResponseImpl;
33  import com.liferay.portlet.shopping.BillingCityException;
34  import com.liferay.portlet.shopping.BillingCountryException;
35  import com.liferay.portlet.shopping.BillingEmailAddressException;
36  import com.liferay.portlet.shopping.BillingFirstNameException;
37  import com.liferay.portlet.shopping.BillingLastNameException;
38  import com.liferay.portlet.shopping.BillingPhoneException;
39  import com.liferay.portlet.shopping.BillingStateException;
40  import com.liferay.portlet.shopping.BillingStreetException;
41  import com.liferay.portlet.shopping.BillingZipException;
42  import com.liferay.portlet.shopping.CCExpirationException;
43  import com.liferay.portlet.shopping.CCNameException;
44  import com.liferay.portlet.shopping.CCNumberException;
45  import com.liferay.portlet.shopping.CCTypeException;
46  import com.liferay.portlet.shopping.ShippingCityException;
47  import com.liferay.portlet.shopping.ShippingCountryException;
48  import com.liferay.portlet.shopping.ShippingEmailAddressException;
49  import com.liferay.portlet.shopping.ShippingFirstNameException;
50  import com.liferay.portlet.shopping.ShippingLastNameException;
51  import com.liferay.portlet.shopping.ShippingPhoneException;
52  import com.liferay.portlet.shopping.ShippingStateException;
53  import com.liferay.portlet.shopping.ShippingStreetException;
54  import com.liferay.portlet.shopping.ShippingZipException;
55  import com.liferay.portlet.shopping.model.ShoppingCart;
56  import com.liferay.portlet.shopping.model.ShoppingOrder;
57  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
58  import com.liferay.portlet.shopping.util.ShoppingPreferences;
59  import com.liferay.portlet.shopping.util.ShoppingUtil;
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 portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          if (redirectToLogin(actionRequest, actionResponse)) {
82              return;
83          }
84  
85          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
86  
87          getLatestOrder(actionRequest);
88  
89          if (cmd.equals(Constants.SAVE)) {
90              updateCart(actionRequest);
91              updateLatestOrder(actionRequest);
92              saveLatestOrder(actionRequest);
93              forwardCheckout(actionRequest, actionResponse);
94          }
95          else if (cmd.equals(Constants.UPDATE)) {
96              try {
97                  updateLatestOrder(actionRequest);
98  
99                  setForward(actionRequest, "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(actionRequest, e.getClass().getName());
126 
127                     setForward(
128                         actionRequest, "portlet.shopping.checkout_first");
129                 }
130                 else if (e instanceof PrincipalException) {
131                     setForward(actionRequest, "portlet.shopping.error");
132                 }
133                 else {
134                     throw e;
135                 }
136             }
137         }
138         else if (cmd.equals(Constants.VIEW)) {
139             setForward(actionRequest, "portlet.shopping.checkout_third");
140         }
141         else {
142             setForward(actionRequest, "portlet.shopping.checkout_first");
143         }
144     }
145 
146     protected void forwardCheckout(
147             ActionRequest actionRequest, ActionResponse actionResponse)
148         throws Exception {
149 
150         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
151             WebKeys.THEME_DISPLAY);
152 
153         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
154 
155         ShoppingOrder order = (ShoppingOrder)actionRequest.getAttribute(
156             WebKeys.SHOPPING_ORDER);
157 
158         ShoppingPreferences preferences = ShoppingPreferences.getInstance(
159             themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
160 
161         String returnURL = ShoppingUtil.getPayPalReturnURL(
162             ((ActionResponseImpl)actionResponse).createActionURL(), order);
163         String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
164 
165         if (preferences.usePayPal()) {
166             double total = ShoppingUtil.calculateTotal(
167                 cart.getItems(), order.getBillingState(), cart.getCoupon(),
168                 cart.getAltShipping(), cart.isInsure());
169 
170             String redirectURL = ShoppingUtil.getPayPalRedirectURL(
171                 preferences, order, total, returnURL, notifyURL);
172 
173             actionResponse.sendRedirect(redirectURL);
174         }
175         else {
176             ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
177 
178             actionResponse.sendRedirect(returnURL);
179         }
180     }
181 
182     protected void getLatestOrder(ActionRequest actionRequest)
183         throws Exception {
184 
185         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
186             WebKeys.THEME_DISPLAY);
187 
188         ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
189             themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
190 
191         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
192     }
193 
194     protected boolean isCheckMethodOnProcessAction() {
195         return _CHECK_METHOD_ON_PROCESS_ACTION;
196     }
197 
198     protected void saveLatestOrder(ActionRequest actionRequest)
199         throws Exception {
200 
201         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
202 
203         ShoppingOrder order =
204             ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
205 
206         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
207     }
208 
209     protected void updateLatestOrder(ActionRequest actionRequest)
210         throws Exception {
211 
212         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
213             WebKeys.THEME_DISPLAY);
214 
215         String billingFirstName = ParamUtil.getString(
216             actionRequest, "billingFirstName");
217         String billingLastName = ParamUtil.getString(
218             actionRequest, "billingLastName");
219         String billingEmailAddress = ParamUtil.getString(
220             actionRequest, "billingEmailAddress");
221         String billingCompany = ParamUtil.getString(
222             actionRequest, "billingCompany");
223         String billingStreet = ParamUtil.getString(
224             actionRequest, "billingStreet");
225         String billingCity = ParamUtil.getString(actionRequest, "billingCity");
226 
227         String billingStateSel = ParamUtil.getString(
228             actionRequest, "billingStateSel");
229         String billingState = billingStateSel;
230         if (Validator.isNull(billingStateSel)) {
231             billingState = ParamUtil.getString(actionRequest, "billingState");
232         }
233 
234         String billingZip = ParamUtil.getString(actionRequest, "billingZip");
235         String billingCountry = ParamUtil.getString(
236             actionRequest, "billingCountry");
237         String billingPhone = ParamUtil.getString(
238             actionRequest, "billingPhone");
239 
240         boolean shipToBilling = ParamUtil.getBoolean(
241             actionRequest, "shipToBilling");
242         String shippingFirstName = ParamUtil.getString(
243             actionRequest, "shippingFirstName");
244         String shippingLastName = ParamUtil.getString(
245             actionRequest, "shippingLastName");
246         String shippingEmailAddress = ParamUtil.getString(
247             actionRequest, "shippingEmailAddress");
248         String shippingCompany = ParamUtil.getString(
249             actionRequest, "shippingCompany");
250         String shippingStreet = ParamUtil.getString(
251             actionRequest, "shippingStreet");
252         String shippingCity = ParamUtil.getString(
253             actionRequest, "shippingCity");
254 
255         String shippingStateSel = ParamUtil.getString(
256             actionRequest, "shippingStateSel");
257         String shippingState = shippingStateSel;
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 }