Skip to content

Commit

Permalink
feat(identity): Java to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlabo committed Dec 5, 2024
1 parent 9f61469 commit d1e34db
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 184 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.getcapacitor.community.stripe.identity

import android.app.Activity
import android.content.Context
import androidx.core.util.Supplier
import com.getcapacitor.Bridge
import com.getcapacitor.JSObject
import com.getcapacitor.Logger
import com.getcapacitor.PluginCall
import com.getcapacitor.community.stripe.identity.models.Executor
import com.google.android.gms.common.util.BiConsumer
import com.stripe.android.identity.IdentityVerificationSheet

class StripeIdentity(
contextSupplier: Supplier<Context?>,
activitySupplier: Supplier<Activity>,
notifyListenersFunction: BiConsumer<String, JSObject>,
pluginLogTag: String
) : Executor(
contextSupplier,
activitySupplier,
notifyListenersFunction,
pluginLogTag,
"StripeIdentityExecutor"
) {
var verificationSheet: IdentityVerificationSheet? = null
private val emptyObject = JSObject()

private var verificationId: String? = null
private var ephemeralKeySecret: String? = null

init {
this.contextSupplier = contextSupplier
}

fun initialize(call: PluginCall) {
call.resolve()
}

fun create(call: PluginCall) {
verificationId = call.getString("verificationId", null)
ephemeralKeySecret = call.getString("ephemeralKeySecret", null)

if (verificationId == null || ephemeralKeySecret == null) {
val errorText =
"Invalid Params. This method require verificationId or ephemeralKeySecret."
notifyListeners(
IdentityVerificationSheetEvent.FailedToLoad.webEventName,
JSObject().put("error", errorText)
)
call.reject(errorText)
return
}

this.notifyListeners(IdentityVerificationSheetEvent.Loaded.webEventName, emptyObject)
call.resolve()
}

fun present(call: PluginCall) {
try {
verificationSheet!!.present(
verificationId!!,
ephemeralKeySecret!!
)
Logger.info("Presented Identity Verification Sheet")
} catch (ex: Exception) {
call.reject(ex.localizedMessage, ex)
}
}

fun onVerificationCompleted(bridge: Bridge, callbackId: String?) {
val call = bridge.getSavedCall(callbackId)
notifyListeners(IdentityVerificationSheetEvent.Completed.webEventName, emptyObject)
call.resolve(
JSObject().put(
"identityVerificationResult",
IdentityVerificationSheetEvent.Completed.webEventName
)
)
}

fun onVerificationCancelled(bridge: Bridge, callbackId: String?) {
val call = bridge.getSavedCall(callbackId)
notifyListeners(IdentityVerificationSheetEvent.Canceled.webEventName, emptyObject)
call.resolve(
JSObject().put(
"identityVerificationResult",
IdentityVerificationSheetEvent.Canceled.webEventName
)
)
}

fun onVerificationFailed(bridge: Bridge, callbackId: String?) {
val call = bridge.getSavedCall(callbackId)
notifyListeners(IdentityVerificationSheetEvent.Failed.webEventName, emptyObject)
call.resolve(
JSObject().put(
"identityVerificationResult",
IdentityVerificationSheetEvent.Failed.webEventName
)
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.getcapacitor.community.stripe.identity

import android.content.ContentResolver
import android.net.Uri
import com.getcapacitor.JSObject
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.CapacitorPlugin
import com.stripe.android.identity.IdentityVerificationSheet
import com.stripe.android.identity.IdentityVerificationSheet.Companion.create
import com.stripe.android.identity.IdentityVerificationSheet.VerificationFlowResult

@CapacitorPlugin(name = "StripeIdentity")
class StripeIdentityPlugin : Plugin() {
private var identityVerificationCallbackId: String? = null

private val implementation = StripeIdentity(
{ this.context },
{ this.activity },
{ eventName: String?, data: JSObject? -> this.notifyListeners(eventName, data) },
logTag
)

override fun load() {
val resources = activity.applicationContext.resources
val resourceId = resources.getIdentifier("ic_launcher", "mipmap", activity.packageName)
val icon = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(resourceId))
.appendPath(resources.getResourceTypeName(resourceId))
.appendPath(resources.getResourceEntryName(resourceId))
.build()

implementation.verificationSheet = create(
activity,
IdentityVerificationSheet.Configuration(icon)
) { verificationFlowResult: VerificationFlowResult? ->
// handle verificationResult
if (verificationFlowResult is VerificationFlowResult.Completed) {
// The user has completed uploading their documents.
// Let them know that the verification is processing.
implementation.onVerificationCompleted(bridge, identityVerificationCallbackId)
} else if (verificationFlowResult is VerificationFlowResult.Canceled) {
// The user did not complete uploading their documents.
// You should allow them to try again.
implementation.onVerificationCancelled(bridge, identityVerificationCallbackId)
} else if (verificationFlowResult is VerificationFlowResult.Failed) {
// If the flow fails, you should display the localized error
// message to your user using throwable.getLocalizedMessage()
implementation.onVerificationFailed(bridge, identityVerificationCallbackId)
}
}
}

@PluginMethod
fun initialize(call: PluginCall) {
implementation.initialize(call)
}

@PluginMethod
fun create(call: PluginCall) {
implementation.create(call)
}

@PluginMethod
fun present(call: PluginCall) {
identityVerificationCallbackId = call.callbackId
bridge.saveCall(call)

implementation.present(call)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.getcapacitor.community.stripe.identity.models

import android.app.Activity
import android.content.Context
import androidx.core.util.Supplier
import com.getcapacitor.JSObject
import com.google.android.gms.common.util.BiConsumer

abstract class Executor(
protected var contextSupplier: Supplier<Context?>,
protected val activitySupplier: Supplier<Activity>,
protected var notifyListenersFunction: BiConsumer<String, JSObject>,
pluginLogTag: String,
executorTag: String
) {
protected val logTag: String = "$pluginLogTag|$executorTag"

// Eventually we can change the notification directly here!
protected fun notifyListeners(eventName: String, data: JSObject) {
notifyListenersFunction.accept(eventName, data)
}
}

0 comments on commit d1e34db

Please sign in to comment.