Paypal更新发票rest api HTTP响应代码:500

时间:2015-05-22 02:30:23

标签: java servlets curl paypal

我目前正在尝试使用Paypal提供的REST API来使用servlet创建自己的服务。我设法使用java将cURL代码传输到HttpsURLConnection。

这是我的代码:

JSONObject returnJson = new JSONObject();
PrintWriter out = response.getWriter();
JSONParser jparser = new JSONParser();
try{
    String inputStr = request.getParameter("input");
    System.out.println(inputStr);
    JSONObject inputJson = (JSONObject) jparser.parse(inputStr);
    String accessToken = (String) inputJson.get("access_token");
    String invoiceId = (String) inputJson.get("invoiceId");

    String url = "https://api.sandbox.paypal.com/v1/invoicing/invoices/"+invoiceId;
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("PUT");
    con.setRequestProperty("Accept-Language", "text/html; charset=UTF-8");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Authorization", "Bearer "+accessToken);

    //Tentatively, the input is hard coded, after integration, the input comes from http request.

    //However, only merchant email in mandatory for invoice creation in sand box so far
    //For details of invoice attributes please refer to this link--> https://developer.paypal.com/docs/api/#update-an-invoice

    String urlJsonString = "{\"id\":\""+invoiceId+"\",\"status\":\"DRAFT\",\"merchant_info\":{\"email\":\"rui.song.2013-facilitator@sis.smu.edu.sg\",\"first_name\":\"Dennis\",\"last_name\":\"Doctor\",\"business_name\":\"MedicalProfessionals,LLC\",\"phone\":{\"country_code\":\"US\",\"national_number\":\"5032141716\"},\"address\":{\"line1\":\"1234MainSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97217\",\"country_code\":\"US\"}},\"billing_info\":[{\"email\":\"sally-patient@example.com\"}],\"shipping_info\":{\"first_name\":\"Sally\",\"last_name\":\"Patient\",\"business_name\":\"Notapplicable\",\"address\":{\"line1\":\"1234BroadSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97216\",\"country_code\":\"US\"}},\"items\":[{\"name\":\"Sutures\",\"quantity\":100,\"unit_price\":{\"currency\":\"USD\",\"value\":\"250\"}}],\"invoice_date\":\"2014-01-07PST\",\"payment_term\":{\"term_type\":\"NO_DUE_DATE\"},\"tax_calculated_after_discount\":false,\"tax_inclusive\":false,\"note\":\"MedicalInvoice16Jul,2013PST\",\"total_amount\":{\"currency\":\"USD\",\"value\":\"250\"}}";

    System.out.println(urlJsonString);
    con.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.write(urlJsonString);
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);
    out.print(responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer res= new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        res.append(inputLine);
    }
    in.close();
    returnJson = (JSONObject) jparser.parse(res.toString());

    System.out.println(returnJson);
}catch(Exception e){
    e.printStackTrace();
    returnJson.put("message", e);
}
out.print(returnJson);

我在localhost上测试服务,我将手动传入两个参数:“access_token”和“invoiceId”,如下所示:

http://localhost:8080/Authentication/PaypalUpdateInvoiceServlet?input={"access_token":"A015Rv3XNo4fmFh4JC2sJiGjl1oEQ5w-B9azU.H6nlzMm1s","invoiceId":"INV2-9TRP-2S2R-OPBD-XK9T"}

这两条信息是由我使用上面提到的类似代码获得的。 我只修改了内部HttpsURLConnection部分中的代码,以便与Paypal网站中提供的cURL请求和响应示例相对应。链接 - >(https://developer.paypal.com/docs/api/#update-an-invoice

到目前为止,我成功实施了Create,Retrieve for invoice。我使用相同的方法使用所需的特定参数进行servlet调用,并且能够在Paypal网站上获得预期的响应显示。

但现在我被更新发票困住了。当我拨打servlet时。

我会收到:

500{"message":java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.sandbox.paypal.com/v1/invoicing/invoices/IINV2-9TRP-2S2R-OPBD-XK9T}

任何人都可以帮我解释为什么我会收到此错误,我该如何解决?

0 个答案:

没有答案