?
Solved

Need Example of PayPal IPN Integration

Posted on 2011-02-27
6
Medium Priority
?
1,309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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