Lua Requires Chilkat v11.0.0+
Lua
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.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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.
local http = chilkat.Http.new()
http.auth_token = "SANDBOX_ACCESS_TOKEN"
local resp = chilkat.HttpResponse.new()
-- Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
http:set_url_var("id", "INVOICE_ID")
local body = chilkat.JsonObject.new()
body:update_int("width", 300)
body:update_int("height", 300)
body:update_string("action", "pay")
local success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code", body, "application/json", resp)
if not success then
print("No HTTP response: " .. http.last_error_text)
return
end
if (resp.status_code < 200) or (resp.status_code >= 300) then
local errorInfo = chilkat.JsonObject.new()
errorInfo:load(resp.body_str)
print("HTTP " .. resp.status_code .. " " .. errorInfo:string_of("name") .. " " .. errorInfo:string_of("details[0].issue"))
print("PayPal debug ID: " .. errorInfo:string_of("debug_id"))
return
end
local result = chilkat.JsonObject.new()
local parsed = result:load(resp.body_str)
if not parsed then
print("Expected a JSON response; HTTP " .. resp.status_code)
return
end
local png = chilkat.BinData.new()
if not result:has_member("image") then
print("QR response did not contain an image.")
return
end
local decoded = png:append_encoded(result:string_of("image"), "base64")
if not decoded then
print("Invalid base64 image.")
return
end
local savedImage = png:write_file("paypal-invoice-qr.png")
if not savedImage then
print("Could not write the invoice QR image.")
return
end
print("Saved paypal-invoice-qr.png.")