Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

PayPal: Remove an external payment recorded on the wrong invoice

See more PayPal Examples

Use the saved external payment record ID. Verify that it belongs to the selected invoice, then remove the bookkeeping entry. The bank transfer itself is unaffected. Record it on the correct invoice as a separate step.

PayPal documentation

Chilkat Lua Downloads

Lua
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()

http:set_url_var("id", "INVOICE_ID")

local success = http:http_no_body("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", 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 paymentId = "EXTERNAL_PAYMENT_ID"
local matched = false
local i = 0
while i < result:size_of_array("payments.transactions") do
    result.i = i
    if (result:string_of_equals("payments.transactions[i].payment_id", paymentId, true)) and (result:string_of_equals("payments.transactions[i].type", "EXTERNAL", true)) then
        matched = true
    end

    i = i + 1
end

if not matched then
    print("This invoice has no matching external payment record.")
    return
end

http:set_url_var("paymentId", paymentId)
success = http:http_no_body("DELETE", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}", 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 removeError = chilkat.JsonObject.new()
    removeError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. removeError:string_of("name") .. " " .. removeError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. removeError:string_of("debug_id"))
    return
end

print("External payment entry removed.")