+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Google Checkout Integration

by Georgi Chokov
Introduction

In a basic view, an e-Commerce system is a product list, a shopping cart and a checkout area. Many businesses will provide you with a shopping cart and checkout area. Often, they pass a small fee to your customers for their services. Sitefinity can provide you with a product list with the added benefit of built-in categories and tag lists. In this example, I shall use the EventsView with a Google Checkout button, demonstrating basic e-Commerce functionality with Sitefinity out of the box.

Installation

For the purposes of this demonstration, I shall provide you with the necessary files to install Google Checkout to allow users to pay for events. Here is how to install them:

1. Download the sample project, which is shown below this article
2. Using the same folder structure, copy the files into Sitefinity
3. In your Web.config and under the </configSections> tag, add the necessary app settings:

    <appSettings> 
        <add key="GoogleMerchantID" value="xxxxx" /> 
        <add key="GoogleMerchantKey" value="xxxxx" /> 
        <add key="GoogleEnvironment" value="Sandbox" /> 
    </appSettings> 
 

4. In your Web.config, add this line of code beneath the <metaFields> tag:

        <add key="Events.Price" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="0" /> 
 


5. Login to your Sitefinity project.
6. Click on Pages
7. Select a page and click Edit this Page or Edit this Language Version if localization is turned on
8. From the toolbox on the right, drag and drop an EventsView
9. Click Edit on the EventsView
10. Click on the Advanced Tab
11. Expand the appearance section and under the ItemListTemplatePath property, enter this path:

~\Sitefinity\ControlTemplates\Events\ContentViewItemView.ascx

11. Click I'm done
12. Click Publish or Save, depending on the workflow settings.

If workflow is turned on, make sure that the page is approved and later published.

Obtaining an Google Merchant ID and a Google Merchant Key

For this example, we will use the Google Sandbox to do test sales. This will ensure that the orders are not processed while we do custom development. As with most 3rd party applications, you must obtain a user name and password to use their API. Often times, this is known as an API key with a special secret key. To obtain this info, please visit Google Checkout's website.

When this is complete, enter the data into the app settings keys of your application. If you'd like to use this in a production environment, you must change the App Settings Environment variable to production and obtain a production Merchant ID and Merchant key.

Create Events with a Price

Once this is complete, login to the CMS and click Modules > Events. Create some new events and add a price for them.

Checking out with Google

Now that this code has been inserted and you have events with prices, here is the end result:



Explaining the code

Google uses a special button control along with a method called AddItem. This method redirects the user to a URL, based on the product name, price and quantity. In this case, I passed the Event's meta data into this method. To get that data, I extracted the event's ID and passed the parameters into the EventManager's GetContent method. Here is the annotated code:

using Telerik.Events; 
using Telerik.Cms.Engine; 
using GCheckout.Checkout; 
using GCheckout.Util; 
 
public partial class Sitefinity_ControlTemplates_Events_ContentViewItemView : System.Web.UI.UserControl 
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    public void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
        //Assign the event ID to the button's command argument, so it can be used later 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
        { 
            EventsManager manager = new EventsManager("Events"); 
            IContent content = (IContent)e.Item.DataItem; 
            GCheckoutButton button = (GCheckoutButton)e.Item.FindControl("GCheckoutButton1"); 
            if (button != null
            { 
                button.CommandName = "Select"
                button.CommandArgument = content.ID.ToString(); 
                //Subscribe to the button's click event 
                button.Click += new ImageClickEventHandler(button_Click); 
            } 
        } 
    } 
 
    void button_Click(object sender, ImageClickEventArgs e) 
    { 
        //Since this button is inside of a repeater, you must cast the sender object as a Google Checkout button 
        GCheckoutButton GCheckoutButton1 =  (GCheckoutButton)sender; 
        //Get the ID of the event 
        Guid EventID = new Guid(GCheckoutButton1.CommandArgument); 
        if (EventID != null
        { 
            EventsManager manager = new EventsManager("Events"); 
            IContent content = manager.Content.GetContent(EventID); 
            //Pass the event's name, description, left null here, price and quantity to Google Checkout 
            CheckoutShoppingCartRequest Req = GCheckoutButton1.CreateRequest(); 
            Req.AddItem(content.GetMetaData("Title").ToString(), String.Empty, Convert.ToDecimal(content.GetMetaData("Price")), 1); 
            GCheckoutResponse Resp = Req.Send(); 
            //Report any errors 
            if (Resp.IsGood) 
            { 
                Response.Redirect(Resp.RedirectUrl, true); 
            } 
            else 
            { 
                Response.Write("ResponseXml = " + Resp.ResponseXml + "<br>"); 
                Response.Write("ErrorMessage = " + Resp.ErrorMessage + "<br>"); 
            }  
        } 
    } 

For more information about Google Checkout, please read Getting Started with Google Checkout. For more info on the Event's module's API please red our Developer Manual.

4 comments

Leave a comment
  1. Gray.Blog Jun 02, 2009
    Pingback from: http://graywebtech.com//blog/2099/06/02/developers-corner-sitefinity-social-media-and-multimedia-enhancements.aspx
  2. indika Jul 22, 2009
    when i submit shoping cart data to google checkout
    /*
    GCheckoutResponse Resp = Req.Send();
    */

    /*
     if (Resp.IsGood)
                {
                    Response.Redirect(Resp.RedirectUrl, true);
                }
    */
    then it redirect to google checkout page.

    then we can have notifications.. after successfully charge for the order it also we notified. then i need to update my database. how can know which order is success. because i don't know at googleordernumber at shoping cart deetail pasing  time.
  3. haktan Jun 01, 2010
    bilgi için teşekkürler

    haktan
  4. monfili Dec 05, 2012
    @indika I'm sure you've worked this out, but the best option is to add Merchant Private Data to the cart when passing it to google. You add an internal order number to your cart as private data and get it back from further notifications (or link it with the Google order number). This allows you to handle concurrence and order tracking. 

    Leave a comment