Unicode C++
Unicode C++
PayPal - Mark Invoice as Paid
See more PayPal Examples
Mark an invoice as paid.Note: This example requires Chilkat v9.5.0.65 or greater.
Chilkat Unicode C++ Downloads
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkRestW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
bool success = false;
// Note: Requires Chilkat v9.5.0.65 or greater.
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load our previously obtained access token. (see PayPal OAuth2 Access Token)
CkJsonObjectW jsonToken;
jsonToken.LoadFile(L"qa_data/tokens/paypal.json");
// Build the Authorization request header field value.
CkStringBuilderW sbAuth;
// token_type should be "Bearer"
sbAuth.Append(jsonToken.stringOf(L"token_type"));
sbAuth.Append(L" ");
sbAuth.Append(jsonToken.stringOf(L"access_token"));
// Make the initial connection.
// A single REST object, once connected, can be used for many PayPal REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
CkRestW rest;
bool bAutoReconnect = true;
success = rest.Connect(L"api.sandbox.paypal.com",443,true,bAutoReconnect);
if (success != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
// ----------------------------------------------------------------------------------------------
// The code above this comment could be placed inside a function/subroutine within the application
// because the connection does not need to be made for every request. Once the connection is made
// the app may send many requests..
// ----------------------------------------------------------------------------------------------
// Build the JSON request body containing the reminder information:
CkJsonObjectW json;
json.put_EmitCompact(false);
json.UpdateString(L"method",L"CREDIT_CARD");
CkDateTimeW dtNow;
dtNow.SetFromCurrentSystemTime();
CkStringBuilderW sbDt;
sbDt.Append(dtNow.getAsTimestamp(false));
int numReplaced = sbDt.Replace(L"T",L" ");
numReplaced = sbDt.Replace(L"Z",L" UTC");
json.UpdateString(L"date",sbDt.getAsString());
json.UpdateString(L"note",L"I got the payment by credit card!");
json.UpdateString(L"amount.currency",L"USD");
json.UpdateString(L"amount.value",L"500.00");
wprintf(L"%s\n",json.emit());
// The JSON created by the above code:
// {
// "method": "CREDIT_CARD",
// "date": "2016-11-28 21:05:38 UTC",
// "note": "I got the payment by credit card!",
// "amount": {
// "currency": "USD",
// "value": "500.00"
// }
// }
// Build a path using a previously-sent invoice ID.
CkStringBuilderW sbPath;
sbPath.Append(L"/v1/invoicing/invoices/invoice_id/record-payment");
int numReplacements = sbPath.Replace(L"invoice_id",L"INV2-QCHR-QHSD-M2VU-AVXC");
json.put_EmitCompact(true);
CkStringBuilderW sbRequestBody;
CkStringBuilderW sbResponseBody;
rest.AddHeader(L"Content-Type",L"application/json");
rest.AddHeader(L"Authorization",sbAuth.getAsString());
json.EmitSb(sbRequestBody);
success = rest.FullRequestSb(L"POST",sbPath.getAsString(),sbRequestBody,sbResponseBody);
if (success != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
wprintf(L"Response Status Code = %d\n",rest.get_ResponseStatusCode());
// Check for a success response.
if (rest.get_ResponseStatusCode() != 204) {
wprintf(L"%s\n",sbResponseBody.getAsString());
wprintf(L"Failed.\n");
return;
}
// For a success response, the response body is empty.
wprintf(L"Success.\n");
}