Last active
October 12, 2020 09:57
-
-
Save kariot/9eedb2b6c685e65605d84fade9fcee41 to your computer and use it in GitHub Desktop.
MasterCard Gateway Android SDK 3DS Secure Auth in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GatewaySecureActivity : AppCompatActivity() { | |
companion object{ | |
/** | |
* The HTML used to initialize the WebView. Should be the HTML content returned from the Gateway | |
* during the Check 3DS Enrollment call | |
*/ | |
val EXTRA_HTML = "com.mastercard.gateway.android.HTML" | |
/** | |
* An OPTIONAL title to display in the toolbar for this activity | |
*/ | |
val EXTRA_TITLE = "com.mastercard.gateway.android.TITLE" | |
/** | |
* The ACS Result data after performing 3DS | |
*/ | |
val EXTRA_ACS_RESULT = "com.mastercard.gateway.android.ACS_RESULT" | |
val REDIRECT_SCHEME = "gatewaysdk" | |
} | |
lateinit var toolbar: Toolbar | |
lateinit var webView: WebView | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_gateway_secure) | |
// init toolbar | |
toolbar = findViewById(R.id.toolbar) | |
toolbar.setNavigationOnClickListener { view -> onBackPressed() } | |
// init web view | |
// init web view | |
webView = findViewById(R.id.webview) | |
webView.webChromeClient = WebChromeClient() | |
webView.settings.domStorageEnabled = true | |
webView.settings.javaScriptEnabled = true | |
webView.webViewClient = buildWebViewClient() | |
init() | |
} | |
fun init() { | |
// init html | |
val extraHtml: String? = getExtraHtml() | |
if (extraHtml == null) { | |
onBackPressed() | |
return | |
} else { | |
setWebViewHtml(extraHtml) | |
} | |
// init title | |
// val defaultTitle: String = getDefaultTitle() | |
// val extraTitle: String = getExtraTitle() | |
// setToolbarTitle(extraTitle ?: defaultTitle) | |
} | |
fun getDefaultTitle(): String? { | |
return getString(com.mastercard.gateway.android.sdk.R.string.gateway_3d_secure_authentication) | |
} | |
fun getExtraTitle(): String? { | |
val extras = intent.extras | |
return extras?.getString(GatewaySecureActivity.EXTRA_TITLE) | |
} | |
fun getExtraHtml(): String? { | |
val extras = intent.extras | |
return extras?.getString(GatewaySecureActivity.EXTRA_HTML) | |
} | |
fun setToolbarTitle(title: String?) { | |
toolbar.title = title | |
} | |
fun setWebViewHtml(html: String?) { | |
webview.webViewClient = object : WebViewClient(){ | |
override fun onReceivedError( | |
view: WebView?, | |
request: WebResourceRequest?, | |
error: WebResourceError? | |
) { | |
Log.e("WEBVIEW ERR",error.toString()) | |
super.onReceivedError(view, request, error) | |
} | |
} | |
webView.loadDataWithBaseURL(null,html, "text/html", "utf-8",null) | |
} | |
fun webViewUrlChanges(uri: Uri) { | |
val scheme = uri.scheme | |
if (REDIRECT_SCHEME.equals(scheme, ignoreCase = true)) { | |
complete(getACSResultFromUri(uri)) | |
} else if ("mailto".equals(scheme, ignoreCase = true)) { | |
intentToEmail(uri) | |
} else { | |
loadWebViewUrl(uri) | |
} | |
} | |
fun complete(acsResult: String?) { | |
val intent = Intent() | |
complete(acsResult, intent) | |
} | |
fun complete(acsResult: String?, intent: Intent) { | |
intent.putExtra(GatewaySecureActivity.EXTRA_ACS_RESULT, acsResult) | |
setResult(Activity.RESULT_OK, intent) | |
finish() | |
} | |
fun loadWebViewUrl(uri: Uri) { | |
webView.loadUrl(uri.toString()) | |
} | |
fun intentToEmail(uri: Uri?) { | |
val emailIntent = Intent(Intent.ACTION_SENDTO) | |
intentToEmail(uri, emailIntent) | |
} | |
// separate for testability | |
fun intentToEmail(uri: Uri?, intent: Intent) { | |
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK | |
intent.data = uri | |
startActivity(intent) | |
} | |
fun getACSResultFromUri(uri: Uri): String? { | |
var result: String? = null | |
val params = uri.queryParameterNames | |
for (param in params) { | |
if ("acsResult".equals(param, ignoreCase = true)) { | |
result = uri.getQueryParameter(param) | |
} | |
} | |
return result | |
} | |
fun buildWebViewClient(): WebViewClient? { | |
return object : WebViewClient() { | |
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { | |
webViewUrlChanges(Uri.parse(url)) | |
return true | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment