Payment failed: 6: BuyFlow UI needs to be shown when trying to set up a google pay method in android

I’m trying to implement a google pay method of paying in my android jetpack compose app, but I get this error “Payment failed: 6: BuyFlow UI needs to be shown” when I run my code. The message is clear and I thought I was doing that by calling this line “val task = paymentsClient.loadPaymentData(paymentDataRequest)”, but it seems it doesn’t work.

This is the code from my viewModel that gets called when I press on the button to “pay now”

private fun getTransactionInfo(price: String): JSONObject {
        return JSONObject()
            .put("totalPrice", price)
            .put("totalPriceStatus", "FINAL")
            .put("countryCode", "US")  // Example country code (or use your specific country code)
            .put("currencyCode", "USD")  // Example currency code
    }

    private val merchantInfo: JSONObject =
        JSONObject().put("merchantName", "ACME")

    private fun getAllowedPaymentMethods(): JSONArray {
        val allowedPaymentMethods = JSONArray()

        val cardPaymentMethod = JSONObject()
            .put("type", "CARD")
            .put("parameters", JSONObject()
                .put("allowedAuthMethods", JSONArray(listOf("PAN_ONLY", "CRYPTOGRAM_3DS")))
                .put("allowedCardNetworks", JSONArray(listOf("AMEX", "DISCOVER", "INTERAC", "JCB", "MASTERCARD", "VISA"))))
            .put("tokenizationSpecification", JSONObject()
                .put("type", "PAYMENT_GATEWAY")
                .put("parameters", JSONObject()
                    .put("gateway", "stripe")
                    .put("gatewayMerchantId", "The code from my google wallet console next to my company name")))

        allowedPaymentMethods.put(cardPaymentMethod)

        return allowedPaymentMethods
    }


    fun getPaymentDataRequest(priceLabel: String): JSONObject {
        return JSONObject()
            .put("apiVersion", 2) // API version for Google Pay
            .put("apiVersionMinor", 0) // API minor version
            .put("allowedPaymentMethods", getAllowedPaymentMethods()) // This function will return allowed payment methods JSON
            .put("transactionInfo", getTransactionInfo(priceLabel))
            .put("merchantInfo", merchantInfo)
            .put("shippingAddressRequired", true)  // Example: Set whether shipping address is required
            .put("shippingAddressParameters", JSONObject()
                .put("phoneNumberRequired", false)
                .put("allowedCountryCodes", JSONArray(listOf("US", "GB"))))  // Shipping address restrictions
    }


    fun getGooglePayPaymentToken(activity: Activity, price: Int) {

        val walletOptions = WalletOptions.Builder()
            .setEnvironment(WalletConstants.ENVIRONMENT_TEST) // or ENVIRONMENT_PRODUCTION for real payments
            .build()


        val paymentsClient = Wallet.getPaymentsClient(activity, walletOptions)
        val isReadyToPayRequest = IsReadyToPayRequest.newBuilder()
            .addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD)

            .build()

        val task = paymentsClient.isReadyToPay(isReadyToPayRequest)
        task.addOnCompleteListener {
            if (it.isSuccessful) {
                val currency: String = "USD"

                Log.i("paymentViewModel", " inside getGooglePayPaymentToken")
                
                val paymentDataRequestJson = getPaymentDataRequest("12.34")

                val paymentDataRequest = PaymentDataRequest.fromJson(paymentDataRequestJson.toString())

                val task = paymentsClient.loadPaymentData(paymentDataRequest)
                Log.i("paymentViewModel", " After load Payment data")
                task.addOnCompleteListener { result: Task<PaymentData> ->
                    if (result.isSuccessful) {
                        Log.i("paymentViewModel", "Inside is succesful")
                        val paymentData = result.result
                        val paymentMethodNonce = paymentData?.paymentMethodToken?.token

                        if (paymentMethodNonce != null) {
                            // Send the Google Pay token to your backend
                            val googlePayPaymentObject =
                                GooglePayPaymentObject(price, currency, paymentMethodNonce)
                            initiateGooglePayPayment(price, googlePayPaymentObject)
                        } else {
                            // Handle error: No payment token received
                            Log.i("paymentViewModel", "Inside no token received")
                        }
                    } else {
                        // Handle error from Google Pay
                        Log.i("paymentViewModel", "Inside payment NOT successful")
                        Log.e("paymentViewModel", "Payment failed: ${result.exception?.message}")
                        // You can print the error message or inspect the exception for more details
                        result.exception?.printStackTrace() // Print the stack trace to log the error in more detail
                    }
                }
            }
        }
    }

So the code runs smoothly, the log statement “Log.i(“paymentViewModel”, ” After load Payment data”)” prints, but when I get to “task.addOnCompleteListener” it is not successful and this log statement prints the error “Log.e(“paymentViewModel”, “Payment failed ${result.exception?.message}”

I have juggled around with different ways to construct the “paymentDataRequest” i first tried with the deprecated “val paymentDataRequest = PaymentDataRequest.newBuilder()” and also with a custom data class to construct the paymentDataRequest, because I thought that the function val task = paymentsClient.loadPaymentData(paymentDataRequest) was having problem with the paymentDataRequest, but it didn’t work. Then I thought that I had a problem with my stripe configuration, but google pay is active and I have a verified domain id and I know that stripe works because I can pay with a test credit card and I don’t think the problem is there because the UI doesn’t load on my android app so i’m my code doesn’t even reach the stripe backend. So maybe there is a problem with my google pay api. I know that there is a section in the google pay and wallet console under google pay api that asks “Integrate with your Android app” but the link doesn’t work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật