Android™ Stripe: Create a Coupon

Back to Index

Creates a coupon object.

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

CURL Command

curl https://api.stripe.com/v1/coupons \
   -u STRIPE_SECRET_KEY: \
   -d percent_off=25 \
   -d duration=repeating \
   -d duration_in_months=3 \
   -d id=25OFF \
   -X POST

Android™ Example

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CkRest rest = new CkRest();
    boolean success;

    //  URL: https://api.stripe.com/v1/coupons
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("api.stripe.com",port,bTls,bAutoReconnect);
    if (success != true) {
        Log.i(TAG, "ConnectFailReason: " + String.valueOf(rest.get_ConnectFailReason()));
        Log.i(TAG, rest.lastErrorText());
        return;
        }

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

    rest.AddQueryParam("percent_off","25");
    rest.AddQueryParam("duration","repeating");
    rest.AddQueryParam("duration_in_months","3");
    rest.AddQueryParam("id","25OFF");

    String strResponseBody = rest.fullRequestFormUrlEncoded("POST","/v1/coupons");
    if (rest.get_LastMethodSuccess() != true) {
        Log.i(TAG, 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;
    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");
    percent_off = jsonResponse.IntOf("percent_off");
    redeem_by = jsonResponse.IsNullOf("redeem_by");
    times_redeemed = jsonResponse.IntOf("times_redeemed");
    valid = jsonResponse.BoolOf("valid");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

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": {},
  "percent_off": 25,
  "redeem_by": null,
  "times_redeemed": 0,
  "valid": true
}