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

PayPal: Revise a draft invoice from its full billing record

See more PayPal Examples

PUT replaces the invoice. This example revises the quantity on the simple two-inspection draft to three. Rebuild the complete editable document from your billing record; omitted items, terms or recipients can be lost. Do not use this body for an unrelated invoice.

PayPal documentation

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW body;
    HCkJsonObjectW errorInfo;
    HCkJsonObjectW result;
    BOOL parsed;

    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.
    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,L"SANDBOX_ACCESS_TOKEN");
    resp = CkHttpResponseW_Create();

    //  Use only the ID of the matching draft from the create example, while it is still a
    //  draft. Preserve all of your real draft fields, including any custom invoice number.
    CkHttpW_SetUrlVar(http,L"id",L"INVOICE_ID");
    CkHttpW_SetRequestHeader(http,L"Prefer",L"return=representation");
    body = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(body,L"detail.currency_code",L"USD");
    CkJsonObjectW_UpdateString(body,L"detail.payment_term.term_type",L"NET_30");
    CkJsonObjectW_UpdateString(body,L"primary_recipients[0].billing_info.email_address",L"buyer@example.com");
    CkJsonObjectW_UpdateString(body,L"items[0].name",L"Equipment inspection");
    CkJsonObjectW_UpdateString(body,L"items[0].quantity",L"3");
    CkJsonObjectW_UpdateString(body,L"items[0].unit_amount.currency_code",L"USD");
    CkJsonObjectW_UpdateString(body,L"items[0].unit_amount.value",L"45.00");

    success = CkHttpW_HttpJson(http,L"PUT",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",body,L"application/json",resp);
    if (success != TRUE) {
        wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        return;
    }

    if ((CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
        errorInfo = CkJsonObjectW_Create();
        CkJsonObjectW_Load(errorInfo,CkHttpResponseW_bodyStr(resp));
        wprintf(L"HTTP %d %s %s\n",CkHttpResponseW_getStatusCode(resp),CkJsonObjectW_stringOf(errorInfo,L"name"),CkJsonObjectW_stringOf(errorInfo,L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",CkJsonObjectW_stringOf(errorInfo,L"debug_id"));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(errorInfo);
        return;
    }

    result = CkJsonObjectW_Create();
    parsed = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
    if (parsed != TRUE) {
        wprintf(L"Expected a JSON response; HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(errorInfo);
        CkJsonObjectW_Dispose(result);
        return;
    }

    wprintf(L"ID: %s\n",CkJsonObjectW_stringOf(result,L"id"));
    wprintf(L"Status: %s\n",CkJsonObjectW_stringOf(result,L"status"));
    wprintf(L"New total: %s\n",CkJsonObjectW_stringOf(result,L"amount.value"));
    wprintf(L"Currency: %s\n",CkJsonObjectW_stringOf(result,L"amount.currency_code"));


    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(errorInfo);
    CkJsonObjectW_Dispose(result);

    }