diff --git a/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java b/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java index c043cc7..0ee3767 100644 --- a/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java +++ b/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java @@ -11,6 +11,7 @@ import android.app.Application; import android.content.Intent; import android.os.Handler; +import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -27,6 +28,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule; @@ -34,11 +36,14 @@ import org.json.JSONObject; import java.lang.ref.WeakReference; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.UUID; import in.juspay.hypercheckoutlite.HyperCheckoutLite; import in.juspay.hypersdk.core.MerchantViewType; @@ -74,6 +79,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A @Nullable private HyperServices hyperServices; + private final Map hyperServicesMap = new HashMap<>(); + private static WeakReference hyperServicesReference = new WeakReference<>(null); private final ReactApplicationContext context; @@ -209,8 +216,59 @@ public void createHyperServices() { "hyperServices instance already exists"); return; } - createHyperService(null, null); + createNewHyperServices("" , ""); + } + } + + @ReactMethod + public void createNewHyperServices(String tenantId, String clientId) { + synchronized (lock) { + if (Objects.equals(tenantId, "") && Objects.equals(clientId, "")){ + createHyperService(null, null); + }else{ + createHyperService(tenantId, clientId); + } + } + } + + @ReactMethod + public void createHyperServicesWithKey(String key, String tenantId, String clientId) { + synchronized (lock) { + if (Objects.equals(tenantId, "") && Objects.equals(clientId, "")){ + createHyperServiceWithKey(key, null, null); + }else{ + createHyperServiceWithKey(key, tenantId, clientId); + } + } + } + + private void createHyperServiceWithKey(String key, @Nullable String tenantId, @Nullable String clientId){ + FragmentActivity activity = (FragmentActivity) getCurrentActivity(); + if (activity == null) { + SdkTracker.trackBootLifecycle( + LogConstants.SUBCATEGORY_HYPER_SDK, + LogConstants.LEVEL_ERROR, + LogConstants.SDK_TRACKER_LABEL, + "createHyperServices", + "activity is null"); + return; + } + Application app = activity.getApplication(); + HyperServices hyperServices; + if (app instanceof ReactApplication) { + reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager(); + } + if (tenantId != null && clientId != null) { + hyperServices = new HyperServices(activity, tenantId, clientId); + } else { + hyperServices = new HyperServices(activity); } + hyperServicesReference = new WeakReference<>(hyperServices); + + requestPermissionsResultDelegate.set(hyperServices); + activityResultDelegate.set(hyperServices); + + hyperServicesMap.put(key, hyperServices); } @ReactMethod @@ -225,12 +283,25 @@ public void createHyperServicesWithTenantId(String tenantId, String clientId) { "hyperServices instance already exists"); return; } - createHyperService(tenantId, clientId); + createNewHyperServices(tenantId, clientId); } } @ReactMethod(isBlockingSynchronousMethod = true) public boolean onBackPressed() { + return onBackPressed(hyperServicesReference.get()); + } + + @ReactMethod(isBlockingSynchronousMethod = true) + public boolean onBackPressedWithKey(String key) { + if (key == null) return false; + + if (hyperServicesMap.get(key) == null) return false; + + return onBackPressed(hyperServicesMap.get(key)); + } + + private boolean onBackPressed(HyperServices hyperServices) { synchronized (lock) { return hyperServices != null && hyperServices.onBackPressed(); } @@ -238,6 +309,18 @@ public boolean onBackPressed() { @ReactMethod public void initiate(String data) { + initiate(HYPER_EVENT, hyperServicesReference.get(), data); + } + + @ReactMethod + public void initiateWithKey(String data, String key) { + if (key == null) { + return; + } + initiate(key, hyperServicesMap.get(key), data); + } + + public void initiate(String key, HyperServices hyperServices, String data) { synchronized (lock) { try { JSONObject payload = new JSONObject(data); @@ -277,7 +360,7 @@ public void onEvent(JSONObject data, JuspayResponseHandler handler) { wasProcessWithActivity = false; processActivityRef = new WeakReference<>(null); } - sendEventToJS(data); + sendEventToJS(key, data); } @Nullable @@ -324,6 +407,7 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy } private void createHyperService(@Nullable String tenantId, @Nullable String clientId) { + String key = UUID.randomUUID().toString(); FragmentActivity activity = (FragmentActivity) getCurrentActivity(); if (activity == null) { SdkTracker.trackBootLifecycle( @@ -353,15 +437,15 @@ private boolean isViewRegistered(String tag) { return registeredComponents.contains(tag); } - private void sendEventToJS(JSONObject data) { + private void sendEventToJS(String key, JSONObject data) { DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = getJSModule(); if (jsModule == null) { Handler handler = new Handler(); - handler.postDelayed(() -> sendEventToJS(data), 200); + handler.postDelayed(() -> sendEventToJS(key, data), 200); return; } - jsModule.emit(HYPER_EVENT, data.toString()); + jsModule.emit(key, data.toString()); } private DeviceEventManagerModule.RCTDeviceEventEmitter getJSModule() { @@ -371,6 +455,17 @@ private DeviceEventManagerModule.RCTDeviceEventEmitter getJSModule() { @ReactMethod public void process(String data) { + process(hyperServicesReference.get(), data); + } + @ReactMethod + public void processWithKey(String data, String key) { + if (key == null) { + return; + } + process(hyperServicesMap.get(key), data); + } + + public void process(HyperServices hyperService, String data) { synchronized (lock) { try { JSONObject payload = new JSONObject(data); @@ -386,7 +481,7 @@ public void process(String data) { return; } - if (hyperServices == null) { + if (hyperService == null) { SdkTracker.trackBootLifecycle( LogConstants.SUBCATEGORY_HYPER_SDK, LogConstants.LEVEL_ERROR, @@ -395,10 +490,10 @@ public void process(String data) { "hyperServices is null"); return; } - hyperServices.setActivityLaunchDelegate(new ReactLaunchDelegate(context)); - hyperServices.setRequestPermissionDelegate(new ReactRequestDelegate(activity)); + hyperService.setActivityLaunchDelegate(new ReactLaunchDelegate(context)); + hyperService.setRequestPermissionDelegate(new ReactRequestDelegate(activity)); - hyperServices.process(activity, payload); + hyperService.process(activity, payload); } catch (JSONException e) { SdkTracker.trackAndLogBootException( NAME, @@ -415,6 +510,18 @@ public void process(String data) { @ReactMethod public void processWithActivity(String data) { + processWithActivity(hyperServicesReference.get(), data); + } + @ReactMethod + public void processWithActivityWithKey(String data, String key) { + if (key == null) { + return; + } + processWithActivity(hyperServicesMap.get(key), data); + } + + + public void processWithActivity(HyperServices hyperService, String data) { synchronized (lock) { try { JSONObject payload = new JSONObject(data); @@ -434,7 +541,7 @@ public void processWithActivity(String data) { ProcessActivity.setActivityCallback(new ActivityCallback() { @Override public void onCreated(@NonNull FragmentActivity fragmentActivity) { - if (hyperServices == null) { + if (hyperService == null) { SdkTracker.trackBootLifecycle( LogConstants.SUBCATEGORY_HYPER_SDK, LogConstants.LEVEL_ERROR, @@ -446,7 +553,7 @@ public void onCreated(@NonNull FragmentActivity fragmentActivity) { wasProcessWithActivity = true; processActivityRef = new WeakReference<>(fragmentActivity); - hyperServices.process(fragmentActivity, payload); + hyperService.process(fragmentActivity, payload); } @Override @@ -456,8 +563,8 @@ public boolean onBackPressed() { @Override public void resetActivity(@NonNull FragmentActivity activity) { - if (hyperServices != null) { - hyperServices.resetActivity(activity); + if (hyperService != null) { + hyperService.resetActivity(activity); } } }); @@ -476,6 +583,11 @@ public void resetActivity(@NonNull FragmentActivity activity) { } + @ReactMethod + public void openPaymentPageWithKey(String data, String key){ + openPaymentPage(data); + } + @ReactMethod public void openPaymentPage(String data) { synchronized (lock) { @@ -505,7 +617,7 @@ public void onEvent(JSONObject data, JuspayResponseHandler handler) { processActivity.overridePendingTransition(0, android.R.anim.fade_out); ProcessActivity.setActivityCallback(null); } - sendEventToJS(data); + sendEventToJS(HYPER_EVENT, data); } }); } @@ -530,6 +642,19 @@ public void resetActivity(@NonNull FragmentActivity activity) { @ReactMethod public void terminate() { + terminate(hyperServicesReference.get()); + } + + @ReactMethod + public void terminateWithKey(String key) { + if (key == null) { + return; + } + terminate(hyperServicesMap.get(key)); + hyperServicesMap.remove(key); + } + + public void terminate(HyperServices hyperServices) { synchronized (lock) { if (hyperServices != null) { hyperServices.terminate(); @@ -547,17 +672,50 @@ public void notifyAboutRegisterComponent(String tag) { @ReactMethod(isBlockingSynchronousMethod = true) public boolean isNull() { + return isNull(hyperServicesReference.get()); + } + + @ReactMethod(isBlockingSynchronousMethod = true) + public boolean isNullWithKey(String key) { + if (key == null) return true; + + if (hyperServicesMap.get(key) == null) return true; + + return isNull(hyperServicesMap.get(key)); + } + + private boolean isNull(HyperServices hyperServices) { return hyperServices == null; } @ReactMethod public void isInitialised(Promise promise) { + isInitialised(hyperServicesReference.get(), promise); + } + + @ReactMethod + public void isInitialisedWithKey(String key, Promise promise) { + if (key == null) { + promise.resolve(false); + return; + } + + if (hyperServicesMap.get(key) != null) { + HyperServices hyperServices = hyperServicesMap.get(key); + isInitialised(hyperServices, promise); + } else { + Log.d("hyperServiceRef is null", hyperServicesMap.get(key).toString()); + promise.resolve(false); + } + } + + public void isInitialised(HyperServices hyperService, Promise promise) { boolean isInitialized = false; synchronized (lock) { - if (hyperServices != null) { + if (hyperService != null) { try { - isInitialized = hyperServices.isInitialised(); + isInitialized = hyperService.isInitialised(); } catch (Exception e) { SdkTracker.trackAndLogBootException( NAME, diff --git a/example/src/HomeScreen.tsx b/example/src/HomeScreen.tsx index 4ed5808..45298ab 100644 --- a/example/src/HomeScreen.tsx +++ b/example/src/HomeScreen.tsx @@ -19,19 +19,24 @@ import { Dimensions, BackHandler, ScrollView, + Alert, } from 'react-native'; import HyperAPIUtils from './API'; import HyperSdkReact from 'hyper-sdk-react'; +import { HyperServiceInstance } from 'hyper-sdk-react'; import HyperUtils from './Utils'; import merchantConfig from './merchant_config.json'; import customerConfig from './customer_config.json'; import { Picker } from '@react-native-picker/picker'; import { useNavigation } from '@react-navigation/native'; +// import HyperServiceInstance from 'src/HyperServiceManager'; class HomeScreen extends React.Component { state = { animation: new Animated.Value(0), pickerSelected: 'pp', + hyperInstances: new Map(), + currentlySelectedInstance: '', }; navigation: any; @@ -202,6 +207,96 @@ class HomeScreen extends React.Component { }} /> + { + if (this.tenantId !== '') { + let hyperserviceInstance = new HyperServiceInstance( + this.tenantId, + this.clientId + ); + let hypInsMap = this.state.hyperInstances; + hypInsMap.set( + hyperserviceInstance.getHyperEventString(), + hyperserviceInstance + ); + this.setState({ + hyperInstances: hypInsMap, + }); + const eventEmitter = new NativeEventEmitter( + NativeModules.HyperSdkReact + ); + eventEmitter.addListener( + hyperserviceInstance.getHyperEventString(), + (resp) => { + HyperUtils.alertCallbackResponse( + 'Custom HomeScreen', + resp + ); + } + ); + // Alert.alert("hyperInstances", JSON.stringify(Object.fromEntries(hypInsMap))) + } else { + let hyperserviceInstance = new HyperServiceInstance( + undefined, + undefined + ); + let hypInsMap = this.state.hyperInstances; + hypInsMap.set( + hyperserviceInstance.getHyperEventString(), + hyperserviceInstance + ); + this.setState({ + hyperInstances: hypInsMap, + }); + const eventEmitter = new NativeEventEmitter( + NativeModules.HyperSdkReact + ); + eventEmitter.addListener( + hyperserviceInstance.getHyperEventString(), + (resp) => { + HyperUtils.alertCallbackResponse( + 'Custom HomeScreen', + resp + ); + } + ); + //Alert.alert("hyperInstances", JSON.stringify(Object.fromEntries(hypInsMap))) + } + }} + /> + + { + Alert.alert( + 'hyperInstances', + JSON.stringify(Object.fromEntries(this.state.hyperInstances)) + ); + }} + /> + + + Select HyperService Instance: + { + this.setState({ currentlySelectedInstance: val }); + console.log(val, index); + }} + > + {this.state.hyperInstances && + Array.from(this.state.hyperInstances.entries()).map( + (val, key) => { + return ( + + ); + } + )} + + + {this.state.pickerSelected === 'pp' ? ( + {this.state.currentlySelectedInstance != '' && ( + { + let hypInstance = this.state.hyperInstances.get( + this.state.currentlySelectedInstance + )!; + this.initiatePayload = + this.state.pickerSelected === 'ec' + ? HyperUtils.generateECInitiatePayload( + this.merchantId, + this.clientId, + this.customerId + ) + : HyperUtils.generatePPInitiatePayload( + this.clientId, + this.merchantId, + JSON.stringify(this.signaturePayload), + this.signature, + this.merchantKeyId + ); + hypInstance.initiate(JSON.stringify(this.initiatePayload)); + }} + /> + )} + {this.state.currentlySelectedInstance != '' && ( + { + this.navigation.navigate('ProcessScreen', { + merchantId: this.merchantId, + clientId: this.clientId, + customerId: this.customerId, + mobile: this.mobile, + email: this.email, + amount: this.amount, + apiKey: this.apiKey, + merchantKeyId: this.merchantKeyId, + privateKey: this.privateKey, + service: this.state.pickerSelected, + instance: this.state.hyperInstances.get( + this.state.currentlySelectedInstance + )!, + }); + }} + /> + )} { @@ -276,12 +418,37 @@ class HomeScreen extends React.Component { }); }} /> + {this.state.currentlySelectedInstance != '' && ( + { + let hypInstance = this.state.hyperInstances.get( + this.state.currentlySelectedInstance + )!; + hypInstance.isInitialised().then((init: boolean) => { + // console.warn('isInitialised:', init); + HyperUtils.showCopyAlert('isInitialised', init + ''); + }); + }} + /> + )} { HyperSdkReact.terminate(); }} /> + {this.state.currentlySelectedInstance != '' && ( + { + let hypInstance = this.state.hyperInstances.get( + this.state.currentlySelectedInstance + )!; + hypInstance.terminate(); + }} + /> + )} { - // HyperUtils.alertCallbackResponse('ProcessScreen', resp); - this.setState({ resultText: resp }); - } - ); + let eventGroup = HyperSdkReact.HyperEvent; + if (this.hyperService) { + eventGroup = this.hyperService.getHyperEventString(); + } + this.eventListener = eventEmitter.addListener(eventGroup, (resp) => { + // HyperUtils.alertCallbackResponse('ProcessScreen', resp); + this.setState({ resultText: resp }); + }); BackHandler.addEventListener('hardwareBackPress', () => { if (this.isPopupVisible) { @@ -836,8 +843,11 @@ class ProcessScreen extends React.Component { this.signature, this.merchantKeyId ); - - HyperSdkReact.process(JSON.stringify(payload)); + if (this.hyperService) { + this.hyperService.process(JSON.stringify(payload)); + } else { + HyperSdkReact.process(JSON.stringify(payload)); + } }} /> @@ -889,7 +906,11 @@ class ProcessScreen extends React.Component { { - HyperSdkReact.terminate(); + if (this.hyperService) { + this.hyperService.terminate(); + } else { + HyperSdkReact.terminate(); + } }} /> {/* @property HyperServices *hyperInstance; @property id delegate; +@property (nonatomic, strong) NSMutableDictionary *hyperServicesDict; @end diff --git a/ios/HyperSdkReact.mm b/ios/HyperSdkReact.mm index 0617394..982b57e 100644 --- a/ios/HyperSdkReact.mm +++ b/ios/HyperSdkReact.mm @@ -154,6 +154,14 @@ @implementation HyperSdkReact NSString *JUSPAY_HEADER_ATTACHED = @"JuspayHeaderAttached"; NSString *JUSPAY_FOOTER_ATTACHED = @"JuspayFooterAttached"; +- (instancetype)init { + if (self = [super init]) { + _hyperServicesDict = [NSMutableDictionary new]; + _keyCounter = 0; + } + return self; +} + - (dispatch_queue_t)methodQueue{ return dispatch_get_main_queue(); } @@ -208,6 +216,11 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(createHyperServicesWithKey:(NSString *) key) { + HyperServices *hyperServices = [HyperServices new]; + [self.hyperServicesDict setObject:hyperServices forKey:key]; +} + RCT_EXPORT_METHOD(createHyperServicesWithTenantId:(NSString *)tenantId clientId:(NSString *)clientId) { if (self.hyperInstance == NULL) { self.hyperInstance = [[HyperServices new] initWithTenantId:tenantId clientId:clientId]; @@ -215,6 +228,11 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(createHyperServicesWithTenantIdWithKey:(NSString *)tenantId clientId:(NSString *)clientId key:(NSString *)key) { + HyperServices *hyperServices = [HyperServices new] initWithTenantId:tenantId clientId:clientId]; + [self.hyperServicesDict setObject:hyperServices forKey:key]; +} + RCT_EXPORT_METHOD(initiate:(NSString *)data) { if (data && data.length>0) { @try { @@ -242,6 +260,34 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(initiateWithKey:(NSString *)data key:(NSString *)key) { + if (data && data.length>0) { + @try { + HyperServices *hyperServices = self.hyperServicesDict[key]; + NSDictionary *jsonData = [HyperSdkReact stringToDictionary:data]; + if (jsonData && [jsonData isKindOfClass:[NSDictionary class]] && jsonData.allKeys.count>0) { + + UIViewController *baseViewController = RCTPresentedViewController(); + __weak HyperSdkReact *weakSelf = self; + self.delegate = [[SdkDelegate alloc] initWithBridge:self.bridge]; + [hyperServices setHyperDelegate: _delegate]; + [hyperServices initiate:baseViewController payload:jsonData callback:^(NSDictionary * _Nullable data) { + [weakSelf sendEventWithName:key body:[[self class] dictionaryToString:data]]; + }]; + } else { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } @catch (NSException *exception) { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } else { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } +} + RCT_EXPORT_METHOD(process:(NSString *)data) { if (data && data.length>0) { @try { @@ -274,6 +320,39 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(processWithKey:(NSString *)data key:(NSString *)key) { + if (data && data.length>0) { + @try { + HyperServices *hyperServices = self.hyperServicesDict[key]; + NSDictionary *jsonData = [HyperSdkReact stringToDictionary:data]; + // Update baseViewController if it's nil or not in the view hierarchy. + if (hyperServices.baseViewController == nil || hyperServices.baseViewController.view.window == nil) { + // Getting topViewController + id baseViewController = RCTPresentedViewController(); + + // Set the presenting ViewController as baseViewController if the topViewController is RCTModalHostViewController. + if ([baseViewController isMemberOfClass:RCTModalHostViewController.class] && [baseViewController presentingViewController]) { + [hyperServices setBaseViewController:[baseViewController presentingViewController]]; + } else { + [hyperServices setBaseViewController:baseViewController]; + } + } + if (jsonData && [jsonData isKindOfClass:[NSDictionary class]] && jsonData.allKeys.count>0) { + [hyperServices process:jsonData]; + } else { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } @catch (NSException *exception) { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } else { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } +} + RCT_EXPORT_METHOD(openPaymentPage:(NSString *)data) { if (data && data.length>0) { @try { @@ -303,10 +382,52 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(openPaymentPageWithKey:(NSString *)data key:(NSString *)key) { + if (data && data.length>0) { + @try { + HyperServices *hyperServices = self.hyperServicesDict[key]; + NSDictionary *sdkPayload = [HyperSdkReact stringToDictionary:data]; + // Update baseViewController if it's nil or not in the view hierarchy. + if (sdkPayload && [sdkPayload isKindOfClass:[NSDictionary class]] && sdkPayload.allKeys.count>0) { + + id baseViewController = RCTPresentedViewController(); + + __weak HyperSdkReact *weakSelf = self; + self.delegate = [[SdkDelegate alloc] initWithBridge:self.bridge]; + [hyperServices setHyperDelegate: _delegate]; + [HyperCheckoutLite openPaymentPage:baseViewController payload:sdkPayload callback:^(NSDictionary * _Nullable data) { + [weakSelf sendEventWithName:key body:[[self class] dictionaryToString:data]]; + }]; + } else { +// Define proper error code and return proper error +// [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } @catch (NSException *exception) { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } + } else { + // Define proper error code and return proper error + // [self sendEventWithName:@"HyperEvent" body:[[self class] dictionaryToString:data]]; + } +} + RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isNull) { return self.hyperInstance == NULL? @true : @false; } +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isNullWithKey:(NSString *)key) { + HyperServices *hyperServices = self.hyperServicesDict[key]; + return hyperServices == NULL? @true : @false; +} + +RCT_EXPORT_METHOD(terminateWithKey:(NSString *)key) { + HyperServices *hyperServices = self.hyperServicesDict[key]; + if (hyperServices) { + [hyperServices terminate]; + } +} + RCT_EXPORT_METHOD(terminate) { if (_hyperInstance) { [_hyperInstance terminate]; @@ -325,6 +446,15 @@ -(void)stopObserving { } } +RCT_EXPORT_METHOD(isInitialisedWithKey:(NSString *)key resolve(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { + HyperServices *hyperServices = self.hyperServicesDict[key]; + if (hyperServices) { + resolve(hyperServices.isInitialised? @true : @false); + } else { + resolve(@false); + } +} + RCT_EXPORT_METHOD(updateBaseViewController) { if (self.hyperInstance && [self.hyperInstance isInitialised]) { self.hyperInstance.baseViewController = RCTPresentedViewController(); diff --git a/package.json b/package.json index 991d9ef..1871698 100644 --- a/package.json +++ b/package.json @@ -161,5 +161,8 @@ } ] ] + }, + "dependencies": { + "react-native-uuid": "^2.0.3" } } diff --git a/src/HyperServiceManager.tsx b/src/HyperServiceManager.tsx new file mode 100644 index 0000000..bcbcc5c --- /dev/null +++ b/src/HyperServiceManager.tsx @@ -0,0 +1,73 @@ +import uuid from 'react-native-uuid'; + +import { NativeModules, Platform } from 'react-native'; + +const HyperSdkReactModule = NativeModules.HyperSdkReact; + +if (!HyperSdkReactModule) { + throw new Error('HyperSdkReactModule is not linked.'); +} + +export default class HyperServiceInstance { + key: string; + + // constructor() { + // this.key = HyperSdkReactModule.createNewHyperServices("tenantIdPlaceholder", "clientIdPlaceholder"); + // } + constructor(tenantId?: string, clientId?: string) { + if (!HyperSdkReactModule) { + throw new Error('HyperSdkReactModule is not linked.'); + } + this.key = uuid.v4(); + if (tenantId || clientId) { + if (tenantId == undefined) tenantId = ''; + if (clientId == undefined) clientId = ''; + HyperSdkReactModule.createHyperServicesWithKey( + this.key, + tenantId, + clientId + ); + } else { + HyperSdkReactModule.createHyperServicesWithKey(this.key, '', ''); + } + } + + initiate(data: string) { + return HyperSdkReactModule.initiateWithKey(data, this.key); + } + + process(data: string) { + return HyperSdkReactModule.processWithKey(data, this.key); + } + + terminate() { + return HyperSdkReactModule.terminateWithKey(this.key); + } + + processWithActivity(data: string) { + if (Platform.OS === 'ios') { + HyperSdkReactModule.processWithKey(data, this.key); + } + return HyperSdkReactModule.processWithActivityWithKey(data, this.key); + } + + openPaymentPage(data: string) { + return HyperSdkReactModule.openPaymentPageWithKey(data, this.key); + } + + isInitialised(): Promise { + return HyperSdkReactModule.isInitialisedWithKey(this.key); + } + + isNull(): boolean { + return HyperSdkReactModule.isNullWithKey(this.key); + } + + onBackPressed(): boolean { + return HyperSdkReactModule.onBackPressedWithKey(this.key); + } + + getHyperEventString(): string { + return this.key; + } +} diff --git a/src/index.tsx b/src/index.tsx index 9928229..9ddb66c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,8 +4,8 @@ * This source code is licensed under the AGPL 3.0 license found in the * LICENSE file in the root directory of this source tree. */ - import { NativeModules, Platform } from 'react-native'; +import HyperServiceInstance from './HyperServiceManager'; const LINKING_ERROR = `The package 'hyper-sdk-react' doesn't seem to be linked. Make sure: \n\n` + @@ -62,3 +62,4 @@ type HyperSdkReactType = { }; export default HyperSdkReact as HyperSdkReactType; +export { HyperServiceInstance };