Skip to content

Commit

Permalink
Add logging to PaymentAuthWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe committed Oct 15, 2019
1 parent 4d309c5 commit c9c2873
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
23 changes: 21 additions & 2 deletions stripe/src/main/java/com/stripe/android/view/PaymentAuthWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.webkit.URLUtil
import android.webkit.WebResourceRequest
Expand Down Expand Up @@ -40,6 +41,7 @@ internal class PaymentAuthWebView : WebView {
clientSecret: String,
returnUrl: String?
) {
Log.d(TAG, "PaymentAuthWebView#init()")
webViewClient = PaymentAuthWebViewClient(activity, activity.packageManager, progressBar,
clientSecret, returnUrl)
}
Expand All @@ -65,12 +67,15 @@ internal class PaymentAuthWebView : WebView {
private set

override fun onPageFinished(view: WebView, url: String?) {
Log.d(TAG, "PaymentAuthWebViewClient#onPageFinished() - $url")
super.onPageFinished(view, url)

// hide the progress bar here because doing it in `onPageCommitVisible()` potentially
// causes a crash
progressBar.visibility = View.GONE

Log.d(TAG, "PaymentAuthWebViewClient#onPageFinished() - hide progress bar")

if (url != null && isCompletionUrl(url)) {
onAuthCompleted()
}
Expand All @@ -91,10 +96,12 @@ internal class PaymentAuthWebView : WebView {
}

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

return if (isReturnUrl(uri)) {
Log.d(TAG, "PaymentAuthWebViewClient#shouldOverrideUrlLoading() - handle return URL")
onAuthCompleted()
true
} else if ("intent".equals(uri.scheme, ignoreCase = true)) {
Expand All @@ -111,6 +118,7 @@ internal class PaymentAuthWebView : WebView {
}

private fun openIntentScheme(uri: Uri) {
Log.d(TAG, "PaymentAuthWebViewClient#openIntentScheme() - $uri")
try {
openIntent(Intent.parseUri(uri.toString(), Intent.URI_INTENT_SCHEME))
} catch (e: Exception) {
Expand All @@ -119,6 +127,7 @@ internal class PaymentAuthWebView : WebView {
}

private fun openIntent(intent: Intent) {
Log.d(TAG, "PaymentAuthWebViewClient#openIntent() - $intent")
if (intent.resolveActivity(packageManager) != null) {
activity.startActivity(intent)
} else {
Expand All @@ -128,6 +137,7 @@ internal class PaymentAuthWebView : WebView {
}

private fun updateCompletionUrl(uri: Uri) {
Log.d(TAG, "PaymentAuthWebViewClient#updateCompletionUrl() - $uri")
val returnUrlParam = if (isAuthenticateUrl(uri.toString())) {
uri.getQueryParameter(PARAM_RETURN_URL)
} else {
Expand All @@ -148,16 +158,20 @@ internal class PaymentAuthWebView : WebView {
}

private fun isReturnUrl(uri: Uri): Boolean {
Log.d(TAG, "PaymentAuthWebViewClient#isReturnUrl() - $uri")
when {
isPredefinedReturnUrl(uri) -> return true

// If the `userReturnUri` is known, look for URIs that match it.
userReturnUri != null ->
userReturnUri != null -> {
Log.d(TAG, "PaymentAuthWebViewClient#isReturnUrl() - userReturnUri is known")
return userReturnUri.scheme != null &&
userReturnUri.scheme == uri.scheme &&
userReturnUri.host != null &&
userReturnUri.host == uri.host
}
else -> {
Log.d(TAG, "PaymentAuthWebViewClient#isReturnUrl() - userReturnUri is unknown")
// Skip opaque (i.e. non-hierarchical) URIs
if (uri.isOpaque) {
return false
Expand All @@ -167,7 +181,7 @@ internal class PaymentAuthWebView : WebView {
// `payment_intent_client_secret` or `setup_intent_client_secret`
// query parameter, and check if its values matches the given `clientSecret`
// as a query parameter.
val paramNames = uri.queryParameterNames
val paramNames = uri.queryParameterNames.toSet()
val clientSecret = when {
paramNames.contains(PARAM_PAYMENT_CLIENT_SECRET) ->
uri.getQueryParameter(PARAM_PAYMENT_CLIENT_SECRET)
Expand All @@ -186,6 +200,7 @@ internal class PaymentAuthWebView : WebView {
}

private fun onAuthCompleted() {
Log.d(TAG, "PaymentAuthWebViewClient#onAuthCompleted()")
activity.finish()
}

Expand All @@ -205,4 +220,8 @@ internal class PaymentAuthWebView : WebView {
private const val PARAM_RETURN_URL = "return_url"
}
}

companion object {
internal const val TAG: String = "StripeSdk"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
Expand All @@ -30,6 +31,9 @@ public class PaymentAuthWebViewActivity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.d(PaymentAuthWebView.TAG, "PaymentAuthWebViewActivity#onCreate");

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

Expand All @@ -43,6 +47,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

final String clientSecret = getIntent()
.getStringExtra(PaymentAuthWebViewStarter.EXTRA_CLIENT_SECRET);

Log.d(PaymentAuthWebView.TAG,
"PaymentAuthWebViewActivity#onCreate - clientSecret: " + clientSecret);

final String returnUrl = getIntent()
.getStringExtra(PaymentAuthWebViewStarter.EXTRA_RETURN_URL);

Expand All @@ -52,7 +60,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
final PaymentAuthWebView webView = findViewById(R.id.auth_web_view);
final ProgressBar progressBar = findViewById(R.id.auth_web_view_progress_bar);
webView.init(this, progressBar, clientSecret, returnUrl);
webView.loadUrl(getIntent().getStringExtra(PaymentAuthWebViewStarter.EXTRA_AUTH_URL));

final String authUrl = getIntent().getStringExtra(PaymentAuthWebViewStarter.EXTRA_AUTH_URL);
Log.d(PaymentAuthWebView.TAG, "Calling PaymentAuthWebView#init: " + authUrl);
webView.loadUrl(authUrl);
}

@Override
Expand Down

0 comments on commit c9c2873

Please sign in to comment.