JavaScript Requires Chilkat v11.0.0+
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var 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.
var http = new CkHttp();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
var 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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var errorInfo = new CkJsonObject();
errorInfo.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
console.log("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
return;
}
var result = new CkJsonObject();
var parsedResult = result.Load(resp.BodyStr);
if (parsedResult !== true) {
console.log("Expected a JSON response; HTTP " + resp.StatusCode);
return;
}
if (result.StringOfEquals("status","DRAFT",true) !== true) {
console.log("Select an unsent draft.");
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
var now = new CkDateTime();
now.SetFromCurrentSystemTime();
var stamp = new CkStringBuilder();
stamp.Append(result.StringOf("detail.invoice_date"));
stamp.Append("T00:00:00Z");
var issue = new CkDateTime();
var validDate = issue.SetFromTimestamp(stamp.GetAsString());
if ((validDate !== true) || (now.DiffSeconds(issue) < 0)) {
console.log("Select a draft with an issue date of today or earlier.");
return;
}
var 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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var sendError = new CkJsonObject();
sendError.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + sendError.StringOf("name") + " " + sendError.StringOf("details[0].issue"));
console.log("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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var invoiceError = new CkJsonObject();
invoiceError.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + invoiceError.StringOf("name") + " " + invoiceError.StringOf("details[0].issue"));
console.log("PayPal debug ID: " + invoiceError.StringOf("debug_id"));
return;
}
var invoice = new CkJsonObject();
var parsedInvoice = invoice.Load(resp.BodyStr);
if (parsedInvoice !== true) {
console.log("Expected a JSON response; HTTP " + resp.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)) {
console.log("Customer payment page: " + invoice.StringOf("detail.metadata.recipient_view_url"));
return;
}
console.log("Invoice is " + invoice.StringOf("status") + ". Retrieve it again when payable.");