Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe.apiKey not resolving in Android

I'm using Stripe as a payment processor in my Android app and trying to charge a card as described by the documentation: https://stripe.com/docs/charges

My issue specifically is that it can not resolve Stripe.apiKey, or can not resolve symbol apiKey

The code that I'm implementing from the documentation:

// 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_********************";//this is where i hit a wall

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

// Charge the user's card:
Map<String, Object> params = new HashMap<String, Object>();
params.put("amount", 1000);
params.put("currency", "usd");
params.put("description", "Example charge");
params.put("source", token);

Charge charge = Charge.create(params);

I have also imported import com.stripe.android.*; at the top of my file.

In my Gradle file I have imported the Stripe libraries:

compile 'com.stripe:stripe-android:2.0.2'

Why isn't Android able to resolve Stripe.apiKey?

like image 300
beckah Avatar asked Feb 06 '23 05:02

beckah


2 Answers

The code you provided is server-side Java code for creating a charge using a token. It is not meant to be used from an Android application.

A payment flow with Stripe is divided in two steps:

  • client-side, in your frontend code, you collect and tokenize the user's payment information (using Checkout or Stripe.js for a web application, or the iOS / Android SDKs for a native mobile application)

  • server-side, in your backend code, you use the resulting token in an API request, e.g. to create a charge or a customer object.

The first step is done with your publishable API key (pk_...). The second step is done with your secret API key (sk_...).

You must never share the secret API key with your frontend code, otherwise an attacker could retrieve it and use it to issue API requests on your behalf.

like image 143
Ywain Avatar answered Feb 15 '23 10:02

Ywain


To solve Stripe.apikey cannot resolve, change Stripe.apiKey to

com.stripe.Stripe.apiKey = "Your secret";
like image 34
Sunil Shreepal Avatar answered Feb 15 '23 10:02

Sunil Shreepal