Dart Requires Chilkat v11.0.0+
Dart
PayPal: Start a PayPal wallet checkout
See more PayPal Examples
Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.
Create a USD 30 digital order from a trusted cart. Store the order ID and redirect the buyer to the payer-action link. After approval, capture this same order from your merchant application.
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 two callback URLs with routes on your site. This order contains no
// physical shipment.
// 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);
http.setRequestHeader('Prefer', 'return=representation');
final body = CkJsonObject();
body.updateString('intent', 'CAPTURE');
body.updateString('purchase_units[0].amount.currency_code', 'USD');
body.updateString('purchase_units[0].amount.value', '30.00');
body.updateString('payment_source.paypal.experience_context.return_url', 'https://merchant.example/paypal/return');
body.updateString('payment_source.paypal.experience_context.cancel_url', 'https://merchant.example/paypal/cancel');
body.updateString('payment_source.paypal.experience_context.shipping_preference', 'NO_SHIPPING');
try {
http.httpJson('POST', 'https://api-m.sandbox.paypal.com/v2/checkout/orders', 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('ID: ${result.stringOf('id')}');
print('Status: ${result.stringOf('status')}');
// Link order is not fixed. Send the buyer to the matching URL in a browser.
var linkIndex = 0;
while (linkIndex < result.sizeOfArray('links')) {
result.i = linkIndex;
if (result.stringOfEquals('links[i].rel', 'payer-action', true)) {
print('Approval URL: ${result.stringOf('links[i].href')}');
}
linkIndex++;
}
}