1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.struts.ActionConstants;
27  import com.liferay.portlet.shopping.NoSuchOrderException;
28  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
29  import com.liferay.util.HttpUtil;
30  
31  import java.io.BufferedReader;
32  import java.io.InputStreamReader;
33  import java.io.PrintWriter;
34  
35  import java.net.URL;
36  import java.net.URLConnection;
37  
38  import java.util.Enumeration;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.jsp.PageContext;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.struts.action.Action;
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="PayPalNotificationAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class PayPalNotificationAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest req,
61              HttpServletResponse res)
62          throws Exception {
63  
64          String invoice = null;
65  
66          try {
67              if (_log.isDebugEnabled()) {
68                  _log.debug("Receiving notification from PayPal");
69              }
70  
71              String query = "cmd=_notify-validate";
72  
73              Enumeration enu = req.getParameterNames();
74  
75              while (enu.hasMoreElements()){
76                  String name = (String)enu.nextElement();
77                  String value = req.getParameter(name);
78  
79                  query = query + "&" + name + "=" + HttpUtil.encodeURL(value);
80              }
81  
82              if (_log.isDebugEnabled()) {
83                  _log.debug("Sending response to PayPal " + query);
84              }
85  
86              URL url = new URL("http://www.paypal.com/cgi-bin/webscr");
87  
88              URLConnection urlc = url.openConnection();
89  
90              urlc.setDoOutput(true);
91              urlc.setRequestProperty(
92                  "Content-Type","application/x-www-form-urlencoded");
93  
94              PrintWriter pw = new PrintWriter(urlc.getOutputStream());
95  
96              pw.println(query);
97  
98              pw.close();
99  
100             BufferedReader br = new BufferedReader(
101                 new InputStreamReader(urlc.getInputStream()));
102 
103             String payPalStatus = br.readLine();
104 
105             br.close();
106 
107             String itemName = ParamUtil.getString(req, "item_name");
108             String itemNumber = ParamUtil.getString(req, "item_number");
109             invoice = ParamUtil.getString(req, "invoice");
110             String txnId = ParamUtil.getString(req, "txn_id");
111             String paymentStatus = ParamUtil.getString(req, "payment_status");
112             double paymentGross = ParamUtil.getDouble(req, "payment_gross");
113             String receiverEmail = ParamUtil.getString(req, "receiver_email");
114             String payerEmail = ParamUtil.getString(req, "payer_email");
115 
116             if (_log.isDebugEnabled()) {
117                 _log.debug("Receiving response from PayPal");
118                 _log.debug("Item name " + itemName);
119                 _log.debug("Item number " + itemNumber);
120                 _log.debug("Invoice " + invoice);
121                 _log.debug("Transaction ID " + txnId);
122                 _log.debug("Payment status " + paymentStatus);
123                 _log.debug("Payment gross " + paymentGross);
124                 _log.debug("Receiver email " + receiverEmail);
125                 _log.debug("Payer email " + payerEmail);
126             }
127 
128             if (payPalStatus.equals("VERIFIED")) {
129                 ShoppingOrderLocalServiceUtil.completeOrder(
130                     invoice, txnId, paymentStatus, paymentGross, receiverEmail,
131                     payerEmail, true);
132             }
133             else if (payPalStatus.equals("INVALID")) {
134             }
135 
136             return mapping.findForward(ActionConstants.COMMON_NULL);
137         }
138         catch (NoSuchOrderException nsoe) {
139             _log.error("Order " + invoice + " does not exist");
140 
141             return mapping.findForward(ActionConstants.COMMON_NULL);
142         }
143         catch (Exception e) {
144             req.setAttribute(PageContext.EXCEPTION, e);
145 
146             return mapping.findForward(ActionConstants.COMMON_ERROR);
147         }
148     }
149 
150     private static Log _log = LogFactory.getLog(PayPalNotificationAction.class);
151 
152 }