C Requires Chilkat v11.0.0+
C
PayPal: Build a worklist of overdue invoices
See more PayPal Examples
Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.
Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
#include <C_CkDateTime.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkHttpResponse resp;
HCkJsonObject body;
HCkDateTime day;
int page;
int lastPage;
HCkStringBuilder url;
HCkJsonObject errorInfo;
HCkJsonObject result;
BOOL parsedResult;
int i;
int count;
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 = CkHttp_Create();
CkHttp_putAuthToken(http,"SANDBOX_ACCESS_TOKEN");
resp = CkHttpResponse_Create();
body = CkJsonObject_Create();
CkJsonObject_UpdateString(body,"status[0]","SENT");
CkJsonObject_UpdateString(body,"status[1]","PARTIALLY_PAID");
CkJsonObject_UpdateString(body,"status[2]","UNPAID");
CkJsonObject_UpdateString(body,"status[3]","SHARED");
// Yesterday is the last overdue date. Include invoices due in the past 90 days.
day = CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(day);
CkDateTime_AddDays(day,-1);
CkJsonObject_UpdateString(body,"due_date_range.end",CkDateTime_getAsIso8601(day,"YYYY-MM-DD",FALSE));
CkDateTime_AddDays(day,-89);
CkJsonObject_UpdateString(body,"due_date_range.start",CkDateTime_getAsIso8601(day,"YYYY-MM-DD",FALSE));
page = 1;
lastPage = 1;
while ((page <= lastPage)) {
url = CkStringBuilder_Create();
CkStringBuilder_Append(url,"https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=");
CkStringBuilder_AppendInt(url,page);
success = CkHttp_HttpJson(http,"POST",CkStringBuilder_getAsString(url),body,"application/json",resp);
if (success != TRUE) {
printf("No HTTP response: %s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(url);
return;
}
if ((CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
errorInfo = CkJsonObject_Create();
CkJsonObject_Load(errorInfo,CkHttpResponse_bodyStr(resp));
printf("HTTP %d %s %s\n",CkHttpResponse_getStatusCode(resp),CkJsonObject_stringOf(errorInfo,"name"),CkJsonObject_stringOf(errorInfo,"details[0].issue"));
printf("PayPal debug ID: %s\n",CkJsonObject_stringOf(errorInfo,"debug_id"));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(errorInfo);
return;
}
result = CkJsonObject_Create();
parsedResult = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
if (parsedResult != TRUE) {
printf("Expected a JSON response; HTTP %d\n",CkHttpResponse_getStatusCode(resp));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(errorInfo);
CkJsonObject_Dispose(result);
return;
}
i = 0;
count = CkJsonObject_SizeOfArray(result,"items");
while ((i < count)) {
CkJsonObject_putI(result,i);
printf("%s due %s %s %s\n",CkJsonObject_stringOf(result,"items[i].detail.invoice_number"),CkJsonObject_stringOf(result,"items[i].detail.payment_term.due_date")
,CkJsonObject_stringOf(result,"items[i].due_amount.value"),CkJsonObject_stringOf(result,"items[i].due_amount.currency_code"));
i = i + 1;
}
lastPage = CkJsonObject_IntOf(result,"total_pages");
page = page + 1;
}
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
CkDateTime_Dispose(day);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(errorInfo);
CkJsonObject_Dispose(result);
}