JavaScript Requires Chilkat v11.0.0+
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var 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.
var http = new CkHttp();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
var resp = new CkHttpResponse();
// Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
http.SetUrlVar("id","INVOICE_ID");
var body = new CkJsonObject();
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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var errorInfo = new CkJsonObject();
errorInfo.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
console.log("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
return;
}
var result = new CkJsonObject();
var parsed = result.Load(resp.BodyStr);
if (parsed !== true) {
console.log("Expected a JSON response; HTTP " + resp.StatusCode);
return;
}
var png = new CkBinData();
if (result.HasMember("image") !== true) {
console.log("QR response did not contain an image.");
return;
}
var decoded = png.AppendEncoded(result.StringOf("image"),"base64");
if (decoded !== true) {
console.log("Invalid base64 image.");
return;
}
var savedImage = png.WriteFile("paypal-invoice-qr.png");
if (savedImage !== true) {
console.log("Could not write the invoice QR image.");
return;
}
console.log("Saved paypal-invoice-qr.png.");