1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class CheckoutAction extends CartAction {
74  
75      public void processAction(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws Exception {
79  
80          if (redirectToLogin(actionRequest, actionResponse)) {
81              return;
82          }
83  
84          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
85  
86          getLatestOrder(actionRequest);
87  
88          if (cmd.equals(Constants.SAVE)) {
89              updateCart(actionRequest);
90              updateLatestOrder(actionRequest);
91              saveLatestOrder(actionRequest);
92              forwardCheckout(actionRequest, actionResponse);
93          }
94          else if (cmd.equals(Constants.UPDATE)) {
95              try {
96                  updateLatestOrder(actionRequest);
97  
98                  setForward(actionRequest, "portlet.shopping.checkout_second");
99              }
100             catch (Exception e) {
101                 if (e instanceof BillingCityException ||
102                     e instanceof BillingCountryException ||
103                     e instanceof BillingEmailAddressException ||
104                     e instanceof BillingFirstNameException ||
105                     e instanceof BillingLastNameException ||
106                     e instanceof BillingPhoneException ||
107                     e instanceof BillingStateException ||
108                     e instanceof BillingStreetException ||
109                     e instanceof BillingZipException ||
110                     e instanceof CCExpirationException ||
111                     e instanceof CCNameException ||
112                     e instanceof CCNumberException ||
113                     e instanceof CCTypeException ||
114                     e instanceof ShippingCityException ||
115                     e instanceof ShippingCountryException ||
116                     e instanceof ShippingEmailAddressException ||
117                     e instanceof ShippingFirstNameException ||
118                     e instanceof ShippingLastNameException ||
119                     e instanceof ShippingPhoneException ||
120                     e instanceof ShippingStateException ||
121                     e instanceof ShippingStreetException ||
122                     e instanceof ShippingZipException) {
123 
124                     SessionErrors.add(actionRequest, e.getClass().getName());
125 
126                     setForward(
127                         actionRequest, "portlet.shopping.checkout_first");
128                 }
129                 else if (e instanceof PrincipalException) {
130                     setForward(actionRequest, "portlet.shopping.error");
131                 }
132                 else {
133                     throw e;
134                 }
135             }
136         }
137         else if (cmd.equals(Constants.VIEW)) {
138             setForward(actionRequest, "portlet.shopping.checkout_third");
139         }
140         else {
141             setForward(actionRequest, "portlet.shopping.checkout_first");
142         }
143     }
144 
145     protected void forwardCheckout(
146             ActionRequest actionRequest, ActionResponse actionResponse)
147         throws Exception {
148 
149         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150             WebKeys.THEME_DISPLAY);
151 
152         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
153 
154         ShoppingOrder order = (ShoppingOrder)actionRequest.getAttribute(
155             WebKeys.SHOPPING_ORDER);
156 
157         ShoppingPreferences prefs = ShoppingPreferences.getInstance(
158             themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
159 
160         String returnURL = ShoppingUtil.getPayPalReturnURL(
161             ((ActionResponseImpl)actionResponse).createActionURL(), order);
162         String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
163 
164         if (prefs.usePayPal()) {
165             double total = ShoppingUtil.calculateTotal(
166                 cart.getItems(), order.getBillingState(), cart.getCoupon(),
167                 cart.getAltShipping(), cart.isInsure());
168 
169             String redirectURL = ShoppingUtil.getPayPalRedirectURL(
170                 prefs, order, total, returnURL, notifyURL);
171 
172             actionResponse.sendRedirect(redirectURL);
173         }
174         else {
175             ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
176 
177             actionResponse.sendRedirect(returnURL);
178         }
179     }
180 
181     protected void getLatestOrder(ActionRequest actionRequest)
182         throws Exception {
183 
184         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
185             WebKeys.THEME_DISPLAY);
186 
187         ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
188             themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
189 
190         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
191     }
192 
193     protected boolean isCheckMethodOnProcessAction() {
194         return _CHECK_METHOD_ON_PROCESS_ACTION;
195     }
196 
197     protected void saveLatestOrder(ActionRequest actionRequest)
198         throws Exception {
199 
200         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
201 
202         ShoppingOrder order =
203             ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
204 
205         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
206     }
207 
208     protected void updateLatestOrder(ActionRequest actionRequest)
209         throws Exception {
210 
211         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
212             WebKeys.THEME_DISPLAY);
213 
214         String billingFirstName = ParamUtil.getString(
215             actionRequest, "billingFirstName");
216         String billingLastName = ParamUtil.getString(
217             actionRequest, "billingLastName");
218         String billingEmailAddress = ParamUtil.getString(
219             actionRequest, "billingEmailAddress");
220         String billingCompany = ParamUtil.getString(
221             actionRequest, "billingCompany");
222         String billingStreet = ParamUtil.getString(
223             actionRequest, "billingStreet");
224         String billingCity = ParamUtil.getString(actionRequest, "billingCity");
225 
226         String billingStateSel = ParamUtil.getString(
227             actionRequest, "billingStateSel");
228         String billingState = billingStateSel;
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         if (Validator.isNull(shippingStateSel)) {
258             shippingState = ParamUtil.getString(actionRequest, "shippingState");
259         }
260 
261         String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
262         String shippingCountry = ParamUtil.getString(
263             actionRequest, "shippingCountry");
264         String shippingPhone = ParamUtil.getString(
265             actionRequest, "shippingPhone");
266 
267         String ccName = ParamUtil.getString(actionRequest, "ccName");
268         String ccType = ParamUtil.getString(actionRequest, "ccType");
269         String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
270         int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
271         int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
272         String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
273 
274         String comments = ParamUtil.getString(actionRequest, "comments");
275 
276         ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
277             themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
278             billingFirstName, billingLastName, billingEmailAddress,
279             billingCompany, billingStreet, billingCity, billingState,
280             billingZip, billingCountry, billingPhone, shipToBilling,
281             shippingFirstName, shippingLastName, shippingEmailAddress,
282             shippingCompany, shippingStreet, shippingCity, shippingState,
283             shippingZip, shippingCountry, shippingPhone, ccName, ccType,
284             ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
285 
286         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
287     }
288 
289     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
290 
291 }