Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

PayPal: List merchant invoices across pages

See more PayPal Examples

List invoices for the authenticated merchant. Request totals, iterate every page, and keep the PayPal ID alongside the human-readable number and current status.

PayPal documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Integer iPage
    Integer iLastPage
    Handle hoUrl
    Handle hoErrorInfo
    Handle hoResult
    Boolean iParsed
    Integer i
    Integer iCount
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1
    Integer iTemp2

    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

    //  Request total_pages so a merchant with more than one page sees every invoice.
    Move 1 To iPage
    Move 1 To iLastPage
    While (iPage <= iLastPage)
        Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
        If (Not(IsComObjectCreated(hoUrl))) Begin
            Send CreateComObject of hoUrl
        End
        Get ComAppend Of hoUrl "https://api-m.sandbox.paypal.com/v2/invoicing/invoices?total_required=true&page_size=20&page=" To iSuccess
        Get ComAppendInt Of hoUrl iPage To iSuccess
        Get ComGetAsString Of hoUrl To sTemp1
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" sTemp1 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

        Move 0 To i
        Get ComSizeOfArray Of hoResult "items" To iCount
        While (i < iCount)
            Set ComI Of hoResult To i
            Get ComStringOf Of hoResult "items[i].id" To sTemp1
            Get ComStringOf Of hoResult "items[i].detail.invoice_number" To sTemp2
            Get ComStringOf Of hoResult "items[i].status" To sTemp3
            Showln sTemp1 "  " sTemp2 "  " sTemp3
            Move (i + 1) To i
        Loop

        Get ComIntOf Of hoResult "total_pages" To iLastPage
        Move (iPage + 1) To iPage
    Loop



End_Procedure