1   /**
2    * Copyright (c) 2000-2008 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.HttpUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.struts.ActionConstants;
28  import com.liferay.portlet.shopping.NoSuchOrderException;
29  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
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<String> enu = req.getParameterNames();
74  
75              while (enu.hasMoreElements()){
76                  String name = enu.nextElement();
77  
78                  String value = req.getParameter(name);
79  
80                  query = query + "&" + name + "=" + HttpUtil.encodeURL(value);
81              }
82  
83              if (_log.isDebugEnabled()) {
84                  _log.debug("Sending response to PayPal " + query);
85              }
86  
87              URL url = new URL("http://www.paypal.com/cgi-bin/webscr");
88  
89              URLConnection urlc = url.openConnection();
90  
91              urlc.setDoOutput(true);
92              urlc.setRequestProperty(
93                  "Content-Type","application/x-www-form-urlencoded");
94  
95              PrintWriter pw = new PrintWriter(urlc.getOutputStream());
96  
97              pw.println(query);
98  
99              pw.close();
100 
101             BufferedReader br = new BufferedReader(
102                 new InputStreamReader(urlc.getInputStream()));
103 
104             String payPalStatus = br.readLine();
105 
106             br.close();
107 
108             String itemName = ParamUtil.getString(req, "item_name");
109             String itemNumber = ParamUtil.getString(req, "item_number");
110             invoice = ParamUtil.getString(req, "invoice");
111             String txnId = ParamUtil.getString(req, "txn_id");
112             String paymentStatus = ParamUtil.getString(req, "payment_status");
113             double paymentGross = ParamUtil.getDouble(req, "payment_gross");
114             String receiverEmail = ParamUtil.getString(req, "receiver_email");
115             String payerEmail = ParamUtil.getString(req, "payer_email");
116 
117             if (_log.isDebugEnabled()) {
118                 _log.debug("Receiving response from PayPal");
119                 _log.debug("Item name " + itemName);
120                 _log.debug("Item number " + itemNumber);
121                 _log.debug("Invoice " + invoice);
122                 _log.debug("Transaction ID " + txnId);
123                 _log.debug("Payment status " + paymentStatus);
124                 _log.debug("Payment gross " + paymentGross);
125                 _log.debug("Receiver email " + receiverEmail);
126                 _log.debug("Payer email " + payerEmail);
127             }
128 
129             if (payPalStatus.equals("VERIFIED")) {
130                 ShoppingOrderLocalServiceUtil.completeOrder(
131                     invoice, txnId, paymentStatus, paymentGross, receiverEmail,
132                     payerEmail, true);
133             }
134             else if (payPalStatus.equals("INVALID")) {
135             }
136 
137             return mapping.findForward(ActionConstants.COMMON_NULL);
138         }
139         catch (NoSuchOrderException nsoe) {
140             _log.error("Order " + invoice + " does not exist");
141 
142             return mapping.findForward(ActionConstants.COMMON_NULL);
143         }
144         catch (Exception e) {
145             req.setAttribute(PageContext.EXCEPTION, e);
146 
147             return mapping.findForward(ActionConstants.COMMON_ERROR);
148         }
149     }
150 
151     private static Log _log = LogFactory.getLog(PayPalNotificationAction.class);
152 
153 }