Unicode C Requires Chilkat v11.0.0+
Unicode C
PayPal: Look up a subscription and its recent payments
See more PayPal Examples
Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDateTimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpResponseW resp;
HCkJsonObjectW errorInfo;
HCkJsonObjectW result;
BOOL parsedResult;
HCkDateTimeW now;
HCkJsonObjectW query;
HCkJsonObjectW historyError;
HCkJsonObjectW history;
BOOL parsedHistory;
int i;
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"SUBSCRIPTION_ID");
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$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();
parsedResult = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
if (parsedResult != 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"Status: %s\n",CkJsonObjectW_stringOf(result,L"status"));
wprintf(L"Next billing: %s\n",CkJsonObjectW_stringOf(result,L"billing_info.next_billing_time"));
now = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(now);
query = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(query,L"end_time",CkDateTimeW_getAsTimestamp(now,FALSE));
CkDateTimeW_AddDays(now,-30);
CkJsonObjectW_UpdateString(query,L"start_time",CkDateTimeW_getAsTimestamp(now,FALSE));
success = CkHttpW_HttpParams(http,L"GET",L"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions",query,resp);
if (success != TRUE) {
wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
CkDateTimeW_Dispose(now);
CkJsonObjectW_Dispose(query);
return;
}
if ((CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
historyError = CkJsonObjectW_Create();
CkJsonObjectW_Load(historyError,CkHttpResponseW_bodyStr(resp));
wprintf(L"HTTP %d %s %s\n",CkHttpResponseW_getStatusCode(resp),CkJsonObjectW_stringOf(historyError,L"name"),CkJsonObjectW_stringOf(historyError,L"details[0].issue"));
wprintf(L"PayPal debug ID: %s\n",CkJsonObjectW_stringOf(historyError,L"debug_id"));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
CkDateTimeW_Dispose(now);
CkJsonObjectW_Dispose(query);
CkJsonObjectW_Dispose(historyError);
return;
}
history = CkJsonObjectW_Create();
parsedHistory = CkJsonObjectW_Load(history,CkHttpResponseW_bodyStr(resp));
if (parsedHistory != 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);
CkDateTimeW_Dispose(now);
CkJsonObjectW_Dispose(query);
CkJsonObjectW_Dispose(historyError);
CkJsonObjectW_Dispose(history);
return;
}
i = 0;
while ((i < CkJsonObjectW_SizeOfArray(history,L"transactions"))) {
CkJsonObjectW_putI(history,i);
wprintf(L"%s %s %s %s %s\n",CkJsonObjectW_stringOf(history,L"transactions[i].time"),CkJsonObjectW_stringOf(history,L"transactions[i].id"),CkJsonObjectW_stringOf(history,L"transactions[i].status")
,CkJsonObjectW_stringOf(history,L"transactions[i].amount_with_breakdown.gross_amount.value"),CkJsonObjectW_stringOf(history,L"transactions[i].amount_with_breakdown.gross_amount.currency_code"));
i = i + 1;
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(errorInfo);
CkJsonObjectW_Dispose(result);
CkDateTimeW_Dispose(now);
CkJsonObjectW_Dispose(query);
CkJsonObjectW_Dispose(historyError);
CkJsonObjectW_Dispose(history);
}