PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms 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. package com.liferay.portlet.shopping.action;
  15. import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
  16. import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
  17. import com.liferay.portal.kernel.log.Log;
  18. import com.liferay.portal.kernel.log.LogFactoryUtil;
  19. import com.liferay.portal.kernel.util.HttpUtil;
  20. import com.liferay.portal.kernel.util.ParamUtil;
  21. import com.liferay.portal.util.PortalUtil;
  22. import com.liferay.portlet.shopping.NoSuchOrderException;
  23. import com.liferay.portlet.shopping.model.ShoppingOrder;
  24. import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
  25. import com.liferay.portlet.shopping.util.ShoppingPreferences;
  26. import com.liferay.portlet.shopping.util.ShoppingUtil;
  27. import java.io.InputStreamReader;
  28. import java.io.PrintWriter;
  29. import java.net.URL;
  30. import java.net.URLConnection;
  31. import java.util.Enumeration;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import org.apache.struts.action.Action;
  35. import org.apache.struts.action.ActionForm;
  36. import org.apache.struts.action.ActionForward;
  37. import org.apache.struts.action.ActionMapping;
  38. /**
  39. * @author Brian Wing Shun Chan
  40. */
  41. public class PayPalNotificationAction extends Action {
  42. public ActionForward execute(
  43. ActionMapping mapping, ActionForm form, HttpServletRequest request,
  44. HttpServletResponse response)
  45. throws Exception {
  46. String invoice = null;
  47. try {
  48. if (_log.isDebugEnabled()) {
  49. _log.debug("Receiving notification from PayPal");
  50. }
  51. String query = "cmd=_notify-validate";
  52. Enumeration<String> enu = request.getParameterNames();
  53. while (enu.hasMoreElements()) {
  54. String name = enu.nextElement();
  55. String value = request.getParameter(name);
  56. query = query + "&" + name + "=" + HttpUtil.encodeURL(value);
  57. }
  58. if (_log.isDebugEnabled()) {
  59. _log.debug("Sending response to PayPal " + query);
  60. }
  61. URL url = new URL("https://www.paypal.com/cgi-bin/webscr");
  62. URLConnection urlc = url.openConnection();
  63. urlc.setDoOutput(true);
  64. urlc.setRequestProperty(
  65. "Content-Type","application/x-www-form-urlencoded");
  66. PrintWriter pw = new UnsyncPrintWriter(urlc.getOutputStream());
  67. pw.println(query);
  68. pw.close();
  69. UnsyncBufferedReader unsyncBufferedReader =
  70. new UnsyncBufferedReader(
  71. new InputStreamReader(urlc.getInputStream()));
  72. String payPalStatus = unsyncBufferedReader.readLine();
  73. unsyncBufferedReader.close();
  74. String itemName = ParamUtil.getString(request, "item_name");
  75. String itemNumber = ParamUtil.getString(request, "item_number");
  76. invoice = ParamUtil.getString(request, "invoice");
  77. String txnId = ParamUtil.getString(request, "txn_id");
  78. String paymentStatus = ParamUtil.getString(
  79. request, "payment_status");
  80. double paymentGross = ParamUtil.getDouble(request, "mc_gross");
  81. String receiverEmail = ParamUtil.getString(
  82. request, "receiver_email");
  83. String payerEmail = ParamUtil.getString(request, "payer_email");
  84. if (_log.isDebugEnabled()) {
  85. _log.debug("Receiving response from PayPal");
  86. _log.debug("Item name " + itemName);
  87. _log.debug("Item number " + itemNumber);
  88. _log.debug("Invoice " + invoice);
  89. _log.debug("Transaction ID " + txnId);
  90. _log.debug("Payment status " + paymentStatus);
  91. _log.debug("Payment gross " + paymentGross);
  92. _log.debug("Receiver email " + receiverEmail);
  93. _log.debug("Payer email " + payerEmail);
  94. }
  95. if (payPalStatus.equals("VERIFIED") && validate(request)) {
  96. ShoppingOrderLocalServiceUtil.completeOrder(
  97. invoice, txnId, paymentStatus, paymentGross, receiverEmail,
  98. payerEmail, true);
  99. }
  100. else if (payPalStatus.equals("INVALID")) {
  101. }
  102. return null;
  103. }
  104. catch (Exception e) {
  105. PortalUtil.sendError(e, request, response);
  106. return null;
  107. }
  108. }
  109. protected boolean validate(HttpServletRequest request) throws Exception {
  110. // Invoice
  111. String ppInvoice = ParamUtil.getString(request, "invoice");
  112. ShoppingOrder order = ShoppingOrderLocalServiceUtil.getOrder(
  113. ppInvoice);
  114. ShoppingPreferences shoppingPrefs = ShoppingPreferences.getInstance(
  115. order.getCompanyId(), order.getGroupId());
  116. // Receiver email address
  117. String ppReceiverEmail = ParamUtil.getString(
  118. request, "receiver_email");
  119. String payPalEmailAddress = shoppingPrefs.getPayPalEmailAddress();
  120. if (!payPalEmailAddress.equals(ppReceiverEmail)) {
  121. return false;
  122. }
  123. // Payment gross
  124. double ppGross = ParamUtil.getDouble(request, "mc_gross");
  125. double orderTotal = ShoppingUtil.calculateTotal(order);
  126. if (orderTotal != ppGross) {
  127. return false;
  128. }
  129. // Payment currency
  130. String ppCurrency = ParamUtil.getString(request, "mc_currency");
  131. String currencyId = shoppingPrefs.getCurrencyId();
  132. if (!currencyId.equals(ppCurrency)) {
  133. return false;
  134. }
  135. // Transaction ID
  136. String ppTxnId = ParamUtil.getString(request, "txn_id");
  137. try {
  138. ShoppingOrderLocalServiceUtil.getPayPalTxnIdOrder(ppTxnId);
  139. return false;
  140. }
  141. catch (NoSuchOrderException nsoe) {
  142. }
  143. return true;
  144. }
  145. private static Log _log = LogFactoryUtil.getLog(
  146. PayPalNotificationAction.class);
  147. }