Lua Requires Chilkat v11.0.0+
Lua
PayPal: Show available, withheld and total balances by currency
See more PayPal Examples
Read the merchant balance snapshot using reporting access. Keep each currency separate and show the last refresh time so accounting knows when PayPal last updated the figures.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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 success = http:http_no_body("GET", "https://api-m.sandbox.paypal.com/v1/reporting/balances", 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
print("Last refresh: " .. result:string_of("last_refresh_time"))
local i = 0
while i < result:size_of_array("balances") do
result.i = i
print(result:string_of("balances[i].currency") .. " total " .. result:string_of("balances[i].total_balance.value") .. " available " .. result:string_of("balances[i].available_balance.value") .. " withheld " .. result:string_of("balances[i].withheld_balance.value"))
i = i + 1
end