Java Requires Chilkat v11.0.0+
Java
PayPal: Revise a draft invoice from its full billing record
See more PayPal Examples
PUT replaces the invoice. This example revises the quantity on the simple two-inspection draft to three. Rebuild the complete editable document from your billing record; omitted items, terms or recipients can be lost. Do not use this body for an unrelated invoice.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// See Unlock Chilkat before making requests.
// For the merchant's internal backoffice application. Load the sandbox token
// from the merchant's credential store. Keep it out of publicly distributed apps.
// See Get a PayPal access token.
Http http = new Http();
http.setAuthToken("SANDBOX_ACCESS_TOKEN");
HttpResponse resp = new HttpResponse();
// Use only the ID of the matching draft from the create example, while it is still a
// draft. Preserve all of your real draft fields, including any custom invoice number.
http.setUrlVar("id", "INVOICE_ID");
http.setRequestHeader("Prefer", "return=representation");
JsonObject body = new JsonObject();
body.updateString("detail.currency_code", "USD");
body.updateString("detail.payment_term.term_type", "NET_30");
body.updateString("primary_recipients[0].billing_info.email_address", "buyer@example.com");
body.updateString("items[0].name", "Equipment inspection");
body.updateString("items[0].quantity", "3");
body.updateString("items[0].unit_amount.currency_code", "USD");
body.updateString("items[0].unit_amount.value", "45.00");
try {
http.httpJson("PUT", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", body, "application/json", resp);
} catch (ChilkatException e) {
System.out.println("No HTTP response: " + e.getLastErrorText());
return;
}
if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
JsonObject errorInfo = new JsonObject();
errorInfo.load(resp.getBodyStr());
System.out.println("HTTP " + resp.getStatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
return;
}
JsonObject result = new JsonObject();
try {
result.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
return;
}
System.out.println("ID: " + result.stringOf("id"));
System.out.println("Status: " + result.stringOf("status"));
System.out.println("New total: " + result.stringOf("amount.value"));
System.out.println("Currency: " + result.stringOf("amount.currency_code"));
}
}