Accept a payment using Stripe Elements and the Charges APICharges API
Accept online payments from US and Canadian customers.
Legacy API
The content of this section refers to a Legacy feature. Use the Payment Intents API instead.
The Charges API doesn’t support the following features, many of which are required for credit card compliance:
- Merchants in India
- Bank requests for card authentication
- Strong Customer Authentication
Use our prebuilt card UI component to create a payment form that securely collects a customer’s card details without handling the sensitive data. The card details are then converted to a representative Token that you can safely send to your servers. Your server can use that token to create a charge.
Nota
The steps in this guide are fully implemented on GitHub. Clone the repo and follow the instructions to run the demo app.
Set up StripeClient-sideServer-side
Server-side
This integration requires endpoints on your server that talk to the Stripe API. Use our official libraries for access to the Stripe API from your server:
Client-side
The Stripe Android SDK is open source and fully documented.
To install the SDK, add stripe-android
to the dependencies
block of your app/build.gradle file:
Nota
For details on the latest SDK release and past versions, see the Releases page on GitHub. To receive notifications when a new release is published, watch releases for the repository.
Configure the SDK with your Stripe publishable key so that it can make requests to the Stripe API, such as in your Application
subclass:
Create your payment formClient-side
Securely collect card information on the client with CardInputWidget, a drop-in UI component provided by the SDK that collects the card number, expiration date, CVC, and postal code
CardInputWidget performs on-the-fly validation and formatting.
Create an instance of the card component and a Pay button by adding the following to your checkout page’s layout
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_checkout" tools:context=".CheckoutActivity"> <!-- ... --> <com.stripe.android.view.CardInputWidget android:id="@+id/cardInputWidget" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> <Button android:text="Pay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/payButton" android:layout_marginTop="20dp" app:layout_constraintTop_toBottomOf="@+id/cardInputWidget" app:layout_constraintStart_toStartOf="@+id/cardInputWidget" app:layout_constraintEnd_toEndOf="@+id/cardInputWidget"/> <!-- ... --> </androidx.constraintlayout.widget.ConstraintLayout>
Run your app, and make sure your checkout page shows the card component and pay button.
Create a tokenClient-side
When the user taps the pay button, convert the card information collected by CardInputWidget
into a Stripe token. Tokenization ensures that no sensitive card data ever needs to touch your server, so that your integration remains PCI compliant.
The following code shows how to use Stripe#createToken(). The method takes a Card
instance as the first parameter. The second parameter is ApiResultCallback<Token> instance that the client invokes on success or failure. When tokenization completes successfully, send the returned token ID to your server.
Send the Token id to your server from the client.
Create a charge with the tokenServer-side
The response from creating a charge will either be a charge or an error with an error code. If the response succeeds you can fulfill the customer’s order and show them a success screen. Otherwise, you can show them an error.
Test your integration
If you can reliably enter a test card in the card element, submit it to the server, and see that your server created the charge, then your integration is finished.
You’ve created a Charges API integration to start accepting card payments right away. This API doesn’t support scaling businesses or customers outside of the US and Canada. For more robust and global payments, learn to accept a payment with the Payment Intents API.