001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.shopping.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.theme.ThemeDisplay;
028: import com.liferay.portal.util.WebKeys;
029: import com.liferay.portlet.ActionResponseImpl;
030: import com.liferay.portlet.shopping.BillingCityException;
031: import com.liferay.portlet.shopping.BillingCountryException;
032: import com.liferay.portlet.shopping.BillingEmailAddressException;
033: import com.liferay.portlet.shopping.BillingFirstNameException;
034: import com.liferay.portlet.shopping.BillingLastNameException;
035: import com.liferay.portlet.shopping.BillingPhoneException;
036: import com.liferay.portlet.shopping.BillingStateException;
037: import com.liferay.portlet.shopping.BillingStreetException;
038: import com.liferay.portlet.shopping.BillingZipException;
039: import com.liferay.portlet.shopping.CCExpirationException;
040: import com.liferay.portlet.shopping.CCNameException;
041: import com.liferay.portlet.shopping.CCNumberException;
042: import com.liferay.portlet.shopping.CCTypeException;
043: import com.liferay.portlet.shopping.ShippingCityException;
044: import com.liferay.portlet.shopping.ShippingCountryException;
045: import com.liferay.portlet.shopping.ShippingEmailAddressException;
046: import com.liferay.portlet.shopping.ShippingFirstNameException;
047: import com.liferay.portlet.shopping.ShippingLastNameException;
048: import com.liferay.portlet.shopping.ShippingPhoneException;
049: import com.liferay.portlet.shopping.ShippingStateException;
050: import com.liferay.portlet.shopping.ShippingStreetException;
051: import com.liferay.portlet.shopping.ShippingZipException;
052: import com.liferay.portlet.shopping.model.ShoppingCart;
053: import com.liferay.portlet.shopping.model.ShoppingOrder;
054: import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
055: import com.liferay.portlet.shopping.util.ShoppingPreferences;
056: import com.liferay.portlet.shopping.util.ShoppingUtil;
057: import com.liferay.util.servlet.SessionErrors;
058:
059: import javax.portlet.ActionRequest;
060: import javax.portlet.ActionResponse;
061: import javax.portlet.PortletConfig;
062:
063: import org.apache.struts.action.ActionForm;
064: import org.apache.struts.action.ActionMapping;
065:
066: /**
067: * <a href="CheckoutAction.java.html"><b><i>View Source</i></b></a>
068: *
069: * @author Brian Wing Shun Chan
070: *
071: */
072: public class CheckoutAction extends CartAction {
073:
074: public void processAction(ActionMapping mapping, ActionForm form,
075: PortletConfig config, ActionRequest req, ActionResponse res)
076: throws Exception {
077:
078: if (redirectToLogin(req, res)) {
079: return;
080: }
081:
082: String cmd = ParamUtil.getString(req, Constants.CMD);
083:
084: getLatestOrder(req);
085:
086: if (cmd.equals(Constants.SAVE)) {
087: updateCart(req);
088: updateLatestOrder(req);
089: saveLatestOrder(req);
090: forwardCheckout(req, res);
091: } else if (cmd.equals(Constants.UPDATE)) {
092: try {
093: updateLatestOrder(req);
094:
095: setForward(req, "portlet.shopping.checkout_second");
096: } catch (Exception e) {
097: if (e instanceof BillingCityException
098: || e instanceof BillingCountryException
099: || e instanceof BillingEmailAddressException
100: || e instanceof BillingFirstNameException
101: || e instanceof BillingLastNameException
102: || e instanceof BillingPhoneException
103: || e instanceof BillingStateException
104: || e instanceof BillingStreetException
105: || e instanceof BillingZipException
106: || e instanceof CCExpirationException
107: || e instanceof CCNameException
108: || e instanceof CCNumberException
109: || e instanceof CCTypeException
110: || e instanceof ShippingCityException
111: || e instanceof ShippingCountryException
112: || e instanceof ShippingEmailAddressException
113: || e instanceof ShippingFirstNameException
114: || e instanceof ShippingLastNameException
115: || e instanceof ShippingPhoneException
116: || e instanceof ShippingStateException
117: || e instanceof ShippingStreetException
118: || e instanceof ShippingZipException) {
119:
120: SessionErrors.add(req, e.getClass().getName());
121:
122: setForward(req, "portlet.shopping.checkout_first");
123: } else if (e instanceof PrincipalException) {
124: setForward(req, "portlet.shopping.error");
125: } else {
126: throw e;
127: }
128: }
129: } else if (cmd.equals(Constants.VIEW)) {
130: setForward(req, "portlet.shopping.checkout_third");
131: } else {
132: setForward(req, "portlet.shopping.checkout_first");
133: }
134: }
135:
136: protected void getLatestOrder(ActionRequest req) throws Exception {
137: ThemeDisplay themeDisplay = (ThemeDisplay) req
138: .getAttribute(WebKeys.THEME_DISPLAY);
139:
140: ShoppingOrder order = ShoppingOrderLocalServiceUtil
141: .getLatestOrder(themeDisplay.getUserId(), themeDisplay
142: .getPortletGroupId());
143:
144: req.setAttribute(WebKeys.SHOPPING_ORDER, order);
145: }
146:
147: protected void forwardCheckout(ActionRequest req, ActionResponse res)
148: throws Exception {
149:
150: ThemeDisplay themeDisplay = (ThemeDisplay) req
151: .getAttribute(WebKeys.THEME_DISPLAY);
152:
153: ShoppingCart cart = ShoppingUtil.getCart(req);
154:
155: ShoppingOrder order = (ShoppingOrder) req
156: .getAttribute(WebKeys.SHOPPING_ORDER);
157:
158: ShoppingPreferences prefs = ShoppingPreferences.getInstance(
159: themeDisplay.getCompanyId(), themeDisplay
160: .getPortletGroupId());
161:
162: String returnURL = ShoppingUtil.getPayPalReturnURL(
163: ((ActionResponseImpl) res).createActionURL(), order);
164: String notifyURL = ShoppingUtil
165: .getPayPalNotifyURL(themeDisplay);
166:
167: if (prefs.usePayPal()) {
168: double total = ShoppingUtil.calculateTotal(cart.getItems(),
169: order.getBillingState(), cart.getCoupon(), cart
170: .getAltShipping(), cart.isInsure());
171:
172: String redirectURL = ShoppingUtil.getPayPalRedirectURL(
173: prefs, order, total, returnURL, notifyURL);
174:
175: res.sendRedirect(redirectURL);
176: } else {
177: ShoppingOrderLocalServiceUtil.sendEmail(order,
178: "confirmation");
179:
180: res.sendRedirect(returnURL);
181: }
182: }
183:
184: protected void saveLatestOrder(ActionRequest req) throws Exception {
185: ShoppingCart cart = ShoppingUtil.getCart(req);
186:
187: ShoppingOrder order = ShoppingOrderLocalServiceUtil
188: .saveLatestOrder(cart);
189:
190: req.setAttribute(WebKeys.SHOPPING_ORDER, order);
191: }
192:
193: protected void updateLatestOrder(ActionRequest req)
194: throws Exception {
195: ThemeDisplay themeDisplay = (ThemeDisplay) req
196: .getAttribute(WebKeys.THEME_DISPLAY);
197:
198: String billingFirstName = ParamUtil.getString(req,
199: "billingFirstName");
200: String billingLastName = ParamUtil.getString(req,
201: "billingLastName");
202: String billingEmailAddress = ParamUtil.getString(req,
203: "billingEmailAddress");
204: String billingCompany = ParamUtil.getString(req,
205: "billingCompany");
206: String billingStreet = ParamUtil
207: .getString(req, "billingStreet");
208: String billingCity = ParamUtil.getString(req, "billingCity");
209:
210: String billingStateSel = ParamUtil.getString(req,
211: "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,
219: "billingCountry");
220: String billingPhone = ParamUtil.getString(req, "billingPhone");
221:
222: boolean shipToBilling = ParamUtil.getBoolean(req,
223: "shipToBilling");
224: String shippingFirstName = ParamUtil.getString(req,
225: "shippingFirstName");
226: String shippingLastName = ParamUtil.getString(req,
227: "shippingLastName");
228: String shippingEmailAddress = ParamUtil.getString(req,
229: "shippingEmailAddress");
230: String shippingCompany = ParamUtil.getString(req,
231: "shippingCompany");
232: String shippingStreet = ParamUtil.getString(req,
233: "shippingStreet");
234: String shippingCity = ParamUtil.getString(req, "shippingCity");
235:
236: String shippingStateSel = ParamUtil.getString(req,
237: "shippingStateSel");
238: String shippingState = shippingStateSel;
239: if (Validator.isNull(shippingStateSel)) {
240: shippingState = ParamUtil.getString(req, "shippingState");
241: }
242:
243: String shippingZip = ParamUtil.getString(req, "shippingZip");
244: String shippingCountry = ParamUtil.getString(req,
245: "shippingCountry");
246: String shippingPhone = ParamUtil
247: .getString(req, "shippingPhone");
248:
249: String ccName = ParamUtil.getString(req, "ccName");
250: String ccType = ParamUtil.getString(req, "ccType");
251: String ccNumber = ParamUtil.getString(req, "ccNumber");
252: int ccExpMonth = ParamUtil.getInteger(req, "ccExpMonth");
253: int ccExpYear = ParamUtil.getInteger(req, "ccExpYear");
254: String ccVerNumber = ParamUtil.getString(req, "ccVerNumber");
255:
256: String comments = ParamUtil.getString(req, "comments");
257:
258: ShoppingOrder order = ShoppingOrderLocalServiceUtil
259: .updateLatestOrder(themeDisplay.getUserId(),
260: themeDisplay.getPortletGroupId(),
261: billingFirstName, billingLastName,
262: billingEmailAddress, billingCompany,
263: billingStreet, billingCity, billingState,
264: billingZip, billingCountry, billingPhone,
265: shipToBilling, shippingFirstName,
266: shippingLastName, shippingEmailAddress,
267: shippingCompany, shippingStreet, shippingCity,
268: shippingState, shippingZip, shippingCountry,
269: shippingPhone, ccName, ccType, ccNumber,
270: ccExpMonth, ccExpYear, ccVerNumber, comments);
271:
272: req.setAttribute(WebKeys.SHOPPING_ORDER, order);
273: }
274:
275: }
|