From 0cef83dbbafbee8ede2439a3d538dddb90f6c90e Mon Sep 17 00:00:00 2001 From: Anas Naouchi <113893333+AnasNaouchi@users.noreply.github.com> Date: Wed, 1 Nov 2023 08:29:01 +0700 Subject: [PATCH] Enable webview for legacy payments (#282) * Enable webview for legacy payments * Extract logic to private functions --- .../android/example/CheckoutActivity.java | 5 +++ app/src/main/res/values/strings.xml | 1 + .../android/ui/AuthorizingPaymentActivity.kt | 39 ++++++++++++++----- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/src/java/java/co/omise/android/example/CheckoutActivity.java b/app/src/java/java/co/omise/android/example/CheckoutActivity.java index 0ecd67116..48ce66185 100644 --- a/app/src/java/java/co/omise/android/example/CheckoutActivity.java +++ b/app/src/java/java/co/omise/android/example/CheckoutActivity.java @@ -230,6 +230,11 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // custom result code when web view is closed + if (resultCode == AuthorizingPaymentActivity.WEBVIEW_CLOSED_RESULT_CODE) { + snackbar.setText(R.string.webview_closed).show(); + return; + } if (resultCode == RESULT_CANCELED) { snackbar.setText(R.string.payment_cancelled).show(); return; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index af7bb8a0a..66b8d3d12 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ THB %.2f Setup Payment creation cancelled + The webView has been closed Choose Payment Method Pay by Credit Card Authorize URL diff --git a/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt b/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt index 46829b7aa..59712b18e 100644 --- a/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt +++ b/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt @@ -42,6 +42,7 @@ class AuthorizingPaymentActivity : AppCompatActivity() { private val webView: WebView by lazy { authorizing_payment_webview } private val verifier: AuthorizingPaymentURLVerifier by lazy { AuthorizingPaymentURLVerifier(intent) } private val uiCustomization: UiCustomization by lazy { intent.getParcelableExtra(EXTRA_UI_CUSTOMIZATION) ?: UiCustomization.default } + private var isWebViewSetup = false private val viewModel: AuthorizingPaymentViewModel by viewModels { viewModelFactory ?: AuthorizingPaymentViewModelFactory( @@ -55,15 +56,8 @@ class AuthorizingPaymentActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_authorizing_payment) - - supportActionBar?.title = uiCustomization.uiCustomization.toolbarCustomization?.headerText - ?: getString(R.string.title_authorizing_payment) - - if (verifier.verifyExternalURL(verifier.authorizedURL)) { - openDeepLink(verifier.authorizedURL) - } - - observeData() + setupActionBarTitle() + handlePaymentAuthorization() } @TestOnly @@ -178,6 +172,25 @@ class AuthorizingPaymentActivity : AppCompatActivity() { } } + private fun setupActionBarTitle() { + supportActionBar?.title = uiCustomization.uiCustomization.toolbarCustomization?.headerText + ?: getString(R.string.title_authorizing_payment) + } + + private fun handlePaymentAuthorization() { + val authUrlString = verifier.authorizedURLString + val authUrl=verifier.authorizedURL + // check for legacy payments that require web view + if (authUrlString.endsWith("/pay")) { + setupWebView() + } else { + // Check if the URL needs to be opened externally + if (verifier.verifyExternalURL(authUrl)) { + openDeepLink(authUrl) + } + observeData() + } + } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_EXTERNAL_CODE) { @@ -201,6 +214,7 @@ class AuthorizingPaymentActivity : AppCompatActivity() { } private fun setupWebView() { + isWebViewSetup = true setupWebViewClient() with(webView.settings) { javaScriptEnabled = true @@ -237,7 +251,7 @@ class AuthorizingPaymentActivity : AppCompatActivity() { } private fun finishActivityWithSuccessful(data: Intent?) { - setResult(Activity.RESULT_OK, data) + setResult(if (isWebViewSetup) AuthorizingPaymentActivity.WEBVIEW_CLOSED_RESULT_CODE else Activity.RESULT_OK, data) finish() } @@ -261,5 +275,10 @@ class AuthorizingPaymentActivity : AppCompatActivity() { * This is an optional parameter. If not provided, the default UI will be used. */ const val EXTRA_UI_CUSTOMIZATION = "OmiseActivity.uiCustomization" + + /** + * A new result code that is not in the default Activity values to indicate that the web view has been closed after the authorization url has been opened using web view + */ + const val WEBVIEW_CLOSED_RESULT_CODE = 5 } }