Skip to content
Open
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 @@ -5,8 +5,8 @@ Written by `bundle/build.mjs`. Do not edit by hand: rebuild instead.
| | |
|---|---|
| Vault version | 1.0.5 |
| Vault source | commit `9f27670bd23ad7cd44a7d195d4582fe5ee2aca01` |
| Hosted JavaScript before bundling | `bundle/build/vault-fields.js`, 360292 bytes, sha1 `4c8bed0e4b7422d73b014a3910605e6872669c77` |
| Vault source | commit `4e666ce9129988d265659dbaa7f2c0a093330e1e` |
| Hosted JavaScript before bundling | `bundle/build/vault-fields.js`, 361157 bytes, sha1 `a769a4709407575798a6f0ceb0e5db2f67ac2766` |
| react-native | 0.86.0 |
| react | 19.2.3 |
| react-native-svg | 15.15.5 |
Expand All @@ -17,8 +17,8 @@ Written by `bundle/build.mjs`. Do not edit by hand: rebuild instead.

| Platform | File | Bytes | sha1 |
|---|---|---|---|
| android | `android/src/main/assets/hyperswitch-vault-fields.bundle` | 1180275 | `594aab0c38cab2cb486599e8b6c01df12f5181e1` |
| ios | `ios/Resources/hyperswitch-vault-fields.bundle` | 1174926 | `4b4d89474d2956a4d7e59a6561c2cbe784ff7491` |
| android | `android/src/main/assets/hyperswitch-vault-fields.bundle` | 1180321 | `eaab6332475ff0071bf8abc217c47833ce2e112f` |
| ios | `ios/Resources/hyperswitch-vault-fields.bundle` | 1174972 | `c11c1bd726587592aab2640161b9dc5603ace7ef` |

The bundle carries the JavaScript of the react-native, react-native-svg and scan-card versions above.
It runs in the app's own native React Native, react-native-svg and scan-card (when the app has it),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
buildscript {
ext.getExtOrDefault = {name ->
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['HyperswitchVault_' + name]
}

repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:8.7.2"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
}
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "com.facebook.react"

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['HyperswitchVault_' + name]
}

def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['HyperswitchVault_' + name]).toInteger()
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

/*
* The minor version of the React Native this app is built with (86 for 0.86.3), or 0 when it cannot
* be found. Read from React Native's own gradle.properties, as its version is not a Gradle property.
*/
def reactNativeMinorVersion() {
try {
def location = rootProject.ext.has("REACT_NATIVE_NODE_MODULES_DIR") ? file(rootProject.ext.get("REACT_NATIVE_NODE_MODULES_DIR")) : null
if (location == null) {
def packageJson = file(
providers.exec {
workingDir(rootDir)
commandLine("node", "--print", "require.resolve('react-native/package.json')")
}.standardOutput.asText.get().trim()
)
location = packageJson.parentFile
}
def properties = new Properties()
file("$location/ReactAndroid/gradle.properties").withInputStream { properties.load(it) }
def version = properties.getProperty("VERSION_NAME")
return version.startsWith("0.0.0-") ? 1000 : version.split("\\.")[1].toInteger()
} catch (Exception ignored) {
return 0
}
}

/*
* Does this app get the vault's own engine (each card field a native view)? Only when all hold:
* - the new architecture is on;
* - React Native is 0.82 or newer, which is what the bundle is built for;
* - the app runs Hermes. The vault's engine is a Hermes instance, and in an app built without
* Hermes React Native's Gradle plugin leaves the Hermes libraries out of the APK, so starting
* the engine there would crash the app. Not set means on: Hermes is React Native's default.
* Otherwise the library compiles src/stub instead: one empty package and no bundle. The app still
* builds, but it has NO card fields: the card fields are native only (decided 2026-12-02), so the
* card form reports unsupported_configuration at run time. This build says so, and why.
*
* There is no opt-out any more. HyperswitchVault_nativeFields is no longer read; an app that still
* sets it is told so.
*/
def MIN_REACT_NATIVE_MINOR = 82
def reactNativeMinor = reactNativeMinorVersion()
def hermes = !project.hasProperty("hermesEnabled") || project.property("hermesEnabled").toString().toLowerCase() != "false"
def hostsOwnEngine = isNewArchitectureEnabled() && reactNativeMinor >= MIN_REACT_NATIVE_MINOR && hermes
if (project.hasProperty("HyperswitchVault_nativeFields")) {
logger.warn("[@juspay-tech/react-native-hyperswitch-vault] HyperswitchVault_nativeFields is no longer read: " +
"the card fields are native only. Remove it from gradle.properties.")
}
if (!hostsOwnEngine) {
logger.warn("[@juspay-tech/react-native-hyperswitch-vault] The card fields are NOT available in this build " +
"(new architecture: ${isNewArchitectureEnabled()}, React Native minor: ${reactNativeMinor}, Hermes: ${hermes}). " +
"They need the new architecture, Hermes and React Native 0.82 to 0.88. " +
"The card form will report unsupported_configuration and draw no field.")
}

