Repository navigation
feat(vault): createDetachedCardForm from the root, with CardForm's settings and update() - #79
Pradeep-kumar1202 wants to merge 4 commits into
Conversation
…m setting and update()
287df1b to
3a598fa
Compare
…t the previous session
update() changes a detached form's settings at once, but the Host reads them when React next draws
it, and its session (from sdkAuthorization / vaultDetails) is worked out while it draws. So
update({sdkAuthorization: fresh}) followed straight away by tokenize() ran with the previous
session: an old-session request, or a wrong session_consumed refusal after the session had been
replaced. (Found in review.)
tokenize() now waits until the Host has committed the settings that are current when it is called
(commitGate.mjs), then runs. It never waits forever: if the Host unmounts it refuses as before (no
Host mounted), and if no commit comes within 5 seconds it refuses with tokenization_failed and
"did not apply its new settings in time". With nothing pending it runs at once, as before.
Tests: the gate on its own (waits for the current settings, follows the newest update, ends on
unmount, times out instead of hanging).
… 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).
tokenize() waited for the Host to draw whichever settings were newest when it committed. So if newer settings replaced the ones a save was called with before the Host drew them (update(A), tokenize(), then update(B) at once; or, in the vault's own engine, a tokenize command carrying A followed by B's props), the save ran with B's session, one it was not called for. (Found in review.) tokenize() now keeps the settings it was called with and waits for exactly those. If newer settings replace them first, it refuses without sending anything: tokenization_failed, "The card form's settings changed before the save could run. Nothing was sent; try again." It checks once more just before it runs. Everything else is as before: no wait when the settings are already drawn, a refusal when the Host unmounts, tokenization_failed after 5 seconds with no commit. Tests: the gate test that expected the wait to follow the newest settings now expects the save to be refused when they are replaced, refused at once when already replaced, and to keep waiting through a commit of older settings.
… 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).
manideepk90
left a comment
There was a problem hiding this comment.
Thanks, the design holds up. It builds (ReScript, tsc, rollup), and yarn test passes 59/59 at 1550bc8.
I checked the two edge cases I was worried about (a second form.Host, and the Host unmounting while its fields stay) against the native side in #104/#105. Neither can happen on the native path:
NativeCardFormcreates oneformIdper instance, andVaultForms.open(Android) /HyperswitchVaultForms open:(iOS) keep one form root performId. Each form root creates its own detached form, so even a re-opened id gets a separate link and store. #103'sopenFormalso refuses a duplicate id.- The Host only unmounts when the whole form root does. That closes the registry entry, so the field roots render nothing and the merchant gets
VAULT_FORM_ERROR.
But createDetachedCardForm is also exported from the package root, so direct callers (and the planned hyperswitch-client-core switch) don't get those guarantees. Two small changes before approval:
1. State the one-Host contract in the code (blocking, a comment is enough)
DetachedCardHost.res: every Host writes link.machinery / submitting on every render, and every Host's cleanup sets link.machinery = None and Resets the shared store. With two Hosts on one form, the single-submit guard (VaultFormCoordinator.res, inFlightRef) is per Host, so two tokenize() calls can reach two machineries. The first Host to unmount also clears the card under the other, and change fires twice.
Please put the contract where the code is, as createCardForm.mjs does for Form ("One instance drives ONE mounted form…"): one Host per form, and unmounting it ends the form. On that path the card is cleared and no final change is sent, so getState() keeps the last snapshot. A guard is welcome but not required. If you add one, mirror <CardForm>'s duplicate-field refusal (VaultFormHost.res, duplicateMessage). Clearing latest next to gate.closed() is a one-line improvement for direct callers.
2. Reuse existing code (can be a follow-up if you note it here)
DetachedCardLink.subscribe/setSubmittingcopyVaultCardStore.subscribe/notifyline for line.detachedSettings.mjsand #103'sforms.mjsadd two more copies. One small subscribe/notify helper would cover all of them.EVENTS,announceandon()increateDetachedCardForm.mjscopycreateCardForm.mjs. A shared helper keeps fixes to event fan-out in one place.
Nits: the description says 55 tests (it's 59) and leaves commitGate.mjs out of the Files table. The useShared doc comment sits above fieldsSettleMs = 500 in DetachedCardHost.res.
…it unmounts Review follow-up. A detached form has exactly one Host: every Host points the form at its own tokenizer, with its own guard against a second submit, and the first to unmount clears the card under any other. The rule is now stated where the code is (createDetachedCardForm.mjs, as createCardForm.mjs states its own, and DetachedCardHost.res), with what unmounting the Host means: the card is cleared, tokenize() refuses, and no final change is sent. getState() now returns null once the Host unmounts, instead of the last snapshot of a card that is gone. The useShared comment in DetachedCardHost.res moves back above useShared.
… 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).
|
@manideepk90 Thanks. Done in
I'm not adding the guard for now, since the native path can't create a second Host. I'll add it when client-core moves to the root export. Agreed on reuse: a shared subscribe/notify helper and the event fan-out helper will come as a follow-up. |
manideepk90
left a comment
There was a problem hiding this comment.
Re-checked at 440500d: the one-Host contract and "unmounting the Host ends the form" are now stated in createDetachedCardForm.mjs, and getState() returns null once the Host unmounts. Build passes and yarn test is 59/59. Approving.
Follow-ups as agreed, fine as separate PRs:
- one shared subscribe/notify helper (
DetachedCardLink,VaultCardStore,detachedSettings.mjs, #103'sforms.mjs); - one shared event fan-out helper for
createCardForm.mjsandcreateDetachedCardForm.mjs; - a second-Host guard once
hyperswitch-client-coreuses the root export directly.
Minor, no change needed: in dev StrictMode the effect cleanup runs once on mount, so getState() is null until the next change.
What
Adds
createDetachedCardForm(config)to@juspay-tech/react-native-hyperswitch-vault, exported from the package root. It builds a card form whose fields are not its children: each field can sit in its own React root, and together they still collect and tokenize one card.JavaScript only. Nothing in this pull request calls the new function, and no existing export changes.
Why
This is the first of four pull requests that make the vault's card fields natively wrapped: each field becomes a native view whose content runs in a JavaScript engine the vault starts itself, so the card number and CVC never sit in the merchant's engine (as hyperswitch-web keeps each field in an iframe).
Each native view holds its own React root. Separate roots cannot share a card through
<CardForm>'s React context. This function gives them one form that needs no common parent. The next pull request uses it inside the vault's own engine: a hidden form root rendersform.Host, and each field root renders its field insideform.Field.createDetachedCardForm: one card across separate React rootsEach 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 MERGEuntil the stack is ready.How it works
VaultCardStore, which holds the card;DetachedCardLink, which holds the Host's tokenizer, a "submitting" flag and the form's logger. No card data;detachedSettings.mjs). No card data.Host(DetachedCardHost.res) renders nothing. It owns the tokenizer and reportsreadyandchange. Render exactly oneHostper form. Unmounting it ends the form: the card is cleared,tokenize()returns a refusal, no finalchangeis sent, andgetState()returnsnull.Field(DetachedCardField.res) gives its children the context a<CardForm>would, and disables them while the Host is submitting or the form is disabled.<CardForm>, with the same defaults:vaultDetails,sdkAuthorization,session,environment,customEndpoints,locale,localisation,appearance,disabled,accessible,enabledCardSchemes,cardholderName,unstyled. (eligibility, which only the checkout SDK passes, is not one of them.)update()cannot lose the card. Settings and card are separate objects, andupdate()only touches the settings. Each root reads them withuseSyncExternalStore. The snapshot stays the same object until a setting really changes, so an equal value rebuilt by the caller re-renders nothing.onReady/onChangeare not settings. They are read when an event fires, so replacing one re-renders nothing.VaultFormHost.useHostover the shared store, and that hook creates a logger. Without care, one form would log under one session id per root. The link hands every root the same logger, only the Host logs what belongs to the form as a whole, and the Host's settings log waits 500 ms after the last field arrived.useHostgets three optional parameters for this (loggerFor,ownsFormLogs,fieldsSettleMs);<CardForm>andHyperswitchVaultFormpass none of them.tokenize,reset,focus,update,getState,on) exposes no store and no card values.tokenize()without a mounted Host resolves to{status: "validation_error", error: {code: "incomplete_field_set"}}; it never throws.Files
src/createDetachedCardForm.mjssrc/detachedSettings.mjssrc/commitGate.mjstokenize()waits until the Host has drawn the settings it was called with, and refuses if newer ones replace themsrc/DetachedCardHost.res,src/DetachedCardField.res,src/DetachedCardLink.ressrc/VaultFormHost.resloggerFor,ownsFormLogs,fieldsSettleMssrc/standalone-entry.mjs,src/public.tstest/detached.test.mjs,package.jsonImpact on existing merchants
createDetachedCardForm. No new entry point.<CardForm>andHyperswitchVaultFormcalluseHostexactly as before: with the new parameters left out, every branch behaves as onmain.@juspay-tech/react-native-hyperswitch-payment-methodsneeds no change.Testing
At this commit (
440500d):yarn testin the vault: 59 pass (48 existing; 11 new: the settings, the shared logger, and the commit gate).update()re-themes every root and keeps the typed number;tokenize()makes exactly one call; one form logs under one session id, with its settings once; no log contains the card number. A plain<CardForm>still collects a card. These tests are not in this pull request, because the vault has no React test harness.Through the rest of the stack: the native path in #103 to #105 runs this function inside the vault's engine. It was run on the Android emulator and the iOS simulator on React Native 0.82 to 0.88 (main flow, keyboard, dark mode, the form's API): every check passes, and the merchant's engine sees no card text.
Not run: the example app with this pull request alone;
hyperswitch-client-core's payment-methods tests.Not in this PR
hyperswitch-client-core: it still requires…-vault/detached(src/payment-methods/hosted/hyperswitchDetached.tsx), inside atry, and pins vault 1.0.1. No published vault has had that subpath, so nothing changes there. To use this function it must import the root and bump its pin; that is a follow-up.Note for the reviewer
form.Hostonce per form. A second Host for the same form is not guarded here: both would emitchange, and the first to unmount would clear the card under the other. feat(vault): hosted entry and bundle for the vault's own engine #103's form root refuses a second Host per form id. A guard on the link itself is a possible follow-up.<CardForm>with no fields already behaves this way for one second.createDetachedCardFormis visible to merchants in the root's types. It needs a vault session to do anything and carries no card data.