All I want to do is to return the email of the user that completes pay transaction… I found this demo over the web but I cant find the link…
don't forget to create paypall sandbox account
So I will need three pages
1. Default.aspx for filling the user email adress and the amount to be send to paypal.
2. Sendpayment.aspx .Sends the data to paypall
3. Paypal .aspx. It is a listener that “listens” paypall transaction status.
• Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Amount<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />email<br />
<asp:TextBox ID="email" runat="server"></asp:TextBox>
<hr />
<asp:Button ID="Button1" runat="server" Text="PayNow" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
Code:
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["totalShoppingAmt"] = TextBox1.Text;
Session["regemail"] = email.Text;
Session["orderID"] = string.Format("{0:d7}", (DateTime.Now.Ticks / 10) % 10000000);
Response.Redirect("sendpayment.aspx");
}
}
• SendPayment
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sendpayment.aspx.cs" Inherits="sendpayment" %>
<!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>Untitled Page</title>
</head>
<body>
<!--form action for live transaction https://www.paypal.com/cgi-bin/webscr-->
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="form1"
name="form1">
<input type="hidden" name="cmd" value="_xclick"/>
<input type="hidden" name="business" value="[email protected] "/><!--Paypal or sandbox Merchant account -->
<input type="hidden" name="item_name" value="<%=Session["orderID"]%>_Books"/>
<input type="hidden" name="custom" value="<%=Session["regemail"]%>"/><!--Custom Field for payer email -->
<input type="hidden" name="item_number" value="1"/>
<input type="hidden" name="amount" value="<%=Session["totalShoppingAmt"]%>"/>
<input type="hidden" name="return" value="http://sitename.com/thanks.aspx"/><!--this page will be your redirection page -->
<input type="hidden" name="cancel_return" value="http://sitename.com/cancel.aspx"/>
<input type="hidden" name="currency_code" value="EUR"/>
<input type="hidden" name="notify_url" value="http://sitename.com/paypal.aspx"/><!--this should be your domain web page where you going to receive all your transaction variables -->
</form>
<script type="text/jscript">
document.form1.submit();
</script>
</body>
</html>
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class sendpayment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
• paypal
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="paypal.aspx.cs" Inherits="paypal" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
public partial class paypal : System.Web.UI.Page
{
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";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//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);
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
string txnid = Server.HtmlEncode(Request.Form["txn_id"]);
string receiv_email =Server.HtmlEncode(Request.Form["receiver_email"]);
string business = Server.HtmlEncode(Request.Form["business"]);
string payer_email=Server.HtmlEncode(Request.Form["payer_email"]);
string tnx_type =Server.HtmlEncode(Request.Form["txn_Type"]);
string payment_type = Server.HtmlEncode(Request.Form["payment_type"]);
string payment_stat=Server.HtmlEncode(Request.Form["payment_status"]);
string regemail=Server.HtmlEncode(Request.Form["custom"]); // this is where i get my member's email adress
string constr = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
string insertsql = "INSERT INTO databasename.dbo.tnx_paypal (txn_id, receiver_email, business, payer_email, tnx_type, payment_status,LocalDate,payment_type,regemail) values('" + txnid + "','" + receiv_email + "','" + business + "','" + payer_email + "','" + tnx_type + "','" + payment_stat + "','" + DateTime.Now.ToString() + "','" + payment_type + "','" + regemail +"')";
using (SqlConnection myConnection = new SqlConnection(constr))
{
myConnection.Open();
SqlCommand mycommand = new SqlCommand(insertsql, myConnection);
mycommand.ExecuteNonQuery();
myConnection.Close();
}
// for testing purposes i did not create any ipn status actions as i wanted to record every paypal status.
if (strResponse == "VERIFIED")
{
//UPDATE YOUR DATABASE
//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")
{
//UPDATE YOUR DATABASE
}
else
{ //UPDATE YOUR DATABASE
}
}
}
I wish it will be helpfull...