📄 checkoutconfirm.aspx.cs
字号:
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.Globalization;
public partial class CheckoutConfirm : System.Web.UI.Page {
//set scope so all methods can access
ShoppingCart cart;
protected void Page_Load(object sender, EventArgs e) {
//this page MUST be protected by SSL!
cart = ShoppingCartManager.GetCart();
if (!Page.IsPostBack) {
BindOrder();
if (Request.QueryString["token"] != null) {
string sToken = Request.QueryString["token"].ToString();
//load up the info
LoadFromPP(sToken);
} else if(Request.QueryString["st"]!=null){
//this is a Payments Standard checkout
//so set the payment method, and hide the other payment
//bits
lblPaymentSummary.Text = "PayPal Payment";
tblBillInfo.Visible = false;
}else{
SaveBillingInfo();
LoadFromProfile();
}
string fromZip = ConfigurationManager.AppSettings["ShipFromZip"].ToString();
BindShipping(fromZip, Profile.Commerce.ShipZip, cart.TotalWeight);
LoadBillingLabels();
LoadShippingLabels();
SetTax(Profile.Commerce.ShipState);
SetShipping();
SetTotals();
}
if (SiteConfiguration.UsePayPalPaymentsStandard)
{
trPlaceOrder.Visible = false;
trRunPayPal.Visible = true;
PPStandardCheckout1.Subtotal = cart.SubTotal;
PPStandardCheckout1.TaxAmount = TaxCalculator.GetTaxByZip(Profile.Commerce.ShipZip, cart.SubTotal);
PPStandardCheckout1.ShippingMethod = ddlShipService.SelectedItem.Text;
PPStandardCheckout1.ShippingAmount = Convert.ToDouble(ddlShipService.SelectedValue);
} else {
trPlaceOrder.Visible = true;
trRunPayPal.Visible = false;
}
}
#region Control Bindings
void BindOrder() {
dgBasket.DataSource = cart.Items;
dgBasket.DataBind();
}
void BindShipping(string fromZip, string toZip, double weight) {
IDataReader rdr = ShippingManager.GetShippingChoices(fromZip, toZip, weight);
ddlShipService.DataSource = rdr;
ddlShipService.DataTextField = "Service";
ddlShipService.DataValueField = "Rate";
ddlShipService.DataBind();
rdr.Close();
//localize it
double dRate=0;
foreach (ListItem l in ddlShipService.Items) {
dRate=Convert.ToDouble(l.Value);
l.Text = l.Text + ": " + dRate.ToString("C");
}
}
void LoadShippingLabels() {
lblShipName.Text = Profile.Commerce.ShipFirst + " " + Profile.Commerce.ShipLast;
lblShipAddress1.Text = Profile.Commerce.ShipAddress1;
lblShipAddress2.Text = Profile.Commerce.ShipAddress2 + "<br>";
lblShipAddress2.Visible = Profile.Commerce.ShipAddress2 != string.Empty;
lblShipCity.Text = Profile.Commerce.ShipCity;
lblShipState.Text = Profile.Commerce.ShipState;
lblShipCountry.Text = Profile.Commerce.ShipCountry;
lblShipZip.Text = Profile.Commerce.ShipZip;
}
void LoadBillingLabels() {
lblName.Text = Profile.First + " " + Profile.Last;
lblAddress1.Text = Profile.Address1;
lblAddress2.Text = Profile.Address2 + "<br>";
lblAddress2.Visible = Profile.Address2 != string.Empty;
lblCity.Text = Profile.City;
lblState.Text = Profile.State;
lblCountry.Text = Profile.Country;
lblZip.Text = Profile.Zip;
}
void LoadFromPP(string sToken) {
Commerce.PayPal.APIWrapper wrapper = new Commerce.PayPal.APIWrapper(
SiteConfiguration.PayPalAPIAccountName, SiteConfiguration.PayPalAPIAccountPassword,
SiteConfiguration.PayPalAPICertificationPath, SiteConfiguration.PayPalAPICertificationPassword);
//they have come back from the PayPal site and have a token, so use this token to go get their info
//and populate the shipping etc.
Commerce.PayPal.APIWrapper.PayerInfo payer = wrapper.GetExpressCheckout(sToken);
SavePayPalInfo(payer);
//populate the final payment screen
lblPaymentSummary.Text = "PayPal account<br>" + payer.PayerEmail;
txtToken.Text = payer.Token;
txtPayerID.Text = payer.PayerID;
}
void LoadFromProfile() {
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
//using a CrossPagePost to hold the credit card. We DO NOT want to save
//this to the DB.
txtCCNumber.Text = GetPreviousTextValue("txtCCNumber");
txtCCAuthCode.Text = GetPreviousTextValue("txtCCAuthCode");
txtCCType.Text = GetPreviousSelectValue("ddlCCType");
txtCCExpMonth.Text = GetPreviousSelectValue("ddlExpMonth");
txtCCExpYear.Text = GetPreviousSelectValue("ddlExpYear");
string CCNumber = txtCCNumber.Text;//Encryption.Decrypt(Profile.Commerce.CCNumber, SiteConfiguration.EncryptionPassword);
string lastFour = "XXXX";
if (CCNumber.Length > 4) {
//get the last 4 digits
lastFour = CCNumber.Substring(CCNumber.Length - 4, 4);
} else {
}
string ccNumReplaced = "";
for (int i = 0; i < CCNumber.Length - 4; i++) {
ccNumReplaced += "X";
}
ccNumReplaced += lastFour;
lblPaymentSummary.Text = "Credit Card Account: <br>" + txtCCType.Text + ": " + ccNumReplaced;
}
}
#endregion
#region Event Handlers
protected void btnOrder_Click(object sender, EventArgs e) {
//if there is a token in the querystring
//this is a return from PayPal, and is therefore
//an express checkout
string transactionID = "";
if (Request.QueryString["token"] != null) {
transactionID=RunExpressCheckout();
} else {
transactionID = RunCharge();
}
//if an error occurred, the error will be shown in
//tablerow trError. if that's visible, don't send them off
if (uResult.Visible == false) {
Response.Redirect(Request.ApplicationPath + "/Receipt.aspx?t=" + transactionID);
}
}
protected void btnChange_Click(object sender, EventArgs e) {
Response.Redirect("Checkout.aspx");
}
protected void ddlShipService_SelectedIndexChanged(object sender, EventArgs e) {
SetShipping();
SetTotals();
}
#endregion
#region Profile Updaters
void SaveBillingInfo() {
//get the billing and save to profile
Profile.First = GetPreviousTextValue("txtBillFirst");
Profile.Last = GetPreviousTextValue("txtBillLast");
Profile.City = GetPreviousTextValue("txtBillCity");
Profile.Zip = GetPreviousTextValue("txtBillZip");
Profile.Address1 = GetPreviousTextValue("txtBillAddress");
Profile.Address2 = GetPreviousTextValue("txtBillAddress2");
Profile.Country = GetPreviousSelectValue("ddlCountry");
if (Profile.Country == "US") {
Profile.State = GetPreviousSelectValue("ddlState");
} else {
Profile.State = GetPreviousTextValue("txtState");
}
Profile.Save();
}
void SavePayPalInfo(Commerce.PayPal.APIWrapper.PayerInfo payer) {
//save the profile info for later
Profile.Commerce.ShipFirst = payer.PayerFirst;
Profile.Commerce.ShipLast = payer.PayerLast;
Profile.Commerce.ShipAddress1 = payer.PayerAddress1;
Profile.Commerce.ShipAddress2 = payer.PayerAddress2;
Profile.Commerce.ShipCity = payer.PayerCity;
Profile.Commerce.ShipCountry = payer.PayerCountry;
Profile.Commerce.ShipZip = payer.PayerZip;
Profile.Commerce.ShipState = payer.PayerState;
Profile.Save();
}
#endregion
#region Helper Functions
Order CreateOrderObject() {
Order orderRec = new Order();
double dSubtotal = cart.SubTotal;
double dShipping = double.Parse(ddlShipService.SelectedValue);
string shippingMethod = ddlShipService.SelectedItem.Text;
double dTax = double.Parse(txtTaxAmount.Text);
string shipSummary = Profile.Commerce.ShipFirst + " " + Profile.Commerce.ShipLast + "<br>" + Profile.Commerce.ShipAddress1 +
"<br>" + Profile.Commerce.ShipAddress2 + "<br>" + Profile.Commerce.ShipCity +
", " + Profile.Commerce.ShipState + " " + Profile.Commerce.ShipZip + "<br>" + Profile.Commerce.ShipCountry;
orderRec.UserName = User.Identity.Name;
orderRec.OrderSubTotal = cart.SubTotal;
orderRec.Shipping = dShipping;
orderRec.ShippingAddress = shipSummary;
orderRec.Tax = dTax;
orderRec.ShippingMethod = shippingMethod;
orderRec.PaymentMethod = lblPaymentSummary.Text;
return orderRec;
}
private Control FindControl(string controlID, ControlCollection controls) {
foreach (Control c in controls) {
if (c.ID == controlID)
return c;
if (c.HasControls()) {
Control cTmp = this.FindControl(controlID, c.Controls);
if (cTmp != null)
return cTmp;
}
}
return null;
}
string GetPreviousTextValue(string controlName) {
string sOut = "";
TextBox txt = (TextBox)FindControl(controlName, PreviousPage.Controls);
if (txt != null)
sOut = txt.Text;
return sOut;
}
string GetPreviousSelectValue(string controlName) {
string sOut = "";
DropDownList drop = (DropDownList)FindControl(controlName, PreviousPage.Controls);
if (drop != null)
sOut = drop.SelectedValue;
return sOut;
}
void ShowMessage(string sMessage) {
uResult.Visible = true;
uResult.ShowFail(sMessage);
}
#endregion
#region Calculators
void SetTax(string sState) {
//tax calculation
//the tax amount is legally set by where the order is
//to be shipped, not where the buyer is located
//when the information is entered for shipping
//calculate the tax rate and output.
//please consult a tax advisor or
//your legal department for proper
//application of tax
double dTax = TaxCalculator.GetTaxByState(sState, cart.SubTotal);
lblTax.Text = dTax.ToString("c");
txtTaxAmount.Text = dTax.ToString();
}
void SetShipping() {
//shipping calculation
double dShipping = 0;
try {
dShipping=Convert.ToDouble(ddlShipService.SelectedValue);
} catch(Exception x) {
}
lblShipping.Text = dShipping.ToString("c");
}
void SetTotals() {
//calculate the final total
try {
double dShipping = Convert.ToDouble(ddlShipService.SelectedValue);
double dTax = Convert.ToDouble(txtTaxAmount.Text);
double dTotal = cart.SubTotal + dTax + dShipping;
lblTotal.Text = dTotal.ToString("c");
} catch {
}
}
#endregion
#region Charger
string RunCharge() {
double dSubtotal = cart.SubTotal;
double dShipping = double.Parse(ddlShipService.SelectedValue);
string shippingMethod = ddlShipService.SelectedItem.Text;
double dTax = 0;
try {
dTax = double.Parse(txtTaxAmount.Text);
} catch {
}
Commerce.Providers.CreditCardType ccType = Commerce.Providers.CreditCardType.Visa;
string sOut = "";
if (txtCCType.Text == "MasterCard") {
ccType = Commerce.Providers.CreditCardType.MasterCard;
} else if (txtCCType.Text == "AMEX") {
ccType = Commerce.Providers.CreditCardType.AMEX;
}
try {
sOut=OrdersManager.RunCharge(Profile.First, Profile.Last, Profile.Address1, Profile.Address2,
Profile.City, Profile.State, Profile.Zip, Profile.Country, txtCCNumber.Text,
int.Parse(txtCCExpMonth.Text), int.Parse(txtCCExpYear.Text), ccType,
txtCCAuthCode.Text, dSubtotal, Math.Round(dTax,2), dShipping, shippingMethod, lblPaymentSummary.Text);
} catch (Exception x) {
ShowMessage(x.Message);
}
return sOut;
}
#endregion
//For use with Express Checkout
#region API Calls
Commerce.PayPal.APIWrapper.OrderItem[] GetOrderItemsArray() {
//loop out the basket items into a an array of
//Commerce.OrderItem[]
Commerce.PayPal.APIWrapper.OrderItem item;
int itemCount =cart.Items.Rows.Count;
Commerce.PayPal.APIWrapper.OrderItem[] items = new Commerce.PayPal.APIWrapper.OrderItem[itemCount];
DataRow dr;
for (int i = 0; i < itemCount; i++) {
dr = cart.Items.Rows[i];
item = new Commerce.PayPal.APIWrapper.OrderItem();
item.ProductName = dr["ModelName"].ToString();
item.SKU = dr["ModelNumber"].ToString();
item.UnitPrice = (double)dr["Price"];
item.Quantity = (int)dr["Quantity"];
//add to the array
items[i] = item;
}
return items;
}
string RunExpressCheckout() {
Commerce.PayPal.APIWrapper.OrderItem[] items = this.GetOrderItemsArray();
string transactionID = "";
try {
//use the wrapper to call PayPal and run the charge
Commerce.PayPal.APIWrapper.OrderInfo order = OrdersManager.RunExpressOrder(CreateOrderObject(),
txtToken.Text,txtPayerID.Text, items);
if (order.Ack == "Success") {
transactionID = order.TransactionID;
} else {
ShowMessage(order.Ack);
}
} catch (Exception x) {
ShowMessage(x.Message);
}
return transactionID;
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -