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

PayPal: Delete an unsent draft invoice

See more PayPal Examples

This example deliberately checks for DRAFT before deletion. Use the invoice ID from your own records. A successful DELETE has no response body; use cancellation for a sent, unpaid invoice.

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;

    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 parsed = result.Load(resp.bodyStr());
    if (parsed != 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"Leaving invoice unchanged: %s\n",result.stringOf(L"status"));
        return;
    }

    success = http.HttpNoBody(L"DELETE",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 deleteError;
        deleteError.Load(resp.bodyStr());
        wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),deleteError.stringOf(L"name"),deleteError.stringOf(L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",deleteError.stringOf(L"debug_id"));
        return;
    }

    wprintf(L"Draft deleted; HTTP %d\n",resp.get_StatusCode());
    }