Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

PayPal: Save a payable invoice QR code as a PNG

See more PayPal Examples

Generate a 300-pixel payment QR code for a sent, payable invoice. Decode the base64 image into a PNG for a printed invoice; the code opens the payment page, and is not proof of payment.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkBinDataW.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;

    //  Use a sent, payable invoice. Width and height are JSON numbers, in pixels.

    http.SetUrlVar(L"id",L"INVOICE_ID");
    CkJsonObjectW body;
    body.UpdateInt(L"width",300);
    body.UpdateInt(L"height",300);
    body.UpdateString(L"action",L"pay");

    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code",body,L"application/json",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 parsed = result.Load(resp.bodyStr());
    if (parsed != true) {
        wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
        return;
    }

    CkBinDataW png;
    if (result.HasMember(L"image") != true) {
        wprintf(L"QR response did not contain an image.\n");
        return;
    }

    bool decoded = png.AppendEncoded(result.stringOf(L"image"),L"base64");
    if (decoded != true) {
        wprintf(L"Invalid base64 image.\n");
        return;
    }

    bool savedImage = png.WriteFile(L"paypal-invoice-qr.png");
    if (savedImage != true) {
        wprintf(L"Could not write the invoice QR image.\n");
        return;
    }

    wprintf(L"Saved paypal-invoice-qr.png.\n");
    }