Lua Requires Chilkat v11.0.0+
Lua
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.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local success = false
-- 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.
local http = chilkat.Http.new()
http.auth_token = "SANDBOX_ACCESS_TOKEN"
local resp = chilkat.HttpResponse.new()
local body = chilkat.JsonObject.new()
body:update_string("status[0]", "SENT")
body:update_string("status[1]", "PARTIALLY_PAID")
body:update_string("status[2]", "UNPAID")
body:update_string("status[3]", "SHARED")
-- Yesterday is the last overdue date. Include invoices due in the past 90 days.
local day = chilkat.DateTime.new()
day:set_from_current_system_time()
day:add_days(-1)
body:update_string("due_date_range.end", day:get_as_iso8601("YYYY-MM-DD", false))
day:add_days(-89)
body:update_string("due_date_range.start", day:get_as_iso8601("YYYY-MM-DD", false))
local page = 1
local lastPage = 1
while page <= lastPage do
local url = chilkat.StringBuilder.new()
url:append("https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=")
url:append_int(page)
success = http:http_json("POST", url:get_as_string(), body, "application/json", resp)
if not success then
print("No HTTP response: " .. http.last_error_text)
return
end
if (resp.status_code < 200) or (resp.status_code >= 300) then
local errorInfo = chilkat.JsonObject.new()
errorInfo:load(resp.body_str)
print("HTTP " .. resp.status_code .. " " .. errorInfo:string_of("name") .. " " .. errorInfo:string_of("details[0].issue"))
print("PayPal debug ID: " .. errorInfo:string_of("debug_id"))
return
end
local result = chilkat.JsonObject.new()
local parsedResult = result:load(resp.body_str)
if not parsedResult then
print("Expected a JSON response; HTTP " .. resp.status_code)
return
end
local i = 0
local count = result:size_of_array("items")
while i < count do
result.i = i
print(result:string_of("items[i].detail.invoice_number") .. " due " .. result:string_of("items[i].detail.payment_term.due_date") .. " " .. result:string_of("items[i].due_amount.value") .. " " .. result:string_of("items[i].due_amount.currency_code"))
i = i + 1
end
lastPage = result:int_of("total_pages")
page = page + 1
end