Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

Need Example of PayPal IPN Integration

The user is being returned to my IPN Listener as expected.

However the request object key count is 0.

What am I missing?

Please correct my code. I have already reviewed over 30 pages of IPN documentation.


<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 

<input type="hidden" name="first_name" value="David"/>
<input type="hidden" name="last_name" value="Martin"/>
<input type="hidden" name="address_street" value="7005 Southwind Drive"/>

<input type="hidden" name="credit_card_number" value="abc"/>
<input type="hidden" name="city" value="Biloxi"/>
<input type="hidden" name="state" value="MS"/>
<input type="hidden" name="zip" value="39532"/>
<input type="hidden" name="telephone" value="228-396-5708"/>
<input type="hidden" name="mm" value="01"/>
<input type="hidden" name="yy" value="13"/>
<input type="hidden" name="email" value="dovberman@cableone.net"/>
<input type="hidden" name="no_email" value="1"/>
   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="hidden" name="cmd" value="_s-xclick"/>
<input type="hidden" name="hosted_button_id" value="2GEF2YNF55VLJ"/>

<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif"  
    name="submit" alt="PayPal - The safer, easier way to pay online!"/>
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"/>

</form>

-------

public partial class PaymentReceipt : System.Web.UI.Page
{
    public string strUserName = "";
    public MembershipUser currentUser;
    public System.Configuration.ConnectionStringSettings connString;
    public string strConnection;
    public SqlConnection conStockSelect;

    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";

        lblCustomerName.Text = Request.QueryString.Keys.Count.ToString(); // displays 0.

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);
        //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();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        lblstrRequest.Text = strResponse; // Returns blank
        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

Open in new window

Avatar of Dovberman
Dovberman
Flag of United States of America image

ASKER

I just finished more debugging.

 string strResponse = streamIn.ReadToEnd();
        lblstrRequest.Text = strResponse; // Returns INVALID
        streamIn.Close();

Why is INVALID returned ?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Manoj Patil
Manoj Patil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am using the same page as the page the customer is returned to and the IPN page.

Should they be 2 different pages?
Suppose I have the following IPN Page named ProcessPayment.aspx

Then in the Page Load code behind:

else if (strResponse == "INVALID")

How do I keep the IPN Page open until the response = "VERFIED" ?

Thanks,
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
            }
            else if (strResponse == "INVALID")
            {
                //log for manual investigation
            }
            else
            {
                //log response/ipn data for manual investigation
            }

Open in new window

I created an IPN listener named ProcessPayment.aspx.

The listener is not called.

I executed a Subscription Update in 2 places. Then checked the SQL Server table.
The update was not executed.




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProcessPayment.aspx.cs" 
Inherits="ProcessPayment" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Process Payment</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

-------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;

public partial class ProcessPayment : System.Web.UI.Page
{
    public string strUserName = "";
    public MembershipUser currentUser;
    public System.Configuration.ConnectionStringSettings connString;
    public string strConnection;
    public SqlConnection conStockSelect;

    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";

        //lblCustomerName.Text = Request.QueryString.Keys.Count.ToString(); // displays 0.

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);
        //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();
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        //lblstrRequest.Text = strResponse;
        streamIn.Close();

        //Get the connection string

        System.Configuration.Configuration rootWebConfig =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");

        connString =
        rootWebConfig.ConnectionStrings.ConnectionStrings["ConStockSelect"];

        // Define the ADO.NET Connection object.
        string strConnection = connString.ToString();
        conStockSelect = new SqlConnection(strConnection);
                
        // Set profile values
        string strUserName = "dovberman";
        MembershipUser currentUser = Membership.GetUser(strUserName);
        Profile.EMail = currentUser.Email;

        string strUserID = currentUser.ProviderUserKey.ToString();
        DateTime dteToday = DateTime.Today;

        string strSQL = "UPDATE Subscriptions ";
        strSQL += "SET IssuesPaid = IssuesPaid + 5 ";
        strSQL += "WHERE UserID = '" + strUserID + "'";         conStockSelect.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = strSQL;
        cmd.Connection = conStockSelect;
        cmd.ExecuteNonQuery();
        conStockSelect.Close();

        if (strResponse == "VERIFIED") // Update Subscription
        {
            string strSQL = "UPDATE Subscriptions ";
            strSQL += "SET IssuesPaid = IssuesPaid + 1 " ;
            strSQL += "WHERE UserID = '" + strUserID + "'";         
            
            conStockSelect.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = strSQL;
            cmd.Connection = conStockSelect;
            cmd.ExecuteNonQuery();
            conStockSelect.Close();
        }
    }
}

Open in new window

This was a clue. But it was not very helpful.