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

Enable webview for legacy payments #282

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 @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="format_price" formatted="false" translatable="false">THB %.2f</string>
<string name="menu_title_setup" translatable="false">Setup</string>
<string name="payment_cancelled" translatable="false">Payment creation cancelled</string>
<string name="webview_closed" translatable="false">The webView has been closed</string>
<string name="button_choose_payment_method" translatable="false">Choose Payment Method</string>
<string name="button_pay_by_credit_card" translatable="false">Pay by Credit Card</string>
<string name="button_authorize_url" translatable="false">Authorize URL</string>
Expand Down
39 changes: 29 additions & 10 deletions sdk/src/main/java/co/omise/android/ui/AuthorizingPaymentActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
aashishgurung marked this conversation as resolved.
Show resolved Hide resolved
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_EXTERNAL_CODE) {
Expand All @@ -201,6 +214,7 @@ class AuthorizingPaymentActivity : AppCompatActivity() {
}

private fun setupWebView() {
isWebViewSetup = true
setupWebViewClient()
with(webView.settings) {
javaScriptEnabled = true
Expand Down Expand Up @@ -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()
}

Expand All @@ -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
}
}
Loading