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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Variant vBody
    Handle hoBody
    Handle hoErrorInfo
    Handle hoResult
    Boolean iParsed
    Handle hoPng
    Boolean iDecoded
    Boolean iSavedImage
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComAuthToken Of hoHttp To "SANDBOX_ACCESS_TOKEN"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

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

    Get ComSetUrlVar Of hoHttp "id" "INVOICE_ID" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateInt Of hoBody "width" 300 To iSuccess
    Get ComUpdateInt Of hoBody "height" 300 To iSuccess
    Get ComUpdateString Of hoBody "action" "pay" To iSuccess

    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code" vBody "application/json" vResp To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln "No HTTP response: " sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Get ComStatusCode Of hoResp To iTemp2
    If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoErrorInfo
        If (Not(IsComObjectCreated(hoErrorInfo))) Begin
            Send CreateComObject of hoErrorInfo
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoErrorInfo sTemp1 To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        Get ComStringOf Of hoErrorInfo "name" To sTemp1
        Get ComStringOf Of hoErrorInfo "details[0].issue" To sTemp2
        Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
        Get ComStringOf Of hoErrorInfo "debug_id" To sTemp1
        Showln "PayPal debug ID: " sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoResult sTemp1 To iParsed
    If (iParsed <> True) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Expected a JSON response; HTTP " iTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoPng
    If (Not(IsComObjectCreated(hoPng))) Begin
        Send CreateComObject of hoPng
    End
    Get ComHasMember Of hoResult "image" To bTemp1
    If (bTemp1 <> True) Begin
        Showln "QR response did not contain an image."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "image" To sTemp1
    Get ComAppendEncoded Of hoPng sTemp1 "base64" To iDecoded
    If (iDecoded <> True) Begin
        Showln "Invalid base64 image."
        Procedure_Return
    End

    Get ComWriteFile Of hoPng "paypal-invoice-qr.png" To iSavedImage
    If (iSavedImage <> True) Begin
        Showln "Could not write the invoice QR image."
        Procedure_Return
    End

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


End_Procedure