---
page_title: Code Snippets
product: Resources - Global
page_source: https://juspay.io/sea/docs/resources-global/docs/common-resources/code-snippets
llms_txt: https://juspay.io/sea/docs/llms.txt
product_llms_txt: https://juspay.io/sea/docs/resources-global/llms.txt
---


# Code Snippets



All HyperSDK and related code snippets.


---



### **isInitialised** 



Checks if current HyperSDK has been initialised. It should be used to check the HyperSDK instance state before calling process API.


#### Java Code Snippet:

```java
/**
Checks if current sdk instance has been initialised.
*/
public boolean isInitialised;


//Example usage
if (hyperServices.isInitialised()) {
    hyperServices.process(processPayload);
} else {
  //Intialise hyperInstance
}
```

#### Swift Code Snippet:

```swift
/**
Checks if current sdk instance has been initialised.
*/
- (Boolean)isInitialised;

//Example usage
if hyperInstance.isInitialised {
    hyperInstance.process(processPayload);
} else {
  //Intialize hyperInstance
}
```

#### Objective - C Code Snippet:

```objective - c
/**
Checks if current sdk instance has been initialised..
*/
- (Boolean)isInitialised;

//Example usage
if ([hyperInstance isInitialised]) {
    [hyperInstance process:processPayload];
} else {
  //Intialize hyperInstance
}
```



### **UUID** 



Sample snippets to generate uuid-v4 string. Used in HyperSDK as [requestId]


#### Java Code Snippet:

```java

# Please scroll down for a sample snippets to generate uuid-v4 string. Used in HyperSDK as [requestId]
UUID.randomUUID().toString()
```

#### Swift Code Snippet:

```swift

# Please scroll down for a sample snippets to generate uuid-v4 string. Used in HyperSDK as [requestId]
NSUUID().uuidString
```

#### Objective - C Code Snippet:

```objective - c

# Please scroll down for a sample snippets to generate uuid-v4 string. Used in HyperSDK as [requestId]
NSUUID.UUID.UUIDString;
```



### **Backpress control** 



Since HyperSDK is a fragment, we will need the back-press event from the current activity to trigger events required for hardware back press.  

**hyperServices.onBackPressed()**  should be called during activity’s **onBackPressed()** . If the result is true, HyperSDK will consume the hardware back press.


#### Java Code Snippet:

```java
//block:start:onBackPressed
@Override
public void onBackPressed() {
    boolean backPressHandled = hyperServices.onBackPressed();
    if(!backPressHandled) {
        super.onBackPressed();
    }
}
//block:end:onBackPressed
```


In case the merchant wants to control backpress, they can call hyperServices.onBackPressed only when the UI is shown and Juspay can handle backpress. They can ignore the boolean response.

> **Warning**
> If the passed activity is terminated in back press (super.onBackPressed or activity.finish), in this case hyper.onBackPressed returns true, which will cause HyperSDK ui to be removed.




### **Handling Permission Results** 



Juspay SDK uses permissions in various flows (Example OTP Auto Read), we will need the merchant to delegate the control to the android lifecycle event **onRequestPermissionsResult()**  so that we can correctly process the result given by that corresponding permission request. To do that, the following code snippet can be used:


#### Java Code Snippet:

```java
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
	super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 	// Merchant code
}
```


> **Note**
> The above snippet can be ignored if the merchant is not overriding the onRequestPermissionsResult method provided by android.



If **super.onRequestPermissionsResult**  is not accessible to the merchant, they can implement it using a similar method provided by HyperServices:


#### Java Code Snippet:

```java
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
	hyperServices.onRequestPermissionsResult(requestCode, permissions, grantResults);
 	// Merchant code
}
```


> **Warning**
> Merchants using ReactActivity must call the above code snippet, for the SDK to function as expected.




### **Launch Activity for React Native Merchants** 



ReactActivity does not forward onActivityResult to AppCompatActivity. This causes onActivityResult's requestCode to be modified, when returned to the SDK. To avoid this, we need to intercept the request before being sent to the AppCompatActivity and make the same via react context.

This can be achieved by implementing the ActivityLaunchDelegate. Sample implementation is below


#### Java Code Snippet:

```java
import com.facebook.react.bridge.ReactApplicationContext;

import in.juspay.hypersdk.ui.ActivityLaunchDelegate;

public final class ReactLaunchDelegate implements ActivityLaunchDelegate {

    private final ReactApplicationContext context;

    public ReactLaunchDelegate(ReactApplicationContext context) {
        this.context = context;
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle bundle) {
        context.startActivityForResult(intent, requestCode, bundle);
    }
}
```


Post this we will need to set the ActivityLaunchDelegate on the hyperServices instance.


#### Java Code Snippet:

```java
hyperServices.setActivityLaunchDelegate(new ReactLaunchDelegate(getReactApplicationContext()));
```



### **Request Permission for React Native Merchants** 



In certain versions of React Native, onRequestPermissionResult crashes before forwarding the result to AppCompatActivity. This can be bypassed by overriding requestPermission

This can be achieved by implementing the RequestPermissionDelegate. Sample implementation is below


#### Java Code Snippet:

```java
import android.app.Activity;
import android.os.Build;

import androidx.annotation.NonNull;
import java.lang.ref.WeakReference;

import in.juspay.hypersdk.ui.RequestPermissionDelegate;

public class ReactRequestDelegate implements RequestPermissionDelegate {

    @NonNull private final WeakReference<Activity> activity;

    public ReactRequestDelegate(Activity activity) {
        this.activity = new WeakReference<>(activity);
    }

    @Override
    public void requestPermission(String[] permissions, int requestCode) {
        Activity activity = this.activity.get();
        if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(permissions, requestCode);
        }
    }
}
```


Post this we will need to set the ReactRequestDelegate on the hyperServices instance.


#### Java Code Snippet:

```java
hyperServices.setRequestPermissionDelegate(new ReactRequestDelegate(activity));
```


---

## See Also

- [Test Resources](https://juspay.io/sea/docs/resources-global/docs/common-resources/test-resources)
- [Support & Contact](https://juspay.io/sea/docs/resources-global/docs/common-resources/support--contact)
