Skip to content

feat(vault): Android host and the native path for the card fields - #104

Open
Pradeep-kumar1202 wants to merge 10 commits into
feat/vault-native-fieldsfrom
feat/vault-native-android
Open

Pradeep-kumar1202 wants to merge 10 commits into
feat/vault-native-fieldsfrom
feat/vault-native-android

Conversation

@Pradeep-kumar1202

@Pradeep-kumar1202 Pradeep-kumar1202 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Changed on 2 Dec (8f28874): native card fields only. There is no plain fallback in the merchant's app any more. Where the native path cannot run, or a native form stops, the form draws no field, calls a new onError prop once with unsupported_configuration and a reason naming the cause, and refuses every tokenize() with the same code and message (the payment-methods SDK already passes that code on). The opt-out HyperswitchVault_nativeFields is removed; the build stub stays, and an unsupported build now warns in the Gradle log. The package ships an opt-in Jest mock (@juspay-tech/react-native-hyperswitch-vault/jest). Inside an SDK's own engine the plain fields still render. The device results under Testing were run before this change.

The native path for the vault's card fields, on Android, and the JavaScript that chooses it.

In an app rebuilt with this code, on the new architecture, with Hermes, and on a tested React Native version (0.82 to 0.88), each card field becomes a native view whose content runs in the vault's own JavaScript engine. The card number and CVC are typed into that engine and never reach the merchant's. Everywhere else there are no card fields: the form reports unsupported_configuration and why (see "When the native path cannot run").

The merchant's code does not change: same components, same props, same events, same tokenize() result. One optional prop is new: onError, called when the card fields cannot run.

Pull request What it adds
#79 createDetachedCardForm: one card across separate React roots
#103 the JavaScript the vault's own engine runs, and the bundle toolchain
#104 (this) the Android host, and the choice: native fields, or a clear error
#105 the iOS host

Each is based on the one before it. Merge order: #79, #103, #104, #105, then the release pull request (README, release notes, version 2.0.0). Labelled DO NOT MERGE until the stack is ready.

Why

Today the fields are React Native text inputs in the merchant's engine, beside every npm package the merchant installed. hyperswitch-web keeps each field in an iframe from the SDK's domain. This is the React Native equivalent: one engine boundary per form.

How it works

In the merchant's engine (src/native/)

File Job
choosers.mjs What the package root now exports as CardForm and the four fields. A form renders the native path, or (since 2 Dec) a refusing form that draws no field and reports why; a field follows its form
handle.mjs The one handle a CardForm gives its ref, for the life of the form. It forwards each call to the form mounted at that moment, so a form that stops (and is replaced by the refusing form) keeps answering a caller that kept the handle (the payment-methods SDK reads the ref once)
availability.mjs, rule.mjs The rule. Native only when: not inside an SDK's own engine, the native module exists, new architecture, the build carries the bundle, the protocol versions match, and the React Native version is in the tested range
NativeCardForm.mjs Opens a form in the vault's engine, sends prop changes as updates, turns events back into onReady / onChange, and gives the same handle (tokenize, reset, focus)
NativeField.mjs The native view for one field, sized by the height the field reports; same callbacks and handle as a plain field
out.mjs What is sent across: data props as JSON text. Callbacks stay behind. Layout styles of styles.root go on the native view, the rest goes inside. Also reads answers back: a tokenize that already reached the vault's engine and then got no answer is unknown_outcome, never "failed"
fieldView.mjs The native view's config, written by hand (see the note for the reviewer)
focus.mjs Makes a focused card field count as the focused text input in the merchant's React Native, so the keyboard closes from outside

createCardForm() renders the chooser too. fieldView.mjs and focus.mjs reach React Native internals by path; they are required only when a native field first renders, so they never load in apps on the plain path or on the web.

In native code (android/)

