I am interested in the building of the html page/form in the background
for submission as you refer to, if you have an example of this, i would
be very grateful, im sure I can work it out myself, but I would
nevertheless like to see how you did it.
This will only work for PayPal, obviously, but several other payment
gateways have similar mechanisms:
private void BindData()
{
Dictionary<string, byte> dicCart = (Dictionary<string,
byte>)Session["dicCart"];
string strPayPalForm;
DataRow objRow = null;
LinkButton lnkRemove;
decimal curItemTotal = 0;
decimal curSubTotal = 0;
decimal curShipping = 0;
decimal curShippingTotal = 0;
decimal curTotal = 0;
try
{
if(dicCart.Count == 0)
{
tblCart.Visible = false;
ClientScript.RegisterStartupScript(GetType(), "emptyCart",
"alert('There are no items in your shopping
cart');window.location='default.aspx';", true);
}
else
{
tblCart.Visible = true;
strPayPalForm = String.Empty;
strPayPalForm += "<form target=\"paypal\" id=\"frmPayPal\"
action=\"" +
System.Configuration.ConfigurationManager.AppSettings["PayPalURL"] + "\"
method=\"post\">\r\n";
strPayPalForm += "\t<input type=\"hidden\" name=\"cmd\" value=\"_cart\"
/>\r\n";
strPayPalForm += "\t<input type=\"hidden\" name=\"upload\" value=\"1\"
/>\r\n";
strPayPalForm += "\t<input type=\"hidden\"
name=\"business\" value=\"" +
System.Configuration.ConfigurationManager.AppSettings["PayPalAccount"] +
"\" />\r\n";
strPayPalForm += "\t<input type=\"hidden\" name=\"currency_code\"
value=\"GBP\" />\r\n";
using (DataSet objDS = new DataSet())
{
objDS.ReadXml(Request.PhysicalApplicationPath +
"\\App_Data\\merch.xml");
DataColumn[] objPK = new DataColumn[1];
objPK[0] = objDS.Tables[0].Columns["id"];
objDS.Tables[0].PrimaryKey = objPK;
int intItem = 1;
foreach (KeyValuePair<string, byte> kvpItem in dicCart)
{
objRow = objDS.Tables[0].Rows.Find(kvpItem.Key);
using (TableRow objTR = new TableRow()) // create a new row
{
// item -----------------------------------------------------
using (TableCell objTD = new TableCell())
{
objTD.Text = objRow["item_type"].ToString();
objTD.HorizontalAlign = HorizontalAlign.Left;
objTR.Cells.Add(objTD);
}
// description ----------------------------------------------
using (TableCell objTD = new TableCell())
{
objTD.Text = objRow["item_description"].ToString();
objTD.HorizontalAlign = HorizontalAlign.Left;
objTR.Cells.Add(objTD);
}
strPayPalForm += "<input type=\"hidden\" name=\"item_name_" +
intItem.ToString() + "\" value=\"" + objRow["item_description"].ToString()
+ "\" />\r\n";
// quantity -------------------------------------------------
using (TableCell objTD = new TableCell())
{
objTD.Controls.Add(new LiteralControl(kvpItem.Value.ToString() +
" "));
lnkRemove = (LinkButton)pnlCart.FindControl("lnkRemove_" +
kvpItem.Key);
lnkRemove.Visible = true;
if (kvpItem.Value == 1)
{
lnkRemove.ToolTip = "Remove this item from your shopping cart";
}
else
{
lnkRemove.ToolTip = "Reduce the quantity of this item";
}
objTD.Controls.Add(lnkRemove);
objTD.HorizontalAlign = HorizontalAlign.Right;
objTR.Cells.Add(objTD);
}
strPayPalForm += "<input type=\"hidden\" name=\"quantity_" +
intItem.ToString() + "\" value=\"" + kvpItem.Value.ToString() + "\"
/>\r\n";
// unit cost ------------------------------------------------
using (TableCell objTD = new TableCell())
{
objTD.Text = objRow["item_cost"].ToString();
objTD.HorizontalAlign = HorizontalAlign.Right;
objTR.Cells.Add(objTD);
}
strPayPalForm += "<input type=\"hidden\" name=\"amount_" +
intItem.ToString() + "\" value=\"" + objRow["item_cost"].ToString() + "\"
/>\r\n";
// total cost -----------------------------------------------
curItemTotal = Convert.ToDecimal(Convert.ToDecimal(kvpItem.Value) *
Convert.ToDecimal(objRow["item_cost"].ToString()));
curSubTotal += curItemTotal;
switch (cmbShipping.SelectedValue)
{
case "1": // UK
{
curShipping =
Convert.ToDecimal(objRow["item_ship_uk"].ToString());
strPayPalForm += "<input type=\"hidden\" name=\"shipping_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
if (kvpItem.Value > 1)
{
strPayPalForm += "<input type=\"hidden\" name=\"shipping2_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
}
curShippingTotal +=
Convert.ToDecimal(Convert.ToDecimal(kvpItem.Value) * curShipping);
break;
}
case "2": // EU
{
curShipping =
Convert.ToDecimal(objRow["item_ship_europe"].ToString());
strPayPalForm += "<input type=\"hidden\" name=\"shipping_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
if (kvpItem.Value > 1)
{
strPayPalForm += "<input type=\"hidden\" name=\"shipping2_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
}
curShippingTotal +=
Convert.ToDecimal(Convert.ToDecimal(kvpItem.Value) * curShipping);
break;
}
case "3": // rest of world
{
curShipping =
Convert.ToDecimal(objRow["item_ship_world"].ToString());
strPayPalForm += "<input type=\"hidden\" name=\"shipping_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
if (kvpItem.Value > 1)
{
strPayPalForm += "<input type=\"hidden\" name=\"shipping2_" +
intItem.ToString() + "\" value=\"" + curShipping.ToString() + "\" />\r\n";
}
curShippingTotal +=
Convert.ToDecimal(Convert.ToDecimal(kvpItem.Value) * curShipping);
break;
}
}
using (TableCell objTD = new TableCell())
{
objTD.Text = curItemTotal.ToString("#,##0.00");
objTD.HorizontalAlign = HorizontalAlign.Right;
objTR.Cells.Add(objTD);
}
tblCart.Rows.AddAt(intItem, objTR); // add the row to the table
}
intItem++;
}
}
lblShipping.Text = curShippingTotal.ToString("#,##0.00");
curTotal = curSubTotal + curShippingTotal;
lblTotal.Text = curTotal.ToString("#,##0.00");
strPayPalForm += "<input type=\"button\" value=\"Continue shopping\"
onclick=\"location.href='default.aspx';\" />\r\n";
strPayPalForm += " \r\n";
strPayPalForm += "<input type=\"submit\" value=\"Proceed to checkout\"
onclick=\"return submitPayPal();\" />";
strPayPalForm += "</form>";
((Literal)Master.FindControl("litAdditional")).Text =
strPayPalForm.Replace("\r\n", String.Empty);
}
}
catch (Exception ex)
{
CApplication.GlobalExceptionHandler(ex);
}
}