The Trustly Android SDK provides an easy way to implement the Trustly Checkout in your Android app. The SDK handles communication with the Web View and exposes Checkout events that allows you to customize your Checkout flow.
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'
...
}
You also need to add the INTERNET
permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Add a WebView to the activity you wish to launch the Trustly Checkout from.
Example usage:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Instantiate a TrustlyWebView by passing the CheckouURL as a parameter from the activity. Assign the TrustlyWebView to the WebView.
WebView main = findViewById(R.id.webview);
TrustlyWebView trustlyView = new TrustlyWebView(this, trustlyCheckoutUrl);
main.addView(trustlyView);
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 a 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.
};
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 re-directs 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.
android:launchMode="singleTop"
parameter in your manifest file.
Below is an example intent filter setup in the manifest xml.
<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>