diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.java b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.java deleted file mode 100644 index c5ebd8285..000000000 --- a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.java +++ /dev/null @@ -1,77 +0,0 @@ -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; - -public class StripeIdentity extends Executor { - - public IdentityVerificationSheet verificationSheet; - private final JSObject emptyObject = new JSObject(); - - private String verificationId; - private String ephemeralKeySecret; - - public StripeIdentity( - Supplier contextSupplier, - Supplier activitySupplier, - BiConsumer notifyListenersFunction, - String pluginLogTag - ) { - super(contextSupplier, activitySupplier, notifyListenersFunction, pluginLogTag, "StripeIdentityExecutor"); - this.contextSupplier = contextSupplier; - } - - public void initialize(final PluginCall call) { - call.resolve(); - } - - public void create(final PluginCall call) { - verificationId = call.getString("verificationId", null); - ephemeralKeySecret = call.getString("ephemeralKeySecret", null); - - if (verificationId == null || ephemeralKeySecret == null) { - String errorText = "Invalid Params. This method require verificationId or ephemeralKeySecret."; - notifyListeners(IdentityVerificationSheetEvent.FailedToLoad.getWebEventName(), new JSObject().put("error", errorText)); - call.reject(errorText); - return; - } - - this.notifyListeners(IdentityVerificationSheetEvent.Loaded.getWebEventName(), emptyObject); - call.resolve(); - } - - public void present(final PluginCall call) { - try { - verificationSheet.present(this.verificationId, this.ephemeralKeySecret); - Logger.info("Presented Identity Verification Sheet"); - } catch (Exception ex) { - call.reject(ex.getLocalizedMessage(), ex); - } - } - - public void onVerificationCompleted(Bridge bridge, String callbackId) { - PluginCall call = bridge.getSavedCall(callbackId); - notifyListeners(IdentityVerificationSheetEvent.Completed.getWebEventName(), emptyObject); - call.resolve(new JSObject().put("identityVerificationResult", IdentityVerificationSheetEvent.Completed.getWebEventName())); - } - - public void onVerificationCancelled(Bridge bridge, String callbackId) { - PluginCall call = bridge.getSavedCall(callbackId); - notifyListeners(IdentityVerificationSheetEvent.Canceled.getWebEventName(), emptyObject); - call.resolve(new JSObject().put("identityVerificationResult", IdentityVerificationSheetEvent.Canceled.getWebEventName())); - } - - public void onVerificationFailed(Bridge bridge, String callbackId) { - PluginCall call = bridge.getSavedCall(callbackId); - notifyListeners(IdentityVerificationSheetEvent.Failed.getWebEventName(), emptyObject); - call.resolve(new JSObject().put("identityVerificationResult", IdentityVerificationSheetEvent.Failed.getWebEventName())); - } -} diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.kt b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.kt new file mode 100644 index 000000000..4956f3bfa --- /dev/null +++ b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentity.kt @@ -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, + activitySupplier: Supplier, + notifyListenersFunction: BiConsumer, + 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 + ) + ) + } +} diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.java b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.java deleted file mode 100644 index 9a3a14a03..000000000 --- a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.getcapacitor.community.stripe.identity; - -import android.content.ContentResolver; -import android.content.res.Resources; -import android.net.Uri; -import com.getcapacitor.Plugin; -import com.getcapacitor.PluginCall; -import com.getcapacitor.PluginMethod; -import com.getcapacitor.annotation.CapacitorPlugin; -import com.stripe.android.identity.IdentityVerificationSheet; - -@CapacitorPlugin(name = "StripeIdentity") -public class StripeIdentityPlugin extends Plugin { - - private String identityVerificationCallbackId; - - private final StripeIdentity implementation = new StripeIdentity( - this::getContext, - this::getActivity, - this::notifyListeners, - getLogTag() - ); - - @Override - public void load() { - Resources resources = getActivity().getApplicationContext().getResources(); - int resourceId = resources.getIdentifier("ic_launcher", "mipmap", getActivity().getPackageName()); - Uri icon = new Uri.Builder() - .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) - .authority(resources.getResourcePackageName(resourceId)) - .appendPath(resources.getResourceTypeName(resourceId)) - .appendPath(resources.getResourceEntryName(resourceId)) - .build(); - - this.implementation.verificationSheet = IdentityVerificationSheet.Companion.create( - getActivity(), - new IdentityVerificationSheet.Configuration(icon), - verificationFlowResult -> { - // handle verificationResult - if (verificationFlowResult instanceof IdentityVerificationSheet.VerificationFlowResult.Completed) { - // The user has completed uploading their documents. - // Let them know that the verification is processing. - this.implementation.onVerificationCompleted(bridge, identityVerificationCallbackId); - } else if (verificationFlowResult instanceof IdentityVerificationSheet.VerificationFlowResult.Canceled) { - // The user did not complete uploading their documents. - // You should allow them to try again. - this.implementation.onVerificationCancelled(bridge, identityVerificationCallbackId); - } else if (verificationFlowResult instanceof IdentityVerificationSheet.VerificationFlowResult.Failed) { - // If the flow fails, you should display the localized error - // message to your user using throwable.getLocalizedMessage() - this.implementation.onVerificationFailed(bridge, identityVerificationCallbackId); - } - } - ); - } - - @PluginMethod - public void initialize(final PluginCall call) { - implementation.initialize(call); - } - - @PluginMethod - public void create(final PluginCall call) { - implementation.create(call); - } - - @PluginMethod - public void present(final PluginCall call) { - identityVerificationCallbackId = call.getCallbackId(); - bridge.saveCall(call); - - implementation.present(call); - } -} diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.kt b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.kt new file mode 100644 index 000000000..8b70c9aac --- /dev/null +++ b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/StripeIdentityPlugin.kt @@ -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) + } +} diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.java b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.java deleted file mode 100644 index c36c6d6a1..000000000 --- a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.java +++ /dev/null @@ -1,33 +0,0 @@ -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; - -public abstract class Executor { - - protected Supplier contextSupplier; - protected final Supplier activitySupplier; - protected BiConsumer notifyListenersFunction; - protected final String logTag; - - // Eventually we can change the notification directly here! - protected void notifyListeners(String eventName, JSObject data) { - notifyListenersFunction.accept(eventName, data); - } - - public Executor( - Supplier contextSupplier, - Supplier activitySupplier, - BiConsumer notifyListenersFunction, - String pluginLogTag, - String executorTag - ) { - this.contextSupplier = contextSupplier; - this.activitySupplier = activitySupplier; - this.notifyListenersFunction = notifyListenersFunction; - this.logTag = pluginLogTag + "|" + executorTag; - } -} diff --git a/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.kt b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.kt new file mode 100644 index 000000000..c653223b4 --- /dev/null +++ b/packages/identity/android/src/main/java/com/getcapacitor/community/stripe/identity/models/Executor.kt @@ -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, + protected val activitySupplier: Supplier, + protected var notifyListenersFunction: BiConsumer, + 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) + } +}