1   /**
2    * Copyright (c) 2000-2007 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 getLatestOrder(ActionRequest req) throws Exception {
146         ThemeDisplay themeDisplay =
147             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
148 
149         ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
150             themeDisplay.getUserId(), themeDisplay.getPortletGroupId());
151 
152         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
153     }
154 
155     protected void forwardCheckout(ActionRequest req, ActionResponse res)
156         throws Exception {
157 
158         ThemeDisplay themeDisplay =
159             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
160 
161         ShoppingCart cart = ShoppingUtil.getCart(req);
162 
163         ShoppingOrder order =
164             (ShoppingOrder)req.getAttribute(WebKeys.SHOPPING_ORDER);
165 
166         ShoppingPreferences prefs = ShoppingPreferences.getInstance(
167             themeDisplay.getCompanyId(), themeDisplay.getPortletGroupId());
168 
169         String returnURL = ShoppingUtil.getPayPalReturnURL(
170             ((ActionResponseImpl)res).createActionURL(), order);
171         String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
172 
173         if (prefs.usePayPal()) {
174             double total = ShoppingUtil.calculateTotal(
175                 cart.getItems(), order.getBillingState(), cart.getCoupon(),
176                 cart.getAltShipping(), cart.isInsure());
177 
178             String redirectURL = ShoppingUtil.getPayPalRedirectURL(
179                 prefs, order, total, returnURL, notifyURL);
180 
181             res.sendRedirect(redirectURL);
182         }
183         else {
184             ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
185 
186             res.sendRedirect(returnURL);
187         }
188     }
189 
190     protected void saveLatestOrder(ActionRequest req) throws Exception {
191         ShoppingCart cart = ShoppingUtil.getCart(req);
192 
193         ShoppingOrder order =
194             ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
195 
196         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
197     }
198 
199     protected void updateLatestOrder(ActionRequest req) throws Exception {
200         ThemeDisplay themeDisplay =
201             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
202 
203         String billingFirstName = ParamUtil.getString(req, "billingFirstName");
204         String billingLastName = ParamUtil.getString(req, "billingLastName");
205         String billingEmailAddress = ParamUtil.getString(
206             req, "billingEmailAddress");
207         String billingCompany = ParamUtil.getString(req, "billingCompany");
208         String billingStreet = ParamUtil.getString(req, "billingStreet");
209         String billingCity = ParamUtil.getString(req, "billingCity");
210 
211         String billingStateSel = ParamUtil.getString(req, "billingStateSel");
212         String billingState = billingStateSel;
213         if (Validator.isNull(billingStateSel)) {
214             billingState = ParamUtil.getString(req, "billingState");
215         }
216 
217         String billingZip = ParamUtil.getString(req, "billingZip");
218         String billingCountry = ParamUtil.getString(req, "billingCountry");
219         String billingPhone = ParamUtil.getString(req, "billingPhone");
220 
221         boolean shipToBilling = ParamUtil.getBoolean(req, "shipToBilling");
222         String shippingFirstName = ParamUtil.getString(
223             req, "shippingFirstName");
224         String shippingLastName = ParamUtil.getString(req, "shippingLastName");
225         String shippingEmailAddress = ParamUtil.getString(
226             req, "shippingEmailAddress");
227         String shippingCompany = ParamUtil.getString(req, "shippingCompany");
228         String shippingStreet = ParamUtil.getString(req, "shippingStreet");
229         String shippingCity = ParamUtil.getString(req, "shippingCity");
230 
231         String shippingStateSel = ParamUtil.getString(req, "shippingStateSel");
232         String shippingState = shippingStateSel;
233         if (Validator.isNull(shippingStateSel)) {
234             shippingState = ParamUtil.getString(req, "shippingState");
235         }
236 
237         String shippingZip = ParamUtil.getString(req, "shippingZip");
238         String shippingCountry = ParamUtil.getString(req, "shippingCountry");
239         String shippingPhone = ParamUtil.getString(req, "shippingPhone");
240 
241         String ccName = ParamUtil.getString(req, "ccName");
242         String ccType = ParamUtil.getString(req, "ccType");
243         String ccNumber = ParamUtil.getString(req, "ccNumber");
244         int ccExpMonth = ParamUtil.getInteger(req, "ccExpMonth");
245         int ccExpYear = ParamUtil.getInteger(req, "ccExpYear");
246         String ccVerNumber = ParamUtil.getString(req, "ccVerNumber");
247 
248         String comments = ParamUtil.getString(req, "comments");
249 
250         ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
251             themeDisplay.getUserId(), themeDisplay.getPortletGroupId(),
252             billingFirstName, billingLastName, billingEmailAddress,
253             billingCompany, billingStreet, billingCity, billingState,
254             billingZip, billingCountry, billingPhone, shipToBilling,
255             shippingFirstName, shippingLastName, shippingEmailAddress,
256             shippingCompany, shippingStreet, shippingCity, shippingState,
257             shippingZip, shippingCountry, shippingPhone, ccName, ccType,
258             ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
259 
260         req.setAttribute(WebKeys.SHOPPING_ORDER, order);
261     }
262 
263 }