Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created January 8, 2013 16:24
Show Gist options
  • Save joshdholtz/4485154 to your computer and use it in GitHub Desktop.
Save joshdholtz/4485154 to your computer and use it in GitHub Desktop.
Using Protocol Android to retrieve Stripe credit card token with an async request
private void getStripeToken(String stripeApi, String cardNumber, String expMonth, String expYear, String cvc) {
// Sets basic auth header with Stripe API key and adds post params for number, exp month, exp year, and cvc
ParamsRequestData requestData = new ParamsRequestData();
requestData.addBearerAuthHeader(stripeApi);
requestData.addParam("card[number]", cardNumber);
requestData.addParam("card[exp_month]", expMonth);
requestData.addParam("card[exp_year]", expYear);
requestData.addParam("card[cvc]", cvc);
// Performs async
final ProgressDialog prog = ProgressDialog.show(getActivity(), "Getting Stripe token", "", true);
ProtocolTask task = new ProtocolTask(HttpMethod.HTTP_POST, "https://api.stripe.com/v1/tokens", requestData, new JSONResponseHandler() {
@Override
public void handleResponse(JSONObject jsonObject, JSONArray jsonArray) {
prog.dismiss();
// Handle successful response
if (this.getStatus() == 200) {
// Gets the token id out of the JSON response
try {
String token = jsonObject.getString("id");
Toast.makeText(getActivity(), "Token - " + token, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {}
}
// Handle error response
else {
// Gets the error message out of the JSON response
try {
String errorMessage = jsonObject.getJSONObject("error").getString("message");
Toast.makeText(getActivity(), "Error - " + errorMessage, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {}
}
}
});
task.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment