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

PayPal: Authorize a buyer-approved order

See more PayPal Examples

Authorize the stored order after the buyer approves it. Save the returned authorization ID and amount for capture or void. An authorization reserves funds; it is not a capture.

PayPal documentation

Chilkat Unicode C++ Downloads

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

    //  Use the approved order created with intent AUTHORIZE.
    http.SetUrlVar(L"id",L"ORDER_ID");
    //  Create and persist this key once per business operation. On a timeout,
    //  retry the same request with its saved key, not a newly generated one.
    CkCrypt2W crypt;
    const wchar_t *requestId = crypt.generateUuid();
    http.SetRequestHeader(L"PayPal-Request-Id",requestId);
    http.SetRequestHeader(L"Prefer",L"return=representation");
    CkJsonObjectW body;

    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/authorize",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"));
    //  A purchase unit may have no authorizations yet, or more than one.
    int i = 0;
    while ((i < result.SizeOfArray(L"purchase_units"))) {
        result.put_I(i);
        int j = 0;
        while ((j < result.SizeOfArray(L"purchase_units[i].payments.authorizations"))) {
            result.put_J(j);
            wprintf(L"Unit: %s  authorization: %s\n",result.stringOf(L"purchase_units[i].reference_id"),result.stringOf(L"purchase_units[i].payments.authorizations[j].id"));
            wprintf(L"  %s  %s %s\n",result.stringOf(L"purchase_units[i].payments.authorizations[j].status"),result.stringOf(L"purchase_units[i].payments.authorizations[j].amount.value")
                ,result.stringOf(L"purchase_units[i].payments.authorizations[j].amount.currency_code"));
            j = j + 1;
        }

        i = i + 1;
    }
    }