Google Checkout Integration

Google Checkout Integration

Posted on May 18, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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.

progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation