---
page_source: https://juspay.io/in/docs/hyper-checkout/android/base-sdk-integration/initiating-sdk
page_title: 2. Initialize SDK
---


# 2. Initiating the SDK



To initialise the SDK, client needs to call the `initiate` SDK function. The initiate function call boots up the Hypercheckout SDK and makes it ready for all other operations

Follow the below steps to make an initiate SDK call

> **Error**
> Initiate is to be called in the onCreate() method of your application's main activity (Home screen). Not following this will lead to increased Hypercheckout latency.




### Step 2.1. Create an instance of HyperServiceHolder


The Hypercheckout SDK exposes the `HyperServiceHolder` class. Create an object of this class for all upcoming operations



#### Code Snippets: -

#### Java Code Snippet:

```java
hyperServicesHolder = new HyperServiceHolder(this);
```

#### Kotlin Code Snippet:

```kotlin
hyperServicesHolder = HyperServiceHolder(this)
```



### Step 2.2. Create Initiate payload


Initiate takes two parameters as input. One of the parameter is a JSON object referred as `InitiatePayload`. This payload contains certain key value pairs used by the SDK to perform a successful initiate

Refer to the following table for information about the description and sample payload.


### Payload
- **RequestId**:
  - Description: Unique uuid-v4 string
  - Value: $requestId
  - Tags: String, Mandatory
- **Service**:
  - Description: Value: in.juspay.hyperpay
  - Tags: String, Mandatory
- **Payload**:
  - Description: Parameters
  - Value:
    - **Action**:
      - Description: Value: initiate
      - Tags: String, Mandatory
    - **MerchantId**:
      - Description: Unique merchant id shared during onboarding
      - Tags: String, Mandatory
    - **ClientId**:
      - Description: Unique Client id shared during onboarding
      - Tags: String, Mandatory
    - **Environment**:
      - Description: Environment to be used for the session. Accepted value is “production” / “sandbox”
      - Tags: String, Mandatory
  - Tags: JSON, Mandatory




#### Code Snippets: -

#### Java Code Snippet:

```java
// This function creates intiate payload.
    private JSONObject createInitiatePayload() {
        JSONObject sdkPayload = new JSONObject();
        JSONObject innerPayload = new JSONObject();
        try {
            // generating inner payload
            innerPayload.put("action", "initiate");
            innerPayload.put("merchantId", "<MERCHANT_ID>");    // Put your Merchant ID here
            innerPayload.put("clientId", "<CLIENT_ID>");          // Put your Client ID here
            innerPayload.put("xRoutingId", "<X_ROUTING_ID>");      // Put your X-Routing ID here
            innerPayload.put("environment", "production");
            sdkPayload.put("requestId",  ""+ UUID.randomUUID());
            sdkPayload.put("service", "in.juspay.hyperpay");
            sdkPayload.put("payload", innerPayload);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdkPayload;
    }
```

#### Kotlin Code Snippet:

```kotlin
private fun createInitiatePayload(): JSONObject {
        val sdkPayload = JSONObject()
        val innerPayload = JSONObject()
        try {
            // generating inner payload
            innerPayload.put("action", "initiate")
            innerPayload.put("merchantId", "<MERCHANT_ID>")   //Your Merchant ID here
            innerPayload.put("clientId", "<CLIENT_ID>")       //Your Client ID here
            innerPayload.put("xRoutingId", "<X_ROUTING_ID>")    //Your X Routing ID here
            innerPayload.put("environment", "production")
            sdkPayload.put("requestId", "" + UUID.randomUUID())
            sdkPayload.put("service", "in.juspay.hyperpay")
            sdkPayload.put("payload", innerPayload)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return sdkPayload
    }
```



### Step 2.3. Create CallbackHandler


During its lifecycle, SDK emits multiple events to communicate about the transaction status. All of these events are received by an instance of `HyperPaymentsCallbackAdapter`.

This callback handler is passed as the second argument to the initiate API call.



#### Code Snippets: -

#### Java Code Snippet:

```java
private HyperPaymentsCallbackAdapter createHyperPaymentsCallbackAdapter() {
        return new HyperPaymentsCallbackAdapter() {
            @Override
            public void onEvent(JSONObject jsonObject, JuspayResponseHandler responseHandler) {
                Intent redirect = new Intent(ProductsActivity.this, ResponsePage.class);
                redirect.putExtra("responsePayload", jsonObject.toString());
                System.out.println("jsonObject>>> " +jsonObject);
                try {
                    String event = jsonObject.getString("event");
                    if (event.equals("hide_loader")) {
                        // Hide Loader
                        CheckoutActivity.dialog.hide();
                    }
                    // Handle Process Result
                    // This case will reach once the Hypercheckout screen closes
                    // block:start:handle-process-result
                    else if (event.equals("process_result")) {
                        boolean error = jsonObject.optBoolean("error");
                        JSONObject innerPayload = jsonObject.optJSONObject("payload");
                        String status = innerPayload.optString("status");

                        if (!error) {
                            switch (status) {
                                case "charged":
                                    // Successful Transaction
                                    // check order status via S2S API
                                    redirect.putExtra("status", "OrderSuccess");
                                    startActivity(redirect);
                                    break;
                                case "cod_initiated":
                                    redirect.putExtra("status", "CODInitiated");
                                    startActivity(redirect);
                                    break;
                            }
                        } else {
                            switch (status) {
                                case "backpressed":
                                    // user back-pressed from PP without initiating transaction

                                    break;
                                case "user_aborted":
                                    // user initiated a txn and pressed back
                                    // check order status via S2S API
                                    Intent successIntent = new Intent(ProductsActivity.this, ResponsePage.class);
                                    redirect.putExtra("status", "UserAborted");
                                    startActivity(redirect);
                                    break;
                                case "pending_vbv":
                                    redirect.putExtra("status", "PendingVBV");
                                    startActivity(redirect);
                                    break;
                                case "authorizing":
                                    // txn in pending state
                                    // check order status via S2S API
                                    redirect.putExtra("status", "Authorizing");
                                    startActivity(redirect);
                                    break;
                                case "authorization_failed":
                                    redirect.putExtra("status", "AuthorizationFailed");
                                    startActivity(redirect);
                                    break;
                                case "authentication_failed":
                                    redirect.putExtra("status", "AuthenticationFailed");
                                    startActivity(redirect);
                                    break;
                                case "api_failure":
                                    redirect.putExtra("status", "APIFailure");
                                    startActivity(redirect);
                                    break;
                                // txn failed
                                // check order status via S2S API
                                default:
                                    redirect.putExtra("status", "APIFailure");
                                    startActivity(redirect);
                                    break;
                            }
                        }
                    }
                    // block:end:handle-process-result
                } catch (Exception e) {
                    // merchant code...
                }
            }
        };
    }
```

