Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need Example of PayPal IPN Integration

Posted on 2011-02-27
6
Medium Priority
?
1,312 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Loops Section Overview
Suggested Courses

610 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