use AIDL and return response to another app

Viewed 68

I using AIDL in a market project for payment and send score and I return orderID for checking result pay when developer send a request I send her request to the server and received orderID return to developers in fun resultPay I send a request to our server for getting result pay with the orderID while response has not returned I have to return the result

class DeveloperApiService : Service() {


private val binder = object : ITiroDevApi.Stub() {
    override fun resultPay(token: String?, orderId: String?): String {
        var result = ""
        ServiceHelper.resultPayment(ActiveOwner(), GamerSharedPreferenceHelper(applicationContext).userID
                , orderId, token,
                object : CustomObserver<ServiceHelper.StringResult>() {
                    override fun onError(e: Throwable?) {
                        super.onError(e)
                        result = "tryAgain"
                    }

                    override fun onNext(response: ServiceHelper.StringResult?) {
                        super.onNext(response)
                        if (response!!.result != "") {
                            result = response.result
                        } else {
                            when (response.error) {
                                "error_token_invalid_or_expired" -> result = "error_token_invalid_or_expired"
                                "error_payment_not_verified" -> result = "error_payment_not_verified"
                            }
                        }
                    }
                })

        if (result == "") Thread.sleep(2000)
        return result

    }


    override fun requestPay(token: String?, price: String?): String {
        var result = ""
        ServiceHelper.requestPayment(ActiveOwner(), token, GamerSharedPreferenceHelper(applicationContext).userID
                , price,
                object : CustomObserver<ServiceHelper.StringResult>() {
                    override fun onError(e: Throwable?) {
                        super.onError(e)
                        result = "tryAgain"
                    }

                    override fun onNext(response: ServiceHelper.StringResult?) {
                        super.onNext(response)
                        if (response!!.result != "") {
                            result = response.result
                            inAppPayment(GamerSharedPreferenceHelper(applicationContext).userID, response.result, token!!)
                        }
                        when (response.error) {
                            "error_token_invalid_or_expired" -> result = "error_token_invalid_or_expired"
                            "error_min_amount" -> result = "error_min_amount"
                        }
                    }
                })


        while (result == "") {
            Thread.sleep(1000)
        }
        return result
    }

private fun inAppPayment(userId: String, orderId: String, token: String) {
    var result = ""
    ServiceHelper.inAppPayment(ActiveOwner(), userId, orderId, token,
            object : CustomObserver<ServiceHelper.StringResult>() {
                override fun onError(e: Throwable?) {
                    super.onError(e)
                }

                override fun onNext(response: ServiceHelper.StringResult?) {
                    super.onNext(response)

                    when (response.error) {
                        "error_already_pay" -> result = ""
                        "error_payment_gateway" -> result = "error_payment_gateway"
                    }
                    if (response.result != "") {
                        val browserIntent = Intent("android.intent.action.VIEW", Uri.parse(response.result))
                        applicationContext.startActivity(browserIntent)
                    }
                }
            })

}

What is your suggestion to solve this problem?

1 Answers

You should make ServiceHelper.paymentResult synchronized.

Related