Dart Requires Chilkat v11.0.0+
Dart
PayPal: Delete an unsent draft invoice
See more PayPal Examples
This example deliberately checks for DRAFT before deletion. Use the invoice ID from your own records. A successful DELETE has no response body; use cancellation for a sent, unpaid invoice.
Chilkat Dart Downloads
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;
}
if (!result.stringOfEquals('status', 'DRAFT', true)) {
print('Leaving invoice unchanged: ${result.stringOf('status')}');
return;
}
try {
http.httpNoBody('DELETE', '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 deleteError = CkJsonObject();
deleteError.load(resp.bodyStr);
print('HTTP ${resp.statusCode} ${deleteError.stringOf('name')} ${deleteError.stringOf('details[0].issue')}');
print('PayPal debug ID: ${deleteError.stringOf('debug_id')}');
return;
}
print('Draft deleted; HTTP ${resp.statusCode}');
}