Dart Requires Chilkat v11.0.0+
Dart
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
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('Select an unsent draft.');
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
final now = CkDateTime();
now.setFromCurrentSystemTime();
final stamp = CkStringBuilder();
stamp.append(result.stringOf('detail.invoice_date'));
stamp.append('T00:00:00Z');
final issue = CkDateTime();
var validDate = true;
try {
issue.setFromTimestamp(stamp.getAsString());
} on ChilkatException {
validDate = false;
}
if ((!validDate) || (now.diffSeconds(issue) < 0)) {
print('Select a draft with an issue date of today or earlier.');
return;
}
final body = CkJsonObject();
body.updateBool('send_to_recipient', false);
body.updateBool('send_to_invoicer', false);
try {
http.httpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{\$id}/send', body, 'application/json', resp);
} on ChilkatException catch (e) {
print('No HTTP response: ${e.lastErrorText}');
return;
}
if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
final sendError = CkJsonObject();
sendError.load(resp.bodyStr);
print('HTTP ${resp.statusCode} ${sendError.stringOf('name')} ${sendError.stringOf('details[0].issue')}');
print('PayPal debug ID: ${sendError.stringOf('debug_id')}');
return;
}
// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
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 invoiceError = CkJsonObject();
invoiceError.load(resp.bodyStr);
print('HTTP ${resp.statusCode} ${invoiceError.stringOf('name')} ${invoiceError.stringOf('details[0].issue')}');
print('PayPal debug ID: ${invoiceError.stringOf('debug_id')}');
return;
}
final invoice = CkJsonObject();
try {
invoice.load(resp.bodyStr);
} on ChilkatException {
print('Expected a JSON response; HTTP ${resp.statusCode}');
return;
}
if (((invoice.stringOfEquals('status', 'SENT', true)) || ((invoice.stringOfEquals('status', 'UNPAID', true)) || (invoice.stringOfEquals('status', 'SHARED', true)))) && (invoice.hasMember('detail.metadata.recipient_view_url'))) {
print('Customer payment page: ${invoice.stringOf('detail.metadata.recipient_view_url')}');
return;
}
print('Invoice is ${invoice.stringOf('status')}. Retrieve it again when payable.');
}