1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Configuration;
5using System.Data;
6using System.Data.SqlClient;
7using System.Drawing;
8using System.Web;
9using System.Web.SessionState;
10using System.Web.UI;
11using System.Web.UI.WebControls;
12using System.Web.UI.HtmlControls;
13using System.Net;
14using System.Text;
15using System.IO;
16
17namespace PayPalTest
18{
19 public class PDTHandler : System.Web.UI.Page
20 {
21 #region Web Form Designer generated code ...
22
23 override protected void OnInit(EventArgs e)
24 {
25 InitializeComponent();
26 base.OnInit(e);
27 }
28
29 private void InitializeComponent()
30 {
31 this.Load += new System.EventHandler(this.Page_Load);
32 }
33 #endregion
34
35 const string PAGENAME = "PDTHandler";
36 #region Page_Load ...
37
38 private void Page_Load(object sender, System.EventArgs e)
39 {
40 //Check if transaction canceled
41 if (Request.QueryString["tx"] == null)
42 Response.Redirect("Message.aspx?M=1");
43 else
44 {
45 //Get the PDT data from PayPal
46 string strPDT = GetPDT();
47
48 //Success?
49 if (strPDT.IndexOf("SUCCESS") > -1)
50 Response.Redirect("Message.aspx?M=2");
51 else
52 Response.Redirect("Message.aspx?M=3");
53 }
54 }
55 #endregion
56 #region GetPDT ...
57
58 private string GetPDT()
59 {
60 string PDTID = ConfigurationSettings.AppSettings["PayPalPDTID"];
61 string strTransID = Request.QueryString["tx"].ToString();
62 string strServerURL ="https://www.paypal.com/cgi-bin/webscr";
63
64 string strFormValues = Request.Form.ToString();
65 string strNewValue;
66 string strResponse;
67
68 //Create the request back
69 HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(strServerURL);
70
71 //Set values for the request back
72 objRequest.Method = "POST";
73 objRequest.ContentType = "application/x-www-form-urlencoded";
74 strNewValue = strFormValues + "&cmd=_notify-synch&at=" + PDTID + "&tx=" + strTransID;
75 objRequest.ContentLength = strNewValue.Length;
76
77 //Write the request back IPN strings
78 StreamWriter objSW = new StreamWriter (objRequest.GetRequestStream(), System.Text.Encoding.ASCII);
79 objSW.Write(strNewValue);
80 objSW.Close();
81
82 //Do the request to PayPal and get the response
83 StreamReader objSR = new StreamReader(objRequest.GetResponse().GetResponseStream());
84 strResponse = objSR.ReadToEnd();
85 objSR.Close();
86
87 //Return the response
88 return Server.UrlDecode(strResponse);
89 }
90 #endregion
91 #region GetPDTValue ...
92
93 string GetPDTValue(string strPDT, string key){
94
95 string[] arrBits = strPDT.Split('\n');
96
97 string strField = "";
98 string strValue = "";
99 string strLine = "";
100
101 string strOut = "";
102 for(int i = 0; i < arrBits.Length; i++)
103 {
104 strLine = arrBits[i].ToString();
105 if(strLine.IndexOf("=") > -1)
106 {
107 strField = strLine.Substring(0, strLine.IndexOf("="));
108 strValue = strLine.Remove(0, strLine.IndexOf("=")+1);
109 if(strField == key)
110 strOut = strValue;
111 }
112 }
113 return strOut;
114 }
115 #endregion
116 }
117}