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

AutoIt
Local $bSuccess = 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.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
$oResp = ObjCreate("Chilkat.HttpResponse")

$oHttp.SetUrlVar("id","INVOICE_ID")

$bSuccess = $oHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",$oResp)
If ($bSuccess <> True) Then
    ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
    Exit
EndIf

If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    $oErrorInfo = ObjCreate("Chilkat.JsonObject")
    $oErrorInfo.Load($oResp.BodyStr)
    ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oErrorInfo.StringOf("name") & " " & $oErrorInfo.StringOf("details[0].issue") & @CRLF)
    ConsoleWrite("PayPal debug ID: " & $oErrorInfo.StringOf("debug_id") & @CRLF)
    Exit
EndIf

$oResult = ObjCreate("Chilkat.JsonObject")
Local $bParsedResult = $oResult.Load($oResp.BodyStr)
If ($bParsedResult <> True) Then
    ConsoleWrite("Expected a JSON response; HTTP " & $oResp.StatusCode & @CRLF)
    Exit
EndIf

Local $sPaymentId = "EXTERNAL_PAYMENT_ID"
Local $bMatched = False
Local $i = 0
While ($i < $oResult.SizeOfArray("payments.transactions"))
    $oResult.I = $i
    If (($oResult.StringOfEquals("payments.transactions[i].payment_id",$sPaymentId,True) = True) And ($oResult.StringOfEquals("payments.transactions[i].type","EXTERNAL",True) = True)) Then
        $bMatched = True
    EndIf

    $i = $i + 1
Wend
If ($bMatched <> True) Then
    ConsoleWrite("This invoice has no matching external payment record." & @CRLF)
    Exit
EndIf

$oHttp.SetUrlVar("paymentId",$sPaymentId)
$bSuccess = $oHttp.HttpNoBody("DELETE","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}",$oResp)
If ($bSuccess <> True) Then
    ConsoleWrite("No HTTP response: " & $oHttp.LastErrorText & @CRLF)
    Exit
EndIf

If (($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    $oRemoveError = ObjCreate("Chilkat.JsonObject")
    $oRemoveError.Load($oResp.BodyStr)
    ConsoleWrite("HTTP " & $oResp.StatusCode & " " & $oRemoveError.StringOf("name") & " " & $oRemoveError.StringOf("details[0].issue") & @CRLF)
    ConsoleWrite("PayPal debug ID: " & $oRemoveError.StringOf("debug_id") & @CRLF)
    Exit
EndIf

ConsoleWrite("External payment entry removed." & @CRLF)