Unicode C Requires Chilkat v11.0.0+
Unicode C
PayPal: Find authorizations for a known order
See more PayPal Examples
Read the known order and visit every purchase unit. Extract its authorizations with their IDs, statuses and amounts. An empty collection means this order has no such resources yet; it says nothing about other orders.
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;
int i;
int j;
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();
CkHttpW_SetUrlVar(http,L"id",L"ORDER_ID");
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api-m.sandbox.paypal.com/v2/checkout/orders/{$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;
}
// A purchase unit may have no authorizations yet, or more than one.
i = 0;
while ((i < CkJsonObjectW_SizeOfArray(result,L"purchase_units"))) {
CkJsonObjectW_putI(result,i);
j = 0;
while ((j < CkJsonObjectW_SizeOfArray(result,L"purchase_units[i].payments.authorizations"))) {
CkJsonObjectW_putJ(result,j);
wprintf(L"Unit: %s authorization: %s\n",CkJsonObjectW_stringOf(result,L"purchase_units[i].reference_id"),CkJsonObjectW_stringOf(result,L"purchase_units[i].payments.authorizations[j].id"));
wprintf(L" %s %s %s\n",CkJsonObjectW_stringOf(result,L"purchase_units[i].payments.authorizations[j].status"),CkJsonObjectW_stringOf(result,L"purchase_units[i].payments.authorizations[j].amount.value")
,CkJsonObjectW_stringOf(result,L"purchase_units[i].payments.authorizations[j].amount.currency_code"));
j = j + 1;
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
}