#### Kotlin Code Snippet:

```kotlin
private fun createHyperPaymentsCallbackAdapter(): HyperPaymentsCallbackAdapter {
        return object : HyperPaymentsCallbackAdapter() {
            override fun onEvent(jsonObject: JSONObject, responseHandler: JuspayResponseHandler?) {
                val redirect = Intent(this@ProductsActivity, ResponsePage::class.java)
                redirect.putExtra("responsePayload", jsonObject.toString())
                try {
                    val event = jsonObject.getString("event")
                    if (event == "hide_loader") {
                        // Hide Loader
                        CheckoutActivity.dialog!!.hide()
                    } else if (event == "process_result") {
                        //block:start:handle-process-result
                        val error = jsonObject.optBoolean("error")
                        val innerPayload = jsonObject.optJSONObject("payload")
                        val status = innerPayload.optString("status")
                        if (!error) {
                            when (status) {
                                "charged" -> {
                                    // Successful Transaction
                                    // check order status via S2S API
                                    redirect.putExtra("status", "OrderSuccess")
                                    startActivity(redirect)
                                }
                                "cod_initiated" -> {
                                    redirect.putExtra("status", "CODInitiated")
                                    startActivity(redirect)
                                }
                            }
                        } else {
                            when (status) {
                                "backpressed" -> {
                                }
                                "user_aborted" -> {
                                    // user initiated a txn and pressed back
                                    // check order status via S2S API
                                    val successIntent = Intent(
                                        this@ProductsActivity,
                                        ResponsePage::class.java
                                    )
                                    redirect.putExtra("status", "UserAborted")
                                    startActivity(redirect)
                                }
                                "pending_vbv" -> {
                                    redirect.putExtra("status", "PendingVBV")
                                    startActivity(redirect)
                                }
                                "authorizing" -> {
                                    // txn in pending state
                                    // check order status via S2S API
                                    redirect.putExtra("status", "Authorizing")
                                    startActivity(redirect)
                                }
                                "authorization_failed" -> {
                                    redirect.putExtra("status", "AuthorizationFailed")
                                    startActivity(redirect)
                                }
                                "authentication_failed" -> {
                                    redirect.putExtra("status", "AuthenticationFailed")
                                    startActivity(redirect)
                                }
                                "api_failure" -> {
                                    redirect.putExtra("status", "APIFailure")
                                    startActivity(redirect)
                                }
                            }
                        }
                        // block:end:handle-process-result
                    }
                } catch (e: Exception) {
                    // merchant code...
                }
            }
        }
    }
```



### Step 2.4. Call initiate


The final step is to call the `Initiate`function.Invoke the method `setCallback` of `HyperServiceHolder` which takes instance of `HyperPaymentsCallbackAdapter` as a parameter.

The initiate method takes the `InitiatePayload`as a parameter. To create the parameter, you can use the createInitiatePayload function in the above steps.

> **Warning**
> Initiate is a fire-and-forget call. For every HyperServiceHolder instance, you should **call initiate only once.** 





#### Code Snippets: -

#### Java Code Snippet:

```java
//This function initiate the Juspay SDK
    private void initiatePaymentsSDK() {
        if(!hyperServicesHolder.isInitialised()){
            initiatePayload = createInitiatePayload();
            hyperServicesHolder.setCallback(createHyperPaymentsCallbackAdapter());
            hyperServicesHolder.initiate(initiatePayload);

            //Showing snackbar
            Helper helper = new Helper();
            CoordinatorLayout coordinatorLayout = findViewById(R.id.coordinatorLayout2);
            helper.showSnackbar("Initiate Called!", coordinatorLayout);
        }
    }
```

#### Kotlin Code Snippet:

```kotlin
private fun initiatePaymentsSDK() {
        if (!hyperServicesHolder!!.isInitialised) {
            initiatePayload = createInitiatePayload()
            hyperServicesHolder!!.setCallback(createHyperPaymentsCallbackAdapter())
            hyperServicesHolder!!.initiate(createInitiatePayload())
            showSnackbar("Initiate Called!")
        }
    }
```
