<div>Have a problem with some code that after several hours, cannot figure out. It seems like I am getting 2 transactions form paypal. The first comes back as 'verified', then the other right after comes back 'invalid'. Here is the 'sending code'</div> <div>
</div>
<div>And, here is the recieving page..</div>
<div>
protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
//string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//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 strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
//this.lblPaypalResult.Text = strResponse_copy;
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
if (strResponse == "VERIFIED")
{
IcanvasdesignserviceClient svc = new IcanvasdesignserviceClient();
NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
string _email = ConfigurationManager.AppSettings["PPEmail"];
string payment_status = these_argies["payment_status"];
string gross = these_argies["mc_gross"];
string fee = these_argies["mc_fee"];
string business = these_argies["business"];
string txn_id = these_argies["txn_id"];
string order_id = these_argies["custom"];
string reason = "";
if (payment_status == "Pending")
{
reason = these_argies["pending_reason"];
}
if (_email != business)
{
}
long check = svc.CheckTransactionID(txn_id);
if (check == 0)
{
this.Label1.Text = "SUCCESS!";
svc.AddTransaction(Convert.ToInt32(order_id), txn_id, "PAYPAL", payment_status, Convert.ToDecimal(gross), Convert.ToDecimal(fee), reason);
}
}
else if (strResponse == "INVALID")
{
Response.Redirect("https://secure.company.com/transaction.aspx?action=2");
}
else
{
}
}
</div> <div></div> <div>When I receive the transaction, it updates the database with the call svc.AddTransaction(), but it also redirects to 'secure.company.com/transaction.aspx?action=2' And, what is strange is that I cannot redirect from the "VERIFIED"
portion at all. Yet, I know the code is hitting it.</div> <div></div> <div>
Well, I believe I have figured out the problem and I want to go ahead and post it here in case someone else has the same issue. The problem wasn't the code, but more of the understanding on how the IPN works. The IPN return address you put into your paypal
settings should NOT be the same as the 'return' value enter into your sending string. What was happening was that it was processing the transaction, then redirecting me to the page which was producing the INVALID because no transaction was sent.
Hope this keeps someone else from banging their head against the wall for hours on end like I did. :)
Jungle
Member
37 Points
22 Posts
Paypal IPN Double Transactions Coming In...
Jan 31, 2012 08:08 AM|LINK
string _email = ConfigurationManager.AppSettings["PPEmail"]; string url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; string _cmd = "_xclick"; string _item_name = "Shopping Cart"; string _orderid = _id.ToString(); string _returnURL = "https://secure.company.com/paypalpurchase.aspx"; string _cancelReturn = "https://secure.company.com/cart.aspx"; string _amount = 50.00; string _currency = "USD"; string bn = "PP-BuyNowBF"; string redirect = ""; redirect += url + "?cmd=" + _cmd + "&business=" + _email; redirect += "&item_name=" + _item_name; redirect += "&amount=" + _amount; redirect += "&item_number=" + _orderid; redirect += "¤cy_code=" + _currency; redirect += "&return=" + _returnURL; redirect += "&bn=" + bn; redirect += "&cancel_return=" +_cancelReturn; redirect += "&custom=" + _orderid; Response.Redirect(redirect);</div> <div>And, here is the recieving page..</div> <div>protected void Page_Load(object sender, EventArgs e) { //Post back to either sandbox or live string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr"; //string strLive = "https://www.paypal.com/cgi-bin/webscr"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox); //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 strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal strRequest += "&cmd=_notify-validate"; req.ContentLength = strRequest.Length; //for proxy //WebProxy proxy = new WebProxy(new Uri("http://url:port#")); //req.Proxy = proxy; //Send the request to PayPal and get the response StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII); streamOut.Write(strRequest); streamOut.Close(); //this.lblPaypalResult.Text = strResponse_copy; StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()); string strResponse = streamIn.ReadToEnd(); streamIn.Close(); if (strResponse == "VERIFIED") { IcanvasdesignserviceClient svc = new IcanvasdesignserviceClient(); NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy); string _email = ConfigurationManager.AppSettings["PPEmail"]; string payment_status = these_argies["payment_status"]; string gross = these_argies["mc_gross"]; string fee = these_argies["mc_fee"]; string business = these_argies["business"]; string txn_id = these_argies["txn_id"]; string order_id = these_argies["custom"]; string reason = ""; if (payment_status == "Pending") { reason = these_argies["pending_reason"]; } if (_email != business) { } long check = svc.CheckTransactionID(txn_id); if (check == 0) { this.Label1.Text = "SUCCESS!"; svc.AddTransaction(Convert.ToInt32(order_id), txn_id, "PAYPAL", payment_status, Convert.ToDecimal(gross), Convert.ToDecimal(fee), reason); } } else if (strResponse == "INVALID") { Response.Redirect("https://secure.company.com/transaction.aspx?action=2"); } else { } }</div> <div></div> <div>When I receive the transaction, it updates the database with the call svc.AddTransaction(), but it also redirects to 'secure.company.com/transaction.aspx?action=2' And, what is strange is that I cannot redirect from the "VERIFIED" portion at all. Yet, I know the code is hitting it.</div> <div></div> <div>
<div> </div> <div> </div> </div>Jungle
Member
37 Points
22 Posts
Re: Paypal IPN Double Transactions Coming In...
Feb 01, 2012 03:12 PM|LINK
Well, I believe I have figured out the problem and I want to go ahead and post it here in case someone else has the same issue. The problem wasn't the code, but more of the understanding on how the IPN works. The IPN return address you put into your paypal settings should NOT be the same as the 'return' value enter into your sending string. What was happening was that it was processing the transaction, then redirecting me to the page which was producing the INVALID because no transaction was sent.
Hope this keeps someone else from banging their head against the wall for hours on end like I did. :)