Dale Moncayo Dale Moncayo - 3 years ago 62
Android Question

Android: Create a new customer record with Stripe API

I have a new customer who is checking out and wants to store their card for future use. If I'm reading the documentation correctly, I need to make separate calls out to Stripe, one to create the customer with the card and another to charge the card.

The question is.. is there a way to create a new Customer record into Stripe by just using Android/Java codes without utilizing any back-end server size codes like PHP, Node.JS, etc?

Cause i'm having trouble with the documentation stated here for Java. I tried to follow the documentation but sadly, the codes will not work in my Android Project (Even though i added it to build.gradle, etc.)

I'm new to Stripe so i'm not all that knowledgeable.

This is the code that i was trying to follow and apply to my project.

// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_ejKNr41rD0SbiNVxkZOcneG0";

// Token is created using Stripe.js or Checkout!
// Get the payment token submitted by the form:
String token = request.getParameter("stripeToken");

// Create a Customer:
Map<String, Object> customerParams = new HashMap<String, Object>();
customerParams.put("email", "paying.user@example.com");
customerParams.put("source", token);
Customer customer = Customer.create(customerParams);

// Charge the Customer instead of the card:
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 1000);
chargeParams.put("currency", "eur");
chargeParams.put("customer", customer.getId());
Charge charge = Charge.create(chargeParams);

// YOUR CODE: Save the customer ID and other info in a database for later.

// YOUR CODE (LATER): When it's time to charge the customer again, retrieve the customer ID.
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 1500); // $15.00 this time
chargeParams.put("currency", "eur");
chargeParams.put("customer", customerId);
Charge charge = Charge.create(chargeParams);

Answer Source

is there a way to create a new Customer record into Stripe by just using Android/Java codes without utilizing any back-end server size codes like PHP, Node.JS, etc?

No. Stripe's Android SDK should only be used to collect and tokenize customer payment information. This operation is done with your publishable API key.

All other operations (e.g. using the token to create a customer object or a charge) are done using your secret key, which must never be shared or embedded in your mobile app as an attacker could then retrieve it and use it to issue API requests on your behalf.

Stripe does provide a Java library, but it is meant for server-side use, not to be used from an Android mobile app.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download