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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Body
oleobject loo_ErrorInfo
oleobject loo_Result
long li_Parsed
oleobject loo_Png
long li_Decoded
long li_SavedImage

li_Success = 0

//  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.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.AuthToken = "SANDBOX_ACCESS_TOKEN"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

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

loo_Http.SetUrlVar("id","INVOICE_ID")
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateInt("width",300)
loo_Body.UpdateInt("height",300)
loo_Body.UpdateString("action","pay")

li_Success = loo_Http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code",loo_Body,"application/json",loo_Resp)
if li_Success <> 1 then
    Write-Debug "No HTTP response: " + loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    return
end if

if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    loo_ErrorInfo = create oleobject
    li_rc = loo_ErrorInfo.ConnectToNewObject("Chilkat.JsonObject")

    loo_ErrorInfo.Load(loo_Resp.BodyStr)
    Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_ErrorInfo.StringOf("name") + " " + loo_ErrorInfo.StringOf("details[0].issue")
    Write-Debug "PayPal debug ID: " + loo_ErrorInfo.StringOf("debug_id")
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    destroy loo_ErrorInfo
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Result.Load(loo_Resp.BodyStr)
if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

loo_Png = create oleobject
li_rc = loo_Png.ConnectToNewObject("Chilkat.BinData")

if loo_Result.HasMember("image") <> 1 then
    Write-Debug "QR response did not contain an image."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    destroy loo_ErrorInfo
    destroy loo_Result
    destroy loo_Png
    return
end if

li_Decoded = loo_Png.AppendEncoded(loo_Result.StringOf("image"),"base64")
if li_Decoded <> 1 then
    Write-Debug "Invalid base64 image."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    destroy loo_ErrorInfo
    destroy loo_Result
    destroy loo_Png
    return
end if

li_SavedImage = loo_Png.WriteFile("paypal-invoice-qr.png")
if li_SavedImage <> 1 then
    Write-Debug "Could not write the invoice QR image."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    destroy loo_ErrorInfo
    destroy loo_Result
    destroy loo_Png
    return
end if

Write-Debug "Saved paypal-invoice-qr.png."


destroy loo_Http
destroy loo_Resp
destroy loo_Body
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Png