Learn how to integrate Trustly Android SDK

Overview

The Trustly Android SDK is the quickest, most secure and failure-proof way to embed Trustly Checkout into your Android app. The SDK is a lightweight drop-in framework that enables the best Trustly user experience from within your app (multi-factor authentication, app-to-app redirection, error handling, etc).

The SDK provides a self-contained WebView bundled with all the necessary handlers that enable the Trustly Checkout within your application.

📘

If you are unable to integrate our SDK we recommend using our fallback method instead.

Android SDK

Our SDK for Android is distributed via GitHub in a public repository. Following sections will cover prerequisites as well as a guide of how to seamlessly integrate the SDK.

👍

Trustly Android SDK: https://github.com/trustly/TrustlyAndroidSdk

1. Register your ReturnToAppURL

The ReturnToAppURL is required to seamlessly transition the user to your app after bank authentication redirects. The redirect URI for your app must be set up as a custom url scheme in your application (such as example://...)

It is important that you only set the ReturnToAppURL attribute for orders originating from your app. Including the ReturnToAppURL for orders from other channels will break the user experience.

{
    ...
    "params": {
        ...
        "Data": { 
            ...
            "Attributes": {
                ...
                "ReturnToAppURL": "myapp://"
                ...

2. Integrate Trustly Android SDK

Integrate the Trustly Android SDK through Maven Central. Add the following to your Project level build.gradle file:

repositories {
  ...
  mavenCentral()
  ...
}

Add Trustly SDK as dependency in your Module level the build.gradle file like so:

dependencies {
  ...
  //replace x.x.x with the current version
  implementation 'com.trustly:trustly-android-library:x.x.x'
  ...
}

❗️

The later version of the SDK does not support the older version of the Trustly Checkout. If you do use the older Checkout, please use version 2.0.0 of the SDK. If you're not sure what version of the Trustly Checkout you are using, please contact our integration support for help.

Create WebView

In your app/src/main/res/layout/acitivity_main.xml, add following code:

<WebView
   android:id="@+id/webview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

Modify MainActivity

Import necessary dependancies and create a TrustlyWebView instance:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import android.webkit.WebView;
import com.trustlyAndroidLibrary.TrustlyWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   
   setContentView(R.layout.activity_main);
   WebView main = findViewById(R.id.webview);

   final TrustlyWebView trustlyView = new TrustlyWebView(this, 
   "<TRUSTLY-URL>");

   main.addView(trustlyView);
}

Add permission for internet

Go to app/src/main/AndroidManifext.xml and add following permissions:

<uses-permission android:name="android.permission.INTERNET"/>

3. Receiving Checkout Events

If you want more control of your Checkout flow you can choose to provide custom handlers.

Provide successHandler, abortHandler and errorHandler lambdas.
In case no custom functionality is provided, the WebView will load the SuccessURL in case of a success event, or the FailURL in case of an error or an abort event.

Read more

trustlyView = new TrustlyWebView(this, trustlyCheckoutUrl);

trustlyView.successHandler = () -> {
  // your custom implementation here.
};

trustlyView.errorHandler = () -> {
  // your custom implementation here.
};

trustlyView.abortHandler = () -> {
  // your custom implementation here.
};

4. Automatic redirects back to your application

It can happen that during the order flow, the user needs to be redirected outside of your application, to a third party application or website (in external stand alone browser). This could be part of the authentication and authorisation process.

To enable automatic redirects back to your native application, you can pass a link as an attribute to the order initiation request. You can pass your link (web, app link or deep link) value by including it in the ReturnToAppURL attribute when making an API call to Trustly. You can read more about it here.

⚠️ It's important that the Activity hosting the checkout doesn't reload when the intent is received. Otherwise the Trustly checkout session will be lost.

When defining you intent filters make sure to set android:launchMode="singleTop" parameter in your manifest file.

<activity
            android:name=".YOUR_TRUSTLY_WEB_VIEW_ACTIVITY"
            android:exported="true"
            android:launchMode="singleTop">
 <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:host="@string/scheme_host_sdk"
                    android:scheme="@string/scheme" />
</intent-filter>

📘

Opting in to events is solely for SDK v3.0.1 or later and is only compatible with the newer version of the Trustly Checkout.