Why some functions not working when navigate to the payment gateway?

I am developing website to navigate a sample Mastercard payment gateway and I want detect order id after the payment. In my code I set functions (callbacks) beforeRedirect , afterRedirect and completeCallback (Javascript functions) mentioned in documentation. I add js alerts for all functions to check they are working. But afterRedirect function is not working (other call backs are working) .The code seems to be correct. I have doubt that its because of security issues or running on localhost. Have you any idea why afterRidirect not working? Please help to solve this problem.

Here is the sample code.
This method is specifically for the full payment page option. Because we leave this page and return to it, we need to preserve the
state of successIndicator and orderId using the beforeRedirect/afterRedirect option

function afterRedirect(data) {
console.log("afterRedirect", data);
// Compare with the resultIndicator saved in the completeCallback() method
if(resultIndicator === "CANCELED"){
    return;
}    
else if (resultIndicator) {
    var result = (resultIndicator === data.successIndicator) ? "SUCCESS" : "ERROR";
    window.location.href = "/hostedCheckout/" + data.orderId + "/" + result;
}
else {
    successIndicator = data.successIndicator;
    orderId = data.orderId;
    sessionId = data.sessionId;
    sessionVersion = data.sessionVersion;
    merchantId = data.merchantId;

    window.location.href = "/hostedCheckout/" + data.orderId + "/" + data.successIndicator + "/" + data.sessionId;
}

This method preserves the current state of successIndicator and orderId, so they’re not overwritten when we return to this page after redirect

function beforeRedirect() { console.log("beforeRedirect");
return {
    successIndicator: successIndicator,
    orderId: orderId
};

}