Ajith
Ajith

Reputation: 2666

How to fix android run time error in Stripe Terminal discover device code build using kotlin

I am trying to integrate stripe terminal code with my android app build using kotlin, unfortunately I am getting the following run time error which I could not able to fix

java.lang.IllegalStateException: initTerminal must be called before attempting to get the instance

The code I have added is used below

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_pay_screen)
    onDiscoverReaders()
}


fun onDiscoverReaders() {
    val config = DiscoveryConfiguration(
        timeout         = 0,
        discoveryMethod = DiscoveryMethod.LOCAL_MOBILE,
        isSimulated     = false,
        location        = "xxxxxxxxxxxxxxx"
    )
    // Save this cancelable to an instance variable
    discoveryCancelable = Terminal.getInstance().discoverReaders(config,

        discoveryListener = object : DiscoveryListener {
            override fun onUpdateDiscoveredReaders(readers: List<Reader>) {

            }
        }

        , object : Callback {
        override fun onSuccess() {
            println("Finished discovering readers")
        }

        override fun onFailure(e: TerminalException) {
            e.printStackTrace()
        }
    })
}

I have added this to one of my activity and my intention is to check if my phone is supporting stripe tap on mobile

I guess the issue could be calling onDiscoverReaders() from a wrong place, someone please help me to fix this issue

Thanks in advance

Upvotes: 0

Views: 895

Answers (2)

Pavel K
Pavel K

Reputation: 46

The error speaks for itself - first you need to initialize the api terminal, and then call the terminal instance.

Based on the documentation, we follow the following steps to get started with the api terminal:

  • Initialize the terminal application in the application class of the application
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        TerminalApplicationDelegate.onCreate(this)
    }
}
  • We request the necessary permissions for correct work with the terminal search (bluetooth, geolocation), if everything is provided, we call the init terminal with parameters like that:
Terminal.initTerminal(
            context = context,
            logLevel = LogLevel.VERBOSE,
            tokenProvider = TokenProvider(),
            listener = object : TerminalListener {
                override fun onUnexpectedReaderDisconnect(reader: Reader) {
                    Log.d("log", "onUnexpectedReaderDisconnect")
                }

                override fun onConnectionStatusChange(status: ConnectionStatus) {
                    super.onConnectionStatusChange(status)
                    Log.d("log", "onConnectionStatusChange")
                }

                override fun onPaymentStatusChange(status: PaymentStatus) {
                    super.onPaymentStatusChange(status)
                    Log.d("log", "onPaymentStatusChange")
                }
            }
        )
  • After this initialization, you can call the terminal instance and work with it.

Upvotes: 1

Omkar
Omkar

Reputation: 3100

In stripe docs you can check

  // Create your listener object. Override any methods that you want to be notified about
val listener = object : TerminalListener {
}

// Choose the level of messages that should be logged to your console
val logLevel = LogLevel.VERBOSE

// Create your token provider.
val tokenProvider = TokenProvider()

// Pass in the current application context, your desired logging level, your token provider, and the listener you created
if (!Terminal.isInitialized()) {
    Terminal.initTerminal(applicationContext, logLevel, tokenProvider, listener)
}

// Since the Terminal is a singleton, you can call getInstance whenever you need it
Terminal.getInstance()

might be you missed to initialise terminal before getting Instance so try add above code before onDiscoverReaders()

Upvotes: 1

Related Questions