Android
The Trustly Lightbox SDK for Android allows you to quickly build a bank authorization workflow in your Android app. Integrate the Select Bank Widget or the Trustly Lightbox to retrieve bank authorization data that can be used with other Trustly APIs.
To use an example project for testing and learning, see the Android Example in GitHub.
If you need help with your integration, contact your Trustly representative or send your request to us.integrations@trustly.com.
PayWithMyBank references are deprecated. If your app is using PayWithMyBank, you must change it to Trustly.
Prerequisites
- Android 5.0 (API level 21) or later
- AndroidX
- Gradle 8 or later
Android 15: Edge-to-edge display mode is the default behavior for Android 15 (API level 35) and later. If your app wasn’t designed for edge-to-edge display mode, critical elements of your app can be obscured. To ensure your app remains functional, you may need to use Window Insets to apply padding or margins to your layouts.
Authentication flow
The following diagram illustrates how the Trustly SDK manages the secure transition between your application, the SDK, and the banking institution.
Configure Gradle and Android dependencies
-
To add the Trustly SDK and the AndroidX Browser Library with Chrome Custom Tabs to your project, open your
build.gradlefile and add the following dependencies: -
Sync your project to enable the dependency changes.
-
If your app does not have internet permissions enabled, open the
AndroidManifest.xmlfile and add theINTERNETpermission: -
Configure an App Link for your app. Pass your App Link URL in
metadata.deepLinkUrlwhen buildingestablishData. Without it, customers are not automatically returned to your app after authenticating with their bank. See Set up App Links below.
Define Establish Data with a Request Signature
To ensure communications between the Trustly Lightbox SDK and the Trustly API are secure, use a requestSignature. You must generate a signature on your server using your access key and pass it to the Android app before rendering the Select Bank Widget or Trustly Lightbox.
Create a helper object in a new file named EstablishData.kt to hold your configuration:
When testing in the sandbox environment, set the env property to sandbox. You must remove the env property before publishing your production application.
To generate a requestSignature, see Generate request signatures. For a full list of parameters, see About OAuth authentication.
Display the Select Bank Widget
Trustly recommends rendering the Select Bank Widget for an optimal customer experience.
-
In your activity’s layout XML (for example,
activity_main.xml), add theTrustlyView: -
In your activity’s
onCreatemethod, initialize the widget and handle the bank selection event. UseonBankSelectedto update yourestablishDatawith the customer’s choice:
Launch the Lightbox
-
Add a button to your activity’s layout XML (for example.,
activity_main.xml) to launch the payment flow: -
Configure the button listener to launch a new activity. For example,
LightboxActivityand pass theestablishData: -
In your
LightboxActivity, callestablishto open the Lightbox: -
Optional. If your application doesn’t specify a screen orientation, add the
android:configChangesattribute to yourAndroidManifest.xmlto handle rotation gracefully. For example:For more information about handling configuration changes in Android apps, see Handle configuration changes.
Add callback functions
The Trustly Lightbox provides two callback functions to handle transaction results:
onReturn: Called when the customer successfully authorizes the transaction.onCancel: Called if the customer exits the process or the authorization fails.
Chain these callbacks to the establish method to handle the transaction result.
In the following example, a redirectToScreen helper function is used to prompt the customer when their transaction is successfully or unsuccessfully authorized:
Your application should retrieve data provided in the onReturn callback (such as the transaction ID) and pass it to your server for validation.
Add an OAuth transition handler
To support the transition from an external OAuth login (such as a bank app) back to the Lightbox, add an onRestart override to your LightboxActivity. This ensures the Lightbox resumes correctly after the user returns to your app from a bank login.
Set up App Links
If your app does not already have an App Link configured, you must set one up. Without it, users will not be automatically redirected to your app after logging in on a mobile banking app.
Android App Links use standard HTTPS URLs to return users directly to your app after bank authentication. Pass your App Link URL as metadata.deepLinkUrl in establishData. Unlike custom URL schemes, App Links are verified against your domain via Digital Asset Links, preventing other apps from intercepting them, and fall back to your website if the app isn’t installed.
Define the digital asset links
Create a JSON file named assetlinks.json and host it at: https://yourdomain.com/.well-known/assetlinks.json
Server requirements:
- Served over HTTPS.
Content-Typeheader set toapplication/json.
Verify your server returns the correct Content-Type:
Configure the manifest
Add an intent filter with autoVerify="true" to your AndroidManifest.xml. Android uses the following attributes to handle App Links:
android:exported="true"— Required to allow your activity to be started by external app links.android:autoVerify="true"— Instructs Android to verify your domain ownership by checkingassetlinks.jsonat install time.<data>elements — Define the HTTPS scheme, domain, and path that trigger your activity.
Handle incoming App Links
Update onCreate in your LightboxActivity to detect whether it was started by a normal launch or by an App Link. Also add onNewIntent so the same handler runs when the activity is already in the back stack. Add the following methods to your existing LightboxActivity class:
For apps that support both App Links and legacy custom URL schemes during a migration period, see Migrate from custom schemes (Android) for a unified router pattern that handles both link types through a single navigation flow.
Configure your deep link strategy (optional)
App Links function without a fallback. However, Trustly recommends having a default deep link strategy configured for your merchant account. This ensures your application has a consistent fallback behavior if a strategy isn’t explicitly provided within the metadata.deepLinkUrl object in your code.
Any settings passed to the establishData object in your code override the default configurations stored in your Trustly account profile.
Because this setup requires internal configuration, you’ll need to contact your Customer Success Manager (CSM) or Trustly Support to enable this fallback for your account.
When you submit your request, you must provide the following information:
- Deep link strategy: Specify
deeplink-url. - App Link: Provide your fully qualified domain (for example,
https://yourdomain.com/).