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..a339fcbe --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Empty/HyperswitchVaultEmpty.m @@ -0,0 +1,10 @@ +/* + * The whole of the vault's native code in an app on the old architecture: nothing. The pod still + * needs one file to compile. There are no card fields: the card form reports unsupported_configuration + * (native fields only). + */ +#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..6447f695 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.h @@ -0,0 +1,69 @@ +#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. 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; + +/* 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; + +/* Whether `module` is that one. Any other instance belongs to another engine (the merchant's). */ +- (BOOL)isAttached:(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..f703ffb3 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultEngine.mm @@ -0,0 +1,280 @@ +#import "HyperswitchVaultEngine.h" + +#import +#import +#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 + +/* + * 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; + + /* + * 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, 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 report that they did not + * start (unsupported_configuration in the app's JavaScript). + */ +- (nullable RCTReactNativeFactory *)factory +{ + 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; +} + +- (nullable HyperswitchVaultSurface *)start:(NSString *)rootName + props:(NSDictionary *)props + owner:(id)owner +{ + RCTReactNativeFactory *factory = _unavailable ? nil : [self factory]; + if (factory == nil) { + return nil; + } + /* + * 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]; + 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)isAttached:(HyperswitchVaultHostedModule *)module +{ + return module != nil && _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..781ca7b4 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultFieldView.mm @@ -0,0 +1,162 @@ +#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]; + if (_surface == nil) { + /* The engine cannot start in this app; the form reports it (unsupported_configuration). */ + return; + } + _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..afff0f43 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultForm.mm @@ -0,0 +1,329 @@ +#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; + +/* + * 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."; +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 }; + +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; + + /* + * 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; + + /* Bumped when the open timeout must no longer fire. */ + NSInteger _openGeneration; +} + ++ (NSString *)refusal:(NSString *)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}); +} + +/* 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]) { + _formId = [formId copy]; + _emit = [emit copy]; + _phase = PhaseOpening; + _whenSettled = [NSMutableArray new]; + _pending = [NSMutableDictionary new]; + } + return self; +} + +- (void)open:(NSString *)settings +{ + _settings = [settings copy]; + 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]; + if (_surface == nil) { + /* Before it mounts, so the merchant's side reports that the form did not start. */ + [self fail:EngineUnavailable]; + return; + } + + 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 +{ + _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; + if (form == nil) { + reply([HyperswitchVaultForm refusal:Closed]); + return; + } + if (form->_phase == PhaseFailed || form->_surface == nil) { + 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(), ^{ + [weakSelf answer:commandId result:[HyperswitchVaultForm refusal:NoAnswer delivered:YES]]; + }); + + NSMutableDictionary *command = [@{ + @"rootTag" : @(form->_surface.rootTag), + @"formId" : form->_formId, + @"commandId" : commandId, + @"name" : name, + } mutableCopy]; + 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]]; + } + }]; +} + +- (void)close +{ + if (_closed) { + return; + } + _closed = YES; + _openGeneration++; + [_surface stop]; + _surface = nil; + [self settle:PhaseFailed failure:Closed]; + [self answerAll:[HyperswitchVaultForm refusal:Closed delivered:YES]]; +} + +#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 delivered:YES]]; + _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..e9b6f77f --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Source/HyperswitchVaultHostedModule.mm @@ -0,0 +1,58 @@ +#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 + +/* + * 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 ?: @"{}"]; +} + +- (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..ba044f71 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/ios/Stub/HyperswitchVaultStub.mm @@ -0,0 +1,132 @@ +/* + * What an app links when the vault's own engine is not built into it: React Native older than the + * 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, 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 +#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 + +/* 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/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..84210496 --- /dev/null +++ b/packages/@juspay-tech/react-native-hyperswitch-vault/react-native-hyperswitch-vault.podspec @@ -0,0 +1,71 @@ +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 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. +# +# 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" +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: " \ + "the card fields are native only." +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}, " \ + "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 + +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'); 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.