📄 paypalevents.java
字号:
/* * $Id: PayPalEvents.java 6642 2006-02-01 02:29:51Z sichen $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.accounting.thirdparty.paypal;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.order.order.OrderChangeHelper;import org.ofbiz.product.catalog.CatalogWorker;import org.ofbiz.product.store.ProductStoreWorker;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.ModelService;import org.ofbiz.service.LocalDispatcher;import org.apache.commons.collections.map.LinkedMap;/** * PayPal Events * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6642 $ * @since 2.0 */public class PayPalEvents { public static final String module = PayPalEvents.class.getName(); /** Initiate PayPal Request */ public static String callPayPal(HttpServletRequest request, HttpServletResponse response) { ServletContext application = ((ServletContext) request.getAttribute("servletContext")); GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); // get the orderId String orderId = (String) request.getAttribute("orderId"); // get the order header GenericValue orderHeader = null; try { orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); } catch (GenericEntityException e) { Debug.logError(e, "Cannot get the order header for order: " + orderId, module); request.setAttribute("_ERROR_MESSAGE_", "Problems getting order header."); return "error"; } // get the order total String orderTotal = UtilFormatOut.formatPrice(orderHeader.getDouble("grandTotal")); // get the webSiteId String webSiteId = CatalogWorker.getWebSiteId(request); // get the product store GenericValue productStore = ProductStoreWorker.getProductStore(request); if (productStore == null) { Debug.logError("ProductStore is null", module); request.setAttribute("_ERROR_MESSAGE_", "Problems getting merchant configuration, please contact customer service."); return "error"; } // get the payment properties file GenericValue paymentConfig = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStore.getString("productStoreId"), "EXT_PAYPAL", null, true); String configString = null; if (paymentConfig != null) { configString = paymentConfig.getString("paymentPropertiesPath"); } if (configString == null) { configString = "payment.properties"; } // get the company name String company = UtilFormatOut.checkEmpty(productStore.getString("companyName"), ""); // create the item name String itemName = "Order #" + orderId + (company != null ? " from " + company : ""); String itemNumber = "0"; // get the redirect url String redirectUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.redirect"); // get the notify url String notifyUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.notify"); // get the return urls String returnUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.return"); String cancelReturnUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.cancelReturn"); // get the image url String imageUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.image"); // get the paypal account String payPalAccount = UtilProperties.getPropertyValue(configString, "payment.paypal.business"); // create the redirect string Map parameters = new LinkedMap(); parameters.put("cmd", "_xclick"); parameters.put("business", payPalAccount); parameters.put("item_name", itemName); parameters.put("item_number", itemNumber); parameters.put("invoice", orderId); parameters.put("custom", userLogin.getString("userLoginId")); parameters.put("amount", orderTotal); parameters.put("return", returnUrl); parameters.put("cancel_return", cancelReturnUrl); parameters.put("notify_url", notifyUrl); parameters.put("image_url", imageUrl); parameters.put("no_note", "1"); // no notes allowed in paypal (not passed back) parameters.put("no_shipping", "1"); // no shipping address required (local shipping used) String encodedParameters = UtilHttp.urlEncodeArgs(parameters, false); String redirectString = redirectUrl + "?" + encodedParameters; // set the order in the session for cancelled orders request.getSession().setAttribute("PAYPAL_ORDER", orderId); // redirect to paypal try { response.sendRedirect(redirectString); } catch (IOException e) { Debug.logError(e, "Problems redirecting to PayPal", module); request.setAttribute("_ERROR_MESSAGE_", "Problems connecting with PayPal, please contact customer service."); return "error"; } return "success"; } /** PayPal Call-Back Event */ public static String payPalIPN(HttpServletRequest request, HttpServletResponse response) { ServletContext application = ((ServletContext) request.getAttribute("servletContext")); GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); // get the webSiteId String webSiteId = CatalogWorker.getWebSiteId(request); // get the product store GenericValue productStore = ProductStoreWorker.getProductStore(request); // get the payment properties file GenericValue paymentConfig = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStore.getString("productStoreId"), "EXT_PAYPAL", null, true); String configString = null; if (paymentConfig != null) { configString = paymentConfig.getString("paymentPropertiesPath"); } if (configString == null) { configString = "payment.properties"; } // get the confirm URL String confirmUrl = UtilProperties.getPropertyValue(configString, "payment.paypal.confirm"); if (confirmUrl == null) { Debug.logError("Payment properties is not configured properly, no confirm URL defined!", module); request.setAttribute("_ERROR_MESSAGE_", "PayPal has not been configured, please contact customer service."); return "error"; } // first verify this is valid from PayPal Map parametersMap = UtilHttp.getParameterMap(request); parametersMap.put("cmd", "_notify-validate"); // send off the confirm request String confirmResp = null; try { String str = UtilHttp.urlEncodeArgs(parametersMap); URL u = new URL("http://www.paypal.com/cgi-bin/webscr"); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.println(str); pw.close(); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); confirmResp = in.readLine(); in.close(); Debug.logError("PayPal Verification Response: " + confirmResp, module); } catch (IOException e) { Debug.logError(e, "Problems sending verification message", module); } if (confirmResp.trim().equals("VERIFIED")) { // we passed verification Debug.logInfo("Got verification from PayPal, processing..", module); } else { Debug.logError("###### PayPal did not verify this request, need investigation!", module); Set keySet = parametersMap.keySet(); Iterator i = keySet.iterator(); while (i.hasNext()) { String name = (String) i.next(); String value = request.getParameter(name); Debug.logError("### Param: " + name + " => " + value, module); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -