1. Add Stripe Dependency
In app/build.gradle:
dependencies {
implementation 'com.stripe:stripe-android:21.29.0'
}
2. XML Layout
activity_payment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<com.stripe.android.view.CardInputWidget
android:id="@+id/cardInputWidget"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pay $10"
android:layout_marginTop="20dp" />
</LinearLayout>
3. Initialize Stripe
In Application class or your Activity:
import com.stripe.android.PaymentConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaymentConfiguration.init(
getApplicationContext(),
"pk_test_YOUR_PUBLISHABLE_KEY"
);
setContentView(R.layout.activity_payment);
}
4. Java Activity Example
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.model.PaymentMethodCreateParams;
import com.stripe.android.view.CardInputWidget;
public class PaymentActivity extends AppCompatActivity {
private CardInputWidget cardInputWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaymentConfiguration.init(
getApplicationContext(),
"pk_test_YOUR_PUBLISHABLE_KEY"
);
setContentView(R.layout.activity_payment);
cardInputWidget = findViewById(R.id.cardInputWidget);
Button btnPay = findViewById(R.id.btnPay);
btnPay.setOnClickListener(v -> {
PaymentMethodCreateParams params =
cardInputWidget.getPaymentMethodCreateParams();
if (params != null) {
// Send payment details to your backend
// Backend creates PaymentIntent
// Return client_secret
// Confirm payment
}
});
}
}
5. Backend (Required)
Create a PaymentIntent on your server:
Example (Node.js):
const stripe = require('stripe')('sk_test_SECRET_KEY');
const paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: 'usd'
});
console.log(paymentIntent.client_secret);
6. Confirm Payment on Android
After receiving the client_secret from your backend:
stripe.confirmPayment(
this,
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
paymentMethodCreateParams,
clientSecret
)
);
Quick Setup Steps
- Create a Stripe account at Stripe Dashboard
- Get your Publishable Key and Secret Key.
- Add the Stripe Android SDK.
- Create a backend endpoint that generates a PaymentIntent.
- Send the PaymentIntent
client_secretto Android. - Call
confirmPayment(). - Handle success/failure callbacks.
Modern Alternative
Stripe now recommends using the PaymentSheet UI, which requires much less code and supports cards, Apple Pay/Google Pay (where applicable), and other payment methods automatically.
