java Paypal IPN 开发
时间: 2023-09-27 22:05:03 浏览: 162
Java Paypal IPN开发可以参照以下步骤进行:
1. 在Paypal账户中设置IPN(Instant Payment Notification),并将IPN URL设置为您的服务器上的Java Servlet。
2. 在Java代码中实现一个Servlet来接收Paypal发送的IPN信息,并解析该信息。
3. 验证Paypal发送的IPN信息的有效性。可以使用Paypal提供的IPN验证工具或者手动验证。
4. 对接收到的IPN信息进行处理,例如更新订单状态或发送电子邮件通知等。
以下是Java Paypal IPN开发的示例代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IPNServlet extends HttpServlet {
private static final String PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr";
private static final String PAYPAL_SANDBOX_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
private static final String VERIFY_CMD = "_notify-validate";
private static final String CHARSET = "UTF-8";
private static final String TXN_TYPE = "txn_type";
private static final String ITEM_NUMBER = "item_number";
private static final String PAYMENT_STATUS = "payment_status";
private static final String MC_GROSS = "mc_gross";
private static final String MC_CURRENCY = "mc_currency";
private static final String RECEIVER_EMAIL = "receiver_email";
private static final String BUSINESS = "business";
private static final String CUSTOM = "custom";
private static final String PAYPAL_EMAIL = "YOUR_PAYPAL_EMAIL";
private static final String PAYPAL_IDENTITY_TOKEN = "YOUR_PAYPAL_IDENTITY_TOKEN";
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String url = PAYPAL_URL;
if (Boolean.parseBoolean(req.getParameter("test_ipn"))) {
url = PAYPAL_SANDBOX_URL;
}
Map<String, String> params = new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
params.putAll(parseQueryString(line));
}
reader.close();
String txnType = params.get(TXN_TYPE);
if (txnType == null) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String itemNumber = params.get(ITEM_NUMBER);
String paymentStatus = params.get(PAYMENT_STATUS);
String mcGross = params.get(MC_GROSS);
String mcCurrency = params.get(MC_CURRENCY);
String receiverEmail = params.get(RECEIVER_EMAIL);
String business = params.get(BUSINESS);
String custom = params.get(CUSTOM);
if (!PAYPAL_EMAIL.equals(receiverEmail) || !PAYPAL_IDENTITY_TOKEN.equals(custom)) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String verifyUrl = url + "?" + VERIFY_CMD;
for (String name : params.keySet()) {
verifyUrl += "&" + name + "=" + URLEncoder.encode(params.get(name), CHARSET);
}
if (!verify(verifyUrl)) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// Handle IPN message
}
private boolean verify(String url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Java IPN Verification");
conn.setRequestProperty("Host", "www.paypal.com");
conn.setRequestProperty("Connection", "close");
conn.getOutputStream().write(("cmd=" + VERIFY_CMD).getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
if ("VERIFIED".equals(line)) {
return true;
} else if ("INVALID".equals(line)) {
return false;
}
}
reader.close();
return false;
}
private Map<String, String> parseQueryString(String queryString) throws UnsupportedEncodingException {
Map<String, String> params = new HashMap<String, String>();
for (String param : queryString.split("&")) {
String pair[] = param.split("=");
String name = URLDecoder.decode(pair[0], CHARSET);
String value = "";
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], CHARSET);
}
params.put(name, value);
}
return params;
}
private String md5(String string) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(string.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
```
以上代码中,IPNServlet类是一个Java Servlet,用于接收Paypal发送的IPN信息,验证IPN信息的有效性并进行处理。在该类中,PAYPAL_EMAIL和PAYPAL_IDENTITY_TOKEN分别为您Paypal账户的电子邮件地址和身份令牌,用于验证IPN信息是否来自Paypal。在实际的应用中,您需要替换这些值为您自己的Paypal账户信息。
在handle IPN message方法中,您可以根据接收到的IPN信息进行相应的处理,例如更新订单状态或发送电子邮件通知等。
阅读全文