我正在尝试在C#MVC网站上创建一个IPN监听器。在线查看后,我设法创建了下面的ActionResult。
听众收到“已验证”的回复,但不幸的是,它似乎没有收到有关付款的任何其他信息。
[HttpPost]
public ActionResult IPN()
{
var log = new LogMessage();
log.LogMessageToFile("IPN recieved!");
var formVals = new Dictionary<string, string>();
formVals.Add("cmd", "_notify-validate");
string response = GetPayPalResponse(formVals, true);
if (response == "VERIFIED")
{
log.LogMessageToFile("IPN VERIFIED!");
//validate the order
string sAmountPaid = Request.QueryString["amt"];
string sPayment = ConfigurationManager.AppSettings["amount"].ToString();
Decimal amountPaid = 0;
Decimal Payment = 0;
Decimal.TryParse(sAmountPaid, out amountPaid);
Decimal.TryParse(sPayment, out Payment);
if (Payment <= amountPaid)
{
log.LogMessageToFile("IPN Correct amount");
//process it
try
{
string GUID = Request.QueryString["cm"];
string strGatewayResponse = Request.QueryString["tx"];
var data = new Datalayer();
data.AddPayment(GUID, amountPaid, strGatewayResponse, true);
log.LogMessageToFile("IPN Commplete");
return Redirect("/Payment/Success");
}
catch
{
log.LogMessageToFile("IPN Error");
return Redirect("/Payment/Error");
}
}
else
{
log.LogMessageToFile("IPN Incorrect amount!");
log.LogMessageToFile("IPN amount:" + Request.QueryString["payment_gross"]);
log.LogMessageToFile("IPN GUID:" + Request.QueryString["custom"]);
log.LogMessageToFile("IPN ID:" + Request.QueryString["txn_id"]);
return Redirect("/Payment/Error");
}
}
log.LogMessageToFile("IPN not verified!");
return View("/Payment/Error");
}
string GetPayPalResponse(Dictionary<string, string> formVals, bool useSandbox)
{
string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
: "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
StringBuilder sb = new StringBuilder();
sb.Append(strRequest);
foreach (string key in formVals.Keys)
{
sb.AppendFormat("&{0}={1}", key, formVals[key]);
}
strRequest += sb.ToString();
req.ContentLength = strRequest.Length;
string response = "";
using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
streamOut.Write(strRequest);
streamOut.Close();
using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
{
response = streamIn.ReadToEnd();
}
}
return response;
}
我很可能错过了一些非常简单的事情。
任何帮助都会很棒。
答案 0 :(得分:0)
我必须做同样的事情,但在Web窗体中。由于我们都在处理HttpWebRequest
个对象,所以它应该同样适用。查看下面的代码 - 这是我的侦听器URL的Page_Load()
方法(Listener.aspx.cs)。请注意if (strResponse == "VERIFIED")
部分,并查看其中string[] responseArray = strRequest.Split('&');
所在的部分 - 这是获取所有响应变量的位置。您将不得不稍微跟踪strRequest
的定义,该定义用于获取值(由&符号分隔)。
另外,请查看您的PayPal帐户的IPN响应。响应将是由&符号分隔的长字符串 - 这与strRequest
的值相同。这是您提取有关付款的其他信息的字符串:
protected void Page_Load(object sender, EventArgs e)
{
//string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
//string strLive = "https://www.paypal.com/cgi-bin/webscr";
string url = ConfigurationManager.AppSettings["PayPalUrl"];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
if (strResponse == "VERIFIED")
{
// strRequest is a long string delimited by '&'
string[] responseArray = strRequest.Split('&');
List<KeyValuePair<string, string>> lkvp = new List<KeyValuePair<string, string>>();
string[] temp;
// for each key value pair
foreach (string i in responseArray)
{
temp = i.Split('=');
lkvp.Add(new KeyValuePair<string, string>(temp[0], temp[1]));
}
// now we have a list of key value pairs
string firstName = string.Empty;
string lastName = string.Empty;
string address = string.Empty;
string city = string.Empty;
string state = string.Empty;
string zip = string.Empty;
string payerEmail = string.Empty;
string contactPhone = string.Empty;
foreach (KeyValuePair<string, string> kvp in lkvp)
{
switch (kvp.Key)
{
case "payer_email":
payerEmail = kvp.Value.Replace("%40", "@");
break;
case "first_name":
firstName = kvp.Value;
break;
case "last_name":
lastName = kvp.Value;
break;
case "address_city":
city = kvp.Value.Replace("+", " ");
break;
case "address_state":
state = kvp.Value.Replace("+", " ");
break;
case "address_street":
address = kvp.Value.Replace("+", " ");
break;
case "address_zip":
zip = kvp.Value;
break;
case "contact_phone":
contactPhone = kvp.Value;
break;
default:
break;
}
}
string userName = payerEmail;
string password = Membership.GeneratePassword(8, 0);
MembershipCreateStatus status = new MembershipCreateStatus();
MembershipUser newUser = Membership.CreateUser(userName, password, userName, null, null, true, out status);
ProfileCommon pc = ProfileCommon.Create(userName) as ProfileCommon;
pc.Address.PostalCode = zip;
pc.Address.Address = address;
pc.Address.City = city;
pc.Address.State = state;
pc.Personal.FirstName = firstName;
pc.Personal.LastName = lastName;
pc.Contacts.DayPhone = contactPhone;
pc.Save();
if (status == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(userName, "User");
//send email to user indicating username and password
SendEmailToUser(userName, password, firstName, lastName, payerEmail);
}
// need to figure out a way to catch unwanted responses here... redirect somehow
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}