Getting started with Sources in the Android SDKDeprecated
Learn how to use Sources in your Android application.
Warning
We deprecated the Sources API and plan to remove support for local payment methods. If you currently handle any local payment methods using the Sources API, you must migrate them to the Payment Methods API.
While we don’t plan to remove support for card payments, we recommend replacing any use of the Sources API with the PaymentMethods API, which provides access to our latest features and payment method types.
Creating a payment using Sources with the Android SDK is a multi-step process:
- Create a Source object that represents your customer’s payment method.
- Check if further action is required from your customer.
If no further action is required:
- Confirm the source is ready to use.
- Create a charge request on your backend using the source.
If further action is required:
- Present the user with any information they may need to authorize the charge.
- On your backend, listen to Stripe webhooks to create a charge with the source.
- In your app, display the appropriate confirmation to your customer based on the source’s status.
Create a Source object
To create a Source
object, use the appropriate creation method for your Source type.
- SourceParams#createBancontactParams()
- SourceParams#createCardParams()
- SourceParams#createGiropayParams()
- SourceParams#createIdealParams()
- SourceParams#createP24Params()
- SourceParams#createSepaDebitParams()
- SourceParams#createSofortParams()
- SourceParams#createThreeDSecureParams()
Each method requires parameters unique to the payment type. Refer to the appropriate payment methods documentation to find out what these are.
Once you have a SourceParams object, create a source with either the Stripe#createSource() or Stripe#createSourceSynchronous(), depending on whether you prefer to manage threading yourself.
Warning
Do not call Stripe#createSourceSynchronous()
on the UI thread as this will crash. All methods labeled “Synchronous” are blocking and meant to be performed on a separate thread. Similarly, you must call createSource
on the UI thread, as Android’s AsyncTask
must be launched from the main thread.
Check if further action is required from your customer
Some payment methods require your customer to complete a certain action before the source can be used in a charge request. For instance, customers using giropay must be redirected to their online banking service to authorize the payment.
For sources that require redirecting your customer, you must specify a return URL when creating the source. This redirect URL should be unique and used consistently for your application. Do not use the same redirect URL in other applications, as it can result in a payment attempt that opens the wrong application after the redirect.
Note
If you would like to accept card payments that are verified with 3D Secure, your integration should use the Payment Intents API instead of sources. Refer to the Payment Methods API documentation to determine if the specific payment methods you wish to use are supported.
Redirect your customer to authorize a source
For sources that require your customer to complete an action (for example, verify using 3D Secure), redirect the customer out of your application to complete this step.
Once the customer has completed the required action, they are redirected to the URL that was provided when creating the source.
When declaring your activity that creates redirect-based sources, list an intent-filter
item in your AndroidManifest.
file. This allows you to accept links into your application. Your activity must include android:launchMode="singleTask"
or else a new copy of it is opened when your customer comes back from the browser.
<activity android:name=".activity.PollingActivity" android:launchMode="singleTask" android:theme="@style/SampleTheme"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="yourapp" android:host="post-authentication-return-url"/> </intent-filter> </activity>
To receive information from this event, listen for your activity getting started back up with a new Intent using the onNewIntent
lifecycle method.
If you’d like more help, check out the example app on Github that demonstrates creating a payment using several different payment methods.