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

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oBody
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed
LOCAL oPng
LOCAL nDecoded
LOCAL nSavedImage

nSuccess := 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.
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := "SANDBOX_ACCESS_TOKEN"
oResp := CreateObject("Chilkat.HttpResponse")

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

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

nSuccess := oHttp:HttpJson("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code", oBody, "application/json", oResp)
IF (nSuccess != 1)
    ? "No HTTP response: " + oHttp:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    RETURN
ENDIF

IF ((oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    oErrorInfo := CreateObject("Chilkat.JsonObject")
    oErrorInfo:Load(oResp:BodyStr)
    ? "HTTP " + Str(oResp:StatusCode) + " " + oErrorInfo:StringOf("name") + " " + oErrorInfo:StringOf("details[0].issue")
    ? "PayPal debug ID: " + oErrorInfo:StringOf("debug_id")
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oErrorInfo:destroy()
    RETURN
ENDIF

oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF (nParsed != 1)
    ? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    RETURN
ENDIF

oPng := CreateObject("Chilkat.BinData")
IF (oResult:HasMember("image") != 1)
    ? "QR response did not contain an image."
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    oPng:destroy()
    RETURN
ENDIF

nDecoded := oPng:AppendEncoded(oResult:StringOf("image"), "base64")
IF (nDecoded != 1)
    ? "Invalid base64 image."
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    oPng:destroy()
    RETURN
ENDIF

nSavedImage := oPng:WriteFile("paypal-invoice-qr.png")
IF (nSavedImage != 1)
    ? "Could not write the invoice QR image."
    oHttp:destroy()
    oResp:destroy()
    oBody:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    oPng:destroy()
    RETURN
ENDIF

? "Saved paypal-invoice-qr.png."

oHttp:destroy()
oResp:destroy()
oBody:destroy()
oErrorInfo:destroy()
oResult:destroy()
oPng:destroy()