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

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oBody
LOCAL oDay
LOCAL nPage
LOCAL nLastPage
LOCAL oUrl
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsedResult
LOCAL i
LOCAL nCount

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")
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("status[0]", "SENT")
oBody:UpdateString("status[1]", "PARTIALLY_PAID")
oBody:UpdateString("status[2]", "UNPAID")
oBody:UpdateString("status[3]", "SHARED")
//  Yesterday is the last overdue date. Include invoices due in the past 90 days.
oDay := CreateObject("Chilkat.CkDateTime")
oDay:SetFromCurrentSystemTime()
oDay:AddDays(-1)
oBody:UpdateString("due_date_range.end", oDay:GetAsIso8601("YYYY-MM-DD", 0))
oDay:AddDays(-89)
oBody:UpdateString("due_date_range.start", oDay:GetAsIso8601("YYYY-MM-DD", 0))
nPage := 1
nLastPage := 1
DO WHILE (nPage <= nLastPage)
    oUrl := CreateObject("Chilkat.StringBuilder")
    oUrl:Append("https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=")
    oUrl:AppendInt(nPage)
    nSuccess := oHttp:HttpJson("POST", oUrl:GetAsString(), oBody, "application/json", oResp)
    IF (nSuccess != 1)
        ? "No HTTP response: " + oHttp:LastErrorText
        oHttp:destroy()
        oResp:destroy()
        oBody:destroy()
        oDay:destroy()
        oUrl: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()
        oDay:destroy()
        oUrl:destroy()
        oErrorInfo:destroy()
        RETURN
    ENDIF

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

    i := 0
    nCount := oResult:SizeOfArray("items")
    DO WHILE (i < nCount)
        oResult:I := i
        ? oResult:StringOf("items[i].detail.invoice_number") + "  due " + oResult:StringOf("items[i].detail.payment_term.due_date") + "  " + oResult:StringOf("items[i].due_amount.value") + " " + oResult:StringOf("items[i].due_amount.currency_code")
        i := i + 1
    ENDDO
    nLastPage := oResult:IntOf("total_pages")
    nPage := nPage + 1
ENDDO

oHttp:destroy()
oResp:destroy()
oBody:destroy()
oDay:destroy()
oUrl:destroy()
oErrorInfo:destroy()
oResult:destroy()