Java Requires Chilkat v11.0.0+
Java
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
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();
http.setUrlVar("id", "INVOICE_ID");
try {
http.httpNoBody("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", 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;
}
if (!result.stringOfEquals("status", "DRAFT", true)) {
System.out.println("Select an unsent draft.");
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
DateTime now = new DateTime();
now.setFromCurrentSystemTime();
StringBuilder stamp = new StringBuilder();
stamp.append(result.stringOf("detail.invoice_date"));
stamp.append("T00:00:00Z");
DateTime issue = new DateTime();
boolean validDate;
try {
issue.setFromTimestamp(stamp.getAsString());
validDate = true;
} catch (ChilkatException e) {
validDate = false;
}
if ((!validDate) || (now.diffSeconds(issue) < 0)) {
System.out.println("Select a draft with an issue date of today or earlier.");
return;
}
JsonObject body = new JsonObject();
body.updateBool("send_to_recipient", false);
body.updateBool("send_to_invoicer", false);
try {
http.httpJson("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send", body, "application/json", resp);
} catch (ChilkatException e) {
System.out.println("No HTTP response: " + e.getLastErrorText());
return;
}
if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
JsonObject sendError = new JsonObject();
sendError.load(resp.getBodyStr());
System.out.println("HTTP " + resp.getStatusCode() + " " + sendError.stringOf("name") + " " + sendError.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + sendError.stringOf("debug_id"));
return;
}
// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
try {
http.httpNoBody("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", resp);
} catch (ChilkatException e) {
System.out.println("No HTTP response: " + e.getLastErrorText());
return;
}
if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
JsonObject invoiceError = new JsonObject();
invoiceError.load(resp.getBodyStr());
System.out.println("HTTP " + resp.getStatusCode() + " " + invoiceError.stringOf("name") + " " + invoiceError.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + invoiceError.stringOf("debug_id"));
return;
}
JsonObject invoice = new JsonObject();
try {
invoice.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
return;
}
if (((invoice.stringOfEquals("status", "SENT", true)) || ((invoice.stringOfEquals("status", "UNPAID", true)) || (invoice.stringOfEquals("status", "SHARED", true)))) && (invoice.hasMember("detail.metadata.recipient_view_url"))) {
System.out.println("Customer payment page: " + invoice.stringOf("detail.metadata.recipient_view_url"));
return;
}
System.out.println("Invoice is " + invoice.stringOf("status") + ". Retrieve it again when payable.");
}
}