1using MetaBuilders.WebControls;
2using PayPal.Web.Controls;
3using System;
4using System.Configuration;
5using System.Collections;
6using System.ComponentModel;
7using System.Data;
8using System.Drawing;
9using System.Web;
10using System.Web.SessionState;
11using System.Web.UI;
12using System.Web.UI.WebControls;
13using System.Web.UI.HtmlControls;
14
15namespace PayPalTest
16{
17 public class Default : System.Web.UI.Page
18 {
19 #region Web Form Designer generated code ...
20
21
22 protected System.Web.UI.WebControls.RadioButton rdoProduct;
23 protected System.Web.UI.WebControls.DataGrid dgProducts;
24 protected System.Web.UI.WebControls.Image Image1;
25 protected PayPal.Web.Controls.UploadCompleteCartButton btnCheckout;
26
27 override protected void OnInit(EventArgs e)
28 {
29 //
30 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
31 //
32 InitializeComponent();
33 base.OnInit(e);
34 }
35
36 /// <summary>
37 /// Required method for Designer support - do not modify
38 /// the contents of this method with the code editor.
39 /// </summary>
40 private void InitializeComponent()
41 {
42 this.Load += new System.EventHandler(this.Page_Load);
43 this.btnCheckout.Click += new System.EventHandler(this.btnCheckout_Click);
44 }
45 #endregion
46 #region Private vars ...
47
48 private DataTable mobjDSProducts = null;
49 #endregion
50 #region Page_Load ...
51
52 private void Page_Load(object sender, System.EventArgs e)
53 {
54 //On first load
55 if (!Page.IsPostBack)
56 {
57 //Create DataTable
58 CreateTestDataTable();
59 //Add products
60 AddRowToDataTable(1, "Apple iPod nano 4GB MP3 Player White",
61 "images/IPodNano.gif", 199.99);
62 AddRowToDataTable(2, "Apple iPod shuffle 1GB MP3 Player",
63 "images/IPodShuffle.gif", 129.99);
64 AddRowToDataTable(3, "Apple iPod MP3 Player with 30GB White",
65 "images/IPodWhite.gif", 299.99);
66 AddRowToDataTable(4, "Apple iPod MP3 Player with 60GB Black",
67 "images/IPodBlack.gif", 399.99);
68 //Bind Products to DataGrid
69 dgProducts.DataSource = mobjDSProducts;
70 dgProducts.DataBind();
71 //Save Products to Session State for retrieval later
72 Session["Products"] = mobjDSProducts;
73 }
74 //Set the PayPal Checkout buttons BusinessEmail property
75 btnCheckout.BusinessEmail =
76 ConfigurationSettings.AppSettings["PayPalEmailAddress"];
77 }
78 #endregion
79 #region CreateTestDataTable ...
80
81 private void CreateTestDataTable()
82 {
83 //Create DataTable
84 mobjDSProducts = new DataTable("Products");
85 //Add Columns
86 DataColumn objDC = new DataColumn("ID");
87 mobjDSProducts.Columns.Add(objDC);
88 objDC = null;
89 objDC = new DataColumn("Description");
90 mobjDSProducts.Columns.Add(objDC);
91 objDC = null;
92 objDC = new DataColumn("Image");
93 mobjDSProducts.Columns.Add(objDC);
94 objDC = null;
95 objDC = new DataColumn("Price");
96 mobjDSProducts.Columns.Add(objDC);
97 objDC = null;
98 }
99 #endregion
100 #region AddRowToDataTable ...
101
102 private void AddRowToDataTable(int intProductID,
103 string strDescription, string strImage, Double dblPrice)
104 {
105 DataRow objRow = mobjDSProducts.NewRow();
106 objRow["ID"] = intProductID.ToString();
107 objRow["Description"] = strDescription +
108 "<br><b>" + String.Format("{0:c}", dblPrice) + "</b>";
109 objRow["Image"] = "<img src=\"" + strImage + "\" border=\"0\">";
110 objRow["Price"] = dblPrice.ToString();
111 mobjDSProducts.Rows.Add(objRow);
112 }
113 #endregion
114 #region btnCheckout_Click ...
115
116 private void btnCheckout_Click(object sender, EventArgs e)
117 {
118 //Load the PayPal cart
119 CartItem objItem = new CartItem();
120 DataRow objRow = GetSelectedProduct();
121 if (objRow != null)
122 {
123 objItem.ItemNumber = objRow["ID"].ToString();
124 objItem.ItemName = objRow["Description"].ToString();
125 objItem.Amount = Single.Parse(objRow["Price"].ToString());
126 objItem.Quantity = 1;
127 btnCheckout.CartItems.Add(objItem);
128
129 //Set the user's info to UploadCompleteCartButton
130 //to prepopulate the PayPal signup page
131 btnCheckout.CustomerInfo = new CustomerInfo("FirstName",
132 "LastName", "Address1", "Address2", "City", "State",
133 "Zipcode", "NightPhoneA", "NightPhoneB", "NightPhoneC",
134 "DayPhoneA", "DayPhoneB", "DayPhoneC");
135
136 //Include the Order Number as part of the payment information.
137 //This will be used by the IpnService to update the proper order.
138 btnCheckout.Custom = "OrderNumber";
139 btnCheckout.ReturnUrl = ConfigurationSettings.AppSettings["Environment"] +
140 "/PDTHandler.aspx";
141 }
142 }
143 #endregion
144 #region GetSelectedProduct ...
145
146 private DataRow GetSelectedProduct()
147 {
148 int intIndex = 0;
149 RowSelectorColumn objRSC = RowSelectorColumn.FindColumn(dgProducts);
150 if (objRSC.SelectedIndexes.Length > 0)
151 {
152 intIndex = Int32.Parse(objRSC.SelectedIndexes[0].ToString());
153 mobjDSProducts = (DataTable)Session["Products"];
154 return mobjDSProducts.Rows[intIndex];
155 }
156 else
157 return null;
158 }
159 #endregion
160 }
161}