basically my application is similar to the link but mine is on MVC C#.
-Make a payment request. -Read the Respond. -Pass the respond to our api.
https://developer.2c2p.com/docs/prepare-payment-request
my payment request code:
function CreateForm(parameters)
{
var form = $('<form id="PaymentForm1"></form>');
var path = url;
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function (key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
console.log(key, value);
form.append(field);
});
$(document.body).append(form);
form.submit();
}
So my problem now is the response result on how to read it… and pass it in our api.
(similar to this but on MVC https://developer.2c2p.com/docs/read-payment-response)
I try to use this code.. WebRequest request = WebRequest.Create(resultURL); but will reload the page, data will be gone.
[HttpGet]
public ActionResult getResultValues()
{
WebRequest request = WebRequest.Create(resultURL);
request.Method = "GET";
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
return View(responseFromServer);
}
Is there a better way to do it? I’m also new on MVC
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
@chenz101
Looking my code
Make a class for below formats
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AspNetMvcJqueryAjaxSerializeForm.Models
{
public class FriendModel
{
public string FriendName { get; set; }
public string Phone { get; set; }
public string State { get; set; }
}
}
Controller
// POST: Friend/AddFriend
[HttpPost]
public ActionResult AddFriend(FriendModel fm)
{
//Write your database insert code / activities
return RedirectToAction("create");
}
This is the your MVC view html page code
<form id="friendform">
<table>
<tr>
<td>Friend Name</td>
<td><input id="txtFriendName" name="FriendName" type="text" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input id="txtPhone" name="Phone" type="text" /></td>
</tr>
<tr>
<td>State</td>
<td><input id="txtState" name="State" type="text" /></td>
</tr>
</table>
<input id="btnsubmit" type="button" value="Submit"/>
</form>
This is the your ajax call code
<script>
$(document).ready(function () {
$("#btnsubmit").click(function (e) {
//Serialize the form datas.
var valdata = $("#friendform").serialize();
//to get alert popup
alert(valdata);
$.ajax({
url: "/Friend/AddFriend",
type: "POST",
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: valdata
});
});
});
</script>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
