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”)”
- 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!
Dali Cos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.