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

Stripe: Preview a plan or seat change

See more Stripe Examples

Preview a seat or plan change and display its proration lines, total, and the timestamp to retain with the quote.

Stripe documentation and cURL example

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Requires an unlocked Chilkat bundle. See Global Unlock Sample.
  // Supply a restricted sandbox key from merchant-controlled configuration.
  final apiKey = 'YOUR_STRIPE_TEST_KEY';

  final curl = CkHttpCurl();
  final cmd = CkStringBuilder();
  final json = CkJsonObject();

  var replaced = 0;

  final subscriptionId = 'sub_FROM_ACCOUNT';

  final itemId = 'si_SELECTED_ITEM';

  final priceId = 'price_FROM_CATALOG';

  final now = CkDateTime();
  now.setFromCurrentSystemTime();
  final prorationDate = now.getAsUnixTimeStr(false);

  // curl -X POST "https://api.stripe.com/v1/invoices/create_preview" \
  //   -u "$STRIPE_API_KEY:" \
  //   -H "Stripe-Version: 2026-10-28.endive" \
  //   -d "subscription={subscriptionId}" \
  //   -d "subscription_details[items][0][id]={itemId}" \
  //   -d "subscription_details[items][0][price]={priceId}" \
  //   -d "subscription_details[items][0][quantity]=5" \
  //   -d "subscription_details[proration_behavior]=create_prorations" \
  //   -d "subscription_details[proration_date]={prorationDate}"
  cmd.setString('curl -X POST "https://api.stripe.com/v1/invoices/create_preview"');
  cmd.append(' -u "@KEY@:"');
  cmd.append(' -H "Stripe-Version: 2026-10-28.endive"');
  cmd.append(' -d "subscription=@SUB@"');
  cmd.append(' -d "subscription_details[items][0][id]=@ITEM@"');
  cmd.append(' -d "subscription_details[items][0][price]=@PRICE@"');
  cmd.append(' -d "subscription_details[items][0][quantity]=5"');
  cmd.append(' -d "subscription_details[proration_behavior]=create_prorations"');
  cmd.append(' -d "subscription_details[proration_date]=@DATE@"');
  replaced = cmd.replace('@SUB@', subscriptionId);
  replaced = cmd.replace('@ITEM@', itemId);
  replaced = cmd.replace('@PRICE@', priceId);
  replaced = cmd.replace('@DATE@', prorationDate);
  replaced = cmd.replace('@KEY@', apiKey);
  try {
    curl.doYourThing(cmd.getAsString());
  } on ChilkatException {
    print('No HTTP response. Chilkat failure code: ${curl.failReason}');
    return;
  }

  success = true;
  try {
    curl.getResponseJson(json);
  } on ChilkatException {
    success = false;
  }
  if ((curl.statusCode != 200) || (!success)) {
    print('Stripe HTTP ${curl.statusCode}  Request-Id: ${curl.getResponseHeaderField('Request-Id')}');
    if (success) {
      print(json.stringOf('error.message'));
    }

    return;
  }

  print('total = ${json.stringOf('total')}');
  print('currency = ${json.stringOf('currency')}');

  print('Save this preview time with the selected changes: $prorationDate');
  var i = 0;
  while (i < json.sizeOfArray('lines.data')) {
    json.i = i;
    if (json.boolOf('lines.data[i].parent.subscription_item_details.proration')) {
      print('${json.stringOf('lines.data[i].description')}  ${json.stringOf('lines.data[i].amount')}');
    }

    i++;
  }

  if (json.boolOf('lines.has_more')) {
    print('Only the first page of preview lines is shown. The total covers all lines.');
  }
}