Sample code for 30+ languages & platforms
Unicode C++ 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 Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.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.
    CkHttpW http;
    http.put_AuthToken(L"SANDBOX_ACCESS_TOKEN");
    CkHttpResponseW resp;

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

    success = http.HttpNoBody(L"GET",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        wprintf(L"No HTTP response: %s\n",http.lastErrorText());
        return;
    }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObjectW errorInfo;
        errorInfo.Load(resp.bodyStr());
        wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),errorInfo.stringOf(L"name"),errorInfo.stringOf(L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",errorInfo.stringOf(L"debug_id"));
        return;
    }

    CkJsonObjectW result;
    bool parsedResult = result.Load(resp.bodyStr());
    if (parsedResult != true) {
        wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
        return;
    }

    if (result.StringOfEquals(L"status",L"DRAFT",true) != true) {
        wprintf(L"Select an unsent draft.\n");
        return;
    }

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

    CkJsonObjectW body;
    body.UpdateBool(L"send_to_recipient",false);
    body.UpdateBool(L"send_to_invoicer",false);
    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,L"application/json",resp);
    if (success != true) {
        wprintf(L"No HTTP response: %s\n",http.lastErrorText());
        return;
    }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObjectW sendError;
        sendError.Load(resp.bodyStr());
        wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),sendError.stringOf(L"name"),sendError.stringOf(L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",sendError.stringOf(L"debug_id"));
        return;
    }

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    success = http.HttpNoBody(L"GET",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        wprintf(L"No HTTP response: %s\n",http.lastErrorText());
        return;
    }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObjectW invoiceError;
        invoiceError.Load(resp.bodyStr());
        wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),invoiceError.stringOf(L"name"),invoiceError.stringOf(L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",invoiceError.stringOf(L"debug_id"));
        return;
    }

    CkJsonObjectW invoice;
    bool parsedInvoice = invoice.Load(resp.bodyStr());
    if (parsedInvoice != true) {
        wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
        return;
    }

    if (((invoice.StringOfEquals(L"status",L"SENT",true) == true) || (invoice.StringOfEquals(L"status",L"UNPAID",true) == true) || (invoice.StringOfEquals(L"status",L"SHARED",true) == true)) && (invoice.HasMember(L"detail.metadata.recipient_view_url") == true)) {
        wprintf(L"Customer payment page: %s\n",invoice.stringOf(L"detail.metadata.recipient_view_url"));
        return;
    }

    wprintf(L"Invoice is %s. Retrieve it again when payable.\n",invoice.stringOf(L"status"));
    }