C++ Requires Chilkat v11.0.0+
C++
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 C++ Downloads
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>
#include <CkDateTime.h>
#include <CkStringBuilder.h>
void ChilkatSample(void)
{
bool 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;
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse resp;
http.SetUrlVar("id","INVOICE_ID");
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
if (success != true) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo;
errorInfo.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << errorInfo.stringOf("name") << " " << errorInfo.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << errorInfo.stringOf("debug_id") << "\r\n";
return;
}
CkJsonObject result;
bool parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
std::cout << "Expected a JSON response; HTTP " << resp.get_StatusCode() << "\r\n";
return;
}
if (result.StringOfEquals("status","DRAFT",true) != true) {
std::cout << "Select an unsent draft." << "\r\n";
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
CkDateTime now;
now.SetFromCurrentSystemTime();
CkStringBuilder stamp;
stamp.Append(result.stringOf("detail.invoice_date"));
stamp.Append("T00:00:00Z");
CkDateTime issue;
bool validDate = issue.SetFromTimestamp(stamp.getAsString());
if ((validDate != true) || (now.DiffSeconds(issue) < 0)) {
std::cout << "Select a draft with an issue date of today or earlier." << "\r\n";
return;
}
CkJsonObject body;
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) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject sendError;
sendError.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << sendError.stringOf("name") << " " << sendError.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << sendError.stringOf("debug_id") << "\r\n";
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) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject invoiceError;
invoiceError.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << invoiceError.stringOf("name") << " " << invoiceError.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << invoiceError.stringOf("debug_id") << "\r\n";
return;
}
CkJsonObject invoice;
bool parsedInvoice = invoice.Load(resp.bodyStr());
if (parsedInvoice != true) {
std::cout << "Expected a JSON response; HTTP " << resp.get_StatusCode() << "\r\n";
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)) {
std::cout << "Customer payment page: " << invoice.stringOf("detail.metadata.recipient_view_url") << "\r\n";
return;
}
std::cout << "Invoice is " << invoice.stringOf("status") << ". Retrieve it again when payable." << "\r\n";
}