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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Body
oleobject loo_Day
long li_Page
long li_LastPage
oleobject loo_Url
oleobject loo_ErrorInfo
oleobject loo_Result
long li_ParsedResult
long i
long li_Count

li_Success = 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.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.AuthToken = "SANDBOX_ACCESS_TOKEN"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("status[0]","SENT")
loo_Body.UpdateString("status[1]","PARTIALLY_PAID")
loo_Body.UpdateString("status[2]","UNPAID")
loo_Body.UpdateString("status[3]","SHARED")
//  Yesterday is the last overdue date. Include invoices due in the past 90 days.
loo_Day = create oleobject
li_rc = loo_Day.ConnectToNewObject("Chilkat.CkDateTime")

loo_Day.SetFromCurrentSystemTime()
loo_Day.AddDays(-1)
loo_Body.UpdateString("due_date_range.end",loo_Day.GetAsIso8601("YYYY-MM-DD",0))
loo_Day.AddDays(-89)
loo_Body.UpdateString("due_date_range.start",loo_Day.GetAsIso8601("YYYY-MM-DD",0))
li_Page = 1
li_LastPage = 1
do while (li_Page <= li_LastPage)
    loo_Url = create oleobject
    li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

    loo_Url.Append("https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=")
    loo_Url.AppendInt(li_Page)
    li_Success = loo_Http.HttpJson("POST",loo_Url.GetAsString(),loo_Body,"application/json",loo_Resp)
    if li_Success <> 1 then
        Write-Debug "No HTTP response: " + loo_Http.LastErrorText
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Body
        destroy loo_Day
        destroy loo_Url
        return
    end if

    if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
        loo_ErrorInfo = create oleobject
        li_rc = loo_ErrorInfo.ConnectToNewObject("Chilkat.JsonObject")

        loo_ErrorInfo.Load(loo_Resp.BodyStr)
        Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_ErrorInfo.StringOf("name") + " " + loo_ErrorInfo.StringOf("details[0].issue")
        Write-Debug "PayPal debug ID: " + loo_ErrorInfo.StringOf("debug_id")
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Body
        destroy loo_Day
        destroy loo_Url
        destroy loo_ErrorInfo
        return
    end if

    loo_Result = create oleobject
    li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

    li_ParsedResult = loo_Result.Load(loo_Resp.BodyStr)
    if li_ParsedResult <> 1 then
        Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Body
        destroy loo_Day
        destroy loo_Url
        destroy loo_ErrorInfo
        destroy loo_Result
        return
    end if

    i = 0
    li_Count = loo_Result.SizeOfArray("items")
    do while (i < li_Count)
        loo_Result.I = i
        Write-Debug loo_Result.StringOf("items[i].detail.invoice_number") + "  due " + loo_Result.StringOf("items[i].detail.payment_term.due_date") + "  " + loo_Result.StringOf("items[i].due_amount.value") + " " + loo_Result.StringOf("items[i].due_amount.currency_code")
        i = i + 1
    loop
    li_LastPage = loo_Result.IntOf("total_pages")
    li_Page = li_Page + 1
loop


destroy loo_Http
destroy loo_Resp
destroy loo_Body
destroy loo_Day
destroy loo_Url
destroy loo_ErrorInfo
destroy loo_Result