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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_ErrorInfo
oleobject loo_Result
long li_ParsedResult
string ls_PaymentId
long li_Matched
long i
oleobject loo_RemoveError

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_Http.SetUrlVar("id","INVOICE_ID")

li_Success = loo_Http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loo_Resp)
if li_Success <> 1 then
    Write-Debug "No HTTP response: " + loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Resp
    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_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_ErrorInfo
    destroy loo_Result
    return
end if

ls_PaymentId = "EXTERNAL_PAYMENT_ID"
li_Matched = 0
i = 0
do while (i < loo_Result.SizeOfArray("payments.transactions"))
    loo_Result.I = i
    if (loo_Result.StringOfEquals("payments.transactions[i].payment_id",ls_PaymentId,1) = 1) AND (loo_Result.StringOfEquals("payments.transactions[i].type","EXTERNAL",1) = 1) then
        li_Matched = 1
    end if

    i = i + 1
loop
if li_Matched <> 1 then
    Write-Debug "This invoice has no matching external payment record."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

loo_Http.SetUrlVar("paymentId",ls_PaymentId)
li_Success = loo_Http.HttpNoBody("DELETE","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}",loo_Resp)
if li_Success <> 1 then
    Write-Debug "No HTTP response: " + loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Resp
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

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

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

Write-Debug "External payment entry removed."


destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_RemoveError