Bug: [FEATURE] handle redirections for iframed content served by HyperSwitch



### Feature Description

HyperSwitch's SDK / links is meant to work within top context at the client side. This behaviour changes when we introduce secure links - which are supposed to be iframed within merchant's top context. Any functionality which was consuming top context at the client side needs to be handled accordingly.

### Possible Implementation

Major things which needs to be handled
- client side redirections to happen at top context
    - client side redirections happen within current window context - which is assumed to be top (since the SDK integration requires this to be at top level)
    - when using iframed resources, SDK should handle redirection and make sure this happens in the top context
- accessing `protocol` and `hostname`
    - this is also fetched from within the current window's context
    - needs to happen at top context

### Have you spent some time checking if this feature request has been raised before?

- [X] I checked and didn't find a similar issue

### Have you read the Contributing Guidelines?

- [X] I have read the [Contributing Guidelines](https://github.com/juspay/hyperswitch/blob/main/docs/CONTRIBUTING.md)

### Are you willing to submit a PR?

Yes, I am willing to submit a PR!
diff --git a/crates/router/src/core/generic_link/payout_link/status/script.js b/crates/router/src/core/generic_link/payout_link/status/script.js
index 42b57903593..ffb554a6829 100644
--- a/crates/router/src/core/generic_link/payout_link/status/script.js
+++ b/crates/router/src/core/generic_link/payout_link/status/script.js
@@ -173,7 +173,17 @@ function redirectToEndUrl(returnUrl) {
       }
       if (secondsLeft === 0) {
         setTimeout(function () {
-          window.location.href = returnUrl.toString();
+          try {
+            window.top.location.href = returnUrl.toString();
+          } catch (error) {
+            console.error(
+              "CRITICAL ERROR",
+              "Failed to redirect top document. Error - ",
+              error
+            );
+            console.info("Redirecting in current document");
+            window.location.href = returnUrl.toString();
+          }
         }, 1000);
       }
     }, i * 1000);
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html
index b66242305b6..54e48a5241f 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html
+++ b/crates/router/src/core/payment_link/payment_link_initiate/payment_link.html
@@ -325,10 +325,10 @@
       </svg>
     </div>
     <script>
+      {{logging_template}}
       {{locale_template}}
       {{rendered_js}}
       {{payment_link_initiator}}
-      {{logging_template}}
     </script>
     {{ hyperloader_sdk_link }}
   </body>
diff --git a/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js b/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
index 777cfd8cadd..48974a564d5 100644
--- a/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
+++ b/crates/router/src/core/payment_link/payment_link_initiate/secure_payment_link_initiator.js
@@ -57,7 +57,7 @@ if (!isFramed) {
     });
     var type =
       paymentDetails.sdk_layout === "spaced_accordion" ||
-      paymentDetails.sdk_layout === "accordion"
+        paymentDetails.sdk_layout === "accordion"
         ? "accordion"
         : paymentDetails.sdk_layout;
 
@@ -103,6 +103,35 @@ if (!isFramed) {
     arr.splice(0, 3);
     arr.unshift("status");
     arr.unshift("payment_link");
-    window.location.href = window.location.origin + "/" + arr.join("/")+ "?locale=" + paymentDetails.locale;
+    let returnUrl =
+      window.location.origin +
+      "/" +
+      arr.join("/") +
+      "?locale=" +
+      paymentDetails.locale;
+    try {
+      window.top.location.href = returnUrl;
+
+      // Push logs to logs endpoint
+    } catch (error) {
+      var url = window.location.href;
+      var { paymentId, merchantId, attemptId, connector } = parseRoute(url);
+      var urlToPost = getEnvRoute(url);
+      var message = {
+        message: "CRITICAL ERROR - Failed to redirect top document. Falling back to redirecting using window.location",
+        reason: error.message,
+      }
+      var log = {
+        message,
+        url,
+        paymentId,
+        merchantId,
+        attemptId,
+        connector,
+      };
+      postLog(log, urlToPost);
+
+      window.location.href = returnUrl;
+    }
   }
 }
diff --git a/crates/router/src/core/payment_link/payment_link_status/status.html b/crates/router/src/core/payment_link/payment_link_status/status.html
index fdc0c3012bb..c78a0bb556d 100644
--- a/crates/router/src/core/payment_link/payment_link_status/status.html
+++ b/crates/router/src/core/payment_link/payment_link_status/status.html
@@ -11,9 +11,9 @@
       href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;800"
     />
     <script>
+      {{logging_template}}
       {{locale_template}}
       {{ rendered_js }}
-      {{logging_template}}
     </script>
   </head>
   <body onload="boot()">

(cherry picked from commit db6df219a9cd12d619268f388b26c88692d858f8)

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [x] Bugfix
- [ ] New feature
- [ ] Enhancement
- [x] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description


### Additional Changes

- [ ] This PR modifies the API contract
- [ ] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## How did you test it?
<details>
    <summary>[Payments] open links (no auth redirections)</summary>

https://github.com/user-attachments/assets/ccca6292-a89c-4e4f-9fc6-8cbb41280337

</details>

<details>
    <summary>[Payments] open links (with auth redirections)</summary>

https://github.com/user-attachments/assets/0ebe3c86-c6eb-450a-8e27-d0274306a033

</details>

<details>
    <summary>[Payments] secure links (no auth redirections)</summary>

https://github.com/user-attachments/assets/97fc209a-faa7-4f92-892d-1dbb6f8390f3

</details>

<details>
    <summary>[Payments] secure links (with auth redirections)</summary>

https://github.com/user-attachments/assets/ccc3510c-facb-4b08-a910-1e51f2090480

</details>

<details>
    <summary>[Payouts] secure links (with status page redirection)</summary>

https://github.com/user-attachments/assets/3aa9f27b-7cf1-4256-8e38-002d7634ccff

</details>


## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code
- [ ] I added unit tests for my changes where possible
