When I ran the following code through the IPN simulator the simulator said it worked however my page received a INVALID response.
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.
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.