Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.HttpCurl;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

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

        HttpCurl curl = new HttpCurl();
        StringBuilder cmd = new StringBuilder();
        JsonObject json = new JsonObject();

        String subscriptionId = "sub_FROM_ACCOUNT";

        String itemId = "si_SELECTED_ITEM";

        String priceId = "price_FROM_CATALOG";

        DateTime now = new DateTime();
        now.setFromCurrentSystemTime();
        String 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@\"");
        int 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());
        } catch (ChilkatException e) {
            System.out.println("No HTTP response. Chilkat failure code: " + curl.getFailReason());
            return;
        }

        try {
            curl.getResponseJson(json);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((curl.getStatusCode() != 200) || (!success)) {
            System.out.println("Stripe HTTP " + curl.getStatusCode() + "  Request-Id: " + curl.getResponseHeaderField("Request-Id"));
            if (success) {
                System.out.println(json.stringOf("error.message"));
            }

            return;
        }

        System.out.println("total = " + json.stringOf("total"));
        System.out.println("currency = " + json.stringOf("currency"));

        System.out.println("Save this preview time with the selected changes: " + prorationDate);
        int i = 0;
        while (i < json.sizeOfArray("lines.data")) {
            json.setI(i);
            if (json.boolOf("lines.data[i].parent.subscription_item_details.proration")) {
                System.out.println(json.stringOf("lines.data[i].description") + "  " + json.stringOf("lines.data[i].amount"));
            }

            i = i + 1;
        }

        if (json.boolOf("lines.has_more")) {
            System.out.println("Only the first page of preview lines is shown. The total covers all lines.");
        }
    }
}