Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

PayPal: Update the price on a reusable payment link

See more PayPal Examples

Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.

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', 'PAYMENT_RESOURCE_ID');

  try {
    http.httpNoBody('GET', 'https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{\$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.sizeOfArray('line_items') != 1) || ((!result.stringOfEquals('line_items[0].name', 'Annual inspection', true)) || (!result.stringOfEquals('line_items[0].unit_amount.currency_code', 'USD', true)))) {
    print('Select the USD inspection link from your catalog.');
    return;
  }

  // Preserve line items, taxes, shipping and checkout settings from the retrieved link.
  // Remove the resource metadata PayPal added to the GET response.
  if (result.hasMember('id')) {
    result.delete('id');
  }

  if (result.hasMember('create_time')) {
    result.delete('create_time');
  }

  if (result.hasMember('update_time')) {
    result.delete('update_time');
  }

  if (result.hasMember('status')) {
    result.delete('status');
  }

  if (result.hasMember('links')) {
    result.delete('links');
  }

  if (result.hasMember('payment_link')) {
    result.delete('payment_link');
  }

  result.updateString('line_items[0].unit_amount.value', '95.00');
  try {
    http.httpJson('PUT', 'https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{\$id}', result, 'application/json', resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

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

  print('The inspection price is now USD 95.00.');
}