Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 175 additions & 17 deletions android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,18 +28,22 @@
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;

import org.json.JSONException;
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;
Expand Down Expand Up @@ -74,6 +79,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A
@Nullable
private HyperServices hyperServices;

private final Map<String, HyperServices> hyperServicesMap = new HashMap<>();

private static WeakReference<HyperServices> hyperServicesReference = new WeakReference<>(null);

private final ReactApplicationContext context;
Expand Down Expand Up @@ -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
Expand All @@ -225,19 +283,44 @@ 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();
}
}

@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);
Expand Down Expand Up @@ -277,7 +360,7 @@ public void onEvent(JSONObject data, JuspayResponseHandler handler) {
wasProcessWithActivity = false;
processActivityRef = new WeakReference<>(null);
}
sendEventToJS(data);
sendEventToJS(key, data);
}

@Nullable
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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);
}
}
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
});
}
Expand All @@ -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();
Expand All @@ -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,
Expand Down
Loading
Loading