PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portlet/shopping/action/PayPalNotificationAction.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 202 lines | 126 code | 55 blank | 21 comment | 12 complexity | 99de1a85b856a541e06bbdfddbdfa14f MD5 | raw file
  1/**
  2 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3 *
  4 * This library is free software; you can redistribute it and/or modify it under
  5 * the terms of the GNU Lesser General Public License as published by the Free
  6 * Software Foundation; either version 2.1 of the License, or (at your option)
  7 * any later version.
  8 *
  9 * This library is distributed in the hope that it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 12 * details.
 13 */
 14
 15package com.liferay.portlet.shopping.action;
 16
 17import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
 18import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
 19import com.liferay.portal.kernel.log.Log;
 20import com.liferay.portal.kernel.log.LogFactoryUtil;
 21import com.liferay.portal.kernel.util.HttpUtil;
 22import com.liferay.portal.kernel.util.ParamUtil;
 23import com.liferay.portal.util.PortalUtil;
 24import com.liferay.portlet.shopping.NoSuchOrderException;
 25import com.liferay.portlet.shopping.model.ShoppingOrder;
 26import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
 27import com.liferay.portlet.shopping.util.ShoppingPreferences;
 28import com.liferay.portlet.shopping.util.ShoppingUtil;
 29
 30import java.io.InputStreamReader;
 31import java.io.PrintWriter;
 32
 33import java.net.URL;
 34import java.net.URLConnection;
 35
 36import java.util.Enumeration;
 37
 38import javax.servlet.http.HttpServletRequest;
 39import javax.servlet.http.HttpServletResponse;
 40
 41import org.apache.struts.action.Action;
 42import org.apache.struts.action.ActionForm;
 43import org.apache.struts.action.ActionForward;
 44import org.apache.struts.action.ActionMapping;
 45
 46/**
 47 * @author Brian Wing Shun Chan
 48 */
 49public class PayPalNotificationAction extends Action {
 50
 51	public ActionForward execute(
 52			ActionMapping mapping, ActionForm form, HttpServletRequest request,
 53			HttpServletResponse response)
 54		throws Exception {
 55
 56		String invoice = null;
 57
 58		try {
 59			if (_log.isDebugEnabled()) {
 60				_log.debug("Receiving notification from PayPal");
 61			}
 62
 63			String query = "cmd=_notify-validate";
 64
 65			Enumeration<String> enu = request.getParameterNames();
 66
 67			while (enu.hasMoreElements()) {
 68				String name = enu.nextElement();
 69
 70				String value = request.getParameter(name);
 71
 72				query = query + "&" + name + "=" + HttpUtil.encodeURL(value);
 73			}
 74
 75			if (_log.isDebugEnabled()) {
 76				_log.debug("Sending response to PayPal " + query);
 77			}
 78
 79			URL url = new URL("https://www.paypal.com/cgi-bin/webscr");
 80
 81			URLConnection urlc = url.openConnection();
 82
 83			urlc.setDoOutput(true);
 84			urlc.setRequestProperty(
 85				"Content-Type","application/x-www-form-urlencoded");
 86
 87			PrintWriter pw = new UnsyncPrintWriter(urlc.getOutputStream());
 88
 89			pw.println(query);
 90
 91			pw.close();
 92
 93			UnsyncBufferedReader unsyncBufferedReader =
 94				new UnsyncBufferedReader(
 95					new InputStreamReader(urlc.getInputStream()));
 96
 97			String payPalStatus = unsyncBufferedReader.readLine();
 98
 99			unsyncBufferedReader.close();
100
101			String itemName = ParamUtil.getString(request, "item_name");
102			String itemNumber = ParamUtil.getString(request, "item_number");
103			invoice = ParamUtil.getString(request, "invoice");
104			String txnId = ParamUtil.getString(request, "txn_id");
105			String paymentStatus = ParamUtil.getString(
106				request, "payment_status");
107			double paymentGross = ParamUtil.getDouble(request, "mc_gross");
108			String receiverEmail = ParamUtil.getString(
109				request, "receiver_email");
110			String payerEmail = ParamUtil.getString(request, "payer_email");
111
112			if (_log.isDebugEnabled()) {
113				_log.debug("Receiving response from PayPal");
114				_log.debug("Item name " + itemName);
115				_log.debug("Item number " + itemNumber);
116				_log.debug("Invoice " + invoice);
117				_log.debug("Transaction ID " + txnId);
118				_log.debug("Payment status " + paymentStatus);
119				_log.debug("Payment gross " + paymentGross);
120				_log.debug("Receiver email " + receiverEmail);
121				_log.debug("Payer email " + payerEmail);
122			}
123
124			if (payPalStatus.equals("VERIFIED") && validate(request)) {
125				ShoppingOrderLocalServiceUtil.completeOrder(
126					invoice, txnId, paymentStatus, paymentGross, receiverEmail,
127					payerEmail, true);
128			}
129			else if (payPalStatus.equals("INVALID")) {
130			}
131
132			return null;
133		}
134		catch (Exception e) {
135			PortalUtil.sendError(e, request, response);
136
137			return null;
138		}
139	}
140
141	protected boolean validate(HttpServletRequest request) throws Exception {
142
143		// Invoice
144
145		String ppInvoice = ParamUtil.getString(request, "invoice");
146
147		ShoppingOrder order = ShoppingOrderLocalServiceUtil.getOrder(
148			ppInvoice);
149
150		ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
151			order.getCompanyId(), order.getGroupId());
152
153		// Receiver email address
154
155		String ppReceiverEmail = ParamUtil.getString(
156			request, "receiver_email");
157
158		String payPalEmailAddress = shoppingPrefs.getPayPalEmailAddress();
159
160		if (!payPalEmailAddress.equals(ppReceiverEmail)) {
161			return false;
162		}
163
164		// Payment gross
165
166		double ppGross = ParamUtil.getDouble(request, "mc_gross");
167
168		double orderTotal = ShoppingUtil.calculateTotal(order);
169
170		if (orderTotal != ppGross) {
171			return false;
172		}
173
174		// Payment currency
175
176		String ppCurrency = ParamUtil.getString(request, "mc_currency");
177
178		String currencyId = shoppingPrefs.getCurrencyId();
179
180		if (!currencyId.equals(ppCurrency)) {
181			return false;
182		}
183
184		// Transaction ID
185
186		String ppTxnId = ParamUtil.getString(request, "txn_id");
187
188		try {
189			ShoppingOrderLocalServiceUtil.getPayPalTxnIdOrder(ppTxnId);
190
191			return false;
192		}
193		catch (NoSuchOrderException nsoe) {
194		}
195
196		return true;
197	}
198
199	private static Log _log = LogFactoryUtil.getLog(
200		PayPalNotificationAction.class);
201
202}