Click here to Skip to main content
15,888,255 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
Hello There, I have used Paypal sandbox IPN but I always get Invalid response, please help me i have posted my code below.

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Mail;


public partial class Paypal : System.Web.UI.Page
{
    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);
        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();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        streamIn.Close();
 
        if (strResponse == "VERIFIED")
        {
            //check the payment_status is Completed
            //check that txn_id has not been previously processed
            //check that receiver_email is your Primary PayPal email
            //check that payment_amount/payment_currency are correct
            //process payment

               Response.Write(strResponse);
        }
        else if (strResponse == "INVALID")
        {
            //log for manual investigation
                 Response.Write(strResponse);
        }
        else
        {
            //log response/ipn data for manual investigation
             Response.Write("Cant Get Response");
        }
    }
}
Posted
Updated 27-Feb-13 20:07pm
v3
Comments
kashyap10 27-Feb-13 2:00am    
@Orcun Iyigun can u please explain me my mistakes...
Azziet 27-Feb-13 2:33am    
he just added the code blocks in you question? there are no mistakes?
kashyap10 27-Feb-13 2:35am    
@Azziet If there is no mistake ... why i get only INVALID response...? please help me
boogac 27-Feb-13 4:46am    
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";

there are // and / both in strings,maybe?
kashyap10 27-Feb-13 4:47am    
than what should i need to do ..?

1 solution

Hey Guys I got the solution.....

Instead of checking the response by Response.write() you should use any other option like send mail or run insert query ....

the basic concept is you dont need to run your "notify_url" ... it will run automatically when paypal respond ... (if you run it manually ... it will always return invalid)

Thanks to All to help me.....
 
Share this answer
 
Comments
Member 11762684 1-Feb-16 4:01am    
i m alwys get the invalid response using paypal ipn....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900