Search code examples
c#asp.net-mvc-4paytm

Mvc4 paytm integration - close popup windows and redirect to main view


I am integrating paytm to my site. I have view i.e. Index. Currently it is simply a view which contains one button which open popup window.

Here is my view.

@{
    ViewBag.Title = "paytm";
}

<style>
    .btn2 {
        background-color: #00b9f5;
        padding-bottom: 10px;
        padding-top: 10px;
    }
</style>
<h2>paytm</h2>
<button class="btn2" style="font-weight: 600;" onclick="popitup('http://localhost:49569/home/paytmcheckout');">Pay With Paytm</button>
<script>
    function popitup(url) {
        window.open(url, "_blank", "width=700,height=700,scrollbars=yes");
    }
</script>

Here is paytmcheckout action

public ActionResult paytmcheckout()
{            
    Dictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("MID", "Some value");
    parameters.Add("CHANNEL_ID", "some value");
    parameters.Add("INDUSTRY_TYPE_ID", "some value");
    parameters.Add("WEBSITE", "some value");
    //parameters.Add("EMAIL", "email value");
    //parameters.Add("MOBILE_NO", "mobile value");
    parameters.Add("CUST_ID", "some value");
    parameters.Add("ORDER_ID", "some value");
    parameters.Add("TXN_AMOUNT", "some value");
    parameters.Add("CALLBACK_URL", "http://localhost:49569/home/thankyou/"); 
    //This parameter is not mandatory. Use this to pass the callback url dynamically.

    string checksum = paytm.CheckSum.generateCheckSum(merchantKey, parameters);

    string paytmURL = "https://pguat.paytm.com/oltp-web/processTransaction";

    string outputHTML = "<html>";
    outputHTML += "<head>";
    outputHTML += "<title>Merchant Check Out Page</title>";
    outputHTML += "</head>";
    outputHTML += "<body>";
    outputHTML += "<center><h1>Please do not refresh this page...</h1></center>";
    outputHTML += "<form method='post' action='" + paytmURL + "' name='f1'>";
    outputHTML += "<table border='1'>";
    outputHTML += "<tbody>";
    foreach (string key in parameters.Keys)
    {
        outputHTML += "<input type='hidden' name='" + key + "' value='" + parameters[key] + "'>";
    }
    outputHTML += "<input type='hidden' name='CHECKSUMHASH' value='" + checksum + "'>";
    outputHTML += "</tbody>";
    outputHTML += "</table>";
    outputHTML += "<script type='text/javascript'>";
    outputHTML += "document.f1.submit();";
    outputHTML += "</script>";
    outputHTML += "</form>";
    outputHTML += "</body>";
    outputHTML += "</html>";
    Response.Write(outputHTML);
    return new EmptyResult();
}

It simple post some data to paytm. You can find out code here https://github.com/Paytm-Payments/Paytm_Web_Sample_Kit_dotNet

When request sent to paytm it work on it and on callback url it send some response. Here in my case I have created one action i.e. thankyou. But I want to close the popup windows(which calls from Index view) and show response in Index view instead of showing thankyou view. Currently popup windows is not closing and showing thankyou view in it. How can I close the popup window and show result in Index view? I am not getting how can I implement this.

I want to implement something like this - http://paywithpaytm.com/demo/


Solution

  • in main window

      <script language="Javascript">
        function popitup(url) {
         newwindow=window.open(url, "_blank", "width=700,height=700,scrollbars=yes");
          if (window.focus) {newwindow.focus()}
        }
    </script>
    

    in child pop up

        <script language="Javascript">
        function redirectToFB(){
            window.opener.location.href="http://bla.bla.bla";
            self.close();
        }
       </script>
    

    instead of using

        return new EmptyResult();
    

    use

     return View();
    

    and in that view file you can code this way

        <script language="Javascript">
        (function(){
            window.opener.location.href="http://bla.bla.bla";
            self.close();
        })();
       </script>
    

    for more details see here