Sample code for 30+ languages & platforms
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 C++ Downloads

C++
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>
#include <CkBinData.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;

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

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

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

    CkBinData png;
    if (result.HasMember("image") != true) {
        std::cout << "QR response did not contain an image." << "\r\n";
        return;
    }

    bool decoded = png.AppendEncoded(result.stringOf("image"),"base64");
    if (decoded != true) {
        std::cout << "Invalid base64 image." << "\r\n";
        return;
    }

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

    std::cout << "Saved paypal-invoice-qr.png." << "\r\n";
    }