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

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loBody
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsed
LOCAL loPng
LOCAL lnDecoded
LOCAL lnSavedImage

lnSuccess = 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.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = CreateObject('Chilkat.HttpResponse')

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

loHttp.SetUrlVar("id","INVOICE_ID")
loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateInt("width",300)
loBody.UpdateInt("height",300)
loBody.UpdateString("action","pay")

lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code",loBody,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loErrorInfo = CreateObject('Chilkat.JsonObject')
    loErrorInfo.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("name") + " " + loErrorInfo.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loErrorInfo
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsed = loResult.Load(loResp.BodyStr)
IF (lnParsed <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

loPng = CreateObject('Chilkat.BinData')
IF (loResult.HasMember("image") <> 1) THEN
    ? "QR response did not contain an image."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loPng
    CANCEL
ENDIF

lnDecoded = loPng.AppendEncoded(loResult.StringOf("image"),"base64")
IF (lnDecoded <> 1) THEN
    ? "Invalid base64 image."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loPng
    CANCEL
ENDIF

lnSavedImage = loPng.WriteFile("paypal-invoice-qr.png")
IF (lnSavedImage <> 1) THEN
    ? "Could not write the invoice QR image."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loBody
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loPng
    CANCEL
ENDIF

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

RELEASE loHttp
RELEASE loResp
RELEASE loBody
RELEASE loErrorInfo
RELEASE loResult
RELEASE loPng