001
014
015 package com.liferay.portlet.shopping.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.security.auth.PrincipalException;
022 import com.liferay.portal.theme.ThemeDisplay;
023 import com.liferay.portal.util.WebKeys;
024 import com.liferay.portlet.ActionResponseImpl;
025 import com.liferay.portlet.shopping.BillingCityException;
026 import com.liferay.portlet.shopping.BillingCountryException;
027 import com.liferay.portlet.shopping.BillingEmailAddressException;
028 import com.liferay.portlet.shopping.BillingFirstNameException;
029 import com.liferay.portlet.shopping.BillingLastNameException;
030 import com.liferay.portlet.shopping.BillingPhoneException;
031 import com.liferay.portlet.shopping.BillingStateException;
032 import com.liferay.portlet.shopping.BillingStreetException;
033 import com.liferay.portlet.shopping.BillingZipException;
034 import com.liferay.portlet.shopping.CCExpirationException;
035 import com.liferay.portlet.shopping.CCNameException;
036 import com.liferay.portlet.shopping.CCNumberException;
037 import com.liferay.portlet.shopping.CCTypeException;
038 import com.liferay.portlet.shopping.ShippingCityException;
039 import com.liferay.portlet.shopping.ShippingCountryException;
040 import com.liferay.portlet.shopping.ShippingEmailAddressException;
041 import com.liferay.portlet.shopping.ShippingFirstNameException;
042 import com.liferay.portlet.shopping.ShippingLastNameException;
043 import com.liferay.portlet.shopping.ShippingPhoneException;
044 import com.liferay.portlet.shopping.ShippingStateException;
045 import com.liferay.portlet.shopping.ShippingStreetException;
046 import com.liferay.portlet.shopping.ShippingZipException;
047 import com.liferay.portlet.shopping.model.ShoppingCart;
048 import com.liferay.portlet.shopping.model.ShoppingOrder;
049 import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
050 import com.liferay.portlet.shopping.util.ShoppingPreferences;
051 import com.liferay.portlet.shopping.util.ShoppingUtil;
052
053 import javax.portlet.ActionRequest;
054 import javax.portlet.ActionResponse;
055 import javax.portlet.PortletConfig;
056
057 import org.apache.struts.action.ActionForm;
058 import org.apache.struts.action.ActionMapping;
059
060
063 public class CheckoutAction extends CartAction {
064
065 public void processAction(
066 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
067 ActionRequest actionRequest, ActionResponse actionResponse)
068 throws Exception {
069
070 if (redirectToLogin(actionRequest, actionResponse)) {
071 return;
072 }
073
074 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
075
076 getLatestOrder(actionRequest);
077
078 if (cmd.equals(Constants.SAVE)) {
079 updateCart(actionRequest, actionResponse);
080 updateLatestOrder(actionRequest);
081 saveLatestOrder(actionRequest);
082 forwardCheckout(actionRequest, actionResponse);
083 }
084 else if (cmd.equals(Constants.UPDATE)) {
085 try {
086 updateLatestOrder(actionRequest);
087
088 setForward(actionRequest, "portlet.shopping.checkout_second");
089 }
090 catch (Exception e) {
091 if (e instanceof BillingCityException ||
092 e instanceof BillingCountryException ||
093 e instanceof BillingEmailAddressException ||
094 e instanceof BillingFirstNameException ||
095 e instanceof BillingLastNameException ||
096 e instanceof BillingPhoneException ||
097 e instanceof BillingStateException ||
098 e instanceof BillingStreetException ||
099 e instanceof BillingZipException ||
100 e instanceof CCExpirationException ||
101 e instanceof CCNameException ||
102 e instanceof CCNumberException ||
103 e instanceof CCTypeException ||
104 e instanceof ShippingCityException ||
105 e instanceof ShippingCountryException ||
106 e instanceof ShippingEmailAddressException ||
107 e instanceof ShippingFirstNameException ||
108 e instanceof ShippingLastNameException ||
109 e instanceof ShippingPhoneException ||
110 e instanceof ShippingStateException ||
111 e instanceof ShippingStreetException ||
112 e instanceof ShippingZipException) {
113
114 SessionErrors.add(actionRequest, e.getClass().getName());
115
116 setForward(
117 actionRequest, "portlet.shopping.checkout_first");
118 }
119 else if (e instanceof PrincipalException) {
120 setForward(actionRequest, "portlet.shopping.error");
121 }
122 else {
123 throw e;
124 }
125 }
126 }
127 else if (cmd.equals(Constants.VIEW)) {
128 setForward(actionRequest, "portlet.shopping.checkout_third");
129 }
130 else {
131 setForward(actionRequest, "portlet.shopping.checkout_first");
132 }
133 }
134
135 protected void forwardCheckout(
136 ActionRequest actionRequest, ActionResponse actionResponse)
137 throws Exception {
138
139 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
140 WebKeys.THEME_DISPLAY);
141
142 ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
143
144 ShoppingOrder order = (ShoppingOrder)actionRequest.getAttribute(
145 WebKeys.SHOPPING_ORDER);
146
147 ShoppingPreferences preferences = ShoppingPreferences.getInstance(
148 themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
149
150 String returnURL = ShoppingUtil.getPayPalReturnURL(
151 ((ActionResponseImpl)actionResponse).createActionURL(), order);
152 String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);
153
154 if (preferences.usePayPal()) {
155 double total = ShoppingUtil.calculateTotal(
156 cart.getItems(), order.getBillingState(), cart.getCoupon(),
157 cart.getAltShipping(), cart.isInsure());
158
159 String redirectURL = ShoppingUtil.getPayPalRedirectURL(
160 preferences, order, total, returnURL, notifyURL);
161
162 actionResponse.sendRedirect(redirectURL);
163 }
164 else {
165 ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation");
166
167 actionResponse.sendRedirect(returnURL);
168 }
169 }
170
171 protected void getLatestOrder(ActionRequest actionRequest)
172 throws Exception {
173
174 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
175 WebKeys.THEME_DISPLAY);
176
177 ShoppingOrder order = ShoppingOrderLocalServiceUtil.getLatestOrder(
178 themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
179
180 actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
181 }
182
183 protected boolean isCheckMethodOnProcessAction() {
184 return _CHECK_METHOD_ON_PROCESS_ACTION;
185 }
186
187 protected void saveLatestOrder(ActionRequest actionRequest)
188 throws Exception {
189
190 ShoppingCart cart = ShoppingUtil.getCart(actionRequest);
191
192 ShoppingOrder order =
193 ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);
194
195 actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
196 }
197
198 protected void updateLatestOrder(ActionRequest actionRequest)
199 throws Exception {
200
201 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
202 WebKeys.THEME_DISPLAY);
203
204 String billingFirstName = ParamUtil.getString(
205 actionRequest, "billingFirstName");
206 String billingLastName = ParamUtil.getString(
207 actionRequest, "billingLastName");
208 String billingEmailAddress = ParamUtil.getString(
209 actionRequest, "billingEmailAddress");
210 String billingCompany = ParamUtil.getString(
211 actionRequest, "billingCompany");
212 String billingStreet = ParamUtil.getString(
213 actionRequest, "billingStreet");
214 String billingCity = ParamUtil.getString(actionRequest, "billingCity");
215
216 String billingStateSel = ParamUtil.getString(
217 actionRequest, "billingStateSel");
218 String billingState = billingStateSel;
219 if (Validator.isNull(billingStateSel)) {
220 billingState = ParamUtil.getString(actionRequest, "billingState");
221 }
222
223 String billingZip = ParamUtil.getString(actionRequest, "billingZip");
224 String billingCountry = ParamUtil.getString(
225 actionRequest, "billingCountry");
226 String billingPhone = ParamUtil.getString(
227 actionRequest, "billingPhone");
228
229 boolean shipToBilling = ParamUtil.getBoolean(
230 actionRequest, "shipToBilling");
231 String shippingFirstName = ParamUtil.getString(
232 actionRequest, "shippingFirstName");
233 String shippingLastName = ParamUtil.getString(
234 actionRequest, "shippingLastName");
235 String shippingEmailAddress = ParamUtil.getString(
236 actionRequest, "shippingEmailAddress");
237 String shippingCompany = ParamUtil.getString(
238 actionRequest, "shippingCompany");
239 String shippingStreet = ParamUtil.getString(
240 actionRequest, "shippingStreet");
241 String shippingCity = ParamUtil.getString(
242 actionRequest, "shippingCity");
243
244 String shippingStateSel = ParamUtil.getString(
245 actionRequest, "shippingStateSel");
246 String shippingState = shippingStateSel;
247 if (Validator.isNull(shippingStateSel)) {
248 shippingState = ParamUtil.getString(actionRequest, "shippingState");
249 }
250
251 String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
252 String shippingCountry = ParamUtil.getString(
253 actionRequest, "shippingCountry");
254 String shippingPhone = ParamUtil.getString(
255 actionRequest, "shippingPhone");
256
257 String ccName = ParamUtil.getString(actionRequest, "ccName");
258 String ccType = ParamUtil.getString(actionRequest, "ccType");
259 String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
260 int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
261 int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
262 String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");
263
264 String comments = ParamUtil.getString(actionRequest, "comments");
265
266 ShoppingOrder order = ShoppingOrderLocalServiceUtil.updateLatestOrder(
267 themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
268 billingFirstName, billingLastName, billingEmailAddress,
269 billingCompany, billingStreet, billingCity, billingState,
270 billingZip, billingCountry, billingPhone, shipToBilling,
271 shippingFirstName, shippingLastName, shippingEmailAddress,
272 shippingCompany, shippingStreet, shippingCity, shippingState,
273 shippingZip, shippingCountry, shippingPhone, ccName, ccType,
274 ccNumber, ccExpMonth, ccExpYear, ccVerNumber, comments);
275
276 actionRequest.setAttribute(WebKeys.SHOPPING_ORDER, order);
277 }
278
279 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
280
281 }