Redirect with POST options in MVC 4 to integrate a payment gateway

I am trying to integrate a payment gateway using mvc4 in a razor. The fact that I need to call a page with a pre-filled message form.

Using the following method, I form the form of the post method:

private static string PreparePOSTForm(string url, System.Collections.Hashtable data)      // post form
    {
        //Set a name for the form
        string formID = "PostForm";
        //Build the form using the specified data to be posted.
        StringBuilder strForm = new StringBuilder();
        strForm.Append("<form id=\"" + formID + "\" name=\"" +
                       formID + "\" action=\"" + url +
                       "\" method=\"POST\">");

        foreach (System.Collections.DictionaryEntry key in data)
        {

            strForm.Append("<input type=\"hidden\" name=\"" + key.Key +
                           "\" value=\"" + key.Value + "\">");
        }

        strForm.Append("</form>");
        //Build the JavaScript which will do the Posting operation.
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        strScript.Append("var v" + formID + " = document." +
                         formID + ";");
        strScript.Append("v" + formID + ".submit();");
        strScript.Append("</script>");
        //Return the form and the script concatenated.
        //(The order is important, Form then JavaScript)
        return strForm.ToString() + strScript.ToString();
    }

And on my controller page, I call PreparePostFormwith the required parameter, and I get the POST request format.

[HttpPost]
 public ActionResult OrderSummary()
        {
            string request=PreparePOSTForm("payment URL","hashdata required for payment")
            return Redirect(request);
        }

But when redirecting, I get below the error.

Invalid request - invalid URL

HTTP Error 400. Invalid request url.

I am missing something here to work with a POST request. Can someone help me.

Thanks in advance.

+3
source share
3

Redirect. View Javascript.

public ActionResult OrderSummary()
{
    string request=PreparePOSTForm("payment URL","hashdata required for payment")
    return View(model:request);
}

OrderSummary:

@model string

@Html.Raw(Model)

<script>
    $(function(){
       $('form').submit();
    })
</script>
+2

Action , Model. Action

[HttpPost]
public ActionResult OrderSummary()
{
    return RedirectToAction("OrderForm", new { HashData = hashData });
}

[HttpGet]
public ViewResult OrderForm(string hashData)
{
    OrderFormModel model = new OrderFormModel();
    model.HashData = hashData;
    return View(model);
}

[HttpPost]
public ActionResult OrderForm(OrderFormModel model)
{
    if(ModelState.IsValid)
    {
        // do processing
    }
}
0

JavaScript.

, html- javascript : .

. html , - , , .

0
source

Source: https://habr.com/ru/post/1673765/


All Articles