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

AutoIt
Local $bSuccess = 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.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
$oResp = ObjCreate("Chilkat.HttpResponse")

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

$oHttp.SetUrlVar("id","INVOICE_ID")
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateInt("width",300)
$oBody.UpdateInt("height",300)
$oBody.UpdateString("action","pay")

$bSuccess = $oHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code",$oBody,"application/json",$oResp)
If ($bSuccess <> True) Then
    ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
    Exit
EndIf

If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    $oErrorInfo = ObjCreate("Chilkat.JsonObject")
    $oErrorInfo.Load($oResp.BodyStr)
    ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("name") & " " & $oErrorInfo.StringOf("details[0].issue") & @CRLF)
    ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
    Exit
EndIf

$oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oResult.Load($oResp.BodyStr)
If ($bParsed <> True) Then
    ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
    Exit
EndIf

$oPng = ObjCreate("Chilkat.BinData")
If ($oResult.HasMember("image") <> True) Then
    ConsoleWrite("QR response did not contain an image." & @CRLF)
    Exit
EndIf

Local $bDecoded = $oPng.AppendEncoded($oResult.StringOf("image"),"base64")
If ($bDecoded <> True) Then
    ConsoleWrite("Invalid base64 image." & @CRLF)
    Exit
EndIf

Local $bSavedImage = $oPng.WriteFile("paypal-invoice-qr.png")
If ($bSavedImage <> True) Then
    ConsoleWrite("Could not write the invoice QR image." & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved paypal-invoice-qr.png." & @CRLF)