File Job
VaultEngine.kt The second React host with its own Hermes instance. Loads only hyperswitch-vault-fields.bundle from the app's assets; packager access and developer support are off in every build type. A fixed package list: React Native's own, react-native-svg's and scan card's when the app has them (found by class name), and the hosted package. If the engine cannot be created, the failure is logged by class name, remembered, never retried, and every form falls back to the plain fields
VaultForm.kt One form: a hidden root, and its commands. Every command is answered exactly once (bundle's answer, form never started, closed, engine stopped, unreachable, timeout). A refusal after the command reached the bundle is marked delivered
HyperswitchVaultFormsModule.kt The module the merchant's engine talks to
HyperswitchVaultHostedModule.kt The module the bundle talks to; exists only in the vault's engine
HyperswitchVaultFieldViewManager.kt The field's native view. It claims touches for itself (ReactCompoundViewGroup), so a tap on a field never resolves to a view of the other engine
src/stub/ and build.gradle A build without the engine (below)

When the native path cannot run (since 2 Dec: no plain fallback)

  • At run time: no native module (app not rebuilt, an over-the-air JavaScript update, Expo Go, tests), old architecture, an untested React Native version, a protocol mismatch, an engine that cannot start, a form that does not come up within 15 seconds, a form that reports an error after it is up, or a field that fails. The form then draws no field (the merchant's own content inside it still draws), calls onError once with unsupported_configuration and a reason naming the cause (never text from the vault's engine), refuses every tokenize() with the same code and message, and says why on the console in a development build. Inside an SDK's own engine (__HYPERSWITCH_SDK_ENGINE__) the plain fields render: that engine is already separate.
  • At build time: below React Native 0.82, on the old architecture, without Hermes (hermesEnabled=false: the engine is a Hermes instance and the app would have no Hermes library), the library compiles src/stub (one empty package, no bundle), so the app still builds, and the Gradle log warns that the card fields are not available in it, and why. There is no opt-out: HyperswitchVault_nativeFields is ignored, with a warning.

Files

  • New: android/ (7 Kotlin files, the stub, build files), src/native/ (9 files), src/specs/ (3 Codegen specs), react-native.config.js, test/native.test.mjs
  • Changed: src/standalone-entry.mjs (root exports bound to the choosers), src/createCardForm.mjs, src/hosted/protocol.mjs and roots.mjs (names shared by both JavaScript sides), rollup.config.mjs (React Native's own files stay external), package.json (codegenConfig, files, test script)
  • Rebuilt: both bundles and BUNDLE-PROVENANCE.md

Impact on existing merchants

  • JavaScript only, no rebuild: the card form draws no field and reports unsupported_configuration ("rebuild the app"). This is a breaking change (hence 2.0.0): apps outside the supported setup lose card fields until they move to it.
  • Merchants' Jest tests that render card fields opt into the mock: jest.mock('@juspay-tech/react-native-hyperswitch-vault', () => require('@juspay-tech/react-native-hyperswitch-vault/jest')).
  • After a rebuild on a supported app: the native path. Same API; the keyboard, dark mode, reset, focus and clear behave as the plain fields do (see Testing).
  • The package now has native code. The README still says "no native step"; that and the version are the release pull request.
  • The published package grows by the Android sources and one 1.18 MB bundle.

What the native path protects, and what it does not

It protects the card number and CVC from code in the merchant's JavaScript that reads or records inputs: a compromised package, a session-replay or analytics SDK. It does not protect against native code in the app, or against code in the merchant's engine written to attack the vault: such code can force the plain fallback, or change the settings it passes (point customEndpoints at its own server, or swap sdkAuthorization). Stopping that needs a design change, not this pull request.

Testing

Since 2 Dec (native fields only): unit tests 91/91; the refusing form checked on a stand-in React Native with React's test renderer (no native module, React Native 0.81, a form that does not start, a form that stops: onError once, tokenize() refused with the same message, the merchant's label still drawn, no text from the vault's engine); the Jest mock rendered. Not yet on a device. The results below were run before 2 Dec, with the plain fallback and the opt-out switch.

Unit tests in the vault: yarn test, 89 pass at this head (11 new here: the rule, what crosses to the other engine, the delivered mapping, the one handle, and the fixed-width box).

On a device: Android 17 emulator, the app installs only the payment-methods SDK, the vault and react-native-svg (the live merchant's setup), with the merchant's screen unchanged. On this code, React Native 0.86, release builds:

Check Result
Three fields draw; two forms in one run tokenize; clear(), reset(), focus() pass
Text changes seen by the app's own JavaScript while typing 0; the same app with the plain fields: 46
Dark mode switched while a card is typed fields re-theme, card kept, tokenizes
Tap outside a field closes the keyboard, as with plain fields
Release build with R8 every check passes
Built with HyperswitchVault_nativeFields=false plain fields, tokenizes
Device log (logcat) after the flows no Updating props for Surface, no sdkAuthorization
Configured with hermesEnabled=false Gradle compiles the stub ("Hermes: false")

Every React Native version, on this code (the whole stack, both platforms, release builds, one row at a time): 35 of 35 rows pass.

React Native Android iOS
0.82, 0.83, 0.84, 0.85, 0.87, 0.88 native path: every check passes, 0 text changes; switch off: plain fields pass native path: every check passes, 0 text changes; switch off: plain fields pass
0.86 as above, plus a release build with R8 as above
0.80, 0.81 (new architecture) stub, plain fields, every check passes stub, plain fields, every check passes
0.79, old architecture stub, plain fields, every check passes empty file, plain fields, every check passes

Device logs after the flows: no Updating props for Surface line and no sdkAuthorization in any of the 80 Android captures, nor on iOS 0.86 and 0.88 (read right after their runs).

Not tested

  • A real phone; an app without Hermes on a device (the build switch was checked: Gradle compiles the stub); an app with scan card installed; the co-badged network picker; TalkBack; an app with both the main SDK and the vault.

Tested since the review (26 Nov, React Native 0.86.3, native and plain paths): the form's first onChange before typing, accessibility labels on the fields, onReady, the cardholder name field, rotation and back, background and back (the card is kept and tokenizes), memory with one form open (+42 MB) and the time to draw the fields (+0.2 s). The merchant's demo app (S2SDemo, React Native 0.87.1, its own code unchanged): a saved card's CVC and a new card with the name on it, both typed into the native fields; both payments succeeded, and the device log held no card number, sdkAuthorization or client secret.

Not in this PR

Note for the reviewer

  • Things the device found, now in comments where they bite: a root's tag exists only after the root starts, so owners are matched when an event arrives (VaultEngine.deliver); Android merges same-named events on one view, so the field event turns that off (canCoalesce); the merchant's React Native must be told a card field is focused, or the keyboard cannot be closed from outside (focus.mjs); and React Native's touch search must not walk into the vault's views, or a tap presses another view of the merchant's screen (VaultFieldView implements ReactCompoundViewGroup).
  • fieldView.mjs is hand-written view config. React Native normally generates it from the spec with a Babel plugin when the app is built; this package ships built JavaScript, so the plugin never sees it. It must match src/specs/HyperswitchVaultFieldViewNativeComponent.ts and the view manager.
  • No opt-out switch: removed on 2 Dec, with the plain fallback.
  • Fixed since the review (26 Nov): a field root's VAULT_FIELD_ERROR, or a form error after the form is up, now stops the form instead of leaving empty boxes (since 2 Dec it reports unsupported_configuration; before, it switched to plain fields), and HyperswitchVaultFormsModule.invalidate() reads the open forms on the main thread (16300bd). A native field whose box has a fixed width (styles.container.width) took the whole row; it now takes the width the plain field would (223bca7, found in S2SDemo's saved-card CVC).
  • Known, not fixed here: whether a tap can also reach an ancestor Pressable of the merchant's screen has not been checked on a device.

@Pradeep-kumar1202
Pradeep-kumar1202 force-pushed the feat/vault-native-android branch from c2ffeff to 6e60c44 Compare November 25, 2026 12:05
@Pradeep-kumar1202
Pradeep-kumar1202 force-pushed the feat/vault-native-android branch from 6e60c44 to 223bca7 Compare November 30, 2026 04:55
Pradeep-kumar1202 added a commit that referenced this pull request Dec 2, 2026
… command can see the old ones

The form room applied a settings update from native in a passive effect, which runs a moment after
the render commits. A tokenize command handled in that moment saw the previous settings, and so
the previous session, as current. The update now runs in a layout effect, while the render commits;
from then on form.tokenize() (#79's createDetachedCardForm) waits for the Host to draw the new
settings, so the command uses the new session.

Not covered here: a command that native code sends before the props update has reached the engine
at all. Keeping that order is the native hosts' part (#104, #105).
Pradeep-kumar1202 added a commit that referenced this pull request Dec 3, 2026
… command can see the old ones

The form room applied a settings update from native in a passive effect, which runs a moment after
the render commits. A tokenize command handled in that moment saw the previous settings, and so
the previous session, as current. The update now runs in a layout effect, while the render commits;
from then on form.tokenize() (#79's createDetachedCardForm) waits for the Host to draw the new
settings, so the command uses the new session.

Not covered here: a command that native code sends before the props update has reached the engine
at all. Keeping that order is the native hosts' part (#104, #105).
@Pradeep-kumar1202
Pradeep-kumar1202 force-pushed the feat/vault-native-android branch from 8f28874 to 5bc745b Compare December 3, 2026 05:51
Pradeep-kumar1202 added a commit that referenced this pull request Dec 3, 2026
… command can see the old ones

The form room applied a settings update from native in a passive effect, which runs a moment after
the render commits. A tokenize command handled in that moment saw the previous settings, and so
the previous session, as current. The update now runs in a layout effect, while the render commits;
from then on form.tokenize() (#79's createDetachedCardForm) waits for the Host to draw the new
settings, so the command uses the new session.

Not covered here: a command that native code sends before the props update has reached the engine
at all. Keeping that order is the native hosts' part (#104, #105).
Each field becomes a native view whose content runs in the vault's own
JavaScript engine, when the app was rebuilt with this code, is on the new
architecture and on a tested React Native version. Everywhere else the
fields render in plain JavaScript as before.

android/: the engine, forms, the field view and two modules.
src/native/: the availability rule, the native form and field, and the
choosers the package root now exports. createCardForm goes through the
chooser too.
…ut the engine

The merchant's React Native now sees a focused card field as its focused
text input, so tapping outside a field and Keyboard.dismiss() close the
keyboard as they do for the plain fields. The native view passes the blur
and focus commands on to the field in the vault's engine.

android/build.gradle compiles a stub (one empty package, no bundle) when
the app is below React Native 0.82, on the old architecture, or sets
HyperswitchVault_nativeFields=false. The app still builds and the fields
render in plain JavaScript.
…ew on Android

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. VaultFieldView
let that walk go into the child hosting the vault engine's surface, so it came
back with an id from the vault's engine. Both engines number their views from
the same small integers, so the merchant's engine pressed whichever of its own
views had that number: a button elsewhere on the screen, or another field.

VaultFieldView now implements ReactCompoundViewGroup: interceptsTouchEvent
returns true and reactTagForTouch returns the view's own id, so the walk stops
at the field. The vault's engine still gets the touch through its own root view.

Tested on the Android emulator, RN 0.82, 0.83, 0.84, 0.85, 0.86 (also a release
build with R8), 0.87 and 0.88: main flow, keyboard, dark mode and vault API
checks pass, and the merchant's engine sees no card text.
…a tokenize already delivered, scan card in the engine

The vault's engine is a Hermes instance. In an app built with
hermesEnabled=false React Native's Gradle plugin leaves the Hermes libraries
out, so the first card field crashed the app. build.gradle now compiles the
stub (plain fields) when Hermes is off, and VaultEngine.get catches any
failure to create the engine, logs its class name, never retries, and the
form falls back to the plain fields.

A command that reached the bundle and then timed out (90 s), or whose form
closed or engine stopped, is refused with "delivered": true. A tokenize may
have saved the card by then; the merchant's side reports unknown_outcome for
it, as the plain path does when a request is cut off. The comment that called
90 s longer than the vault's network timeouts was wrong: tokenize has none.

When the app has @juspay-tech/react-native-hyperswitch-scancard, its package
is added to the engine by class name, as react-native-svg's is, so the number
field's scan button works on the native path.
…eact Native internals loaded only on the native path

The chooser gives its ref one handle for the life of the form
(src/native/handle.mjs). A native form that fails and switches to the plain
fields no longer leaves a caller holding a dead handle: the payment-methods
SDK reads the ref once, so every tokenize() after a fallback was refused with
"The card form is not open." The handle forwards each call to the form
mounted at that moment, and confirmPayment answers unsupported_configuration
on the native path instead of being missing.

tokenizeResultOf maps a refusal marked delivered to unknown_outcome, and
builds its results with VaultResult's own helpers.

fieldView.mjs and focus.mjs require React Native's internal files the first
time a native field renders, not when the vault's root is imported. Imported
at the top they ran in every app, and on the web (hyperswitch-client-core
bundles the vault for a browser) they throw while loading.
…s back to the plain fields

Every VAULT_FORM_ERROR after mount means the vault's form is gone (a crash in its root, a stopped
engine, settings it cannot read), and so is whatever was typed. The native form then left blank
fields and a tokenize() that blamed the payer. It now takes the same fallback as before mount, and a
VAULT_FIELD_ERROR takes the whole form there too. In development builds a warning says so, with no
text from the vault's engine.

Android: invalidate() runs on React Native's background thread, so it now reads and closes the
opened forms on main, where they change; a form asked for after that is not opened.
… plain field would

On the plain path a field's outer view has no width of its own: in a column it stretches, and in a
row it shrinks to its content. The native view filled its parent's width, so a field placed
straight in a row (S2SDemo's saved-card CVC, styles.container.width 68 next to a flex: 1 selector)
took the whole row and squeezed the selector to a sliver.

When the box (styles.container) has a fixed width and the root sets no width or flex, the native
view now takes the box's width plus its horizontal margins and the root's horizontal padding and
border: the plain outer view's width. Otherwise it fills, as before.
…ain fallback, and a Jest mock

In the merchant's engine the card fields now run only on the native path, so the card never sits
in the merchant's JavaScript. Where the native path cannot run (no native module: the app was not
rebuilt, an over-the-air update onto an older build, Expo Go, unit tests; the old architecture; a
build without the bundle; a protocol mismatch; React Native outside 0.82 to 0.88), and when a native
form does not start or stops, the form no longer switches to plain fields. It draws no field (the
merchant's own content inside it still draws), calls the new onError prop once with
unsupported_configuration and a message saying why, refuses every tokenize() with the same code
and message, and says why on the console in a development build. The reasons name the app's setup
only, never text from the vault's engine. The payment-methods SDK already passes
unsupported_configuration on to the merchant.

Inside an SDK's own engine (__HYPERSWITCH_SDK_ENGINE__) the plain fields still render: that engine
is already separate from the merchant's.

Android: the opt-out HyperswitchVault_nativeFields is removed (a build that still sets it is told it
is ignored). An unsupported build still compiles the stub, and now warns that the card fields are
not available in it, and why.

Jest: the package ships an opt-in mock at "@juspay-tech/react-native-hyperswitch-vault/jest"
(placeholder fields, a fixed test token, mockTokenizeResult()). The package never loads it itself:
a run-time "is this a test?" switch could be flipped by other code in an app.

The hosted bundles are unchanged (rebuilt: same bytes); only a comment in protocol.mjs changed.
…ields, one reason per failure, settings on save

- Split screen (Android): VaultEngine follows the Activity that owns the vault's React host, the
  last one resumed. Pause and destroy reach React Native only for that Activity; when another
  followed Activity is still in front, the host moves to it. React Native 0.82 to 0.88 asserts on a
  pause from any other Activity, which ended the app with two vault screens open at once.
- Detached fields stay native only: in the app's own engine a card field outside a <CardForm> draws
  nothing (with a development-build message), and the root createDetachedCardForm refuses (no
  field, tokenize() gives unsupported_configuration). Both still work in an SDK's own engine.
- One failure, one answer: a save that never reached the vault's engine gives
  unsupported_configuration with the reason the form reports to onError (did not start, stopped,
  or a field stopped; the first reason only), as every later save does.
- Settings on a save: new settings go to native as the render that made them commits (a layout
  effect), so a render React drops never reaches the form. Android attaches the settings a save was
  accepted with, and refuses, unsent, a save queued during startup whose settings changed before
  it could run (tokenization_failed, try again). The VaultCommand spec lists `settings`.
- The hosted entry sets __HYPERSWITCH_SDK_ENGINE__ again, in the PR whose availability check reads
  it.

Tests: a save that never reached the engine agrees with onError and later saves; a save refused
because its settings changed is an ordinary, retryable failure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant