Unicode C Requires Chilkat v11.0.0+
Unicode C
PayPal: Read an invoice and its outstanding balance
See more PayPal Examples
Use the PayPal invoice ID to retrieve its number, status, total and balance due. A partial external payment can reduce the balance without settling the whole invoice.
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpResponseW resp;
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();
// The original total and the balance still due may differ after a payment.
CkHttpW_SetUrlVar(http,L"id",L"INVOICE_ID");
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
if (success != TRUE) {
wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
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(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(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"Number: %s\n",CkJsonObjectW_stringOf(result,L"detail.invoice_number"));
wprintf(L"Total: %s\n",CkJsonObjectW_stringOf(result,L"amount.value"));
wprintf(L"Currency: %s\n",CkJsonObjectW_stringOf(result,L"amount.currency_code"));
wprintf(L"Balance due: %s\n",CkJsonObjectW_stringOf(result,L"due_amount.value"));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
}