Java Stripe: Update a Coupon

Back to Index

Updates the metadata of a coupon. Other coupon details (currency, duration, amount_off) are, by design, not editable.

Documentation: https://stripe.com/docs/api/curl#update_coupon

CURL Command

curl https://api.stripe.com/v1/coupons/25OFF \
   -u STRIPE_SECRET_KEY: \
   -d metadata[order_id]=6735 \
   -X POST

Java Example

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRest rest = new CkRest();
    boolean success;

    //  URL: https://api.stripe.com/v1/coupons/25OFF
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("api.stripe.com",port,bTls,bAutoReconnect);
    if (success != true) {
        System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
        System.out.println(rest.lastErrorText());
        return;
        }

    rest.SetAuthBasic("STRIPE_SECRET_KEY","");

    rest.AddQueryParam("metadata[order_id]","6735");

    String strResponseBody = rest.fullRequestFormUrlEncoded("POST","/v1/coupons/25OFF");
    if (rest.get_LastMethodSuccess() != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.Load(strResponseBody);

    String id;
    String object;
    boolean amount_off;
    int created;
    boolean currency;
    String duration;
    int duration_in_months;
    boolean livemode;
    boolean max_redemptions;
    String metadataOrder_id;
    int percent_off;
    boolean redeem_by;
    int times_redeemed;
    boolean valid;

    id = jsonResponse.stringOf("id");
    object = jsonResponse.stringOf("object");
    amount_off = jsonResponse.IsNullOf("amount_off");
    created = jsonResponse.IntOf("created");
    currency = jsonResponse.IsNullOf("currency");
    duration = jsonResponse.stringOf("duration");
    duration_in_months = jsonResponse.IntOf("duration_in_months");
    livemode = jsonResponse.BoolOf("livemode");
    max_redemptions = jsonResponse.IsNullOf("max_redemptions");
    metadataOrder_id = jsonResponse.stringOf("metadata.order_id");
    percent_off = jsonResponse.IntOf("percent_off");
    redeem_by = jsonResponse.IsNullOf("redeem_by");
    times_redeemed = jsonResponse.IntOf("times_redeemed");
    valid = jsonResponse.BoolOf("valid");
  }
}

Sample JSON Response Body

{
  "id": "25OFF",
  "object": "coupon",
  "amount_off": null,
  "created": 1516662783,
  "currency": null,
  "duration": "repeating",
  "duration_in_months": 3,
  "livemode": false,
  "max_redemptions": null,
  "metadata": {
    "order_id": "6735"
  },
  "percent_off": 25,
  "redeem_by": null,
  "times_redeemed": 0,
  "valid": true
}