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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final http = CkHttp();
  http.authToken = 'SANDBOX_ACCESS_TOKEN';
  final resp = CkHttpResponse();

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

  try {
    http.httpNoBody('GET', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{\$id}', resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

  if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
    final errorInfo = CkJsonObject();
    errorInfo.load(resp.bodyStr);
    print('HTTP ${resp.statusCode} ${errorInfo.stringOf('name')} ${errorInfo.stringOf('details[0].issue')}');
    print('PayPal debug ID: ${errorInfo.stringOf('debug_id')}');
    return;
  }

  final result = CkJsonObject();
  try {
    result.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON response; HTTP ${resp.statusCode}');
    return;
  }

  final paymentId = 'EXTERNAL_PAYMENT_ID';
  var matched = false;
  var i = 0;
  while (i < result.sizeOfArray('payments.transactions')) {
    result.i = i;
    if ((result.stringOfEquals('payments.transactions[i].payment_id', paymentId, true)) && (result.stringOfEquals('payments.transactions[i].type', 'EXTERNAL', true))) {
      matched = true;
    }

    i++;
  }

  if (!matched) {
    print('This invoice has no matching external payment record.');
    return;
  }

  http.setUrlVar('paymentId', paymentId);
  try {
    http.httpNoBody('DELETE', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{\$id}/payments/{\$paymentId}', resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

  if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
    final removeError = CkJsonObject();
    removeError.load(resp.bodyStr);
    print('HTTP ${resp.statusCode} ${removeError.stringOf('name')} ${removeError.stringOf('details[0].issue')}');
    print('PayPal debug ID: ${removeError.stringOf('debug_id')}');
    return;
  }

  print('External payment entry removed.');
}