C++ Requires Chilkat v11.0.0+
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 C++ Downloads
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>
#include <CkDateTime.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.
CkHttp http;
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse resp;
http.SetUrlVar("id","SUBSCRIPTION_ID");
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}",resp);
if (success != true) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo;
errorInfo.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << errorInfo.stringOf("name") << " " << errorInfo.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << errorInfo.stringOf("debug_id") << "\r\n";
return;
}
CkJsonObject result;
bool parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
std::cout << "Expected a JSON response; HTTP " << resp.get_StatusCode() << "\r\n";
return;
}
std::cout << "Status: " << result.stringOf("status") << "\r\n";
std::cout << "Next billing: " << result.stringOf("billing_info.next_billing_time") << "\r\n";
CkDateTime now;
now.SetFromCurrentSystemTime();
CkJsonObject query;
query.UpdateString("end_time",now.getAsTimestamp(false));
now.AddDays(-30);
query.UpdateString("start_time",now.getAsTimestamp(false));
success = http.HttpParams("GET","https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions",query,resp);
if (success != true) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject historyError;
historyError.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << historyError.stringOf("name") << " " << historyError.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << historyError.stringOf("debug_id") << "\r\n";
return;
}
CkJsonObject history;
bool parsedHistory = history.Load(resp.bodyStr());
if (parsedHistory != true) {
std::cout << "Expected a JSON response; HTTP " << resp.get_StatusCode() << "\r\n";
return;
}
int i = 0;
while ((i < history.SizeOfArray("transactions"))) {
history.put_I(i);
std::cout << history.stringOf("transactions[i].time") << " " << history.stringOf("transactions[i].id") << " " << history.stringOf("transactions[i].status")
<< " " << history.stringOf("transactions[i].amount_with_breakdown.gross_amount.value") << " " << history.stringOf("transactions[i].amount_with_breakdown.gross_amount.currency_code") << "\r\n";
i = i + 1;
}
}