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

PayPal: Create an itemized draft invoice

See more PayPal Examples

Bill a sandbox recipient for two inspections at USD 45 each, due in 30 days. Request the invoice representation and save its ID for sending. Creating the draft does not notify the customer.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.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;

    //  Replace buyer@example.com with your sandbox buyer. PayPal uses the authenticated
    //  merchant as the invoicer.
    http.SetRequestHeader(L"Prefer",L"return=representation");
    CkJsonObjectW body;
    body.UpdateString(L"detail.currency_code",L"USD");
    body.UpdateString(L"detail.payment_term.term_type",L"NET_30");
    body.UpdateString(L"primary_recipients[0].billing_info.email_address",L"buyer@example.com");
    body.UpdateString(L"items[0].name",L"Equipment inspection");
    body.UpdateString(L"items[0].quantity",L"2");
    body.UpdateString(L"items[0].unit_amount.currency_code",L"USD");
    body.UpdateString(L"items[0].unit_amount.value",L"45.00");

    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices",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 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 parsed = result.Load(resp.bodyStr());
    if (parsed != true) {
        wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
        return;
    }

    wprintf(L"ID: %s\n",result.stringOf(L"id"));
    wprintf(L"Status: %s\n",result.stringOf(L"status"));
    wprintf(L"Invoice number: %s\n",result.stringOf(L"detail.invoice_number"));
    wprintf(L"Total: %s\n",result.stringOf(L"amount.value"));
    }