Paypal空指针异常nvp.get(“ACK”)Java
我一整天都在为这种看似简单的整合而挣扎。我真的希望你能帮助我 我需要在我的Java webapp中使用PayPal express签出。我只需要下一个订单,然后将客户端重定向到PayPal页面 主页工作得很好,但当我试图去检查时,我得到一个NullPointerException。NullPointerException在我的Checkout类的第36行抛出。当我试图得到确认时,它被调用 我一整天都在努力解决这个问题 不确定这是否相关: 我做了第二个项目,tapestry5项目,并使用了RESTSDK,但当我尝试调用setExpressCheckoutResponse.getAck()时,仍然出现异常。我发现该项目中引发异常的行是service.setExpressCheckout(setExpressCheckoutReq) 如果你知道我如何解决这个问题,任何关于paypal hello world和servlet的教程,或者任何我可以查看的资源,请帮助我 以下是我一直在尝试的代码: My web.xmlPaypal空指针异常nvp.get(“ACK”)Java,java,servlets,paypal,nullpointerexception,paypal-sandbox,Java,Servlets,Paypal,Nullpointerexception,Paypal Sandbox,我一整天都在为这种看似简单的整合而挣扎。我真的希望你能帮助我 我需要在我的Java webapp中使用PayPal express签出。我只需要下一个订单,然后将客户端重定向到PayPal页面 主页工作得很好,但当我试图去检查时,我得到一个NullPointerException。NullPointerException在我的Checkout类的第36行抛出。当我试图得到确认时,它被调用 我一整天都在努力解决这个问题 不确定这是否相关: 我做了第二个项目,tapestry5项目,并使用了REST
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Integration Wizard</display-name>
<description></description>
<servlet>
<servlet-name>Checkout</servlet-name>
<servlet-class>Checkout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Checkout</servlet-name>
<url-pattern>/checkout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>OrderConfirm</servlet-name>
<servlet-class>OrderConfirm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OrderConfirm</servlet-name>
<url-pattern>/orderconfirm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Cancel</servlet-name>
<servlet-class>Cancel</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Cancel</servlet-name>
<url-pattern>/cancel</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
My PaypalFunctions.java
public class PaypalFunctions {
private String gv_APIUserName;
private String gv_APIPassword;
private String gv_APISignature;
private String gv_APIEndpoint;
private String gv_BNCode;
private String gv_Version;
private String gv_nvpHeader;
private String gv_ProxyServer;
private String gv_ProxyServerPort;
private int gv_Proxy;
private boolean gv_UseProxy;
private String PAYPAL_URL;
public PaypalFunctions() {
gv_APIUserName = "dusan.stanojevic95-facilitator_api1.gmail.com";
gv_APIPassword = "1405890612";
gv_APISignature = "AexbutlMdUCD618gs-q4m-DJpUxEAoU-uNDtNNLAb47Su-QoOlNbIjeo";
boolean bSandbox = true;
String PAYPAL_DG_URL = null;
if (bSandbox == true) {
gv_APIEndpoint = "https://api-3t.sandbox.paypal.com/nvp";
PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
PAYPAL_DG_URL = "https://www.sandbox.paypal.com/incontext?token=";
} else {
gv_APIEndpoint = "https://api-3t.paypal.com/nvp";
PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
PAYPAL_DG_URL = "https://www.paypal.com/incontext?token=";
}
String HTTPREQUEST_PROXYSETTING_SERVER = "";
String HTTPREQUEST_PROXYSETTING_PORT = "";
boolean USE_PROXY = false;
gv_Version = "84";
// WinObjHttp Request proxy settings.
gv_ProxyServer = HTTPREQUEST_PROXYSETTING_SERVER;
gv_ProxyServerPort = HTTPREQUEST_PROXYSETTING_PORT;
gv_Proxy = 2; // 'setting for proxy activation
gv_UseProxy = USE_PROXY;
}
/*********************************************************************************
* SetExpressCheckout: Function to perform the SetExpressCheckout API call
*
* Inputs: paymentAmount: Total value of the purchase currencyCodeType:
* Currency code value the PayPal API paymentType: 'Sale' for Digital Goods
* returnURL: the page where buyers return to after they are done with the
* payment review on PayPal cancelURL: the page where buyers return to when
* they cancel the payment review on PayPal
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap setExpressCheckout(String paymentAmount, String returnURL,
String cancelURL, Map item) {
/*
* '------------------------------------ ' The currencyCodeType and
* paymentType ' are set to the selections made on the Integration
* Assistant '------------------------------------
*/
String currencyCodeType = "USD";
String paymentType = "Sale";
/*
* Construct the parameter string that describes the PayPal payment the
* varialbes were set in the web form, and the resulting string is
* stored in $nvpstr
*/
String nvpstr = "&PAYMENTREQUEST_0_AMT=" + paymentAmount
+ "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType
+ "&RETURNURL=" + URLEncoder.encode(returnURL) + "&CANCELURL="
+ URLEncoder.encode(cancelURL)
+ "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType
+ "&REQCONFIRMSHIPPING=0" + "&NOSHIPPING=1"
+ "&L_PAYMENTREQUEST_0_NAME0=" + item.get("name")
+ "&L_PAYMENTREQUEST_0_AMT0=" + item.get("amt")
+ "&L_PAYMENTREQUEST_0_QTY0=" + item.get("qty")
+ "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
/*
* Make the call to PayPal to get the Express Checkout token If the API
* call succeded, then redirect the buyer to PayPal to begin to
* authorize payment. If an error occured, show the resulting errors
*/
HashMap nvp = httpcall("SetExpressCheckout", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null && strAck.equalsIgnoreCase("Success")) {
return nvp;
}
return null;
}
/*********************************************************************************
* GetShippingDetails: Function to perform the GetExpressCheckoutDetails API
* call
*
* Inputs: None
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap getPaymentDetails(String token) {
/*
* Build a second API request to PayPal, using the token as the ID to
* get the details on the payment authorization
*/
String nvpstr = "&TOKEN=" + token;
/*
* Make the API call and store the results in an array. If the call was
* a success, show the authorization details, and provide an action to
* complete the payment. If failed, show the error
*/
HashMap nvp = httpcall("GetExpressCheckoutDetails", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null
&& (strAck.equalsIgnoreCase("Success") || strAck
.equalsIgnoreCase("SuccessWithWarning"))) {
return nvp;
}
return null;
}
/*********************************************************************************
* ConfirmPayment: Function to perform the DoExpressCheckoutPayment API call
*
* Inputs: None
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap confirmPayment(String token, String payerID,
String finalPaymentAmount, String serverName, Map item) {
/*
* '------------------------------------ ' The currencyCodeType and
* paymentType ' are set to the selections made on the Integration
* Assistant '------------------------------------
*/
String currencyCodeType = "USD";
String paymentType = "Sale";
/*
* '----------------------------------------------------------------------------
* '---- Use the values stored in the session from the previous SetEC
* call
* '----------------------------------------------------------------------------
*/
String nvpstr = "&TOKEN=" + token + "&PAYERID=" + payerID
+ "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType + "&PAYMENTREQUEST_0_AMT="
+ finalPaymentAmount;
nvpstr = nvpstr + "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType + "&IPADDRESS="
+ serverName;
nvpstr = nvpstr + "&L_PAYMENTREQUEST_0_NAME0=" + item.get("name")
+ "&L_PAYMENTREQUEST_0_AMT0=" + item.get("amt")
+ "&L_PAYMENTREQUEST_0_QTY0=" + item.get("qty")
+ "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
/*
* Make the call to PayPal to finalize payment If an error occured, show
* the resulting errors
*/
HashMap nvp = httpcall("DoExpressCheckoutPayment", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null
&& (strAck.equalsIgnoreCase("Success") || strAck
.equalsIgnoreCase("SuccessWithWarning"))) {
return nvp;
}
return null;
}
/*********************************************************************************
* httpcall: Function to perform the API call to PayPal using API signature @
* methodName is name of API method. @ nvpStr is nvp string. returns a NVP
* string containing the response from the server.
*********************************************************************************/
public HashMap httpcall(String methodName, String nvpStr) {
String version = "2.3";
String agent = "Mozilla/4.0";
String respText = "";
HashMap nvp = null; // lhuynh not used?
// deformatNVP( nvpStr );
String encodedData = "METHOD=" + methodName + "&VERSION=" + gv_Version
+ "&PWD=" + gv_APIPassword + "&USER=" + gv_APIUserName
+ "&SIGNATURE=" + gv_APISignature + nvpStr;
try {
URL postURL = new URL(gv_APIEndpoint);
HttpURLConnection conn = (HttpURLConnection) postURL
.openConnection();
// Set connection parameters. We need to perform input and output,
// so set both as true.
conn.setDoInput(true);
conn.setDoOutput(true);
// Set the content type we are POSTing. We impersonate it as
// encoded form data
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", agent);
// conn.setRequestProperty( "Content-Type", type );
conn.setRequestProperty("Content-Length",
String.valueOf(encodedData.length()));
conn.setRequestMethod("POST");
// get the output stream to POST to.
DataOutputStream output = new DataOutputStream(
conn.getOutputStream());
output.writeBytes(encodedData);
output.flush();
output.close();
// Read input from the input stream.
DataInputStream in = new DataInputStream(conn.getInputStream());
int rc = conn.getResponseCode();
if (rc != -1) {
BufferedReader is = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String _line = null;
while (((_line = is.readLine()) != null)) {
respText = respText + _line;
}
nvp = deformatNVP(respText);
}
return nvp;
} catch (IOException e) {
// handle the error here
return null;
}
}
/*********************************************************************************
* deformatNVP: Function to break the NVP string into a HashMap pPayLoad is
* the NVP string. returns a HashMap object containing all the name value
* pairs of the string.
*********************************************************************************/
public HashMap deformatNVP(String pPayload) {
HashMap nvp = new HashMap();
StringTokenizer stTok = new StringTokenizer(pPayload, "&");
while (stTok.hasMoreTokens()) {
StringTokenizer stInternalTokenizer = new StringTokenizer(
stTok.nextToken(), "=");
if (stInternalTokenizer.countTokens() == 2) {
String key = URLDecoder.decode(stInternalTokenizer.nextToken());
String value = URLDecoder.decode(stInternalTokenizer
.nextToken());
nvp.put(key.toUpperCase(), value);
}
}
return nvp;
}
/*********************************************************************************
* RedirectURL: Function to redirect the user to the PayPal site token is
* the parameter that was returned by PayPal returns a HashMap object
* containing all the name value pairs of the string.
*********************************************************************************/
public void RedirectURL(HttpServletResponse response, String token) {
String payPalURL = PAYPAL_URL + token;
// response.sendRedirect( payPalURL );
response.setStatus(302);
response.setHeader("Location", payPalURL);
response.setHeader("Connection", "close");
}
// end class
}
我没能解决我的问题,但我能完成我的项目。我删除了所有代码并尝试了以下示例项目: 项目成功了,所以我只是更改了值,删除了所有不必要的代码,并重新编写了我的Tapestry5应用程序 总有一天,我会回到过去,找出为什么以前使用的代码不起作用,我认为maven依赖性不好,但我们会看到的
谢谢大家的帮助。请更改以下变量:
String returnURL = "localhost:8080/1/HelloWorld";
String cancelURL = "localhost:8080/1/HelloWorld";
到
问题:它无法验证Url,因为它没有“http://”您能告诉我们哪个是
签出
类中的第72行吗?是这一行吗:String strAck=nvp.get(“ACK”).toString();-如果是,则nvp为null或在映射中找不到“ACK”键的值。你能调试看看吗?非常抱歉,它在第36行String strAck=nvp.get(“ACK”).toString();我已检查,nvp为空。我曾经考虑过我没有正确设置测试PayPal的可能性,我不知道我在哪里会犯错误,但这可能是导致这种奇怪错误行为的原因吗?
public class PaypalFunctions {
private String gv_APIUserName;
private String gv_APIPassword;
private String gv_APISignature;
private String gv_APIEndpoint;
private String gv_BNCode;
private String gv_Version;
private String gv_nvpHeader;
private String gv_ProxyServer;
private String gv_ProxyServerPort;
private int gv_Proxy;
private boolean gv_UseProxy;
private String PAYPAL_URL;
public PaypalFunctions() {
gv_APIUserName = "dusan.stanojevic95-facilitator_api1.gmail.com";
gv_APIPassword = "1405890612";
gv_APISignature = "AexbutlMdUCD618gs-q4m-DJpUxEAoU-uNDtNNLAb47Su-QoOlNbIjeo";
boolean bSandbox = true;
String PAYPAL_DG_URL = null;
if (bSandbox == true) {
gv_APIEndpoint = "https://api-3t.sandbox.paypal.com/nvp";
PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
PAYPAL_DG_URL = "https://www.sandbox.paypal.com/incontext?token=";
} else {
gv_APIEndpoint = "https://api-3t.paypal.com/nvp";
PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
PAYPAL_DG_URL = "https://www.paypal.com/incontext?token=";
}
String HTTPREQUEST_PROXYSETTING_SERVER = "";
String HTTPREQUEST_PROXYSETTING_PORT = "";
boolean USE_PROXY = false;
gv_Version = "84";
// WinObjHttp Request proxy settings.
gv_ProxyServer = HTTPREQUEST_PROXYSETTING_SERVER;
gv_ProxyServerPort = HTTPREQUEST_PROXYSETTING_PORT;
gv_Proxy = 2; // 'setting for proxy activation
gv_UseProxy = USE_PROXY;
}
/*********************************************************************************
* SetExpressCheckout: Function to perform the SetExpressCheckout API call
*
* Inputs: paymentAmount: Total value of the purchase currencyCodeType:
* Currency code value the PayPal API paymentType: 'Sale' for Digital Goods
* returnURL: the page where buyers return to after they are done with the
* payment review on PayPal cancelURL: the page where buyers return to when
* they cancel the payment review on PayPal
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap setExpressCheckout(String paymentAmount, String returnURL,
String cancelURL, Map item) {
/*
* '------------------------------------ ' The currencyCodeType and
* paymentType ' are set to the selections made on the Integration
* Assistant '------------------------------------
*/
String currencyCodeType = "USD";
String paymentType = "Sale";
/*
* Construct the parameter string that describes the PayPal payment the
* varialbes were set in the web form, and the resulting string is
* stored in $nvpstr
*/
String nvpstr = "&PAYMENTREQUEST_0_AMT=" + paymentAmount
+ "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType
+ "&RETURNURL=" + URLEncoder.encode(returnURL) + "&CANCELURL="
+ URLEncoder.encode(cancelURL)
+ "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType
+ "&REQCONFIRMSHIPPING=0" + "&NOSHIPPING=1"
+ "&L_PAYMENTREQUEST_0_NAME0=" + item.get("name")
+ "&L_PAYMENTREQUEST_0_AMT0=" + item.get("amt")
+ "&L_PAYMENTREQUEST_0_QTY0=" + item.get("qty")
+ "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
/*
* Make the call to PayPal to get the Express Checkout token If the API
* call succeded, then redirect the buyer to PayPal to begin to
* authorize payment. If an error occured, show the resulting errors
*/
HashMap nvp = httpcall("SetExpressCheckout", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null && strAck.equalsIgnoreCase("Success")) {
return nvp;
}
return null;
}
/*********************************************************************************
* GetShippingDetails: Function to perform the GetExpressCheckoutDetails API
* call
*
* Inputs: None
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap getPaymentDetails(String token) {
/*
* Build a second API request to PayPal, using the token as the ID to
* get the details on the payment authorization
*/
String nvpstr = "&TOKEN=" + token;
/*
* Make the API call and store the results in an array. If the call was
* a success, show the authorization details, and provide an action to
* complete the payment. If failed, show the error
*/
HashMap nvp = httpcall("GetExpressCheckoutDetails", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null
&& (strAck.equalsIgnoreCase("Success") || strAck
.equalsIgnoreCase("SuccessWithWarning"))) {
return nvp;
}
return null;
}
/*********************************************************************************
* ConfirmPayment: Function to perform the DoExpressCheckoutPayment API call
*
* Inputs: None
*
* Output: Returns a HashMap object containing the response from the server.
*********************************************************************************/
public HashMap confirmPayment(String token, String payerID,
String finalPaymentAmount, String serverName, Map item) {
/*
* '------------------------------------ ' The currencyCodeType and
* paymentType ' are set to the selections made on the Integration
* Assistant '------------------------------------
*/
String currencyCodeType = "USD";
String paymentType = "Sale";
/*
* '----------------------------------------------------------------------------
* '---- Use the values stored in the session from the previous SetEC
* call
* '----------------------------------------------------------------------------
*/
String nvpstr = "&TOKEN=" + token + "&PAYERID=" + payerID
+ "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType + "&PAYMENTREQUEST_0_AMT="
+ finalPaymentAmount;
nvpstr = nvpstr + "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType + "&IPADDRESS="
+ serverName;
nvpstr = nvpstr + "&L_PAYMENTREQUEST_0_NAME0=" + item.get("name")
+ "&L_PAYMENTREQUEST_0_AMT0=" + item.get("amt")
+ "&L_PAYMENTREQUEST_0_QTY0=" + item.get("qty")
+ "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
/*
* Make the call to PayPal to finalize payment If an error occured, show
* the resulting errors
*/
HashMap nvp = httpcall("DoExpressCheckoutPayment", nvpstr);
String strAck = nvp.get("ACK").toString();
if (strAck != null
&& (strAck.equalsIgnoreCase("Success") || strAck
.equalsIgnoreCase("SuccessWithWarning"))) {
return nvp;
}
return null;
}
/*********************************************************************************
* httpcall: Function to perform the API call to PayPal using API signature @
* methodName is name of API method. @ nvpStr is nvp string. returns a NVP
* string containing the response from the server.
*********************************************************************************/
public HashMap httpcall(String methodName, String nvpStr) {
String version = "2.3";
String agent = "Mozilla/4.0";
String respText = "";
HashMap nvp = null; // lhuynh not used?
// deformatNVP( nvpStr );
String encodedData = "METHOD=" + methodName + "&VERSION=" + gv_Version
+ "&PWD=" + gv_APIPassword + "&USER=" + gv_APIUserName
+ "&SIGNATURE=" + gv_APISignature + nvpStr;
try {
URL postURL = new URL(gv_APIEndpoint);
HttpURLConnection conn = (HttpURLConnection) postURL
.openConnection();
// Set connection parameters. We need to perform input and output,
// so set both as true.
conn.setDoInput(true);
conn.setDoOutput(true);
// Set the content type we are POSTing. We impersonate it as
// encoded form data
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", agent);
// conn.setRequestProperty( "Content-Type", type );
conn.setRequestProperty("Content-Length",
String.valueOf(encodedData.length()));
conn.setRequestMethod("POST");
// get the output stream to POST to.
DataOutputStream output = new DataOutputStream(
conn.getOutputStream());
output.writeBytes(encodedData);
output.flush();
output.close();
// Read input from the input stream.
DataInputStream in = new DataInputStream(conn.getInputStream());
int rc = conn.getResponseCode();
if (rc != -1) {
BufferedReader is = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String _line = null;
while (((_line = is.readLine()) != null)) {
respText = respText + _line;
}
nvp = deformatNVP(respText);
}
return nvp;
} catch (IOException e) {
// handle the error here
return null;
}
}
/*********************************************************************************
* deformatNVP: Function to break the NVP string into a HashMap pPayLoad is
* the NVP string. returns a HashMap object containing all the name value
* pairs of the string.
*********************************************************************************/
public HashMap deformatNVP(String pPayload) {
HashMap nvp = new HashMap();
StringTokenizer stTok = new StringTokenizer(pPayload, "&");
while (stTok.hasMoreTokens()) {
StringTokenizer stInternalTokenizer = new StringTokenizer(
stTok.nextToken(), "=");
if (stInternalTokenizer.countTokens() == 2) {
String key = URLDecoder.decode(stInternalTokenizer.nextToken());
String value = URLDecoder.decode(stInternalTokenizer
.nextToken());
nvp.put(key.toUpperCase(), value);
}
}
return nvp;
}
/*********************************************************************************
* RedirectURL: Function to redirect the user to the PayPal site token is
* the parameter that was returned by PayPal returns a HashMap object
* containing all the name value pairs of the string.
*********************************************************************************/
public void RedirectURL(HttpServletResponse response, String token) {
String payPalURL = PAYPAL_URL + token;
// response.sendRedirect( payPalURL );
response.setStatus(302);
response.setHeader("Location", payPalURL);
response.setHeader("Connection", "close");
}
// end class
}
String returnURL = "localhost:8080/1/HelloWorld";
String cancelURL = "localhost:8080/1/HelloWorld";
String returnURL = "http://localhost:8080/1/HelloWorld";
String cancelURL = "http://localhost:8080/1/HelloWorld";