-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckout.aspx.cs
121 lines (113 loc) · 3.23 KB
/
Checkout.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web;
using CommerceLib;
public partial class Checkout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateControls();
}
// fill controls with data
private void PopulateControls()
{
// get the items in the shopping cart
DataTable dt = ShoppingCartAccess.GetItems();
// populate the list with the shopping cart contents
grid.DataSource = dt;
grid.DataBind();
grid.Visible = true;
// display the total amount
decimal amount = ShoppingCartAccess.GetTotalAmount();
totalAmountLabel.Text = String.Format("{0:c}", amount);
// check customer details
bool addressOK = true;
bool cardOK = true;
if (Profile.Address1 + Profile.Address2 == ""
|| Profile.ShippingRegion == ""
|| Profile.ShippingRegion == "Please Select"
|| Profile.Country == "")
{
addressOK = false;
}
if (Profile.CreditCard == "")
{
cardOK = false;
}
// report / hide place order button
if (!addressOK)
{
if (!cardOK)
{
InfoLabel.Text =
"You must provide a valid address and credit card "
+ "before placing your order.";
}
else
{
InfoLabel.Text =
"You must provide a valid address before placing your "
+ "order.";
}
}
else if (!cardOK)
{
InfoLabel.Text = "You must provide a credit card before "
+ "placing your order.";
}
else
{
InfoLabel.Text =
"Please confirm that the above details are "
+ "correct before proceeding.";
}
placeOrderButton.Visible = addressOK && cardOK;
shippingSelection.Visible = addressOK && cardOK;
// Populate shipping selection
if (addressOK && cardOK)
{
int shippingRegionId = int.Parse(Profile.ShippingRegion);
List<ShippingInfo> shippingInfoData =
CommerceLibAccess.GetShippingInfo(shippingRegionId);
foreach (ShippingInfo shippingInfo in shippingInfoData)
{
shippingSelection.Items.Add(
new ListItem(shippingInfo.ShippingType,
shippingInfo.ShippingID.ToString()));
}
shippingSelection.SelectedIndex = 0;
}
}
protected void placeOrderButton_Click(object sender, EventArgs e)
{
// Store the total amount
decimal amount = ShoppingCartAccess.GetTotalAmount();
// Get shipping ID or default to 0
int shippingId = 0;
int.TryParse(shippingSelection.SelectedValue, out shippingId);
// Get tax ID or default to "No tax"
string shippingRegion =
(HttpContext.Current.Profile as ProfileCommon).ShippingRegion;
int taxId;
switch (shippingRegion)
{
case "2":
taxId = 1;
break;
default:
taxId = 2;
break;
}
// Create the order and store the order ID
string orderId = ShoppingCartAccess.CreateCommerceLibOrder(shippingId, taxId);
// Process order
OrderProcessor processor = new OrderProcessor(orderId);
processor.Process();
// Redirect to the conformation page
Response.Redirect("OrderPlaced.aspx");
}
}