SDK Response Handling

Integration Sample

public void onEvent(JSONObject data, JuspayResponsehandler handler) {
    String event = data.optString("event");
    switch (event) {
        case "initiate_result":
            boolean error = data.optBoolean("error");
      
            JSONObject innerPayload = data.optJSONObject("payload");
            String status = innerPayload.optString("status");
      
            if (!error) {
                // handle initiate response -- log it
                // do what you want here 
            } else {
                String errorCode = data.optString("errorCode");   
                String errorMessage = data.optString("errorMessage");
               // handle error
               // errorMessage can also be shown in UI
            }
            break;

        case "hide_loader":
            // stop the processing loader 
            break;
    
        case "log_stream":
          // Consume the click events
          break;
      
        case "process_result":
            boolean error = data.optBoolean("error");

            JSONObject innerPayload = data.optJSONObject("payload");
            String status = innerPayload.optString("status");
            String pi = innerPayload.optString("paymentInstrument");
            String pig = innerPayload.optString("paymentInstrumentGroup"); 

            if (!error) {
                // "charged", if order was successful
                // "cod_initiated", if user opted for cash on delivery option displayed on payment page
                         // process data -- show pi and pig in UI maybe also?
                         // example -- pi: "PAYTM", pig: "WALLET"
                         // call orderStatus once to verify (false positives)
            } else {
                String errorCode = data.optString("errorCode");
                String errorMessage = data.optString("errorMessage");
                switch (status) {
                    case "backpressed":
                        // user back-pressed from PP without
                                       // without initiating txn
                        break;
                    case "user_aborted":
                        // user initiated a txn and pressed back
                        // poll order status
                        break;
                    case "pending_vbv":
                    case "authorizing":
                        // txn in pending state
                        // poll order status until backend says fail or success
                        break;
                    case "authorization_failed":
                    case "authentication_failed":
                    case "api_failure":
                        // txn failed
                        // poll orderStatus to verify
                                       // (false negatives)
                        break;
                    case "new":
                        // order created but txn failed
                        // very rare
                        // also failure
                        // poll order status
                        break;
                    default:
                        // unknown status, this is also failure
                        // poll order status
                }
                // if txn was attempted, pi and pig would be non-empty
                // can be used to show in UI if you are into that
                // errorMessage can also be shown in UI
            }
    }
}
fun onEvent(data: JSONObject, handler: JuspayResponsehandler?) {
    val event: String = data.optString("event")
    when (event) {
        "initiate_result" -> {
            val error: Boolean = data.optBoolean("error")
            val innerPayload: JSONObject = data.optJSONObject("payload")
            val status: String = innerPayload.optString("status")
                
            if (!error) {
                // handle initiate response -- log it
                // do what you want here 
            } else {
                val errorCode: String = data.optString("errorCode")
                val errorMessage: String = data.optString("errorMessage")  
                // handle error
                // errorMessage can also be shown in UI
            }
        }
        "hide_loader" -> {
        // stop the processing loader 
        }
        "process_result" -> {
            val error: Boolean = data.optBoolean("error")
            val innerPayload: JSONObject = data.optJSONObject("payload")
            val status: String = innerPayload.optString("status")
            val pi: String = innerPayload.optString("paymentInstrument")
            val pig: String = innerPayload.optString("paymentInstrumentGroup")
            if (!error) {
                // txn success, status should be "charged"
                // process data -- show pi and pig in UI maybe also?
                // example -- pi: "PAYTM", pig: "WALLET"
                // call orderStatus once to verify (false positives)
            } else {
                val errorCode: String = data.optString("errorCode")
                val errorMessage: String = data.optString("errorMessage")
                when (status) {
                    "backpressed" -> {
                      // user back-pressed from PP without initiating txn
                    }
                    "user_aborted" -> {
                      // user initiated a txn and pressed back
                                 // poll order status
                    }
                    "pending_vbv", "authorizing" -> {
                      // txn in pending state
                              // poll order status until backend says fail or success
                    }
                    "authorization_failed", "authentication_failed", "api_failure" -> {
                      // txn failed
                        // poll orderStatus to verify
                                  
                    }
                    "new" -> {
                      // order created but txn failed
                                 // very rare
                                // also failure
                               // poll order status
                    }
                    else -> {
                      // unknown status, this is also failure
                        // poll order status
                    }
                }
                // if txn was attempted, pi and pig would be non-empty
                // can be used to show in UI if you are into that
                // errorMessage can also be shown in UI
            }
        }
    }
}
self.hyper.initiate(self, payload: initiatePayload, callback: { [weak self] (response) in

    if let data = response, let event = data["event"] as? String {
        if (event == "hide_loader") {
            // hide loader
        } else if (event == "initiate_result") {
            // handle initiate response
        } else if (event == "process_result") {
            let error = data["error"] as! Bool
            
            let innerPayload = data["payload"] as! [ String: Any ]
            let status = innerPayload["status"] as! String
            let pi = innerPayload["paymentInstrument"] as? String
            let pig = innerPayload["paymentInstrumentGroup"] as? String
            
            if (!error) {
                // txn success, status should be "charged"
                // process data -- show pi and pig in UI maybe also?
                // example -- pi: "PAYTM", pig: "WALLET"
                // call orderStatus once to verify (false positives)
            } else {
                
                let errorCode = data["errorCode"] as! String
                let errorMessage = data["errorMessage"] as! String
                switch (status) {
                    case "backpressed":
                        // user back-pressed from PP without initiating any txn
                        break;
                    case "user_aborted":
                        // user initiated a txn and pressed back
                        // poll order status
                        break;
                    case "pending_vbv", "authorizing":
                        // txn in pending state
                        // poll order status until backend says fail or success
                        break;
                    case "authorization_failed", "authentication_failed", "api_failure":
                        // txn failed
                        // poll orderStatus to verify (false negatives)
                        break;
                    case "new":
                        // order created but txn failed
                        // very rare for V2 (signature based)
                        // also failure
                        // poll order status
                        break;
                    default:
                        // unknown status, this is also failure
                        // poll order status
                        break;
                }
                // if txn was attempted, pi and pig would be non-empty
                // can be used to show in UI if you are into that
                // errorMessage can also be shown in UI
            }
        }
    }
})
[self.hyperInstance initiate:self payload:initiatePayload callback:^(NSDictionary<NSString *,id> * _Nullable response) {
        
    NSString *event = response[@"event"];
    
    if ([event isEqualToString:@"hide_loader"]) {
        // hide loader
    } else if ([event isEqualToString:@"initiate_result"]) {
        // handle initiate response
    } else if ([event isEqualToString:@"process_result"]) {
        
        BOOL error = response[@"error"];
        
        NSDictionary *innerPayload = response[@"payload"];
        NSString *status = innerPayload[@"status"];
        NSString *pi = innerPayload[@"paymentInstrument"];
        NSString *pig = innerPayload[@"paymentInstrumentGroup"];
        
        if (!error) {
            // txn success, status should be "charged"
            // process data -- show pi and pig in UI maybe also?
            // example -- pi: "PAYTM", pig: "WALLET"
            // call orderStatus once to verify (false positives)
        } else {
            
            NSString *errorCode = response[@"errorCode"];
            NSString *errorMessage = response[@"errorMessage"];
            
            if ([status isEqualToString:@"backpressed"]) {
                // user back-pressed from PP without initiating any txn
            } else if ([status isEqualToString:@"user_aborted"]) {
                // user initiated a txn and pressed back
                // poll order status
            } else if ([status isEqualToString:@"pending_vbv"] || [status isEqualToString:@"authorizing"]) {
                // txn in pending state
                // poll order status until backend says fail or success
            } else if ([status isEqualToString:@"authorization_failed"] || [status isEqualToString:@"authentication_failed"] || [status isEqualToString:@"api_failure"]) {
                // txn failed
                // poll orderStatus to verify (false negatives)
            } else if ([status isEqualToString:@"new"]) {
                // order created but txn failed
                // very rare for V2 (signature based)
                // also failure
                // poll order status
            } else {
                // unknown status, this is also failure
                // poll order status
            }
            
            // if txn was attempted, pi and pig would be non-empty
            // can be used to show in UI if you are into that
            // errorMessage can also be shown in UI
        }
    }
}];
const eventEmitter = new NativeEventEmitter(NativeModules.HyperSdkReact);
this.eventListener = eventEmitter.addListener('HyperEvent', (resp) => {
    const data = JSON.parse(resp);
    const event = data.event || '';
​
    switch (event) {
        case 'initiate_result':
            // handle initiate response -- log it maybe?
            // do what you want here
            break;
​
        case 'hide_loader':
            // stop the processing loader
            break;
​
        case 'process_result':
            const error = data.error || false;
​
            const innerPayload = data.payload || {};
            const status = innerPayload.status || '';
            const pi = innerPayload.paymentInstrument || '';
            const pig = innerPayload.paymentInstrumentGroup || '';
​
            if (!error) {
                // txn success, status should be "charged"
                // process data -- show pi and pig in UI maybe also?
                // example -- pi: "PAYTM", pig: "WALLET"
                // call orderStatus once to verify (false positives)
​
            } else {
                const errorCode = data.errorCode || '';
                const errorMessage = data.errorMessage || '';
                switch (status) {
                    case 'backpressed':
                        // user back-pressed from PP without initiating any txn
                        break;
                    case 'user_aborted':
                        // user initiated a txn and pressed back
                        // poll order status
                        break;
                    case 'pending_vbv':
                    case 'authorizing':
                        // txn in pending state
                        // poll order status until backend says fail or success
                        break;
                    case 'authorization_failed':
                    case 'authentication_failed':
                    case 'api_failure':
                        // txn failed
                        // poll orderStatus to verify (false negatives)
                        break;
                    case 'new':
                        // order created but txn failed
                        // very rare for V2 (signature based)
                        // also failure
                        // poll order status
                        break;
                    default:
                        // unknown status, this is also failure
                        // poll order status
                }
                // if txn was attempted, pi and pig would be non-empty
                // can be used to show in UI if you are into that
                // errorMessage can also be shown in UI
​
            }
     }
​
});

