From f6e50592f95f1e524574c4f529a441757dbae342 Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Thu, 26 Oct 2023 16:16:13 +0700 Subject: [PATCH 1/2] Enable webview for legacy payments --- .../android/example/CheckoutActivity.java | 5 ++++ app/src/main/res/values/strings.xml | 1 + .../android/ui/AuthorizingPaymentActivity.kt | 23 +++++++++++++++---- 3 files changed, 24 insertions(+), 5 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..837b724ab 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( @@ -59,11 +60,17 @@ class AuthorizingPaymentActivity : AppCompatActivity() { supportActionBar?.title = uiCustomization.uiCustomization.toolbarCustomization?.headerText ?: getString(R.string.title_authorizing_payment) - if (verifier.verifyExternalURL(verifier.authorizedURL)) { - openDeepLink(verifier.authorizedURL) + val authUrl = verifier.authorizedURLString + // check for legacy payments that require web view + if (authUrl.endsWith("/pay")) { + setupWebView() + } else { + // Check if the URL needs to be opened externally + if (verifier.verifyExternalURL(verifier.authorizedURL)) { + openDeepLink(verifier.authorizedURL) + } + observeData() } - - observeData() } @TestOnly @@ -201,6 +208,7 @@ class AuthorizingPaymentActivity : AppCompatActivity() { } private fun setupWebView() { + isWebViewSetup = true setupWebViewClient() with(webView.settings) { javaScriptEnabled = true @@ -237,7 +245,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 +269,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 } } From 986aaa445eeb3044fd86b8e22a82a3dbd2a9ad4e Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Tue, 31 Oct 2023 13:05:58 +0700 Subject: [PATCH 2/2] Extract logic to private functions --- .../android/ui/AuthorizingPaymentActivity.kt | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) 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 837b724ab..59712b18e 100644 --- a/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt +++ b/sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt @@ -56,21 +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) - - val authUrl = verifier.authorizedURLString - // check for legacy payments that require web view - if (authUrl.endsWith("/pay")) { - setupWebView() - } else { - // Check if the URL needs to be opened externally - if (verifier.verifyExternalURL(verifier.authorizedURL)) { - openDeepLink(verifier.authorizedURL) - } - observeData() - } + setupActionBarTitle() + handlePaymentAuthorization() } @TestOnly @@ -185,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) {