SDK Response Handling

public void onEvent(JSONObject data, JuspayResponsehandler handler) {
    String event = data.optString("event");
    switch (event) {
        case "initiate_result":
            // handle initiate response -- log it
            // do what you want here
            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" -> {
          // handle initiate response -- log it
               // do what you want here
        }
        "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
​
            }
     }
​
});

What’s Next