Jun 04 2007

Implementation step 3

Posted by admin under Commerce and payment

ALL ARTICLES IN THIS SERIE

The download solution will look like this:

I have tried to keep it as clean and simple - but still wanted to keep some sort of structure and object hierarchy which will ease your own implementation.

 

article.cs

 

 You should modify this to match your own product/article implementation, and more importantly - I use a static coillection  - public static ArticleCollection Instance()  -  and this is where you should ready articles from a database instead.

shoppingcart.cs

 

Implements the shoppingcart.  I have chosen to store the shoppingcart in Session variable and it is available though a static function:



            public static ShoppingCart GetCurrentUsersCart
            {
                get
                {
                    if (System.Web.HttpContext.Current.Session["ShoppingCart"] == null)
                        System.Web.HttpContext.Current.Session["ShoppingCart"] = new ShoppingCart();
                    return System.Web.HttpContext.Current.Session["ShoppingCart"] as ShoppingCart;
                }
            }


default.aspx/cs

Here we have the GUI. The shopping cart and articlelist are simply repeaters not much to talk about. However - one piece of code I would like to talk about:



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["addcart"] != null)
                {
                    string sWhat = Request["addcart"];
                    ObjClasses.Article oArt = ObjClasses.ArticleCollection.Instance().Find(sWhat);
                    if (oArt != null)
                    {
                        ObjClasses.ShoppingCart.GetCurrentUsersCart.AddCount(oArt.Id, 1);
                        Response.Redirect("default.aspx", true);
                    }
                }

                //Cart
                plCart.DataSource = ObjClasses.ShoppingCart.GetCurrentUsersCart;
                plCart.DataBind();

                if ( ObjClasses.ShoppingCart.GetCurrentUsersCart.Count == 0 )
                    btnPay.Visible = false;
                else
                    btnPay.Visible = true;


                //Setup products...
                repArt.DataSource = ObjClasses.ArticleCollection.Instance();
                repArt.DataBind();
            }
        }



As you can see I have implemented the adding of items through query parameters. I.e adding an item is done by "default.aspx?addcart=<artid>"

The reason I have done it that way - and not by a postback is simply because this allows you (on other pages of your website) just have a "Buy now" link to this page and when clicking on it it will show the shopping cart with the product inside it already.

Now - when clicking on the Buy button we are supposed to get redirected to paypal, right:



        protected void btnPay_Click(object sender, EventArgs e)
        {
            Response.Redirect("shopcheckout.aspx", true);
        }



Continued in step 4