First you create your "Buy Now" (or "Add to Cart", "Subscribe"...) button. You can do this at https://www.paypal.com/cgi-bin/webscr?cmd=_wp-standard-overview-outside. This wizard creates a form with a couple of hidden parameters. It doesn't set the notify url however, which is the url PayPal IPN needs to call to notify you the user made a purchase. You can add this url to the form as follows:
<input type="hidden" name="notify_url" value="<h:outputText value="#{paypalBean.notifyURL}"/>" />
This sample uses JSF to create the URL, but you can use any tag here (or use a hardcoded URL).
Here is the code that generates the URL:
public String getNotifyURL() { HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); String nUrl = getPath("PaypalListenerServlet"); return response.encodeURL(nUrl); } public final static String getPath(String path) { HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); StringBuffer url = new StringBuffer(); String s = request.getProtocol(); url.append(s.substring(0, s.indexOf('/')).toLowerCase()); url.append("://"); url.append(request.getServerName()); url.append(":"); url.append(request.getServerPort()); url.append(request.getContextPath()); if (path.charAt(0) != '/') { url.append("/"); } url.append(path); return url.toString(); }
Thank you! but I have a problem, paypal does not call the servlet PaypalListenerServlet when payment is completed from Sandbox.
ReplyDeleteI tested from localhost and from server and does not work. Any suggestions? Do I have to configure anything in sanbox?
Thank you.