2012-02-10 69 views
2

我有一個用於PayPal IPN消息的Java接收器,它在沙箱中完美工作。但是,在將其設置爲真正的Paypal環境後,我注意到它獲得了大部分消息的「INVALID」響應(儘管一些付款獲得了VERIFIED響應)。我有認爲它可能是一個編碼問題。我讀到編碼出現在正確的消息中(我已經看到它有一個叫做「charset」的參數)和其他地方的編碼應該設置爲「windows-1252」。所以我正在從正確的消息中讀取字符集,並用該字符集編碼參數。在所有收到的編碼信息中都是「windows-1252」,但只有少數的響應爲「VERIFIED」(其餘均無效)。如果我重新發送帳戶中的IPN歷史記錄中的「無效」消息,它們將再次被視爲無效。對PayPal中的一些IPN消息的INVALID響應

Doe任何人都知道可能發生什麼?

謝謝。

String paypalURL = "https://www.paypal.com/cgi-bin/webscr"; 

// read post from PayPal system and add 'cmd' 
Enumeration en = request.getParameterNames(); 
String str = "cmd=_notify-validate"; 
String charset = request.getParameter("charset"); //this gets windows-1252 

while(en.hasMoreElements()){ 
    String paramName = (String)en.nextElement(); 
    String paramValue = request.getParameter(paramName); 
    str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, charset); 
} 
// post back to PayPal system to validate 
// NOTE: change http: to https: in the following URL to verify using SSL (for increased security). 
// using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE) 
// and configured for older versions. 
URL u; 
u = new URL(paypalURL); 
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())); 
String res = in.readLine(); //It's always INVALID - It should be "VERIFIED" 
in.close(); 

// assign posted variables to local variables 
//String itemName = request.getParameter("item_name"); 
//String itemNumber = request.getParameter("item_number"); 
//String quantity = request.getParameter("quantity"); 
String paymentStatus = request.getParameter("payment_status"); 
String paymentAmount = request.getParameter("mc_gross"); 
String paymentCurrency = request.getParameter("mc_currency"); 
String txnId = request.getParameter("txn_id"); 
String receiverEmail = request.getParameter("receiver_email"); 
//String payerEmail = request.getParameter("payer_email"); 
String username = request.getParameter("custom"); 
//check notification validation 

if(res.equals("VERIFIED")) { //It's always INVALID - It should be "VERIFIED" 

回答

0

貝寶還要求參數的發送順序與它們相同。當我實現了其中的一個,我結束了讀取參數爲從請求讀者LinkedHashMap中ALA

String line = null; 
BuferedReader incomingParams = request.getReader(); 
StringBuilder params = new StringBuilder(); 
while ((line = incomingParams.readLine()) != null) { 
    params = params.append(line); 
} 

String[] nvPairs = params.toString().split("&"); 
String[] nvPair = null; 
String value = null; 
for (int i = 0; i < nvPairs.length; ++i) { 
    nvPair = nvPairs[i].split("="); 
    if (nvPair != null && nvPair.length >= 1) { 
     if (nvPair.length >= 2) { 
      value = nvPair[1]; 
     } 
     else { 
      value = ""; 
     } 
     paramMap.put(nvPair[0], value); 
    } 
} 

,然後讀取參數背出來的圖類似於你做了什麼。還有一點需要注意的是,我並沒有重新編碼這些參數,因爲我發回了這些參數,儘管看起來應該如此。