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

PayPal: Build a worklist of overdue invoices

See more PayPal Examples

Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.

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 hoDay
    Integer iPage
    Integer iLastPage
    Handle hoUrl
    Handle hoErrorInfo
    Handle hoResult
    Boolean iParsedResult
    Integer i
    Integer iCount
    String sTemp1
    String sTemp2
    String sTemp3
    String sTemp4
    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
    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateString Of hoBody "status[0]" "SENT" To iSuccess
    Get ComUpdateString Of hoBody "status[1]" "PARTIALLY_PAID" To iSuccess
    Get ComUpdateString Of hoBody "status[2]" "UNPAID" To iSuccess
    Get ComUpdateString Of hoBody "status[3]" "SHARED" To iSuccess
    //  Yesterday is the last overdue date. Include invoices due in the past 90 days.
    Get Create (RefClass(cComCkDateTime)) To hoDay
    If (Not(IsComObjectCreated(hoDay))) Begin
        Send CreateComObject of hoDay
    End
    Get ComSetFromCurrentSystemTime Of hoDay To iSuccess
    Get ComAddDays Of hoDay -1 To iSuccess
    Get ComGetAsIso8601 Of hoDay "YYYY-MM-DD" False To sTemp1
    Get ComUpdateString Of hoBody "due_date_range.end" sTemp1 To iSuccess
    Get ComAddDays Of hoDay -89 To iSuccess
    Get ComGetAsIso8601 Of hoDay "YYYY-MM-DD" False To sTemp1
    Get ComUpdateString Of hoBody "due_date_range.start" sTemp1 To iSuccess
    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/search-invoices?total_required=true&page_size=50&page=" To iSuccess
        Get ComAppendInt Of hoUrl iPage To iSuccess
        Get ComGetAsString Of hoUrl To sTemp1
        Get pvComObject of hoBody to vBody
        Get pvComObject of hoResp to vResp
        Get ComHttpJson Of hoHttp "POST" sTemp1 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 iParsedResult
        If (iParsedResult <> 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].detail.invoice_number" To sTemp1
            Get ComStringOf Of hoResult "items[i].detail.payment_term.due_date" To sTemp2
            Get ComStringOf Of hoResult "items[i].due_amount.value" To sTemp3
            Get ComStringOf Of hoResult "items[i].due_amount.currency_code" To sTemp4
            Showln sTemp1 "  due " sTemp2 "  " sTemp3 " " sTemp4
            Move (i + 1) To i
        Loop

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



End_Procedure