1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.ActionResponseImpl;
30  import com.liferay.portlet.shopping.BillingCityException;
31  import com.liferay.portlet.shopping.BillingCountryException;
32  import com.liferay.portlet.shopping.BillingEmailAddressException;
33  import com.liferay.portlet.shopping.BillingFirstNameException;
34  import com.liferay.portlet.shopping.BillingLastNameException;
35  import com.liferay.portlet.shopping.BillingPhoneException;
36  import com.liferay.portlet.shopping.BillingStateException;
37  import com.liferay.portlet.shopping.BillingStreetException;
38  import com.liferay.portlet.shopping.BillingZipException;
39  import com.liferay.portlet.shopping.CCExpirationException;
40  import com.liferay.portlet.shopping.CCNameException;
41  import com.liferay.portlet.shopping.CCNumberException;
42  import com.liferay.portlet.shopping.CCTypeException;
43  import com.liferay.portlet.shopping.ShippingCityException;
44  import com.liferay.portlet.shopping.ShippingCountryException;
45  import com.liferay.portlet.shopping.ShippingEmailAddressException;
46  import com.liferay.portlet.shopping.ShippingFirstNameException;
47  import com.liferay.portlet.shopping.ShippingLastNameException;
48  import com.liferay.portlet.shopping.ShippingPhoneException;
49  import com.liferay.portlet.shopping.ShippingStateException;
50  import com.liferay.portlet.shopping.ShippingStreetException;
51  import com.liferay.portlet.shopping.ShippingZipException;
52  import com.liferay.portlet.shopping.model.ShoppingCart;
53  import com.liferay.portlet.shopping.model.ShoppingOrder;
54  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
55  import com.liferay.portlet.shopping.util.ShoppingPreferences;
56  import com.liferay.portlet.shopping.util.ShoppingUtil;
57  
58  import javax.portlet.ActionRequest;
59  import javax.portlet.ActionResponse;
60  import javax.portlet.PortletConfig;
61  
62  import org.apache.struts.action.ActionForm;
63  import org.apache.struts.action.ActionMapping;
64  
65  /**
66   * <a href="CheckoutAction.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Brian Wing Shun Chan
69   *
70   */
71  public class CheckoutAction extends CartAction {
72  
73      public void processAction(
74              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
75              ActionRequest actionRequest, ActionResponse actionResponse)
76          throws Exception {
77  
78          if (redirectToLogin(actionRequest, actionResponse)) {
79              return;
80          }
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          getLatestOrder(actionRequest);
85  
86          if (cmd.equals(Constants.SAVE)) {
87              updateCart(actionRequest);
88              updateLatestOrder(actionRequest);
89              saveLatestOrder(actionRequest);
90              forwardCheckout(actionRequest, actionResponse);
91          }
92          else if (cmd.equals(Constants.UPDATE)) {
93              try {
94                  updateLatestOrder(actionRequest);
95  
96                  setForward(actionRequest, "portlet.shopping.checkout_second");
97              }
98              catch (Exception e) {
99                  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().getName());
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 forwardCheckout(
144             ActionRequest actionRequest, ActionResponse actionResponse)
145         throws Exception {
146 
147         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148             WebKeys.THEME_DISPLAY);
149 
150         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
151 
152         ShoppingOrder order = (ShoppingOrder)actionRequest.getAttribute(
153             WebKeys.SHOPPING_ORDER);
154 
155         ShoppingPreferences prefs = ShoppingPreferences.getInstance(
156             themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
157 
158         String returnURL = ShoppingUtil.getPayPalReturnURL(
159             ((ActionResponseImpl)actionResponse).createActionURL(), order);
160         String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
161 
162         if (prefs.usePayPal()) {
163             double total = ShoppingUtil.calculateTotal(
164                 cart.getItems(), order.getBillingState(), cart.getCoupon(),
165                 cart.getAltShipping(), cart.isInsure());
166 
167             String redirectURL = ShoppingUtil.getPayPalRedirectURL(
168                 prefs, order, total, returnURL, notifyURL);
169 
170             actionResponse.sendRedirect(redirectURL);
171         }
172         else {
173             ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
174 
175             actionResponse.sendRedirect(returnURL);
176         }
177     }
178 
179     protected void getLatestOrder(ActionRequest actionRequest)
180         throws Exception {
181 
182         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
183             WebKeys.THEME_DISPLAY);
184 
185         ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
186             themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
187 
188         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
189     }
190 
191     protected boolean isCheckMethodOnProcessAction() {
192         return _CHECK_METHOD_ON_PROCESS_ACTION;
193     }
194 
195     protected void saveLatestOrder(ActionRequest actionRequest)
196         throws Exception {
197 
198         ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
199 
200         ShoppingOrder order =
201             ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
202 
203         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
204     }
205 
206     protected void updateLatestOrder(ActionRequest actionRequest)
207         throws Exception {
208 
209         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
210             WebKeys.THEME_DISPLAY);
211 
212         String billingFirstName = ParamUtil.getString(
213             actionRequest, "billingFirstName");
214         String billingLastName = ParamUtil.getString(
215             actionRequest, "billingLastName");
216         String billingEmailAddress = ParamUtil.getString(
217             actionRequest, "billingEmailAddress");
218         String billingCompany = ParamUtil.getString(
219             actionRequest, "billingCompany");
220         String billingStreet = ParamUtil.getString(
221             actionRequest, "billingStreet");
222         String billingCity = ParamUtil.getString(actionRequest, "billingCity");
223 
224         String billingStateSel = ParamUtil.getString(
225             actionRequest, "billingStateSel");
226         String billingState = billingStateSel;
227         if (Validator.isNull(billingStateSel)) {
228             billingState = ParamUtil.getString(actionRequest, "billingState");
229         }
230 
231         String billingZip = ParamUtil.getString(actionRequest, "billingZip");
232         String billingCountry = ParamUtil.getString(
233             actionRequest, "billingCountry");
234         String billingPhone = ParamUtil.getString(
235             actionRequest, "billingPhone");
236 
237         boolean shipToBilling = ParamUtil.getBoolean(
238             actionRequest, "shipToBilling");
239         String shippingFirstName = ParamUtil.getString(
240             actionRequest, "shippingFirstName");
241         String shippingLastName = ParamUtil.getString(
242             actionRequest, "shippingLastName");
243         String shippingEmailAddress = ParamUtil.getString(
244             actionRequest, "shippingEmailAddress");
245         String shippingCompany = ParamUtil.getString(
246             actionRequest, "shippingCompany");
247         String shippingStreet = ParamUtil.getString(
248             actionRequest, "shippingStreet");
249         String shippingCity = ParamUtil.getString(
250             actionRequest, "shippingCity");
251 
252         String shippingStateSel = ParamUtil.getString(
253             actionRequest, "shippingStateSel");
254         String shippingState = shippingStateSel;
255         if (Validator.isNull(shippingStateSel)) {
256             shippingState = ParamUtil.getString(actionRequest, "shippingState");
257         }
258 
259         String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
260         String shippingCountry = ParamUtil.getString(
261             actionRequest, "shippingCountry");
262         String shippingPhone = ParamUtil.getString(
263             actionRequest, "shippingPhone");
264 
265         String ccName = ParamUtil.getString(actionRequest, "ccName");
266         String ccType = ParamUtil.getString(actionRequest, "ccType");
267         String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
268         int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
269         int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
270         String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
271 
272         String comments = ParamUtil.getString(actionRequest, "comments");
273 
274         ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
275             themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
276             billingFirstName, billingLastName, billingEmailAddress,
277             billingCompany, billingStreet, billingCity, billingState,
278             billingZip, billingCountry, billingPhone, shipToBilling,
279             shippingFirstName, shippingLastName, shippingEmailAddress,
280             shippingCompany, shippingStreet, shippingCity, shippingState,
281             shippingZip, shippingCountry, shippingPhone, ccName, ccType,
282             ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
283 
284         actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
285     }
286 
287     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
288 
289 }