Unicode C
Unicode C
PayPal Generate QR Code
See more PayPal Examples
Generates a QR code for an invoice, by ID.The QR code is a PNG image in Base64-encoded format that corresponds to the invoice ID.
Note: A QR code can only be generated for an invoice that has been both created and sent.
See also Generate QR Code
Chilkat Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkRestW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW jsonToken;
HCkStringBuilderW sbAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkStringBuilderW sbPath;
int numReplacements;
HCkStringBuilderW sbJsonResponse;
HCkJsonObjectW json;
HCkBinDataW pngData;
success = FALSE;
// Note: Requires Chilkat v9.5.0.64 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)
jsonToken = CkJsonObjectW_Create();
CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/paypal.json");
// Build the Authorization request header field value.
sbAuth = CkStringBuilderW_Create();
// token_type should be "Bearer"
CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"token_type"));
CkStringBuilderW_Append(sbAuth,L" ");
CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,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.
rest = CkRestW_Create();
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"api.sandbox.paypal.com",443,TRUE,bAutoReconnect);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkJsonObjectW_Dispose(jsonToken);
CkStringBuilderW_Dispose(sbAuth);
CkRestW_Dispose(rest);
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 a path using an invoice ID.
sbPath = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbPath,L"/v1/invoicing/invoices/invoice_id/qr-code");
numReplacements = CkStringBuilderW_Replace(sbPath,L"invoice_id",L"INV2-ZG2H-HKAW-PMWU-N6ZR");
CkRestW_AddHeader(rest,L"Authorization",CkStringBuilderW_getAsString(sbAuth));
// A 500x500 image results in a PNG file of approx. 4K bytes in size.
CkRestW_AddQueryParam(rest,L"width",L"500");
CkRestW_AddQueryParam(rest,L"height",L"500");
CkRestW_AddQueryParam(rest,L"action",L"pay");
// Send the GET request and get the PNG image data as base64.
// A successful JSON response looks like this:
// {"image":"iVBORw0KGgoAA....AASUVORK5CYII="}
sbJsonResponse = CkStringBuilderW_Create();
success = CkRestW_FullRequestNoBodySb(rest,L"GET",CkStringBuilderW_getAsString(sbPath),sbJsonResponse);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkJsonObjectW_Dispose(jsonToken);
CkStringBuilderW_Dispose(sbAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkStringBuilderW_Dispose(sbJsonResponse);
return;
}
json = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(json,FALSE);
CkJsonObjectW_LoadSb(json,sbJsonResponse);
wprintf(L"Response Status Code = %d\n",CkRestW_getResponseStatusCode(rest));
// Did we get a 200 success response?
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"%s\n",CkJsonObjectW_emit(json));
wprintf(L"Failed.\n");
CkJsonObjectW_Dispose(jsonToken);
CkStringBuilderW_Dispose(sbAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkStringBuilderW_Dispose(sbJsonResponse);
CkJsonObjectW_Dispose(json);
return;
}
// If the 200 response is received, the response body contains the PNG data.
// Save the PNG to a file.
pngData = CkBinDataW_Create();
CkBinDataW_AppendEncoded(pngData,CkJsonObjectW_stringOf(json,L"image"),L"base64");
success = CkBinDataW_WriteFile(pngData,L"qa_output/paypal_qr_code.png");
if (success == FALSE) {
wprintf(L"Failed to save PNG file.\n");
CkJsonObjectW_Dispose(jsonToken);
CkStringBuilderW_Dispose(sbAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkStringBuilderW_Dispose(sbJsonResponse);
CkJsonObjectW_Dispose(json);
CkBinDataW_Dispose(pngData);
return;
}
wprintf(L"Success.\n");
CkJsonObjectW_Dispose(jsonToken);
CkStringBuilderW_Dispose(sbAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbPath);
CkStringBuilderW_Dispose(sbJsonResponse);
CkJsonObjectW_Dispose(json);
CkBinDataW_Dispose(pngData);
}