Solved

Need Example of PayPal IPN Integration

Posted on 2011-02-27
6
1,304 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Dovberman
  • 5
6 Comments
 

Author Comment

by:Dovberman
ID: 34990973
I just finished more debugging.

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

Why is INVALID returned ?

Thanks,
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 34991452
Hello Dovberman,

Let me tell you that PayPal IPN works as background Process. It Does not return immediately to your IPN page after Payment Process.
So if you debug the page then initially it will return INVALID.
0
 

Author Comment

by:Dovberman
ID: 34991563
I am using the same page as the page the customer is returned to and the IPN page.

Should they be 2 different pages?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Dovberman
ID: 34991698
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

0
 

Author Comment

by:Dovberman
ID: 34992105
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

0
 

Author Closing Comment

by:Dovberman
ID: 34994792
This was a clue. But it was not very helpful.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question