PayPalWebCheckoutClient not confirming the payment. onPayPalWebCanceled

I am new to android app developing. My app with PayPal payment sandbox
In sandbox I have no return_url defined.
Please help me out of this PayPal API mess 😉

Kotlin implementation of PayPalWebCheckoutClient

First question is how to onNewIntent?
The PayPal documentation seems not quit working:

override fun onNewIntent(newIntent: Intent?) {
        super.onNewIntent(intent)
        intent = newIntent
    }

instead I use:

override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        // Pass the intent to PayPalWebCheckoutClient to handle the PayPal response
        handlePayPalRedirect(intent)
    }
private fun handlePayPalRedirect(intent: Intent?) {
        Log.d("handlePayPalRedirect","handlePayPalRedirect...")
        Toast.makeText(this@MyPaymentsActivity, "handlePayPalRedirect...", Toast.LENGTH_LONG).show()
        intent?.data?.let { uri ->
            // Process the PayPal payment result by extracting query parameters from the URI
            val queryParams = uri.queryParameterNames

            Log.d("handlePayPalRedirect","queryParams: $queryParams")

            // Continue with PayPal-specific logic
            if (queryParams.contains("opType")) {
                val opType = uri.getQueryParameter("opType")
                if (opType == "cancel") {
                    Log.d("handlePayPalRedirect", "Payment canceled by user")
                    Toast.makeText(this@MyPaymentsActivity, "Payment was canceled.", Toast.LENGTH_LONG).show()
                    finish() // Close the activity or handle accordingly
                    return
                }
            }

            if (queryParams.contains("token") && queryParams.contains("PayerID")) {
                val token = uri.getQueryParameter("token")
                val payerId = uri.getQueryParameter("PayerID")

                if (token != null && payerId != null) {
                    captureOrder(token, payerId)
                } else {
                    Log.e("handlePayPalRedirect", "Token or PayerID is null")
                }
            } else {
                Log.e("handlePayPalRedirect", "Required query parameters are missing")
            }
        }
    }

Still I don’t get into “if (opType == “cancel”)”

  1. second question: always get into listener: “onPayPalWebCanceled”

Here my createOrderId function:

suspend fun createOrderId(accessToken: String): String {
        val client = OkHttpClient()

//        val amount = Constants.PAYPAL_AMOUNT

        // Your access token obtained from PayPal
        val accessToken = BuildConfig.PAYPAL_ACCESS_TOKEN

        // Request payload
        val jsonPayload = JSONObject()
            .put("intent", "CAPTURE")
            .put(
                "purchase_units", JSONArray().put(
                    JSONObject()
                        .put(
                            "amount", JSONObject()
                                .put("currency_code", "EUR")
                                .put("value", amount)
                        )
                )
            )
            .put(
                "payment_source", JSONObject()
                    .put(
                        "paypal", JSONObject()
                            .put(
                                "experience_context", JSONObject()
                                    .put("user_action", "PAY_NOW")
                                    .put("return_url", "custom-url-scheme://paypal")
                                    .put("cancel_url", "custom-url-scheme://paypal?opType=cancel")
                            )
                    )
            ).toString()

        // Build the request
        val request = Request.Builder()
            .url("https://api-m.sandbox.paypal.com/v2/checkout/orders/")
            .post(jsonPayload.toRequestBody("application/json; charset=utf-8".toMediaType()))
            .addHeader("Authorization", "Bearer $accessToken")
            .addHeader("Content-Type", "application/json")
            .build()

        return try {
            // Execute the request
            val response = client.newCall(request).execute()
            if (!response.isSuccessful) {
                throw IOException("Unexpected code $response")
            }

            // Extract the order ID from the response
            val responseBody = response.body?.string()
            val jsonResponse = JSONObject(responseBody)
            jsonResponse.getString("id") // Extract the order ID from the response
        } catch (e: Exception) {
            // Handle errors
            throw IOException("Error creating PayPal order: ${e.message}")
        }
    }

The manifest activity intent-filter:

<data android:scheme="custom-url-scheme" />

All this is from my MainActivity from a dialog:

private fun showSupportDeveloperDialog() {
        val builder = AlertDialog.Builder(this)
        val inflater = layoutInflater
        val dialogLayout = inflater.inflate(R.layout.dialog_support_developer, null)
        builder.setView(dialogLayout)
        val dialog = builder.create()

        val amountEditText: EditText = dialogLayout.findViewById(R.id.amountEditText)

        val payPalButton: PayPalButton = dialogLayout.findViewById(R.id.paypal_button)

        payPalButton.setOnClickListener {
            val amount = amountEditText.text.toString().trim().ifEmpty {
                Constants.PAYPAL_AMOUNT.toString()
            }

            // Start the MyPaymentsActivity and pass the amount
            val intent = Intent(this, MyPaymentsActivity::class.java)
            intent.putExtra("amount", amount)
            startActivity(intent)
            dialog.dismiss()
        }

building variant debug. Emulator API 30.
also apk in Xiami 12lite

In sandbox account no payment is done!

New contributor

Dali Cos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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