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

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
errorInfo: TChilkatJsonObject;
result: TChilkatJsonObject;
parsedResult: Integer;
paymentId: WideString;
matched: Integer;
i: Integer;
removeError: TChilkatJsonObject;

begin
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.
http := TChilkatHttp.Create(Self);
http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
resp := TChilkatHttpResponse.Create(Self);

http.SetUrlVar('id','INVOICE_ID');

success := http.HttpNoBody('GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',resp.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + http.LastErrorText);
    Exit;
  end;
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    errorInfo := TChilkatJsonObject.Create(Self);
    errorInfo.Load(resp.BodyStr);
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
    Exit;
  end;

result := TChilkatJsonObject.Create(Self);
parsedResult := result.Load(resp.BodyStr);
if (parsedResult <> 1) then
  begin
    Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(resp.StatusCode));
    Exit;
  end;
paymentId := 'EXTERNAL_PAYMENT_ID';
matched := 0;
i := 0;
while (i < result.SizeOfArray('payments.transactions')) do
  begin
    result.I := i;
    if ((result.StringOfEquals('payments.transactions[i].payment_id',paymentId,1) = 1) and (result.StringOfEquals('payments.transactions[i].type','EXTERNAL',1) = 1)) then
      begin
        matched := 1;
      end;
    i := i + 1;
  end;

if (matched <> 1) then
  begin
    Memo1.Lines.Add('This invoice has no matching external payment record.');
    Exit;
  end;
http.SetUrlVar('paymentId',paymentId);
success := http.HttpNoBody('DELETE','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}',resp.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + http.LastErrorText);
    Exit;
  end;
if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
  begin
    removeError := TChilkatJsonObject.Create(Self);
    removeError.Load(resp.BodyStr);
    Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ' ' + removeError.StringOf('name') + ' ' + removeError.StringOf('details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + removeError.StringOf('debug_id'));
    Exit;
  end;
Memo1.Lines.Add('External payment entry removed.');