android {
namespace "io.hyperswitch.vault"

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}

buildFeatures {
buildConfig true
}

sourceSets.main {
java.srcDirs = hostsOwnEngine ? ["src/main/java"] : ["src/stub/java"]
assets.srcDirs = hostsOwnEngine ? ["src/main/assets"] : []
}

buildTypes {
release {
minifyEnabled false
}
}

lintOptions {
disable "GradleCompatible"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}

repositories {
mavenCentral()
google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
implementation "com.facebook.react:react-android"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

react {
jsRootDir = file("../src/specs/")
libraryName = "HyperswitchVaultSpec"
codegenJavaPackageName = "io.hyperswitch.vault"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HyperswitchVault_kotlinVersion=2.0.21
HyperswitchVault_minSdkVersion=24
HyperswitchVault_targetSdkVersion=34
HyperswitchVault_compileSdkVersion=35
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package io.hyperswitch.vault

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.FrameLayout
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.ReactCompoundViewGroup
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.ViewManagerDelegate
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.events.Event
import com.facebook.react.viewmanagers.HyperswitchVaultFieldViewManagerDelegate
import com.facebook.react.viewmanagers.HyperswitchVaultFieldViewManagerInterface

/*
* The native view the merchant's engine places for one card field. It is an empty frame in the
* merchant's layout; inside it sits a root of the vault's own engine, and that root draws the
* field. The merchant's engine sets three props and hears one event; it never sees what is typed.
*/
class HyperswitchVaultFieldViewManager :
SimpleViewManager<VaultFieldView>(),
HyperswitchVaultFieldViewManagerInterface<VaultFieldView> {

private val delegate: ViewManagerDelegate<VaultFieldView> = HyperswitchVaultFieldViewManagerDelegate(this)

override fun getDelegate(): ViewManagerDelegate<VaultFieldView> = delegate

override fun getName(): String = NAME

override fun createViewInstance(context: ThemedReactContext) = VaultFieldView(context)

@ReactProp(name = "formId")
override fun setFormId(view: VaultFieldView, value: String?) {
view.formId = value
}

@ReactProp(name = "elementType")
override fun setElementType(view: VaultFieldView, value: String?) {
view.elementType = value
}

@ReactProp(name = "settings")
override fun setSettings(view: VaultFieldView, value: String?) {
view.settings = value
}

/* Once per batch of prop changes, after every setter above has run. */
override fun onAfterUpdateTransaction(view: VaultFieldView) {
super.onAfterUpdateTransaction(view)
view.render()
}

/*
* The merchant's React Native treats this view as the focused text input while its field has the
* keyboard, and sends it the commands it would send a text input: "blur" when the user taps
* outside a field or the app calls Keyboard.dismiss(), "focus" to give it the keyboard. The real
* input is in the vault's engine, so the command is passed on to the field there.
*/
override fun receiveCommand(view: VaultFieldView, commandId: String, args: ReadableArray?) {
when (commandId) {
"blur", "focus" -> view.command(commandId)
else -> super.receiveCommand(view, commandId, args)
}
}

override fun onDropViewInstance(view: VaultFieldView) {
view.stop()
super.onDropViewInstance(view)
}

override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> =
mutableMapOf(FIELD_EVENT to mutableMapOf("registrationName" to "onFieldEvent"))

companion object {
const val NAME = "HyperswitchVaultFieldView"
const val FIELD_EVENT = "topFieldEvent"
}
}

@SuppressLint("ViewConstructor")
class VaultFieldView(private val reactContext: ThemedReactContext) :
FrameLayout(reactContext),
ReactCompoundViewGroup {
var formId: String? = null
var elementType: String? = null
var settings: String? = null

/*
* A touch on this view belongs to this view, as far as the merchant's engine is concerned. To
* find what was touched, the merchant's React Native walks down the Android views under the
* finger and takes the id of the deepest one. Left alone it would walk into the child added in
* render(), whose views belong to the vault's engine, and come back with one of their ids. Both
* engines number their views from the same small integers, so the merchant's engine would then
* press whichever of its own views has that number: a button somewhere else on the screen.
* These two answers stop the walk here. The vault's engine still gets the touch, the normal
* Android way, through its own root view.
*/
override fun interceptsTouchEvent(touchX: Float, touchY: Float): Boolean = true

override fun reactTagForTouch(touchX: Float, touchY: Float): Int = id

private var surface: VaultSurface? = null

/* Which form and field the running root was started for, and the props it last received. */
private var startedFor: String? = null
private var renderedSettings: String? = null

private val events = VaultEventTarget(::onEvent)

fun render() {
val formId = formId?.takeIf { it.isNotEmpty() } ?: return
val elementType = elementType?.takeIf { it.isNotEmpty() } ?: return
val settings = settings ?: "{}"
val identity = "$formId/$elementType"

val running = surface
if (running != null && startedFor == identity) {
/* Same field: new props for the running root, which keeps what was typed. */
if (settings != renderedSettings) {
renderedSettings = settings
running.update(propsOf(formId, elementType, settings))
}
return
}
/* A different form or field in this view: the old root goes. */
if (running != null) stop()

/* Read now, not kept. With no Activity yet, the next prop batch or attach tries again. */
val activity = reactContext.currentActivity ?: return
/* The form reports a missing bundle or a stopped engine; its fields just stay empty. */
if (!VaultEngine.isBundled(activity)) return
val engine = VaultEngine.get(activity.application) ?: return
if (engine.failed) return
engine.follow(activity)

val started = engine.start(reactContext, VaultProtocol.FIELD_ROOT, propsOf(formId, elementType, settings), events)
surface = started
startedFor = identity
renderedSettings = settings
started.view?.let { addView(it, LayoutParams(MATCH_PARENT, WRAP_CONTENT)) }
}

/* A command for this view's field, sent through its form. Nothing waits for the answer. */
fun command(name: String) {
val formId = formId?.takeIf { it.isNotEmpty() } ?: return
val elementType = elementType?.takeIf { it.isNotEmpty() } ?: return
VaultForms.get(formId)?.command(name, elementType) {}
}

fun stop() {
surface?.stop()
surface = null
startedFor = null
renderedSettings = null
removeAllViews()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (surface == null) render()
}

/*
* The merchant's React Native lays this view out, and knows nothing of the child added above, so
* it never measures it. The child is measured here instead: as wide as this view, as tall as its
* own content. The merchant's side then makes this view that tall (VAULT_FIELD_LAYOUT).
*/
override fun requestLayout() {
super.requestLayout()
post(measureAndLayout)
}

override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {
super.onSizeChanged(width, height, oldWidth, oldHeight)
if (width != oldWidth) post(measureAndLayout)
}

private val measureAndLayout = Runnable {
val child = getChildAt(0) ?: return@Runnable
if (width == 0) return@Runnable
child.measure(
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
)
child.layout(0, 0, width, child.measuredHeight)
}

/* From the field's root, on the main thread: on to the merchant's engine as this view's event. */
private fun onEvent(name: String, payloadJson: String) {
UIManagerHelper.getEventDispatcherForReactTag(reactContext, id)
?.dispatchEvent(FieldEvent(UIManagerHelper.getSurfaceId(this), id, name, payloadJson))
}

private fun propsOf(formId: String, elementType: String, settings: String) = Bundle().apply {
putString("formId", formId)
putString("elementType", elementType)
putInt("protocolVersion", VaultProtocol.VERSION)
putString("settings", settings)
}

private class FieldEvent(surfaceId: Int, viewId: Int, private val name: String, private val payloadJson: String) :
Event<FieldEvent>(surfaceId, viewId) {
override fun getEventName() = HyperswitchVaultFieldViewManager.FIELD_EVENT

/*
* Every field event must arrive. React Native merges events of one name on one view and keeps
* only the newest, which is right for a scroll position and wrong here: all of a field's events
* share this one name, so its height would be thrown away whenever "ready" came right after it.
*/
override fun canCoalesce() = false

override fun getEventData(): WritableMap = Arguments.createMap().apply {
putString("name", name)
putString("payload", payloadJson)
}
}
}
Loading