Handling Clickstream Events(case "log_stream":)

This is very similar to the handling of "status" in the case for "process_result" in the above code. The handling depends on the "label" parameter, which indicates the type of click operation. (see Clickstream Events)

case "log_stream":
      boolean error = data.optBoolean("error");

      JSONObject clickPayload = data.optJSONObject("payload");
      String category = clickPayload.optString("category");
      String subcategory = clickPayload.optString("subcategory");
      String label = clickPayload.optString("label");
      JSONObject value = clickPayload.optJSONObject("value");
    
      if (!error) {
      // handle clickstream event
      switch (label) {
          case "current_screen":
              String URL  = value.optString("url")
              String screenName  = value.optString("screen_name")
              String presentationType  = value.optString("presentation_type")
              String ID  = value.optString("id")
              // provides information on the screen currently being displayed
              break;
          case "button_clicked":  
              String buttonName  = value.optString("button_name")  
              // provides the name of the button clicked 
                 switch (buttonName) {
                    case "proceed_to_pay":
                    // handles the "proceed_to_pay"action
                    break;
                    case "back_button_pressed":
                    // handles "back_button_pressed" action
                    break;}
              break;
          case "checkbox_clicked":
              JSONObject checkboxDetails  = value.opt JSONObject ("checkbox_details")
              String checkboxName = checkboxDetails.optString("checkbox_name")
              String  checkboxState= checkboxDetails.optString("state")
              // provides the name of the checkbox along with the state of the checkbox
              break;
          case "payment_instrument_group": 
              String pigName  = value.optString("pig_name")
              // handle payment_instrument  and payment_instrument_group selection and possible transaction
              break;
          case "stored_card_selected":
              JSONObject cardDetails = value.opt JSONObject ("card_details")
              String cardType = cardDetails.optString("card_type")
              String cardBrand = cardDetails.optString("card_brand")
              String bankName = cardDetails.optString("bank_name")
              // handle stored card selection and possible transaction
              break;
          case "card_selected":
              JSONObject cardDetails = value.opt JSONObject ("card_details")
              String cardType = cardDetails.optString("card_type")
              String cardBrand = cardDetails.optString("card_brand")
              String bankName = cardDetails.optString("bank_name")
              // handle add card flow and possible transaction
              break;
          case "bank_selected":   
              String bankName = value.optString("bank_name")
              // provide bank name for "nb" pig and handle possible transaction
              break;
          case "wallet_selected": 
              String unlinkedWallet = value.optString("unlinked_wallet")
              // provide wallet name for "wallet" pig and handle possible transaction
              break;
          case "upi_apps":
              JSONObject upiAppSelected = value.opt JSONObject ("upi_app_selected")
              String packageName = upiAppSelected.optString("packageName")
              String appName = upiAppSelected.optString("appName")
              // This provides the UPI app name for upi pig and handles possible transaction
              break;
          case "expiry_date_changed":   
              String AddCardScreen = value.optString("AddCardScreen")
              // provides info when user changes the expiry value, 
              // and screen on which it was changed
              break;
          case "cvv_changed":      
              String AddCardScreen = value.optString("AddCardScreen")
              // provides info when user changes the CVV text, 
              // and screen on which it was changed
              break;
          case "card_number_changed":   
              String AddCardScreen = value.optString("AddCardScreen")
              // provides info when user changes the card number, 
              // and screen on which it was changed
              break;
          case "upi_id_changed":  
              String VpaScreen= value.optString("VpaScreen")
              // provides info when user changes the UPI VPA in the collect flow, 
              // and screen on which it was changed
              break;
          default:    // unknown label
    }
    } else {
          String errorCode = data.optString("errorCode");
          String errorMessage = data.optString("errorMessage");
         //handle error
         // errorMessage can be shown in UI
      }
      break;

Did this page help you?