Accepting Stripe payment in an Android app

Leave a comment

Android Integration from the official site:
https://stripe.com/docs/mobile/android

-- Add this to your build.gradle
implementation 'com.stripe:stripe-android:6.1.2'
-- We will use  built-in Stripe cardinputwidget to collect card information
-- in the view's layout.xml e.g. activity_layout.xml include
<com.stripe.android.view.CardInputWidget
  android:id="@+id/card_input_widget"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
/>
-- In the activity file MyActivity.java
import com.stripe.android.view.CardInputWidget;
import com.stripe.android.Stripe;
import com.stripe.android.model.Token;
CardInputWidget mCardInputWidget = (CardInputWidget) findViewById(R.id.card_input_widget);
Card cardToSave = mCardInputWidget.getCard();
if (cardToSave == null) {
    mErrorDialogHandler.showError("Invalid Card Data");
}

-- Replace the key with your public TEST key
Stripe stripe = new Stripe(mContext, "pk_test_g6do5S237ekq10r65BnxO6S0");
stripe.createToken(
  cardToSave,
  new TokenCallback() {
    public void onSuccess(Token token) {
      // Send token to your server
      // call the checkout.php file here using HTTP post with SSL.
      // the script will make the charge.      
    }
    public void onError(Exception error) {
      // Show localized error message
      Toast.makeText(getContext(),
        error.getLocalizedString(getContext()),
        Toast.LENGTH_LONG
      ).show();
    }
  }
)

Get stripe API key link:
https://dashboard.stripe.com/account/apikeys

<?php
session_start();

// Do not access this script directly via browser.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    http_response_code(404);
    exit();
}

// Check token set
if (isset($_POST['stripeToken'])){    
    echo $_POST["stripeToken"];
}

It will just echo the token back. Make sure you catch it and check if there is indeed a token like ‘tok_andfdxexdfer’

To host php file the hosting server needs to be SSL certified. Here’s a trick for Free SSL certificate for Shared Godaddy hosting:

How to install a Let’s Encrypt SSL on a shared GoDaddy hosting account

If not try creating the SSL certificate on Zerossl.

Leave a Reply