Lua Requires Chilkat v11.0.0+
Lua
PayPal: Record an external refund against an invoice
See more PayPal Examples
Record USD 5 already returned by bank transfer for a paid invoice. This call does not move money. To refund a PayPal capture, use the Payments v2 capture refund example instead.
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()
-- Record only money already returned outside PayPal. Reconcile first if a previous
-- attempt timed out.
http:set_url_var("id", "INVOICE_ID")
local body = chilkat.JsonObject.new()
body:update_string("method", "BANK_TRANSFER")
body:update_string("amount.currency_code", "USD")
body:update_string("amount.value", "5.00")
-- Use the actual date of the transfer; this example records one made today.
local date = chilkat.DateTime.new()
date:set_from_current_system_time()
body:update_string("refund_date", date:get_as_iso8601("YYYY-MM-DD", false))
local success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/refunds", 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 parsed = result:load(resp.body_str)
if not parsed then
print("Expected a JSON response; HTTP " .. resp.status_code)
return
end
print("Record ID: " .. result:string_of("refund_id"))