Skip to content

Instantly share code, notes, and snippets.

@jimdanz
Last active December 23, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimdanz/6617901 to your computer and use it in GitHub Desktop.
Save jimdanz/6617901 to your computer and use it in GitHub Desktop.
/**
* Example usage of Stripe java bindings.
* See https://stripe.com/docs/libraries#java-library
*
* Usage:
* mkdir stripe-test
* cd stripe-test
* wget https://code.stripe.com/stripe-java-latest.jar
* wget http://google-gson.googlecode.com/files/google-gson-2.2.2-release.zip
* unzip google-gson-2.2.2-release.zip
* javac -classpath stripe-java-latest.jar StripeTest.java
* java -classpath stripe-java-latest.jar:google-gson-2.2.2/gson-2.2.2.jar:. StripeTest
*/
import java.util.Map;
import java.util.HashMap;
import com.stripe.model.Charge;
import com.stripe.Stripe;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.CardException;
import com.stripe.exception.APIException;
public class StripeTest {
public static void main(String[] args) throws AuthenticationException, InvalidRequestException,
APIConnectionException, CardException, APIException {
Stripe.apiKey = "tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I"; // stripe public
// test key
final Map<String, Object> cardParams = new HashMap<String, Object>();
cardParams.put("number", "4242424242424242");
cardParams.put("exp_month", 12);
cardParams.put("exp_year", 2015);
cardParams.put("cvc", "123");
cardParams.put("name", "J Bindings Cardholder");
cardParams.put("address_line1", "140 2nd Street");
cardParams.put("address_line2", "4th Floor");
cardParams.put("address_city", "San Francisco");
cardParams.put("address_zip", "94105");
cardParams.put("address_state", "CA");
cardParams.put("address_country", "USA");
final Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 100);
chargeParams.put("currency", "usd");
chargeParams.put("card", cardParams);
final Charge charge = Charge.create(chargeParams);
System.out.println(charge);
}
}
@remicito
Copy link

I am trying to run this code but i get this exception

  • java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 33 column 15

@harmeetsingh0013
Copy link

This exception will occur if we are using maven dependency 1.14. There are two ways to resolve this:

  1. use Maven dependencies 1.15:
    <dependency>
    <groupId>com.stripe</groupId>
    <artifactId>stripe-java</artifactId>
    <version>1.15.0</version>
    </dependency>
  2. Download latest Java API source code and use that project dependency in your test example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment