Dart Requires Chilkat v11.0.0+
Dart
PayPal: Create a reusable payment link for a service
See more PayPal Examples
Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.
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();
// Replace the thank-you URL with your business website. The same link can be shared
// with several customers.
// Create and persist this key once per business operation. On a timeout,
// retry the same request with its saved key, not a newly generated one.
final crypt = CkCrypt2();
final requestId = crypt.generateUuid();
http.setRequestHeader('PayPal-Request-Id', requestId);
final body = CkJsonObject();
body.updateString('integration_mode', 'LINK');
body.updateString('type', 'BUY_NOW');
body.updateString('reusable', 'MULTIPLE');
body.updateString('return_url', 'https://merchant.example/thank-you');
body.updateString('line_items[0].name', 'Annual inspection');
body.updateString('line_items[0].unit_amount.currency_code', 'USD');
body.updateString('line_items[0].unit_amount.value', '90.00');
try {
http.httpJson('POST', 'https://api-m.sandbox.paypal.com/v1/checkout/payment-resources', body, 'application/json', 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;
}
print('Catalog payment resource ID: ${result.stringOf('id')}');
// Link order is not fixed. Find the customer-facing link to copy into an email.
var linkIndex = 0;
while (linkIndex < result.sizeOfArray('links')) {
result.i = linkIndex;
if (result.stringOfEquals('links[i].rel', 'payment_link', true)) {
print('Share this payment link: ${result.stringOf('links[i].href')}');
}
linkIndex++;
}
}