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 <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.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;
http.SetUrlVar(L"id",L"SUBSCRIPTION_ID");
success = http.HttpNoBody(L"GET",L"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}",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 parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
return;
}
wprintf(L"Status: %s\n",result.stringOf(L"status"));
wprintf(L"Next billing: %s\n",result.stringOf(L"billing_info.next_billing_time"));
CkDateTimeW now;
now.SetFromCurrentSystemTime();
CkJsonObjectW query;
query.UpdateString(L"end_time",now.getAsTimestamp(false));
now.AddDays(-30);
query.UpdateString(L"start_time",now.getAsTimestamp(false));
success = http.HttpParams(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",http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObjectW historyError;
historyError.Load(resp.bodyStr());
wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),historyError.stringOf(L"name"),historyError.stringOf(L"details[0].issue"));
wprintf(L"PayPal debug ID: %s\n",historyError.stringOf(L"debug_id"));
return;
}
CkJsonObjectW history;
bool parsedHistory = history.Load(resp.bodyStr());
if (parsedHistory != true) {
wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
return;
}
int i = 0;
while ((i < history.SizeOfArray(L"transactions"))) {
history.put_I(i);
wprintf(L"%s %s %s %s %s\n",history.stringOf(L"transactions[i].time"),history.stringOf(L"transactions[i].id"),history.stringOf(L"transactions[i].status")
,history.stringOf(L"transactions[i].amount_with_breakdown.gross_amount.value"),history.stringOf(L"transactions[i].amount_with_breakdown.gross_amount.currency_code"));
i = i + 1;
}
}