Saboor880 9 Junior Poster

Hello to all! Hope you are fine. I am developing an e-commerce android app and integrating Stripe payment gateway in it and using Google Fire base as real time database.

I browsed the official website of stripe and took the source code from there. I used my test key from there. I successfully generated Stripe Token and saved it on my server (i-e Fire base). But I am unable to make an actual charge by using that token. That means no transaction shows on my stripe account. I am pasting my code, guide me what should i do to create/make charge?

Card cardToSave = cardInputWidget.getCard();
               if (cardToSave == null) {
                  Toast.makeText(getActivity(),"Invalid Card Data",Toast.LENGTH_LONG).show();

               }
               else{

                   Stripe stripe = new Stripe(MainActivity.mcont,
               "pk_test_XZFc6CW7wmDMl4WWESxtvWd300ibs1wr85");

                         stripe.createToken(
                           cardToSave,
                           new TokenCallback() {
                               public void onSuccess(final Token token) {
                                   // Send token to your server
                                   customer_ref =
                              database.getReference("Customers").child(MainActivity.user.getUid());
                                    final String tokenid = token.getId();

                                             //saving the tokenID on my server(i-e fiebase)

                                           final Map<String, Object> params = new HashMap<>();
                                           params.put("amount", paid);
                                           params.put("currency", "usd");
                                           params.put("description", "Example charge");
                                           params.put("source", tokenid);

                                           HashMap map = new HashMap();
                                           map.put("StripeToken",tokenid);
                                           customer_ref.updateChildren(map);

                                           try {
                                               // creating charge object
                                               Charge charge = Charge.create(params);

                                           } catch (AuthenticationException e) {
                                               e.printStackTrace();
                                           } catch (InvalidRequestException e) {
                                               e.printStackTrace();
                                           } catch (APIConnectionException e) {
                                               e.printStackTrace();
                                           } catch (CardException e) {
                                               e.printStackTrace();
                                           } catch (APIException e) {
                                               e.printStackTrace();
                                           }

Dani AI

Generated

Short diagnosis: you correctly create a Stripe token in the Android client, but you are trying to create the charge from inside the app. Creating charges (or any call that requires your secret key) must run on a secure server — never embed or use your secret key in client code. Tokenization is only half the flow; the token needs to be sent to a server that calls Stripe with your secret key. ()

Correct, modern flow (minimal):

  1. Client (Android) — use the publishable key to create a token or collect card data (you already do this).
  2. Send the token + order details over HTTPS to a backend you control (Cloud Function / server). Don’t just write the token into a publicly readable Realtime DB node.
  3. Backend — use your Stripe secret key to create a PaymentIntent (recommended) or a Charge, then return the result or the PaymentIntent client_secret for the client to confirm. Note: amounts are integers in the smallest currency unit (e.g., cents). ()

Example (server-side Firebase Cloud Function, Node.js — set your secret via firebase functions:config:set stripe.secret="sk_test_..." and never hardcode it):

const functions = require('firebase-functions');
const Stripe = require('stripe');
const stripe = Stripe(functions.config().stripe.secret);

exports.createPayment = functions.https.onCall(async (data, context) => {
  const amount = data.amount; // in cents
  const intent = await stripe.paymentIntents.create({
    amount,
    currency: 'usd',
    payment_method: data.payment_method_id, // or create/attach a PaymentMethod
    confirmation_method: 'manual',
    confirm: true,
  });
  return intent;
});

See Firebase + Stripe samples for deployment details and secure config. (firebase.google.com)

Quick troubleshooting checklist: make sure the server call uses your secret key (not the publishable key), pass amount as integer cents, check Stripe Dashboard API logs (and your Cloud Function logs) for authentication/errors, use idempotency keys to avoid double charges, and prefer PaymentIntents for SCA/3DS flows. If you share the server error/stacktrace or the Cloud Function logs, it will be possible to point to the exact failure. ()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.