From 0545e0a78c0ced4c412ad82f6c8418b94634703c Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Tue, 24 Nov 2026 13:47:08 +0530 Subject: [PATCH 1/5] feat(vault): iOS host for the native card fields The iOS twin of the Android host: a second React Native factory that runs the vault's own bundle, a form class, the two modules and the field view, in Objective-C++. The field view takes the blur and focus commands the merchant's React Native sends a focused text input. The podspec compiles a stand-in with no engine and no bundle when React Native is below 0.82 or pod install runs with HYPERSWITCH_VAULT_NATIVE_FIELDS=0, and nothing on the old architecture. --- .../ios/Empty/HyperswitchVaultEmpty.m | 9 + .../ios/Source/HyperswitchVaultEngine.h | 63 ++++ .../ios/Source/HyperswitchVaultEngine.mm | 197 +++++++++++++ .../ios/Source/HyperswitchVaultFieldView.h | 14 + .../ios/Source/HyperswitchVaultFieldView.mm | 158 ++++++++++ .../ios/Source/HyperswitchVaultForm.h | 49 ++++ .../ios/Source/HyperswitchVaultForm.mm | 277 ++++++++++++++++++ .../ios/Source/HyperswitchVaultFormsModule.h | 18 ++ .../ios/Source/HyperswitchVaultFormsModule.mm | 120 ++++++++ .../ios/Source/HyperswitchVaultHostedModule.h | 21 ++ .../Source/HyperswitchVaultHostedModule.mm | 47 +++ .../ios/Source/HyperswitchVaultProtocol.h | 29 ++ .../ios/Source/HyperswitchVaultSpec.h | 8 + .../ios/Stub/HyperswitchVaultStub.mm | 126 ++++++++ .../package.json | 11 + .../react-native-hyperswitch-vault.podspec | 58 ++++ ...yperswitchVaultFieldViewNativeComponent.ts | 4 +- 17 files changed, 1206 insertions(+), 3 deletions(-) create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultProtocol.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultSpec.h create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm create mode 100644 packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m new file mode 100644 index 00000000..c3645610 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m @@ -0,0 +1,9 @@ +/* + * The whole of the vault's native code in an app on the old architecture: nothing. The pod still + * needs one file to compile. The card fields render in plain JavaScript. + */ +#import + +void HyperswitchVaultHasNoNativeFieldsInThisBuild(void) +{ +} diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h new file mode 100644 index 00000000..1e4c0465 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h @@ -0,0 +1,63 @@ +#import + +#import "HyperswitchVaultProtocol.h" + +NS_ASSUME_NONNULL_BEGIN + +@class HyperswitchVaultHostedModule; + +/* One running root of the bundle, and who receives its events. */ +@interface HyperswitchVaultSurface : NSObject + +/* What the bundle calls rootTag. */ +@property (nonatomic, readonly) NSInteger rootTag; + +/* The view that draws the root. A form's root has one too, and it is never put on screen. */ +@property (nonatomic, readonly) UIView *view; + +@property (nonatomic, weak, readonly, nullable) id owner; + +/* New props for the running root. The root keeps its state: an update, not a restart. */ +- (void)update:(NSDictionary *)props; + +- (void)stop; + +@end + +/* + * The vault's own JavaScript engine: a second React host, started the first time a card form opens + * and kept for the life of the app process. It runs only hyperswitch-vault-fields.bundle. The card + * number and the CVC exist in THIS engine's memory and in the text inputs it draws; the merchant's + * engine never holds them. + * + * NO CODE FROM OUTSIDE. The bundle is read from the app, in every build type. This engine never + * loads JavaScript from a development server. + * + * Everything here runs on the main thread. + */ +@interface HyperswitchVaultEngine : NSObject + ++ (instancetype)shared; + +/* Whether this build of the app carries the bundle. */ ++ (BOOL)isBundled; + +/* Starts a root of the bundle. `owner` receives its events until stop. */ +- (HyperswitchVaultSurface *)start:(NSString *)rootName + props:(NSDictionary *)props + owner:(id)owner; + +/* From the bundle: hand the event to the root's owner. */ +- (void)deliver:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payloadJson; + +/* NO when the bundle cannot be reached: the caller answers the command itself. */ +- (BOOL)send:(NSDictionary *)command; + +/* The module of this engine that carries commands to the bundle. */ +- (void)attach:(HyperswitchVaultHostedModule *)module; + +- (void)forget:(HyperswitchVaultSurface *)surface; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm new file mode 100644 index 00000000..4c3bfde0 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -0,0 +1,197 @@ +#import "HyperswitchVaultEngine.h" + +#import +#import +#import +#import +#import +#import +#import + +#import "HyperswitchVaultHostedModule.h" + +#pragma mark - Surface + +@interface HyperswitchVaultSurface () +@property (nonatomic, weak) HyperswitchVaultEngine *engine; +@property (nonatomic, strong) UIView *rootView; +@property (nonatomic, strong, nullable) id surface; +@property (nonatomic, weak, readwrite, nullable) id owner; +@end + +@implementation HyperswitchVaultSurface + +- (NSInteger)rootTag +{ + return _surface != nil ? _surface.rootTag : -1; +} + +- (UIView *)view +{ + return _rootView; +} + +- (void)update:(NSDictionary *)props +{ + _surface.properties = props; +} + +- (void)stop +{ + [_engine forget:self]; + _owner = nil; + [_surface stop]; + [_rootView removeFromSuperview]; +} + +@end + +#pragma mark - The second host's delegate + +/* + * What makes the second host the vault's: where its bundle is, and the one module that exists for + * the bundle to talk to. Every other native module and view it can reach is what the app links. + */ +@interface HyperswitchVaultEngineDelegate : RCTDefaultReactNativeFactoryDelegate +@property (nonatomic, weak) HyperswitchVaultEngine *engine; +@end + +@implementation HyperswitchVaultEngineDelegate + ++ (NSURL *)bundleFile +{ + return [[NSBundle bundleForClass:HyperswitchVaultEngine.class] URLForResource:HyperswitchVaultBundleName + withExtension:HyperswitchVaultBundleExtension]; +} + +/* The bundle inside the app, in every build type: never a development server. */ +- (NSURL *)bundleURL +{ + return [HyperswitchVaultEngineDelegate bundleFile]; +} + +- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge +{ + return [self bundleURL]; +} + +/* The hosted module of THIS host is made here, so the engine knows which instance carries its commands. */ +- (id)getModuleInstanceFromClass:(Class)moduleClass +{ + if (moduleClass == HyperswitchVaultHostedModule.class) { + HyperswitchVaultHostedModule *module = [HyperswitchVaultHostedModule new]; + [_engine attach:module]; + return (id)module; + } + return nil; +} + +@end + +#pragma mark - Engine + +@implementation HyperswitchVaultEngine { + HyperswitchVaultEngineDelegate *_delegate; + RCTReactNativeFactory *_factory; + __weak HyperswitchVaultHostedModule *_module; + + /* + * The running roots. An event from the bundle names its root by root tag, and the owner is found + * by asking each root for its tag when the event arrives. + */ + NSMutableArray *_running; +} + ++ (instancetype)shared +{ + static HyperswitchVaultEngine *engine; + static dispatch_once_t once; + dispatch_once(&once, ^{ + engine = [HyperswitchVaultEngine new]; + }); + return engine; +} + ++ (BOOL)isBundled +{ + return [HyperswitchVaultEngineDelegate bundleFile] != nil; +} + +- (instancetype)init +{ + if (self = [super init]) { + _running = [NSMutableArray new]; + _delegate = [HyperswitchVaultEngineDelegate new]; + _delegate.engine = self; + _delegate.dependencyProvider = [RCTAppDependencyProvider new]; + } + return self; +} + +/* The second host, made the first time a root is asked for. */ +- (RCTReactNativeFactory *)factory +{ + if (_factory == nil) { + _factory = [[RCTReactNativeFactory alloc] initWithDelegate:_delegate]; + } + return _factory; +} + +- (HyperswitchVaultSurface *)start:(NSString *)rootName + props:(NSDictionary *)props + owner:(id)owner +{ + UIView *rootView = [[self factory].rootViewFactory viewWithModuleName:rootName initialProperties:props]; + rootView.backgroundColor = UIColor.clearColor; + + HyperswitchVaultSurface *surface = [HyperswitchVaultSurface new]; + surface.engine = self; + surface.rootView = rootView; + surface.owner = owner; + if ([rootView isKindOfClass:RCTSurfaceHostingProxyRootView.class]) { + RCTSurfaceHostingProxyRootView *hosting = (RCTSurfaceHostingProxyRootView *)rootView; + /* As wide as the view it is put in, as tall as its own content. */ + hosting.sizeFlexibility = RCTRootViewSizeFlexibilityHeight; + surface.surface = hosting.surface; + } + [_running addObject:surface]; + return surface; +} + +- (void)forget:(HyperswitchVaultSurface *)surface +{ + [_running removeObject:surface]; +} + +- (void)deliver:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payloadJson +{ + dispatch_block_t work = ^{ + for (HyperswitchVaultSurface *surface in [self->_running copy]) { + if (surface.rootTag == rootTag) { + [surface.owner vaultEvent:eventName payload:payloadJson]; + return; + } + } + }; + if (NSThread.isMainThread) { + work(); + } else { + dispatch_async(dispatch_get_main_queue(), work); + } +} + +- (void)attach:(HyperswitchVaultHostedModule *)module +{ + _module = module; +} + +- (BOOL)send:(NSDictionary *)command +{ + HyperswitchVaultHostedModule *module = _module; + if (module == nil) { + return NO; + } + return [module sendCommand:command]; +} + +@end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.h new file mode 100644 index 00000000..106975d9 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.h @@ -0,0 +1,14 @@ +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +/* + * 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. + */ +@interface HyperswitchVaultFieldView : RCTViewComponentView +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm new file mode 100644 index 00000000..614cbfe4 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm @@ -0,0 +1,158 @@ +#import "HyperswitchVaultFieldView.h" + +#import +#import +#import +#import + +#import "HyperswitchVaultEngine.h" +#import "HyperswitchVaultForm.h" +#import "HyperswitchVaultProtocol.h" + +using namespace facebook::react; + +static NSString *StringOf(const std::string &value) +{ + return [[NSString alloc] initWithBytes:value.data() length:value.size() encoding:NSUTF8StringEncoding] ?: @""; +} + +@interface HyperswitchVaultFieldView () +@end + +@implementation HyperswitchVaultFieldView { + HyperswitchVaultSurface *_surface; + + NSString *_formId; + NSString *_elementType; + NSString *_settings; + + /* Which form and field the running root was started for, and the props it last received. */ + NSString *_startedFor; + NSString *_renderedSettings; +} + ++ (ComponentDescriptorProvider)componentDescriptorProvider +{ + return concreteComponentDescriptorProvider(); +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + static const auto defaultProps = std::make_shared(); + _props = defaultProps; + } + return self; +} + +- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps +{ + const auto &next = *std::static_pointer_cast(props); + _formId = StringOf(next.formId); + _elementType = StringOf(next.elementType); + _settings = StringOf(next.settings); + [super updateProps:props oldProps:oldProps]; + [self render]; +} + +- (void)render +{ + if (_formId.length == 0 || _elementType.length == 0) { + return; + } + NSString *settings = _settings.length > 0 ? _settings : @"{}"; + NSString *identity = [NSString stringWithFormat:@"%@/%@", _formId, _elementType]; + + if (_surface != nil && [_startedFor isEqualToString:identity]) { + /* Same field: new props for the running root, which keeps what was typed. */ + if (![settings isEqualToString:_renderedSettings]) { + _renderedSettings = settings; + [_surface update:[self propsOf:settings]]; + } + return; + } + /* A different form or field in this view: the old root goes. */ + [self stop]; + + /* The form reports a missing bundle; its fields just stay empty. */ + if (![HyperswitchVaultEngine isBundled]) { + return; + } + _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFieldRoot props:[self propsOf:settings] owner:self]; + _startedFor = identity; + _renderedSettings = settings; + + UIView *root = _surface.view; + root.frame = self.bounds; + root.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + [self addSubview:root]; +} + +- (void)stop +{ + [_surface stop]; + _surface = nil; + _startedFor = nil; + _renderedSettings = nil; +} + +/* React Native reuses native views. A reused view must not show the last field's root. */ +- (void)prepareForRecycle +{ + [super prepareForRecycle]; + [self stop]; + _formId = nil; + _elementType = nil; + _settings = nil; +} + +- (void)dealloc +{ + [_surface stop]; +} + +/* + * 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. + */ +- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args +{ + NSString *name = [commandName copy]; + if (([name isEqualToString:@"blur"] || [name isEqualToString:@"focus"]) && _formId.length > 0 && _elementType.length > 0) { + [[HyperswitchVaultForms get:_formId] command:name + elementType:_elementType + reply:^(NSString *resultJson) { + }]; + } +} + +#pragma mark - Events from the field's root + +/* On the main thread: on to the merchant's engine as this view's event. */ +- (void)vaultEvent:(NSString *)name payload:(NSString *)payloadJson +{ + if (_eventEmitter == nullptr) { + return; + } + std::static_pointer_cast(_eventEmitter) + ->onFieldEvent({.name = std::string(name.UTF8String ?: ""), .payload = std::string(payloadJson.UTF8String ?: "{}")}); +} + +- (NSDictionary *)propsOf:(NSString *)settings +{ + return @{ + @"formId" : _formId, + @"elementType" : _elementType, + @"protocolVersion" : @(HyperswitchVaultProtocolVersion), + @"settings" : settings, + }; +} + +@end + +Class HyperswitchVaultFieldViewCls(void) +{ + return HyperswitchVaultFieldView.class; +} diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.h new file mode 100644 index 00000000..59fc4f96 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.h @@ -0,0 +1,49 @@ +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef void (^HyperswitchVaultEmit)(NSString *name, NSString *payloadJson); +typedef void (^HyperswitchVaultReply)(NSString *resultJson); + +/* + * One card form, as native code sees it: a hidden root in the vault's engine that owns the form, + * and the commands on their way to it. Everything here runs on the main thread. + * + * EVERY COMMAND IS ANSWERED EXACTLY ONCE. A command's reply is a promise in the merchant's engine + * (await tokenize()), so each way a command can end replies: the bundle's answer, a form that never + * started, a form that was closed, an unreachable bundle, and silence for too long. Removing the + * command from `pending` is the one gate: whoever removes it replies, and nobody can remove it twice. + * + * Payloads are JSON text made by the bundle's allow-list. This class reads `commandId` and + * `message` out of them and nothing else, and never logs one. + */ +@interface HyperswitchVaultForm : NSObject + +@property (nonatomic, copy, readonly) NSString *formId; + +- (instancetype)initWithFormId:(NSString *)formId emit:(HyperswitchVaultEmit)emit; + +- (void)open:(NSString *)settings; + +/* A prop update for the running root: the form applies it and keeps the card. */ +- (void)update:(NSString *)settings; + +- (void)command:(NSString *)name elementType:(NSString *)elementType reply:(HyperswitchVaultReply)reply; + +- (void)close; + +/* A command that did not run: the same shape the bundle uses for one. */ ++ (NSString *)refusal:(NSString *)message; + +@end + +/* The open forms, by formId. Main thread only. */ +@interface HyperswitchVaultForms : NSObject + ++ (void)open:(NSString *)formId settings:(NSString *)settings emit:(HyperswitchVaultEmit)emit; ++ (nullable HyperswitchVaultForm *)get:(NSString *)formId; ++ (void)close:(NSString *)formId; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm new file mode 100644 index 00000000..229502c5 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm @@ -0,0 +1,277 @@ +#import "HyperswitchVaultForm.h" + +#import "HyperswitchVaultEngine.h" +#import "HyperswitchVaultProtocol.h" + +/* The bundle has to load and its root has to mount. Long for a slow first start, short for a person waiting. */ +static const NSTimeInterval OpenTimeout = 15; + +/* Longer than the vault's own network timeouts, so a tokenize that is still working is not cut off. */ +static const NSTimeInterval CommandTimeout = 90; + +static NSString *const NotBundled = @"This build of the app has no card-form bundle."; +static NSString *const NotStarted = @"The card form did not start."; +static NSString *const NotOpen = @"The card form is not open."; +static NSString *const Closed = @"The card form was closed."; +static NSString *const Unreachable = @"The card form cannot be reached."; +static NSString *const NoAnswer = @"The card form did not answer."; + +typedef NS_ENUM(NSInteger, Phase) { PhaseOpening, PhaseOpen, PhaseFailed }; + +static NSString *FieldOf(NSString *json, NSString *key) +{ + NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; + if (data == nil) { + return nil; + } + id parsed = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + if (![parsed isKindOfClass:NSDictionary.class]) { + return nil; + } + id value = ((NSDictionary *)parsed)[key]; + return [value isKindOfClass:NSString.class] && [(NSString *)value length] > 0 ? value : nil; +} + +static NSString *JsonOf(NSDictionary *value) +{ + NSData *data = [NSJSONSerialization dataWithJSONObject:value options:0 error:nil]; + return data != nil ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : @"{}"; +} + +@interface HyperswitchVaultForm () +@end + +@implementation HyperswitchVaultForm { + HyperswitchVaultEmit _emit; + HyperswitchVaultSurface *_surface; + Phase _phase; + NSString *_failure; + BOOL _closed; + + /* Commands that arrived before the form's root was up. Run, in order, once it is up or has failed. */ + NSMutableArray *_whenSettled; + NSMutableDictionary *_pending; + + /* Bumped when the open timeout must no longer fire. */ + NSInteger _openGeneration; +} + ++ (NSString *)refusal:(NSString *)message +{ + return JsonOf(@{@"ok" : @NO, @"message" : message}); +} + +- (instancetype)initWithFormId:(NSString *)formId emit:(HyperswitchVaultEmit)emit +{ + if (self = [super init]) { + _formId = [formId copy]; + _emit = [emit copy]; + _phase = PhaseOpening; + _whenSettled = [NSMutableArray new]; + _pending = [NSMutableDictionary new]; + } + return self; +} + +- (void)open:(NSString *)settings +{ + if (![HyperswitchVaultEngine isBundled]) { + [self fail:NotBundled]; + return; + } + /* The hidden root's view is never put on screen. */ + _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFormRoot props:[self propsOf:settings] owner:self]; + + NSInteger generation = ++_openGeneration; + __weak HyperswitchVaultForm *weakSelf = self; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(OpenTimeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + HyperswitchVaultForm *form = weakSelf; + if (form != nil && form->_openGeneration == generation) { + [form fail:NotStarted]; + } + }); +} + +- (void)update:(NSString *)settings +{ + [_surface update:[self propsOf:settings]]; +} + +- (void)command:(NSString *)name elementType:(NSString *)elementType reply:(HyperswitchVaultReply)reply +{ + __weak HyperswitchVaultForm *weakSelf = self; + [self whenFormSettles:^{ + HyperswitchVaultForm *form = weakSelf; + if (form == nil) { + reply([HyperswitchVaultForm refusal:Closed]); + return; + } + if (form->_phase == PhaseFailed || form->_surface == nil) { + reply([HyperswitchVaultForm refusal:form->_failure ?: NotOpen]); + return; + } + NSString *commandId = NSUUID.UUID.UUIDString; + form->_pending[commandId] = [reply copy]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CommandTimeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [weakSelf answer:commandId result:[HyperswitchVaultForm refusal:NoAnswer]]; + }); + + NSMutableDictionary *command = [@{ + @"rootTag" : @(form->_surface.rootTag), + @"formId" : form->_formId, + @"commandId" : commandId, + @"name" : name, + } mutableCopy]; + if (elementType.length > 0) { + command[@"elementType"] = elementType; + } + if (![HyperswitchVaultEngine.shared send:command]) { + [form answer:commandId result:[HyperswitchVaultForm refusal:Unreachable]]; + } + }]; +} + +- (void)close +{ + if (_closed) { + return; + } + _closed = YES; + _openGeneration++; + [_surface stop]; + _surface = nil; + [self settle:PhaseFailed failure:Closed]; + [self answerAll:[HyperswitchVaultForm refusal:Closed]]; +} + +#pragma mark - Events from the form's root + +- (void)vaultEvent:(NSString *)name payload:(NSString *)payloadJson +{ + if (_closed) { + return; + } + if ([name isEqualToString:HyperswitchVaultCommandResult]) { + /* For the caller of the command, not for the merchant's listeners. */ + NSString *commandId = FieldOf(payloadJson, @"commandId"); + if (commandId != nil) { + [self answer:commandId result:payloadJson]; + } + return; + } + if ([name isEqualToString:HyperswitchVaultFormMounted]) { + _openGeneration++; + [self settle:PhaseOpen failure:nil]; + } else if ([name isEqualToString:HyperswitchVaultFormError]) { + _openGeneration++; + [self settle:PhaseFailed failure:FieldOf(payloadJson, @"message") ?: NotStarted]; + } + _emit(name, payloadJson); +} + +#pragma mark - Endings + +/* The form cannot go on, for a reason native code found. The merchant's side is told once. */ +- (void)fail:(NSString *)message +{ + if (_closed || _phase == PhaseFailed) { + return; + } + _openGeneration++; + _phase = PhaseFailed; + _failure = message; + [self drain]; + [self answerAll:[HyperswitchVaultForm refusal:message]]; + _emit(HyperswitchVaultFormError, JsonOf(@{@"message" : message})); +} + +- (void)whenFormSettles:(dispatch_block_t)work +{ + if (_phase == PhaseOpening) { + [_whenSettled addObject:[work copy]]; + } else { + work(); + } +} + +- (void)settle:(Phase)next failure:(NSString *)failure +{ + if (_phase != PhaseOpening) { + return; + } + _phase = next; + _failure = failure; + [self drain]; +} + +- (void)drain +{ + NSArray *waiting = [_whenSettled copy]; + [_whenSettled removeAllObjects]; + for (dispatch_block_t work in waiting) { + work(); + } +} + +- (void)answer:(NSString *)commandId result:(NSString *)resultJson +{ + HyperswitchVaultReply reply = _pending[commandId]; + if (reply == nil) { + return; + } + [_pending removeObjectForKey:commandId]; + reply(resultJson); +} + +- (void)answerAll:(NSString *)resultJson +{ + for (NSString *commandId in _pending.allKeys) { + [self answer:commandId result:resultJson]; + } +} + +- (NSDictionary *)propsOf:(NSString *)settings +{ + return @{ + @"formId" : _formId, + @"protocolVersion" : @(HyperswitchVaultProtocolVersion), + @"settings" : settings ?: @"{}", + }; +} + +@end + +@implementation HyperswitchVaultForms + ++ (NSMutableDictionary *)forms +{ + static NSMutableDictionary *forms; + static dispatch_once_t once; + dispatch_once(&once, ^{ + forms = [NSMutableDictionary new]; + }); + return forms; +} + ++ (void)open:(NSString *)formId settings:(NSString *)settings emit:(HyperswitchVaultEmit)emit +{ + /* A formId is opened once. Opening it again replaces the old form, which answers its commands as closed. */ + [[self forms][formId] close]; + HyperswitchVaultForm *form = [[HyperswitchVaultForm alloc] initWithFormId:formId emit:emit]; + [self forms][formId] = form; + [form open:settings]; +} + ++ (HyperswitchVaultForm *)get:(NSString *)formId +{ + return [self forms][formId]; +} + ++ (void)close:(NSString *)formId +{ + HyperswitchVaultForm *form = [self forms][formId]; + [[self forms] removeObjectForKey:formId]; + [form close]; +} + +@end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.h new file mode 100644 index 00000000..c6b1b3d8 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.h @@ -0,0 +1,18 @@ +#import + +#import "HyperswitchVaultSpec.h" + +NS_ASSUME_NONNULL_BEGIN + +/* + * The module the MERCHANT's engine talks to (src/native in the vault's JavaScript). It opens, + * updates and closes card forms in the vault's own engine and carries commands to them. + * + * What crosses here, in both directions, is JSON text. Going in: the merchant's own settings. + * Coming out: events and results the hosted bundle has already cut down to its allow-list. This + * class passes the text on and never logs it. + */ +@interface HyperswitchVaultFormsModule : NativeHyperswitchVaultFormsSpecBase +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.mm new file mode 100644 index 00000000..43d4738e --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFormsModule.mm @@ -0,0 +1,120 @@ +#import "HyperswitchVaultFormsModule.h" + +#import +#import + +#import "HyperswitchVaultEngine.h" +#import "HyperswitchVaultForm.h" +#import "HyperswitchVaultProtocol.h" + +static NSString *const NotOpen = @"The card form is not open."; + +@implementation HyperswitchVaultFormsModule { + /* The forms this engine opened, so they end with it (a reload of the merchant's JavaScript). */ + NSMutableSet *_opened; +} + +RCT_EXPORT_MODULE(HyperswitchVaultForms) + ++ (BOOL)requiresMainQueueSetup +{ + return YES; +} + +/* Forms are opened, updated and closed on the main thread, in the order the calls were made. */ +- (dispatch_queue_t)methodQueue +{ + return dispatch_get_main_queue(); +} + +- (instancetype)init +{ + if (self = [super init]) { + _opened = [NSMutableSet new]; + } + return self; +} + +- (facebook::react::ModuleConstants)constantsToExport +{ + return [self getConstants]; +} + +- (facebook::react::ModuleConstants)getConstants +{ + NSNumber *minor = RCTGetReactNativeVersion()[@"minor"]; + return facebook::react::typedConstants({ + .newArchitecture = RCTIsNewArchEnabled(), + .reactNativeMinor = [minor isKindOfClass:NSNumber.class] ? minor.doubleValue : 0, + .protocolVersion = (double)HyperswitchVaultProtocolVersion, + .bundled = [HyperswitchVaultEngine isBundled], + }); +} + +- (void)openForm:(NSString *)formId settings:(NSString *)settings +{ + [_opened addObject:formId]; + __weak HyperswitchVaultFormsModule *weakSelf = self; + [HyperswitchVaultForms open:formId + settings:settings + emit:^(NSString *name, NSString *payloadJson) { + [weakSelf send:formId name:name payload:payloadJson]; + }]; +} + +- (void)updateForm:(NSString *)formId settings:(NSString *)settings +{ + [[HyperswitchVaultForms get:formId] update:settings]; +} + +- (void)closeForm:(NSString *)formId +{ + [_opened removeObject:formId]; + [HyperswitchVaultForms close:formId]; +} + +/* Always resolves. A command that could not run resolves with {"ok":false,"message":...}. */ +- (void)sendCommand:(NSString *)formId + name:(NSString *)name + elementType:(NSString *)elementType + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject +{ + HyperswitchVaultForm *form = [HyperswitchVaultForms get:formId]; + if (form == nil) { + resolve([HyperswitchVaultForm refusal:NotOpen]); + return; + } + [form command:name + elementType:elementType ?: @"" + reply:^(NSString *resultJson) { + resolve(resultJson); + }]; +} + +- (void)invalidate +{ + NSArray *leaving = _opened.allObjects; + [_opened removeAllObjects]; + dispatch_async(dispatch_get_main_queue(), ^{ + for (NSString *formId in leaving) { + [HyperswitchVaultForms close:formId]; + } + }); +} + +- (void)send:(NSString *)formId name:(NSString *)name payload:(NSString *)payloadJson +{ + if (!_eventEmitterCallback) { + return; + } + [self emitOnFormEvent:@{@"formId" : formId, @"name" : name, @"payload" : payloadJson ?: @"{}"}]; +} + +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} + +@end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.h new file mode 100644 index 00000000..9619a8aa --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.h @@ -0,0 +1,21 @@ +#import + +#import "HyperswitchVaultSpec.h" + +NS_ASSUME_NONNULL_BEGIN + +/* + * The module the hosted bundle talks to (src/hosted/bridge.mjs). The instance that matters lives in + * the vault's own engine. Events come out of the bundle through it, already cut down to the + * allow-list by the bundle and written as JSON text; commands go in. + * + * It passes the text on untouched and never logs it. + */ +@interface HyperswitchVaultHostedModule : NativeHyperswitchVaultHostedSpecBase + +/* NO when the bundle is not listening yet. */ +- (BOOL)sendCommand:(NSDictionary *)command; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm new file mode 100644 index 00000000..1ceec9c7 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm @@ -0,0 +1,47 @@ +#import "HyperswitchVaultHostedModule.h" + +#import "HyperswitchVaultEngine.h" + +@implementation HyperswitchVaultHostedModule + +RCT_EXPORT_MODULE(HyperswitchVaultHosted) + ++ (BOOL)requiresMainQueueSetup +{ + return YES; +} + +/* Events are handed to their owners on the main thread, so they arrive in the order they were sent. */ +- (dispatch_queue_t)methodQueue +{ + return dispatch_get_main_queue(); +} + +- (BOOL)sendCommand:(NSDictionary *)command +{ + if (!_eventEmitterCallback) { + return NO; + } + [self emitOnCommand:command]; + return YES; +} + +#pragma mark - NativeHyperswitchVaultHostedSpec + +- (void)emitFieldEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload +{ + [HyperswitchVaultEngine.shared deliver:rootTag eventName:eventName payload:payload ?: @"{}"]; +} + +- (void)emitFormEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload +{ + [HyperswitchVaultEngine.shared deliver:rootTag eventName:eventName payload:payload ?: @"{}"]; +} + +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} + +@end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultProtocol.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultProtocol.h new file mode 100644 index 00000000..17e88842 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultProtocol.h @@ -0,0 +1,29 @@ +/* + * The names this native code shares with the vault's hosted bundle (src/hosted/protocol.mjs) and + * with the vault's JavaScript in the merchant's engine (src/native). All three hold a copy; + * the version is how they notice they have drifted. Nothing else in this folder spells these names. + */ +#import + +NS_ASSUME_NONNULL_BEGIN + +static const NSInteger HyperswitchVaultProtocolVersion = 1; + +/* The bundle the vault's own engine runs: hyperswitch-vault-fields.bundle. */ +static NSString *const HyperswitchVaultBundleName = @"hyperswitch-vault-fields"; +static NSString *const HyperswitchVaultBundleExtension = @"bundle"; + +/* The roots the bundle registers. */ +static NSString *const HyperswitchVaultFormRoot = @"HyperswitchVaultForm"; +static NSString *const HyperswitchVaultFieldRoot = @"HyperswitchVaultField"; + +static NSString *const HyperswitchVaultFormMounted = @"VAULT_FORM_MOUNTED"; +static NSString *const HyperswitchVaultFormError = @"VAULT_FORM_ERROR"; +static NSString *const HyperswitchVaultCommandResult = @"VAULT_COMMAND_RESULT"; + +/* Receives the events of one React root in the vault's engine. Called on the main thread. */ +@protocol HyperswitchVaultEventTarget +- (void)vaultEvent:(NSString *)name payload:(NSString *)payloadJson; +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultSpec.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultSpec.h new file mode 100644 index 00000000..c791298e --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultSpec.h @@ -0,0 +1,8 @@ +/* The interfaces React Native's Codegen generates from src/specs, wherever the app's build puts them. */ +#if __has_include() +#import +#elif __has_include() +#import +#else +#import "HyperswitchVaultSpec/HyperswitchVaultSpec.h" +#endif diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm new file mode 100644 index 00000000..acb46f9d --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm @@ -0,0 +1,126 @@ +/* + * What an app links when the vault's own engine is not built into it: React Native older than the + * bundle is built for, or the app chose to build without the native fields (the podspec decides). + * + * React Native's generated code names these three classes for every app on the new architecture, + * and a missing view class would crash the app at start. So they exist here, and do nothing: the + * module reports that there is no bundle, the vault's JavaScript reads that and renders the card + * fields in plain JavaScript, as it did before there was a native path. + */ +#import +#import +#import + +#if __has_include() +#import +#elif __has_include() +#import +#else +#import "HyperswitchVaultSpec/HyperswitchVaultSpec.h" +#endif + +#import +#import + +using namespace facebook::react; + +#pragma mark - The forms module: present, and says there is no engine + +@interface HyperswitchVaultFormsModule : NativeHyperswitchVaultFormsSpecBase +@end + +@implementation HyperswitchVaultFormsModule + +RCT_EXPORT_MODULE(HyperswitchVaultForms) + +- (ModuleConstants)constantsToExport +{ + return [self getConstants]; +} + +- (ModuleConstants)getConstants +{ + NSNumber *minor = RCTGetReactNativeVersion()[@"minor"]; + return typedConstants({ + .newArchitecture = RCTIsNewArchEnabled(), + .reactNativeMinor = [minor isKindOfClass:NSNumber.class] ? minor.doubleValue : 0, + .protocolVersion = 1, + /* No bundle in this build: the JavaScript takes the plain path on this alone. */ + .bundled = NO, + }); +} + +- (void)openForm:(NSString *)formId settings:(NSString *)settings +{ +} + +- (void)updateForm:(NSString *)formId settings:(NSString *)settings +{ +} + +- (void)closeForm:(NSString *)formId +{ +} + +- (void)sendCommand:(NSString *)formId + name:(NSString *)name + elementType:(NSString *)elementType + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject +{ + resolve(@"{\"ok\":false,\"message\":\"This build of the app has no card-form engine.\"}"); +} + +- (std::shared_ptr)getTurboModule:(const ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} + +@end + +#pragma mark - The hosted module: nothing hosts a bundle here + +@interface HyperswitchVaultHostedModule : NativeHyperswitchVaultHostedSpecBase +@end + +@implementation HyperswitchVaultHostedModule + +RCT_EXPORT_MODULE(HyperswitchVaultHosted) + +- (void)emitFieldEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload +{ +} + +- (void)emitFormEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload +{ +} + +- (std::shared_ptr)getTurboModule:(const ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} + +@end + +#pragma mark - The field view: an empty view that is never rendered + +@interface HyperswitchVaultFieldView : RCTViewComponentView +@end + +@implementation HyperswitchVaultFieldView + ++ (ComponentDescriptorProvider)componentDescriptorProvider +{ + return concreteComponentDescriptorProvider(); +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + static const auto defaultProps = std::make_shared(); + _props = defaultProps; + } + return self; +} + +@end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/package.json b/packages/@juspay-tech/react-native-hyperswitch-vault/package.json index 41a7d859..2598fcc9 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/package.json +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/package.json @@ -42,6 +42,8 @@ "dist/types/**", "dist/assets/**", "android", + "ios", + "react-native-hyperswitch-vault.podspec", "src/specs", "react-native.config.js", "jest", @@ -60,6 +62,15 @@ "jsSrcsDir": "src/specs", "android": { "javaPackageName": "io.hyperswitch.vault" + }, + "ios": { + "modulesProvider": { + "HyperswitchVaultForms": "HyperswitchVaultFormsModule", + "HyperswitchVaultHosted": "HyperswitchVaultHostedModule" + }, + "componentProvider": { + "HyperswitchVaultFieldView": "HyperswitchVaultFieldView" + } } }, "engines": { diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec new file mode 100644 index 00000000..affa79d3 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec @@ -0,0 +1,58 @@ +require "json" + +package = JSON.parse(File.read(File.join(__dir__, "package.json"))) + +# The minor version of the React Native this app is built with (86 for 0.86.3), or 0 when it cannot +# be found. (Written inline: a method defined at the top of a podspec is not visible inside it.) +react_native_minor = + begin + root = Pod::Config.instance.installation_root.to_s + path = `node --print "require.resolve('react-native/package.json', { paths: ['#{root}'] })" 2>/dev/null`.strip + version = JSON.parse(File.read(path))["version"] + version.start_with?("0.0.0-") ? 1000 : version.split(".")[1].to_i + rescue StandardError + 0 + end + +# 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 did not opt out with HYPERSWITCH_VAULT_NATIVE_FIELDS=0 when running `pod install`. +# Otherwise the pod compiles a stand-in with no engine and no bundle. The app still builds, and the +# card fields render in plain JavaScript as before. +new_architecture = react_native_minor >= 82 || ENV["RCT_NEW_ARCH_ENABLED"] == "1" +opted_out = ENV["HYPERSWITCH_VAULT_NATIVE_FIELDS"] == "0" +hosts_own_engine = new_architecture && react_native_minor >= 82 && !opted_out + +unless hosts_own_engine + Pod::UI.puts "[@juspay-tech/react-native-hyperswitch-vault] card fields: plain JavaScript in this build " \ + "(new architecture: #{new_architecture}, React Native minor: #{react_native_minor}, opted out: #{opted_out})." +end + +Pod::Spec.new do |s| + s.name = "react-native-hyperswitch-vault" + s.version = package["version"] + s.summary = "Hyperswitch card form for React Native: native card fields." + s.description = package["description"] + s.homepage = "https://github.com/juspay/react-native-hyperswitch" + s.license = package["license"] + s.authors = "Juspay" + s.platforms = { :ios => "15.1" } + s.source = { :git => "https://github.com/juspay/react-native-hyperswitch.git", :tag => "#{s.version}" } + + if hosts_own_engine + # The vault's own engine: the native host, and the bundle it runs. + s.source_files = "ios/Source/**/*.{h,m,mm}" + s.resources = ["ios/Resources/hyperswitch-vault-fields.bundle"] + # The second host is given the same native modules and views the app links. + s.dependency "ReactAppDependencyProvider" + s.dependency "React-RCTAppDelegate" + elsif new_architecture + # The three classes React Native's generated code names, doing nothing. + s.source_files = "ios/Stub/**/*.{h,m,mm}" + else + s.source_files = "ios/Empty/**/*.{h,m}" + end + + install_modules_dependencies(s) +end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/HyperswitchVaultFieldViewNativeComponent.ts b/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/HyperswitchVaultFieldViewNativeComponent.ts index 81c7edf4..131bc711 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/HyperswitchVaultFieldViewNativeComponent.ts +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/HyperswitchVaultFieldViewNativeComponent.ts @@ -19,6 +19,4 @@ export interface NativeProps extends ViewProps { }>; } -export default codegenNativeComponent('HyperswitchVaultFieldView', { - excludedPlatforms: ['iOS'], -}); +export default codegenNativeComponent('HyperswitchVaultFieldView'); From c3a2e11bdcd9f779b7c5e87db7ca359a66719c69 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Wed, 25 Nov 2026 17:34:50 +0530 Subject: [PATCH 2/5] fix(vault): iOS: the second React Native factory can no longer crash the app, and a delivered tokenize is an unknown outcome RCTReactNativeFactory assumes it is the app's only factory. Creating the vault's raised an uncaught NSException when the app's own factory used a non-Stable release level (Expo exposes it as reactNativeReleaseLevel), and it replaced the app's third-party Fabric component provider and default colour space for the whole process. The vault's factory now uses the app's release level (Info.plist ReactNativeReleaseLevel, as Expo writes it; Stable otherwise), puts the component provider and the colour space back right after it is made, and catches any Objective-C or C++ exception while it is made. A failure is remembered and never retried; the form then fails before it mounts and the fields fall back to plain JavaScript. As on Android, a command that reached the bundle and then timed out, or whose form closed or failed, is refused with "delivered": true, which the merchant's side reports as unknown_outcome. --- .../ios/Source/HyperswitchVaultEngine.h | 11 +-- .../ios/Source/HyperswitchVaultEngine.mm | 72 ++++++++++++++++--- .../ios/Source/HyperswitchVaultFieldView.mm | 4 ++ .../ios/Source/HyperswitchVaultForm.mm | 31 ++++++-- 4 files changed, 101 insertions(+), 17 deletions(-) diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h index 1e4c0465..0a550e40 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h @@ -42,10 +42,13 @@ NS_ASSUME_NONNULL_BEGIN /* Whether this build of the app carries the bundle. */ + (BOOL)isBundled; -/* Starts a root of the bundle. `owner` receives its events until stop. */ -- (HyperswitchVaultSurface *)start:(NSString *)rootName - props:(NSDictionary *)props - owner:(id)owner; +/* + * Starts a root of the bundle. `owner` receives its events until stop. nil when the engine cannot + * start in this app: the form then fails before it mounts, and the fields render the plain way. + */ +- (nullable HyperswitchVaultSurface *)start:(NSString *)rootName + props:(NSDictionary *)props + owner:(id)owner; /* From the bundle: hand the event to the root's owner. */ - (void)deliver:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payloadJson; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm index 4c3bfde0..1bcd491a 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -1,6 +1,8 @@ #import "HyperswitchVaultEngine.h" #import +#import +#import #import #import #import @@ -90,9 +92,32 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge #pragma mark - Engine +/* + * The release level the app's own React Native factory uses. React Native allows one per process: + * a second factory created with another level raises. Expo apps record theirs in Info.plist + * (expo-build-properties `ios.reactNativeReleaseLevel`); without that key it is Stable, React + * Native's default. + */ +static RCTReleaseLevel AppReleaseLevel(void) +{ + id value = [NSBundle.mainBundle objectForInfoDictionaryKey:@"ReactNativeReleaseLevel"]; + if ([value isKindOfClass:NSString.class]) { + NSString *level = [(NSString *)value lowercaseString]; + if ([level isEqualToString:@"experimental"]) { + return Experimental; + } + if ([level isEqualToString:@"canary"]) { + return Canary; + } + } + return Stable; +} + @implementation HyperswitchVaultEngine { HyperswitchVaultEngineDelegate *_delegate; RCTReactNativeFactory *_factory; + /* Set once, if the factory could not be made. Never retried in this process. */ + BOOL _unavailable; __weak HyperswitchVaultHostedModule *_module; /* @@ -128,20 +153,51 @@ - (instancetype)init return self; } -/* The second host, made the first time a root is asked for. */ -- (RCTReactNativeFactory *)factory +/* + * The second host, made the first time a root is asked for, or nil when it cannot be made here. + * + * RCTReactNativeFactory assumes it is the app's only factory. Creating it + * - raises if the app's own factory chose another release level (one per process); + * - makes itself the process-wide provider of third-party Fabric components; + * - sets the process-wide default colour space. + * So it is created with the app's release level, the provider and the colour space are put back + * right after, and any failure (an Objective-C or a C++ exception) is caught, remembered and never + * retried: React Native's set-up may be half done after one. The forms then use the plain fields. + */ +- (nullable RCTReactNativeFactory *)factory { - if (_factory == nil) { - _factory = [[RCTReactNativeFactory alloc] initWithDelegate:_delegate]; + if (_factory != nil || _unavailable) { + return _factory; } + RCTComponentViewFactory *components = [RCTComponentViewFactory currentComponentViewFactory]; + id provider = components.thirdPartyFabricComponentsProvider; + RCTColorSpace colorSpace = RCTGetDefaultColorSpace(); + RCTReactNativeFactory *factory = nil; + @try { + try { + factory = [[RCTReactNativeFactory alloc] initWithDelegate:_delegate releaseLevel:AppReleaseLevel()]; + } catch (...) { + factory = nil; + } + } @catch (NSException *exception) { + factory = nil; + } + components.thirdPartyFabricComponentsProvider = provider; + RCTSetDefaultColorSpace(colorSpace); + _factory = factory; + _unavailable = factory == nil; return _factory; } -- (HyperswitchVaultSurface *)start:(NSString *)rootName - props:(NSDictionary *)props - owner:(id)owner +- (nullable HyperswitchVaultSurface *)start:(NSString *)rootName + props:(NSDictionary *)props + owner:(id)owner { - UIView *rootView = [[self factory].rootViewFactory viewWithModuleName:rootName initialProperties:props]; + RCTReactNativeFactory *factory = [self factory]; + if (factory == nil) { + return nil; + } + UIView *rootView = [factory.rootViewFactory viewWithModuleName:rootName initialProperties:props]; rootView.backgroundColor = UIColor.clearColor; HyperswitchVaultSurface *surface = [HyperswitchVaultSurface new]; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm index 614cbfe4..7256bada 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm @@ -79,6 +79,10 @@ - (void)render return; } _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFieldRoot props:[self propsOf:settings] owner:self]; + if (_surface == nil) { + /* The engine cannot start in this app; the form reports it and falls back to plain fields. */ + return; + } _startedFor = identity; _renderedSettings = settings; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm index 229502c5..e347cd49 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm @@ -6,10 +6,15 @@ /* The bundle has to load and its root has to mount. Long for a slow first start, short for a person waiting. */ static const NSTimeInterval OpenTimeout = 15; -/* Longer than the vault's own network timeouts, so a tokenize that is still working is not cut off. */ +/* + * The only limit on a command: the vault's tokenize request has no timeout of its own, and neither + * has React Native's HTTP client. A tokenize cut off here was delivered and may still save the + * card, so its refusal says so (see refusal:delivered:). + */ static const NSTimeInterval CommandTimeout = 90; static NSString *const NotBundled = @"This build of the app has no card-form bundle."; +static NSString *const EngineUnavailable = @"The card form's engine could not start."; static NSString *const NotStarted = @"The card form did not start."; static NSString *const NotOpen = @"The card form is not open."; static NSString *const Closed = @"The card form was closed."; @@ -58,7 +63,18 @@ @implementation HyperswitchVaultForm { + (NSString *)refusal:(NSString *)message { - return JsonOf(@{@"ok" : @NO, @"message" : message}); + return [self refusal:message delivered:NO]; +} + +/* + * A command with no answer from the bundle. `delivered` marks one that HAD reached the bundle + * (timeout, form closed, engine stopped): a tokenize may then have saved the card, and the + * merchant's side reports an unknown outcome, never a failure. + */ ++ (NSString *)refusal:(NSString *)message delivered:(BOOL)delivered +{ + return delivered ? JsonOf(@{@"ok" : @NO, @"message" : message, @"delivered" : @YES}) + : JsonOf(@{@"ok" : @NO, @"message" : message}); } - (instancetype)initWithFormId:(NSString *)formId emit:(HyperswitchVaultEmit)emit @@ -81,6 +97,11 @@ - (void)open:(NSString *)settings } /* The hidden root's view is never put on screen. */ _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFormRoot props:[self propsOf:settings] owner:self]; + if (_surface == nil) { + /* Before it mounts, so the merchant's side shows the plain fields. */ + [self fail:EngineUnavailable]; + return; + } NSInteger generation = ++_openGeneration; __weak HyperswitchVaultForm *weakSelf = self; @@ -113,7 +134,7 @@ - (void)command:(NSString *)name elementType:(NSString *)elementType reply:(Hype NSString *commandId = NSUUID.UUID.UUIDString; form->_pending[commandId] = [reply copy]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CommandTimeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [weakSelf answer:commandId result:[HyperswitchVaultForm refusal:NoAnswer]]; + [weakSelf answer:commandId result:[HyperswitchVaultForm refusal:NoAnswer delivered:YES]]; }); NSMutableDictionary *command = [@{ @@ -141,7 +162,7 @@ - (void)close [_surface stop]; _surface = nil; [self settle:PhaseFailed failure:Closed]; - [self answerAll:[HyperswitchVaultForm refusal:Closed]]; + [self answerAll:[HyperswitchVaultForm refusal:Closed delivered:YES]]; } #pragma mark - Events from the form's root @@ -181,7 +202,7 @@ - (void)fail:(NSString *)message _phase = PhaseFailed; _failure = message; [self drain]; - [self answerAll:[HyperswitchVaultForm refusal:message]]; + [self answerAll:[HyperswitchVaultForm refusal:message delivered:YES]]; _emit(HyperswitchVaultFormError, JsonOf(@{@"message" : message})); } From 5826e10b2119257372c22892cfa26597b08bd077 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 26 Nov 2026 10:56:54 +0530 Subject: [PATCH 3/5] fix(vault): iOS: only the vault engine's own module carries events, and the stub links on React Native 0.76 HyperswitchVaultHosted is registered for every engine in the app on iOS (codegen's modulesProvider), so code in the merchant's engine could deliver a VAULT_FORM_ERROR to a form root by its tag and push the form to the plain fields. The module now ignores emits from any instance but the one the vault's engine attached. The stub gains HyperswitchVaultFieldViewCls, which React Native 0.76's generated component provider links against by name. --- .../ios/Source/HyperswitchVaultEngine.h | 3 +++ .../ios/Source/HyperswitchVaultEngine.mm | 5 +++++ .../ios/Source/HyperswitchVaultHostedModule.mm | 11 +++++++++++ .../ios/Stub/HyperswitchVaultStub.mm | 6 ++++++ .../src/specs/NativeHyperswitchVaultHosted.ts | 6 ++++-- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h index 0a550e40..6447f695 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h @@ -59,6 +59,9 @@ NS_ASSUME_NONNULL_BEGIN /* The module of this engine that carries commands to the bundle. */ - (void)attach:(HyperswitchVaultHostedModule *)module; +/* Whether `module` is that one. Any other instance belongs to another engine (the merchant's). */ +- (BOOL)isAttached:(HyperswitchVaultHostedModule *)module; + - (void)forget:(HyperswitchVaultSurface *)surface; @end diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm index 1bcd491a..db7eaef4 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -241,6 +241,11 @@ - (void)attach:(HyperswitchVaultHostedModule *)module _module = module; } +- (BOOL)isAttached:(HyperswitchVaultHostedModule *)module +{ + return module != nil && _module == module; +} + - (BOOL)send:(NSDictionary *)command { HyperswitchVaultHostedModule *module = _module; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm index 1ceec9c7..e9b6f77f 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm @@ -28,13 +28,24 @@ - (BOOL)sendCommand:(NSDictionary *)command #pragma mark - NativeHyperswitchVaultHostedSpec +/* + * On iOS this module is registered for every engine in the app (codegen's modulesProvider), so the + * merchant's engine can call it too. Only the instance the vault's own engine made carries events: + * any other is ignored, so code in the merchant's engine cannot fail or end a form from outside. + */ - (void)emitFieldEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload { + if (![HyperswitchVaultEngine.shared isAttached:self]) { + return; + } [HyperswitchVaultEngine.shared deliver:rootTag eventName:eventName payload:payload ?: @"{}"]; } - (void)emitFormEvent:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payload { + if (![HyperswitchVaultEngine.shared isAttached:self]) { + return; + } [HyperswitchVaultEngine.shared deliver:rootTag eventName:eventName payload:payload ?: @"{}"]; } diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm index acb46f9d..de601ae6 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm @@ -124,3 +124,9 @@ - (instancetype)initWithFrame:(CGRect)frame } @end + +/* React Native 0.76's generated component provider links against this function by name. */ +Class HyperswitchVaultFieldViewCls(void) +{ + return HyperswitchVaultFieldView.class; +} diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/NativeHyperswitchVaultHosted.ts b/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/NativeHyperswitchVaultHosted.ts index 81095ec8..c863ae2d 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/NativeHyperswitchVaultHosted.ts +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/src/specs/NativeHyperswitchVaultHosted.ts @@ -1,6 +1,8 @@ /* - * The native module the vault's OWN engine talks to (src/hosted/bridge.mjs). It exists only in that - * engine: the merchant's engine never has it. Events go out through it, and commands come in. + * The native module the vault's OWN engine talks to (src/hosted/bridge.mjs). Events go out through + * it, and commands come in. On Android only that engine has it. On iOS it is registered for every + * engine in the app, but only the vault engine's own instance carries events; calls from any other + * engine are ignored (HyperswitchVaultHostedModule.mm). * * `rootTag` is the tag of the React root the event comes from, or the command goes to. Native code * finds the owner of that root by it. From 379e92613752dc24e15a3250aaac37ba21f660b1 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Wed, 2 Dec 2026 10:27:39 +0530 Subject: [PATCH 4/5] feat(vault): iOS: native card fields only, no opt-out The iOS side of "native card fields only" (the JavaScript is in the commit before this stack's iOS commits). The podspec no longer reads HYPERSWITCH_VAULT_NATIVE_FIELDS; a `pod install` that still sets it is told it is ignored. An app without the new architecture or below React Native 0.82 still builds with the stand-in, and `pod install` now warns that the card fields are not available in it, and why: at run time the card form reports unsupported_configuration and draws no field. The comments that said the forms fall back to plain fields now say what happens instead. --- .../ios/Empty/HyperswitchVaultEmpty.m | 3 ++- .../ios/Source/HyperswitchVaultEngine.mm | 3 ++- .../ios/Source/HyperswitchVaultFieldView.mm | 2 +- .../ios/Source/HyperswitchVaultForm.mm | 2 +- .../ios/Stub/HyperswitchVaultStub.mm | 6 ++--- .../react-native-hyperswitch-vault.podspec | 24 ++++++++++++------- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m index c3645610..a339fcbe 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m @@ -1,6 +1,7 @@ /* * The whole of the vault's native code in an app on the old architecture: nothing. The pod still - * needs one file to compile. The card fields render in plain JavaScript. + * needs one file to compile. There are no card fields: the card form reports unsupported_configuration + * (native fields only). */ #import diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm index db7eaef4..cd6b8efe 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -162,7 +162,8 @@ - (instancetype)init * - sets the process-wide default colour space. * So it is created with the app's release level, the provider and the colour space are put back * right after, and any failure (an Objective-C or a C++ exception) is caught, remembered and never - * retried: React Native's set-up may be half done after one. The forms then use the plain fields. + * retried: React Native's set-up may be half done after one. The forms then report that they did not + * start (unsupported_configuration in the app's JavaScript). */ - (nullable RCTReactNativeFactory *)factory { diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm index 7256bada..781ca7b4 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm @@ -80,7 +80,7 @@ - (void)render } _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFieldRoot props:[self propsOf:settings] owner:self]; if (_surface == nil) { - /* The engine cannot start in this app; the form reports it and falls back to plain fields. */ + /* The engine cannot start in this app; the form reports it (unsupported_configuration). */ return; } _startedFor = identity; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm index e347cd49..855be02b 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm @@ -98,7 +98,7 @@ - (void)open:(NSString *)settings /* The hidden root's view is never put on screen. */ _surface = [HyperswitchVaultEngine.shared start:HyperswitchVaultFormRoot props:[self propsOf:settings] owner:self]; if (_surface == nil) { - /* Before it mounts, so the merchant's side shows the plain fields. */ + /* Before it mounts, so the merchant's side reports that the form did not start. */ [self fail:EngineUnavailable]; return; } diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm index de601ae6..ba044f71 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm @@ -1,11 +1,11 @@ /* * What an app links when the vault's own engine is not built into it: React Native older than the - * bundle is built for, or the app chose to build without the native fields (the podspec decides). + * bundle is built for (the podspec decides, and says so in the `pod install` output). * * React Native's generated code names these three classes for every app on the new architecture, * and a missing view class would crash the app at start. So they exist here, and do nothing: the - * module reports that there is no bundle, the vault's JavaScript reads that and renders the card - * fields in plain JavaScript, as it did before there was a native path. + * module reports that there is no bundle, and the vault's JavaScript reads that: the card fields are + * native only, so the card form reports unsupported_configuration and draws no field. */ #import #import diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec index affa79d3..b2db721e 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec @@ -16,17 +16,25 @@ react_native_minor = # 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 did not opt out with HYPERSWITCH_VAULT_NATIVE_FIELDS=0 when running `pod install`. -# Otherwise the pod compiles a stand-in with no engine and no bundle. The app still builds, and the -# card fields render in plain JavaScript as before. +# - React Native is 0.82 or newer, which is what the bundle is built for. +# Otherwise the pod compiles a stand-in with no engine 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. `pod install` says so, and why. +# +# There is no opt-out any more. HYPERSWITCH_VAULT_NATIVE_FIELDS is no longer read; a `pod install` +# that still sets it is told so. new_architecture = react_native_minor >= 82 || ENV["RCT_NEW_ARCH_ENABLED"] == "1" -opted_out = ENV["HYPERSWITCH_VAULT_NATIVE_FIELDS"] == "0" -hosts_own_engine = new_architecture && react_native_minor >= 82 && !opted_out +hosts_own_engine = new_architecture && react_native_minor >= 82 +if ENV.key?("HYPERSWITCH_VAULT_NATIVE_FIELDS") + Pod::UI.warn "[@juspay-tech/react-native-hyperswitch-vault] HYPERSWITCH_VAULT_NATIVE_FIELDS is no longer read: " \ + "the card fields are native only." +end unless hosts_own_engine - Pod::UI.puts "[@juspay-tech/react-native-hyperswitch-vault] card fields: plain JavaScript in this build " \ - "(new architecture: #{new_architecture}, React Native minor: #{react_native_minor}, opted out: #{opted_out})." + Pod::UI.warn "[@juspay-tech/react-native-hyperswitch-vault] The card fields are NOT available in this build " \ + "(new architecture: #{new_architecture}, React Native minor: #{react_native_minor}). " \ + "They need the new architecture and React Native 0.82 to 0.88. " \ + "The card form will report unsupported_configuration and draw no field." end Pod::Spec.new do |s| From 8e6bf870156d7bd601103a02aa3f9f7c2163d46a Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 3 Dec 2026 11:22:04 +0530 Subject: [PATCH 5/5] fix(vault): iOS review fixes: no crash on third-party JavaScriptCore, settings on save - An app on community JavaScriptCore (USE_THIRD_PARTY_JSC=1) gets the stand-in, and pod install says why: React Native's default delegate raises in createJSRuntimeFactory there, which would end the app at the first card field. As a second line of defence, creating the vault engine's root is caught (Objective-C and C++ exceptions), remembered and never retried, and the form reports that it did not start (unsupported_configuration). - A save carries the settings it was accepted with. One queued while the form starts whose settings changed before it could run is refused, unsent (tokenization_failed, try again), as on Android. --- .../ios/Source/HyperswitchVaultEngine.mm | 25 +++++++++++++-- .../ios/Source/HyperswitchVaultForm.mm | 31 +++++++++++++++++++ .../react-native-hyperswitch-vault.podspec | 13 +++++--- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm index cd6b8efe..f703ffb3 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -194,11 +194,32 @@ - (nullable HyperswitchVaultSurface *)start:(NSString *)rootName props:(NSDictionary *)props owner:(id)owner { - RCTReactNativeFactory *factory = [self factory]; + RCTReactNativeFactory *factory = _unavailable ? nil : [self factory]; if (factory == nil) { return nil; } - UIView *rootView = [factory.rootViewFactory viewWithModuleName:rootName initialProperties:props]; + /* + * The first root also creates the engine's runtime, and that can raise outside the factory's own + * set-up above: React Native's default delegate raises in createJSRuntimeFactory when the app uses + * third-party JavaScriptCore (the podspec builds the stand-in for such apps; this is the second + * line of defence). As there, the failure is caught, remembered and never retried, and the form + * reports that it did not start (unsupported_configuration in the app's JavaScript) instead of + * ending the app. + */ + UIView *rootView = nil; + @try { + try { + rootView = [factory.rootViewFactory viewWithModuleName:rootName initialProperties:props]; + } catch (...) { + rootView = nil; + } + } @catch (NSException *exception) { + rootView = nil; + } + if (rootView == nil) { + _unavailable = YES; + return nil; + } rootView.backgroundColor = UIColor.clearColor; HyperswitchVaultSurface *surface = [HyperswitchVaultSurface new]; diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm index 855be02b..afff0f43 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm @@ -20,6 +20,7 @@ static NSString *const Closed = @"The card form was closed."; static NSString *const Unreachable = @"The card form cannot be reached."; static NSString *const NoAnswer = @"The card form did not answer."; +static NSString *const SettingsChanged = @"The card form's settings changed before the save could run."; typedef NS_ENUM(NSInteger, Phase) { PhaseOpening, PhaseOpen, PhaseFailed }; @@ -53,6 +54,13 @@ @implementation HyperswitchVaultForm { NSString *_failure; BOOL _closed; + /* + * The form's settings as last sent by the merchant's side (JSON text). A tokenize command carries + * them: React in the vault's engine draws a props update in a later task, so a command sent right + * after one could otherwise run with the previous settings and session (hosted/commands.mjs). + */ + NSString *_settings; + /* Commands that arrived before the form's root was up. Run, in order, once it is up or has failed. */ NSMutableArray *_whenSettled; NSMutableDictionary *_pending; @@ -77,6 +85,12 @@ + (NSString *)refusal:(NSString *)message delivered:(BOOL)delivered : JsonOf(@{@"ok" : @NO, @"message" : message}); } +/* A queued save refused, unsent, because the form's settings changed while it waited. */ ++ (NSString *)settingsChanged +{ + return JsonOf(@{@"ok" : @NO, @"message" : SettingsChanged, @"settingsChanged" : @YES}); +} + - (instancetype)initWithFormId:(NSString *)formId emit:(HyperswitchVaultEmit)emit { if (self = [super init]) { @@ -91,6 +105,7 @@ - (instancetype)initWithFormId:(NSString *)formId emit:(HyperswitchVaultEmit)emi - (void)open:(NSString *)settings { + _settings = [settings copy]; if (![HyperswitchVaultEngine isBundled]) { [self fail:NotBundled]; return; @@ -115,11 +130,20 @@ - (void)open:(NSString *)settings - (void)update:(NSString *)settings { + _settings = [settings copy]; [_surface update:[self propsOf:settings]]; } - (void)command:(NSString *)name elementType:(NSString *)elementType reply:(HyperswitchVaultReply)reply { + /* + * A save runs with the settings it was accepted with. One queued while the form starts can wait + * up to the open timeout; if the merchant's side sent new settings meanwhile (a new session, + * say), the save belonged to the checkout that was replaced, so it is refused, unsent, rather + * than run with settings the payer did not save with. Updates and commands both run on the main + * queue, in the order the merchant's side made them. + */ + NSString *acceptedWith = _settings ?: @""; __weak HyperswitchVaultForm *weakSelf = self; [self whenFormSettles:^{ HyperswitchVaultForm *form = weakSelf; @@ -131,6 +155,10 @@ - (void)command:(NSString *)name elementType:(NSString *)elementType reply:(Hype reply([HyperswitchVaultForm refusal:form->_failure ?: NotOpen]); return; } + if ([name isEqualToString:@"tokenize"] && ![acceptedWith isEqualToString:form->_settings ?: @""]) { + reply([HyperswitchVaultForm settingsChanged]); + return; + } NSString *commandId = NSUUID.UUID.UUIDString; form->_pending[commandId] = [reply copy]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CommandTimeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ @@ -146,6 +174,9 @@ - (void)command:(NSString *)name elementType:(NSString *)elementType reply:(Hype if (elementType.length > 0) { command[@"elementType"] = elementType; } + if ([name isEqualToString:@"tokenize"]) { + command[@"settings"] = acceptedWith; + } if (![HyperswitchVaultEngine.shared send:command]) { [form answer:commandId result:[HyperswitchVaultForm refusal:Unreachable]]; } diff --git a/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec index b2db721e..84210496 100644 --- a/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec @@ -16,7 +16,10 @@ react_native_minor = # 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. +# - React Native is 0.82 or newer, which is what the bundle is built for; +# - the app runs Hermes, React Native's default. With third-party JavaScriptCore +# (USE_THIRD_PARTY_JSC=1, React Native's own flag) the vault's engine has no runtime: React +# Native's default delegate raises in createJSRuntimeFactory, which would end the app. # Otherwise the pod compiles a stand-in with no engine 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. `pod install` says so, and why. @@ -24,7 +27,8 @@ react_native_minor = # There is no opt-out any more. HYPERSWITCH_VAULT_NATIVE_FIELDS is no longer read; a `pod install` # that still sets it is told so. new_architecture = react_native_minor >= 82 || ENV["RCT_NEW_ARCH_ENABLED"] == "1" -hosts_own_engine = new_architecture && react_native_minor >= 82 +third_party_jsc = ENV["USE_THIRD_PARTY_JSC"] == "1" +hosts_own_engine = new_architecture && react_native_minor >= 82 && !third_party_jsc if ENV.key?("HYPERSWITCH_VAULT_NATIVE_FIELDS") Pod::UI.warn "[@juspay-tech/react-native-hyperswitch-vault] HYPERSWITCH_VAULT_NATIVE_FIELDS is no longer read: " \ @@ -32,8 +36,9 @@ if ENV.key?("HYPERSWITCH_VAULT_NATIVE_FIELDS") end unless hosts_own_engine Pod::UI.warn "[@juspay-tech/react-native-hyperswitch-vault] The card fields are NOT available in this build " \ - "(new architecture: #{new_architecture}, React Native minor: #{react_native_minor}). " \ - "They need the new architecture and React Native 0.82 to 0.88. " \ + "(new architecture: #{new_architecture}, React Native minor: #{react_native_minor}, " \ + "third-party JavaScriptCore: #{third_party_jsc}). " \ + "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." end