[英]PayPal IPN Error
When I ran the following code through the IPN simulator the simulator said it worked however my page received a INVALID response. 当我通过IPN模拟器运行以下代码时,模拟器表示它可以工作,但是我的页面收到了无效响应。
I did some reading and paypal said it uses charset UTF-8 but when I changed to that the simulator failed and I received no response. 我做了一些阅读,贝宝说它使用字符集UTF-8,但是当我更改为模拟器失败时,我没有收到任何响应。
The code runs on pageload 代码在页面加载时运行
string postUrl = ConfigurationManager.AppSettings["PayPalSubmitUrlSandBox"];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
//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);
string ipnPost = strRequest;
strRequest = "cmd=_notify-validate&" + strRequest;
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")
{
//removed this because I don't think it is what is causing the trouble }
}
else if (strResponse == "INVALID")
{
//removed this because I don't think it is what is causing the trouble
}
else
{
//removed this because I don't think it is what is causing the trouble
}
Try changing your code to the following: 尝试将代码更改为以下内容:
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;
Also, make sure you're logged into the sandbox BEFORE you do the test... 另外,在进行测试之前,请确保已登录到沙箱...
Figured it out... not really sure what changed from the previous code. 想通了...不十分确定以前的代码有什么变化。 I just found code on a tutorial and did some minor modifications. 我只是在教程中找到了代码,并做了一些小的修改。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;
public partial class paypal_IPNHandler : System.Web.UI.Page
{
string payerEmail;
string paymentStatus;
string receiverEmail;
string amount;
/// <summary>
/// Process an incoming Instant Payment Notification (IPN)
/// from PayPal, at conclusion of a received payment from a
/// customer
/// </summary>
///
protected void Page_Load(object sender, EventArgs e)
{
// receive PayPal ipn data
// extract ipn data into a string
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
// append PayPal verification code to end of string
strRequest = "cmd=_notify-validate&" + strRequest;
// create an HttpRequest channel to perform handshake with PayPal
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://www.paypal.com/cgi-bin/webscr");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = strRequest.Length;
// send data back to PayPal to request verification
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
// receive response from PayPal
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
// if PayPal response is successful / verified
if (strResponse.Equals("VERIFIED"))
{
// paypal has verified the data, it is safe for us to perform processing now
// extract the form fields expected: buyer and seller email, payment status, amount
payerEmail = Request.Form["payer_email"];
paymentStatus = Request.Form["payment_status"];
receiverEmail = Request.Form["receiver_email"];
amount = Request.Form["mc_gross"];
}
// else
else
{
// payment not complete yet, may be undergoing additional verification or processing
// do nothing - wait for paypal to send another IPN when payment is complete
}
String insertConnString = System.Configuration.ConfigurationManager.ConnectionStrings["BeachConnectionString"].ConnectionString;
using (SqlConnection insertcon = new SqlConnection(insertConnString))
using (SqlCommand cmdinsert = insertcon.CreateCommand())
{
string insertprofile = "INSERT INTO Test (Status, Text) VALUES (@Status, @Text)";
cmdinsert.CommandText = insertprofile;
cmdinsert.Parameters.Add("@Status", SqlDbType.VarChar).Value = strResponse.ToString();
cmdinsert.Parameters.Add("@Text", SqlDbType.VarChar).Value = strRequest.ToString();
cmdinsert.Connection.Open();
cmdinsert.ExecuteNonQuery();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.