Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to PaymentAuthWebViewActivity #1677

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PaymentMultilineActivity : AppCompatActivity() {
.doOnComplete { progressDialogController.dismiss() }
.subscribe(
{ addToList(it) },
{ throwable -> errorDialogHandler.show(throwable.localizedMessage) }
{ throwable -> errorDialogHandler.show(throwable.localizedMessage.orEmpty()) }
)
)
}
Expand Down
9 changes: 9 additions & 0 deletions stripe/src/main/java/com/stripe/android/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal interface Logger {

fun info(msg: String)

fun debug(msg: String)

companion object {
internal fun getInstance(enableLogging: Boolean): Logger {
return if (enableLogging) {
Expand All @@ -26,6 +28,10 @@ internal interface Logger {
override fun error(msg: String, t: Throwable?) {
Log.e(TAG, msg, t)
}

override fun debug(msg: String) {
Log.d(TAG, msg)
}
}

private val NOOP_LOGGER = object : Logger {
Expand All @@ -34,6 +40,9 @@ internal interface Logger {

override fun error(msg: String, t: Throwable?) {
}

override fun debug(msg: String) {
}
}

internal fun real(): Logger {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import com.stripe.android.view.PaymentAuthWebViewActivity
internal class PaymentAuthWebViewStarter @JvmOverloads internal constructor(
private val host: AuthActivityStarter.Host,
private val requestCode: Int,
private val toolbarCustomization: StripeToolbarCustomization? = null
private val toolbarCustomization: StripeToolbarCustomization? = null,
private val enableLogging: Boolean = false
) : AuthActivityStarter<PaymentAuthWebViewStarter.Data> {

override fun start(data: Data) {
Logger.getInstance(enableLogging).debug("PaymentAuthWebViewStarter#start()")
val extras = Bundle()
extras.putString(EXTRA_CLIENT_SECRET, data.clientSecret)
extras.putString(EXTRA_AUTH_URL, data.url)
extras.putString(EXTRA_RETURN_URL, data.returnUrl)
extras.putBoolean(EXTRA_ENABLE_LOGGING, enableLogging)
extras.putParcelable(EXTRA_UI_CUSTOMIZATION, toolbarCustomization)

host.startActivityForResult(PaymentAuthWebViewActivity::class.java, extras, requestCode)
Expand All @@ -28,13 +31,14 @@ internal class PaymentAuthWebViewStarter @JvmOverloads internal constructor(
internal class Data(
val clientSecret: String,
val url: String,
val returnUrl: String?
val returnUrl: String? = null
)

companion object {
const val EXTRA_AUTH_URL = "auth_url"
const val EXTRA_CLIENT_SECRET = "client_secret"
const val EXTRA_RETURN_URL = "return_url"
const val EXTRA_UI_CUSTOMIZATION = "ui_customization"
internal const val EXTRA_AUTH_URL = "auth_url"
internal const val EXTRA_CLIENT_SECRET = "client_secret"
internal const val EXTRA_RETURN_URL = "return_url"
internal const val EXTRA_UI_CUSTOMIZATION = "ui_customization"
internal const val EXTRA_ENABLE_LOGGING = "enable_logging"
}
}
27 changes: 17 additions & 10 deletions stripe/src/main/java/com/stripe/android/PaymentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit
internal open class PaymentController @VisibleForTesting constructor(
context: Context,
private val stripeRepository: StripeRepository,
enableLogging: Boolean = false,
private val enableLogging: Boolean = false,
private val messageVersionRegistry: MessageVersionRegistry =
MessageVersionRegistry(),
private val config: PaymentAuthConfig =
Expand Down Expand Up @@ -249,7 +249,8 @@ internal open class PaymentController @VisibleForTesting constructor(
host,
getRequestCode(stripeIntent),
stripeIntent.clientSecret ?: "",
Stripe3dsRedirect.create(sdkData).url
Stripe3dsRedirect.create(sdkData).url,
enableLogging = enableLogging
)
}
else -> // authentication type is not supported
Expand All @@ -273,7 +274,8 @@ internal open class PaymentController @VisibleForTesting constructor(
getRequestCode(stripeIntent),
stripeIntent.clientSecret ?: "",
redirectData?.url.toString(),
redirectData?.returnUrl
redirectData?.returnUrl,
enableLogging = enableLogging
)
}
else -> // next action type is not supported, so bypass authentication
Expand Down Expand Up @@ -337,7 +339,7 @@ internal open class PaymentController @VisibleForTesting constructor(
Stripe3ds2AuthCallback(host, stripeRepository, transaction, timeout,
stripeIntent, stripe3ds2Fingerprint.source, requestOptions,
analyticsRequestExecutor, analyticsDataFactory,
challengeFlowStarter)
challengeFlowStarter, enableLogging)
)
}

Expand Down Expand Up @@ -412,7 +414,8 @@ internal open class PaymentController @VisibleForTesting constructor(
private val paymentRelayStarter: PaymentRelayStarter,
private val analyticsRequestExecutor: FireAndForgetRequestExecutor,
private val analyticsDataFactory: AnalyticsDataFactory,
private val challengeFlowStarter: ChallengeFlowStarter
private val challengeFlowStarter: ChallengeFlowStarter,
private val enableLogging: Boolean = false
) : ApiResultCallback<Stripe3ds2AuthResult> {

constructor(
Expand All @@ -425,15 +428,17 @@ internal open class PaymentController @VisibleForTesting constructor(
requestOptions: ApiRequest.Options,
analyticsRequestExecutor: FireAndForgetRequestExecutor,
analyticsDataFactory: AnalyticsDataFactory,
challengeFlowStarter: ChallengeFlowStarter
challengeFlowStarter: ChallengeFlowStarter,
enableLogging: Boolean
) :
this(
host, stripeRepository, transaction, maxTimeout, stripeIntent,
sourceId, requestOptions,
PaymentRelayStarter(host, getRequestCode(stripeIntent)),
analyticsRequestExecutor,
analyticsDataFactory,
challengeFlowStarter
challengeFlowStarter,
enableLogging
)

override fun onSuccess(result: Stripe3ds2AuthResult) {
Expand All @@ -459,7 +464,8 @@ internal open class PaymentController @VisibleForTesting constructor(
host,
getRequestCode(stripeIntent),
stripeIntent.clientSecret ?: "",
result.fallbackRedirectUrl
result.fallbackRedirectUrl,
enableLogging = enableLogging
)
} else {
val error = result.error
Expand Down Expand Up @@ -750,9 +756,10 @@ internal open class PaymentController @VisibleForTesting constructor(
requestCode: Int,
clientSecret: String,
authUrl: String,
returnUrl: String? = null
returnUrl: String? = null,
enableLogging: Boolean = false
) {
PaymentAuthWebViewStarter(host, requestCode).start(
PaymentAuthWebViewStarter(host, requestCode, enableLogging = enableLogging).start(
PaymentAuthWebViewStarter.Data(clientSecret, authUrl, returnUrl))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ProgressBar
import com.stripe.android.Logger

/**
* A `WebView` used for authenticating payment details
Expand All @@ -33,25 +34,16 @@ internal class PaymentAuthWebView @JvmOverloads constructor(

fun init(
activity: Activity,
logger: Logger,
progressBar: ProgressBar,
clientSecret: String,
returnUrl: String?
returnUrl: String? = null
) {
webViewClient = PaymentAuthWebViewClient(activity, activity.packageManager, progressBar,
clientSecret, returnUrl)
webViewClient = PaymentAuthWebViewClient(activity, activity.packageManager, logger,
progressBar, clientSecret, returnUrl)
setWebViewClient(webViewClient)
}

fun onForegrounded() {
if (webViewClient?.hasOpenedApp == true) {
// If another app was opened, assume it was a bank app where payment authentication
// was completed. Upon foregrounding this screen, load the completion URL.
webViewClient?.completionUrlParam?.let {
loadUrl(it)
}
}
}

@SuppressLint("SetJavaScriptEnabled")
private fun configureSettings() {
settings.javaScriptEnabled = true
Expand All @@ -78,6 +70,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
internal class PaymentAuthWebViewClient(
private val activity: Activity,
private val packageManager: PackageManager,
private val logger: Logger,
private val progressBar: ProgressBar,
private val clientSecret: String,
returnUrl: String?
Expand All @@ -93,11 +86,13 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
private set

override fun onPageCommitVisible(view: WebView, url: String) {
logger.debug("PaymentAuthWebViewClient#onPageCommitVisible() - $url")
super.onPageCommitVisible(view, url)
hideProgressBar()
}

override fun onPageFinished(view: WebView, url: String?) {
logger.debug("PaymentAuthWebViewClient#onPageFinished() - $url")
super.onPageFinished(view, url)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
// hide the progress bar here because `onPageCommitVisible()`
Expand All @@ -110,6 +105,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

private fun hideProgressBar() {
logger.debug("PaymentAuthWebViewClient#hideProgressBar()")
progressBar.visibility = View.GONE
}

Expand All @@ -128,10 +124,12 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

override fun shouldOverrideUrlLoading(view: WebView, urlString: String): Boolean {
logger.debug("PaymentAuthWebViewClient#shouldOverrideUrlLoading() - $urlString")
val uri = Uri.parse(urlString)
updateCompletionUrl(uri)

return if (isReturnUrl(uri)) {
logger.debug("PaymentAuthWebViewClient#shouldOverrideUrlLoading() - handle return URL")
onAuthCompleted()
true
} else if ("intent".equals(uri.scheme, ignoreCase = true)) {
Expand All @@ -148,6 +146,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

private fun openIntentScheme(uri: Uri) {
logger.debug("PaymentAuthWebViewClient#openIntentScheme()")
try {
openIntent(Intent.parseUri(uri.toString(), Intent.URI_INTENT_SCHEME))
} catch (e: Exception) {
Expand All @@ -156,6 +155,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

private fun openIntent(intent: Intent) {
logger.debug("PaymentAuthWebViewClient#openIntent()")
if (intent.resolveActivity(packageManager) != null) {
hasOpenedApp = true
activity.startActivity(intent)
Expand All @@ -166,6 +166,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

private fun updateCompletionUrl(uri: Uri) {
logger.debug("PaymentAuthWebViewClient#updateCompletionUrl()")
val returnUrlParam = if (isAuthenticateUrl(uri.toString())) {
uri.getQueryParameter(PARAM_RETURN_URL)
} else {
Expand All @@ -182,10 +183,12 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
view: WebView,
request: WebResourceRequest
): Boolean {
logger.debug("PaymentAuthWebViewClient#shouldOverrideUrlLoading(WebResourceRequest)")
return shouldOverrideUrlLoading(view, request.url.toString())
}

private fun isReturnUrl(uri: Uri): Boolean {
logger.debug("PaymentAuthWebViewClient#isReturnUrl()")
when {
isPredefinedReturnUrl(uri) -> return true

Expand Down Expand Up @@ -224,6 +227,7 @@ internal class PaymentAuthWebView @JvmOverloads constructor(
}

private fun onAuthCompleted() {
logger.debug("PaymentAuthWebViewClient#onAuthCompleted()")
activity.finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.stripe.android.Logger
import com.stripe.android.PaymentAuthWebViewStarter
import com.stripe.android.R
import com.stripe.android.stripe3ds2.init.ui.StripeToolbarCustomization
Expand All @@ -21,9 +22,16 @@ import com.ults.listeners.SdkChallengeInterface.UL_HANDLE_CHALLENGE_ACTION
class PaymentAuthWebViewActivity : AppCompatActivity() {

private var toolbarCustomization: ToolbarCustomization? = null
private lateinit var logger: Logger

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

logger = Logger.getInstance(
intent.getBooleanExtra(PaymentAuthWebViewStarter.EXTRA_ENABLE_LOGGING, false)
)
logger.debug("PaymentAuthWebViewActivity#onCreate()")

LocalBroadcastManager.getInstance(this)
.sendBroadcast(Intent().setAction(UL_HANDLE_CHALLENGE_ACTION))

Expand All @@ -34,28 +42,38 @@ class PaymentAuthWebViewActivity : AppCompatActivity() {
toolbarCustomization = intent.getParcelableExtra<StripeToolbarCustomization>(
PaymentAuthWebViewStarter.EXTRA_UI_CUSTOMIZATION
)
customizeToolbar(toolbar)

val clientSecret = intent
.getStringExtra(PaymentAuthWebViewStarter.EXTRA_CLIENT_SECRET)
val returnUrl = intent
.getStringExtra(PaymentAuthWebViewStarter.EXTRA_RETURN_URL)
customizeToolbar(toolbar)

val clientSecret =
intent.getStringExtra(PaymentAuthWebViewStarter.EXTRA_CLIENT_SECRET)
val returnUrl =
intent.getStringExtra(PaymentAuthWebViewStarter.EXTRA_RETURN_URL)
setResult(Activity.RESULT_OK, Intent()
.putExtra(StripeIntentResultExtras.CLIENT_SECRET, clientSecret))

if (clientSecret.isNullOrBlank()) {
logger.debug("PaymentAuthWebViewActivity#onCreate() - clientSecret is null or blank")
finish()
return
}

val webView = findViewById<PaymentAuthWebView>(R.id.auth_web_view)
val progressBar = findViewById<ProgressBar>(R.id.auth_web_view_progress_bar)
webView.init(this, progressBar, clientSecret!!, returnUrl)

logger.debug("PaymentAuthWebViewActivity#onCreate() - PaymentAuthWebView init and loadUrl")
webView.init(this, logger, progressBar, clientSecret, returnUrl)
webView.loadUrl(intent.getStringExtra(PaymentAuthWebViewStarter.EXTRA_AUTH_URL))
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
logger.debug("PaymentAuthWebViewActivity#onCreateOptionsMenu()")
menuInflater.inflate(R.menu.payment_auth_web_view_menu, menu)

toolbarCustomization?.let {
val buttonText = it.buttonText
if (!buttonText.isNullOrBlank()) {
logger.debug("PaymentAuthWebViewActivity#customizeToolbar() - updating close button text")
val closeMenuItem = menu.findItem(R.id.action_close)
closeMenuItem.title = buttonText
}
Expand All @@ -65,6 +83,7 @@ class PaymentAuthWebViewActivity : AppCompatActivity() {
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
logger.debug("PaymentAuthWebViewActivity#onOptionsItemSelected()")
if (item.itemId == R.id.action_close) {
finish()
return true
Expand All @@ -73,13 +92,16 @@ class PaymentAuthWebViewActivity : AppCompatActivity() {
}

private fun customizeToolbar(toolbar: Toolbar) {
logger.debug("PaymentAuthWebViewActivity#customizeToolbar()")
toolbarCustomization?.let { toolbarCustomization ->
if (!toolbarCustomization.headerText.isNullOrBlank()) {
logger.debug("PaymentAuthWebViewActivity#customizeToolbar() - updating toolbar title")
toolbar.title = CustomizeUtils.buildStyledText(this,
toolbarCustomization.headerText, toolbarCustomization)
}

toolbarCustomization.backgroundColor?.let { backgroundColor ->
logger.debug("PaymentAuthWebViewActivity#customizeToolbar() - updating toolbar background color")
@ColorInt val backgroundColorInt = Color.parseColor(backgroundColor)
toolbar.setBackgroundColor(backgroundColorInt)
CustomizeUtils.setStatusBarColor(this, backgroundColorInt)
Expand Down
3 changes: 3 additions & 0 deletions stripe/src/test/java/com/stripe/android/FakeLogger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.stripe.android

internal class FakeLogger : Logger {
override fun debug(msg: String) {
}

override fun error(msg: String, t: Throwable?) {
}

Expand Down
Loading