Solved

Need Example of PayPal IPN Integration

Posted on 2011-02-27
6
1,298 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now