Sample code for 30+ languages & platforms
CkJava Requires Chilkat v11.0.0+

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.

PayPal documentation

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  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.
    CkHttp http = new CkHttp();
    http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
    CkHttpResponse resp = new CkHttpResponse();

    http.SetUrlVar("id","INVOICE_ID");

    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        System.out.println("No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObject errorInfo = new CkJsonObject();
        errorInfo.Load(resp.bodyStr());
        System.out.println("HTTP " + resp.get_StatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
        System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
        return;
        }

    CkJsonObject result = new CkJsonObject();
    boolean parsedResult = result.Load(resp.bodyStr());
    if (parsedResult != true) {
        System.out.println("Expected a JSON response; HTTP " + resp.get_StatusCode());
        return;
        }

    if (result.StringOfEquals("status","DRAFT",true) != true) {
        System.out.println("Select an unsent draft.");
        return;
        }

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    CkDateTime now = new CkDateTime();
    now.SetFromCurrentSystemTime();
    CkStringBuilder stamp = new CkStringBuilder();
    stamp.Append(result.stringOf("detail.invoice_date"));
    stamp.Append("T00:00:00Z");
    CkDateTime issue = new CkDateTime();
    boolean validDate = issue.SetFromTimestamp(stamp.getAsString());
    if ((validDate != true) || (now.DiffSeconds(issue) < 0)) {
        System.out.println("Select a draft with an issue date of today or earlier.");
        return;
        }

    CkJsonObject body = new CkJsonObject();
    body.UpdateBool("send_to_recipient",false);
    body.UpdateBool("send_to_invoicer",false);
    success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp);
    if (success != true) {
        System.out.println("No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObject sendError = new CkJsonObject();
        sendError.Load(resp.bodyStr());
        System.out.println("HTTP " + resp.get_StatusCode() + " " + 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.
    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        System.out.println("No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObject invoiceError = new CkJsonObject();
        invoiceError.Load(resp.bodyStr());
        System.out.println("HTTP " + resp.get_StatusCode() + " " + invoiceError.stringOf("name") + " " + invoiceError.stringOf("details[0].issue"));
        System.out.println("PayPal debug ID: " + invoiceError.stringOf("debug_id"));
        return;
        }

    CkJsonObject invoice = new CkJsonObject();
    boolean parsedInvoice = invoice.Load(resp.bodyStr());
    if (parsedInvoice != true) {
        System.out.println("Expected a JSON response; HTTP " + resp.get_StatusCode());
        return;
        }

    if (((invoice.StringOfEquals("status","SENT",true) == true) || (invoice.StringOfEquals("status","UNPAID",true) == true) || (invoice.StringOfEquals("status","SHARED",true) == true)) && (invoice.HasMember("detail.metadata.recipient_view_url") == true)) {
        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.");
  }
}