/swedbank-pay-sdk-android

Swedbank Pay SDK for Android

Primary LanguageKotlinApache License 2.0Apache-2.0

Swedbank Pay SDK for Android

Swedbank Pay SDK for Android

Download Run Checks License

The Swedbank Pay Android SDK facilitates embedding of Swedbank Pay payments to an Android application.

Installation

Gradle:

dependencies {
  implementation 'com.swedbankpay.mobilesdk:mobilesdk:4.0.0'
}

Usage

Please refer to the Developer Portal for usage instructions.

To explore a working app using the SDK, see the Example Project.

Onboarding / Quick intro

After reading this, checkout SwedbankPays Android SDK documentation for how to setup the app and more in-depth details.

To start making payments you need four things:

  1. A Configuration object that describes how to communicate with your backend. To get started quickly a default implementation is provided, called MerchantBackendConfiguration.
  2. A paymentOrder that describes what to purchase, the cost, currency and similar information.
  3. Give that paymentOrder to an instance of a PaymentFragment and show it to the user.
  4. Observe the state of the PaymentViewModel to keep track of the payment process callbacks and wait for payment to succeed or fail.

Instead of just talking about it we have provided you with an example app, showing you in detail how integration can be done. Use that as a reference when building your own solution:

Android example app

1. Configuration details

Using the MerchantBackendConfiguration you only need to provide the URL for your backend and header values for api key and an access token. Have a look at the configuration variable in Environment.kt in the example app for a reference.

The SDK will then communicate with your backend, expecting the same API as our example backends. You don't have to provide all of the API, making payments only require /paymentorders, but you will want to support /tokens and /patch soon as well. To get started you can look at our backend example implementations which provides a complete set of functionality and describes in a very clear and easy manner how requests can be handled.

Using the Merchant example backend you can setup (for example) a Node.js backend and have it serve a client in debug-mode while integrating the app. Remember to supply your api-key and other values in the appconfig.json file in order for requests to work properly.

2. PaymentOrder details

PaymentOrder is a data class with sensible defaults for most values, create one and use it when building a PaymentFragment to start the payment process. It can be created like this:

val paymentOrder = PaymentOrder(
    currency = Currency.getInstance("SEK"),
    amount = 1500L,
    vatAmount = 375L,
    description = "Test Purchase",
    language = Language.SWEDISH,
    urls = PaymentOrderUrls(context, backendUrl),
    payeeInfo = PayeeInfo(
        //
        payeeName = "Merchant1",
        productCategory = "A123",
        orderReference = "or-123456"
    ),
    orderItems = listOf(
        OrderItem(
            reference = "P1",
            name = "Product1",
            type = ItemType.PRODUCT,
            `class` = "ProductGroup1",
            itemUrl = "https://example.com/products/123",
            imageUrl = "https://example.com/product123.jpg",
            description = "Product 1 description",
            discountDescription = "Volume discount",
            quantity = 4,
            quantityUnit = "pcs",
            unitPrice = 300L,
            discountPrice = 200L,
            vatPercent = 2500,
            amount = 1000L,
            vatAmount = 250L
        )
    )
)

3. Presenting the payment menu

Give the PaymentOrder to the PaymentFragment as an argument and present it to the user. As a reference you can see how it was implemented in the example app, or build it like this:

val arguments = PaymentFragment.ArgumentsBuilder()
    .checkoutV3(true)
    .paymentOrder(paymentOrder)
    .build()

val paymentFragment = PaymentFragment()
paymentFragment.arguments = arguments

// Now use FragmentManager to show paymentFragment.
// You can also make a navigation graph with PaymentFragment
// and do something like
// findNavController().navigate(R.id.showPaymentFragment, arguments)

4. Observe PaymentViewModel's state

Use the observer pattern to keep track of current state. Typically you need to at least know when payments succeed, is canceled or fail.

paymentViewModel.state.observe(this, Observer {
    if (it.isFinal == true) {
        // Remove PaymentFragment
        // Check payment status from your backend
        // Notify user
    }
})

Integration conclusions

This is all you need to get started and accepting payments, the next step is to let your customers save their card details, or to create purchase tokens for subscriptions or tokens for charges at a later stage. Depending on your specific use case.

For more in-depth information about how to integrate your Android app, please refer to SwedbankPays Android SDK documentation.

Continue reading the integrate tokens walkthrough for a continued discussion on payment tokens. These features are also well documented in Swedbank pay's developer portal, under "optional features".

License

Swedbank Pay Android SDK is released under the Apache 2.0 license.